GRPC C++  1.13.0-dev
server_context.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_H
20 #define GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_H
21 
22 #include <map>
23 #include <memory>
24 #include <vector>
25 
27 
36 
37 struct grpc_metadata;
38 struct grpc_call;
39 struct census_context;
40 
41 namespace grpc {
42 class ClientContext;
43 template <class W, class R>
44 class ServerAsyncReader;
45 template <class W>
46 class ServerAsyncWriter;
47 template <class W>
48 class ServerAsyncResponseWriter;
49 template <class W, class R>
50 class ServerAsyncReaderWriter;
51 template <class R>
52 class ServerReader;
53 template <class W>
54 class ServerWriter;
55 namespace internal {
56 template <class W, class R>
57 class ServerReaderWriterBody;
58 template <class ServiceType, class RequestType, class ResponseType>
59 class RpcMethodHandler;
60 template <class ServiceType, class RequestType, class ResponseType>
61 class ClientStreamingHandler;
62 template <class ServiceType, class RequestType, class ResponseType>
63 class ServerStreamingHandler;
64 template <class ServiceType, class RequestType, class ResponseType>
65 class BidiStreamingHandler;
66 class UnknownMethodHandler;
67 template <class Streamer, bool WriteNeeded>
68 class TemplatedBidiStreamingHandler;
69 class Call;
70 } // namespace internal
71 
72 class CompletionQueue;
73 class Server;
74 class ServerInterface;
75 
76 namespace testing {
77 class InteropServerContextInspector;
78 class ServerContextTestSpouse;
79 } // namespace testing
80 
97  public:
98  ServerContext(); // for async calls
99  ~ServerContext();
100 
102  std::chrono::system_clock::time_point deadline() const {
103  return Timespec2Timepoint(deadline_);
104  }
105 
107  gpr_timespec raw_deadline() const { return deadline_; }
108 
121  void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
122 
135  void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
136 
140  bool IsCancelled() const;
141 
158  void TryCancel() const;
159 
169  const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata()
170  const {
171  return *client_metadata_.map();
172  }
173 
176  return compression_level_;
177  }
178 
183  compression_level_set_ = true;
184  compression_level_ = level;
185  }
186 
190  bool compression_level_set() const { return compression_level_set_; }
191 
197  return compression_algorithm_;
198  }
202  void set_compression_algorithm(grpc_compression_algorithm algorithm);
203 
205  void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data);
206 
210  std::shared_ptr<const AuthContext> auth_context() const {
211  if (auth_context_.get() == nullptr) {
212  auth_context_ = CreateAuthContext(call_);
213  }
214  return auth_context_;
215  }
216 
221  grpc::string peer() const;
222 
224  const struct census_context* census_context() const;
225 
229  void AsyncNotifyWhenDone(void* tag) {
230  has_notify_when_done_tag_ = true;
231  async_notify_when_done_tag_ = tag;
232  }
233 
236  grpc_call* c_call() { return call_; }
237 
238  private:
239  friend class ::grpc::testing::InteropServerContextInspector;
240  friend class ::grpc::testing::ServerContextTestSpouse;
241  friend class ::grpc::ServerInterface;
242  friend class ::grpc::Server;
243  template <class W, class R>
244  friend class ::grpc::ServerAsyncReader;
245  template <class W>
246  friend class ::grpc::ServerAsyncWriter;
247  template <class W>
248  friend class ::grpc::ServerAsyncResponseWriter;
249  template <class W, class R>
250  friend class ::grpc::ServerAsyncReaderWriter;
251  template <class R>
252  friend class ::grpc::ServerReader;
253  template <class W>
254  friend class ::grpc::ServerWriter;
255  template <class W, class R>
256  friend class ::grpc::internal::ServerReaderWriterBody;
257  template <class ServiceType, class RequestType, class ResponseType>
258  friend class ::grpc::internal::RpcMethodHandler;
259  template <class ServiceType, class RequestType, class ResponseType>
260  friend class ::grpc::internal::ClientStreamingHandler;
261  template <class ServiceType, class RequestType, class ResponseType>
262  friend class ::grpc::internal::ServerStreamingHandler;
263  template <class Streamer, bool WriteNeeded>
264  friend class ::grpc::internal::TemplatedBidiStreamingHandler;
265  friend class ::grpc::internal::UnknownMethodHandler;
266  friend class ::grpc::ClientContext;
267 
270  ServerContext& operator=(const ServerContext&);
271 
272  class CompletionOp;
273 
274  void BeginCompletionOp(internal::Call* call);
276  internal::CompletionQueueTag* GetCompletionOpTag();
277 
279 
280  void set_call(grpc_call* call) { call_ = call; }
281 
282  uint32_t initial_metadata_flags() const { return 0; }
283 
284  CompletionOp* completion_op_;
285  bool has_notify_when_done_tag_;
286  void* async_notify_when_done_tag_;
287 
288  gpr_timespec deadline_;
289  grpc_call* call_;
290  CompletionQueue* cq_;
291  bool sent_initial_metadata_;
292  mutable std::shared_ptr<const AuthContext> auth_context_;
293  internal::MetadataMap client_metadata_;
294  std::multimap<grpc::string, grpc::string> initial_metadata_;
295  std::multimap<grpc::string, grpc::string> trailing_metadata_;
296 
297  bool compression_level_set_;
298  grpc_compression_level compression_level_;
299  grpc_compression_algorithm compression_algorithm_;
300 
303  pending_ops_;
304  bool has_pending_ops_;
305 };
306 
307 } // namespace grpc
308 
309 #endif // GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
std::string string
Definition: config.h:35
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
Definition: metadata_map.h:27
Primary implementation of CallOpSetInterface.
Definition: call.h:619
void set_compression_level(grpc_compression_level level)
Set algorithm to be the compression algorithm used for the server call.
Definition: server_context.h:182
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the server call will request be used.
Definition: server_context.h:196
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context.h:229
Definition: grpc_types.h:466
grpc_compression_level
Compression levels allow a party with knowledge of its peer&#39;s accepted encodings to request compressi...
Definition: compression_types.h:70
const std::multimap< grpc::string_ref, grpc::string_ref > & client_metadata() const
Return a collection of initial metadata key-value pairs sent from the client.
Definition: server_context.h:169
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:175
std::chrono::system_clock::time_point deadline() const
Return the deadline for the server call.
Definition: server_context.h:102
bool compression_level_set() const
Return a bool indicating whether the compression level for this call has been set (either implicitly ...
Definition: server_context.h:190
A single metadata element.
Definition: grpc_types.h:423
Definition: call.h:268
grpc_compression_algorithm
The various compression algorithms supported by gRPC.
Definition: compression_types.h:56
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:96
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the server call&#39;s deadline.
Definition: server_context.h:107
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:94
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this server call.
Definition: server_context.h:210
Analogous to struct timespec.
Definition: gpr_types.h:47
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: server_context.h:236
Straightforward wrapping of the C call object.
Definition: call.h:660