GRPC C++  1.17.0
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 
39 
40 struct grpc_metadata;
41 struct grpc_call;
42 struct census_context;
43 
44 namespace grpc {
45 class ClientContext;
46 template <class W, class R>
47 class ServerAsyncReader;
48 template <class W>
49 class ServerAsyncWriter;
50 template <class W>
51 class ServerAsyncResponseWriter;
52 template <class W, class R>
53 class ServerAsyncReaderWriter;
54 template <class R>
55 class ServerReader;
56 template <class W>
57 class ServerWriter;
58 namespace internal {
59 template <class W, class R>
60 class ServerReaderWriterBody;
61 template <class ServiceType, class RequestType, class ResponseType>
62 class RpcMethodHandler;
63 template <class ServiceType, class RequestType, class ResponseType>
64 class ClientStreamingHandler;
65 template <class ServiceType, class RequestType, class ResponseType>
66 class ServerStreamingHandler;
67 template <class ServiceType, class RequestType, class ResponseType>
68 class BidiStreamingHandler;
69 template <class ServiceType, class RequestType, class ResponseType>
70 class CallbackUnaryHandler;
71 template <class Streamer, bool WriteNeeded>
72 class TemplatedBidiStreamingHandler;
73 template <StatusCode code>
74 class ErrorMethodHandler;
75 class Call;
76 } // namespace internal
77 
78 class CompletionQueue;
79 class Server;
80 class ServerInterface;
81 
82 namespace testing {
83 class InteropServerContextInspector;
84 class ServerContextTestSpouse;
85 } // namespace testing
86 
103  public:
104  ServerContext(); // for async calls
105  ~ServerContext();
106 
108  std::chrono::system_clock::time_point deadline() const {
109  return Timespec2Timepoint(deadline_);
110  }
111 
113  gpr_timespec raw_deadline() const { return deadline_; }
114 
127  void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
128 
141  void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
142 
146  bool IsCancelled() const;
147 
164  void TryCancel() const;
165 
175  const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata()
176  const {
177  return *client_metadata_.map();
178  }
179 
182  return compression_level_;
183  }
184 
189  compression_level_set_ = true;
190  compression_level_ = level;
191  }
192 
196  bool compression_level_set() const { return compression_level_set_; }
197 
203  return compression_algorithm_;
204  }
208  void set_compression_algorithm(grpc_compression_algorithm algorithm);
209 
211  void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data);
212 
216  std::shared_ptr<const AuthContext> auth_context() const {
217  if (auth_context_.get() == nullptr) {
218  auth_context_ = CreateAuthContext(call_);
219  }
220  return auth_context_;
221  }
222 
227  grpc::string peer() const;
228 
230  const struct census_context* census_context() const;
231 
237  void AsyncNotifyWhenDone(void* tag) {
238  has_notify_when_done_tag_ = true;
239  async_notify_when_done_tag_ = tag;
240  }
241 
244  grpc_call* c_call() { return call_; }
245 
246  private:
247  friend class ::grpc::testing::InteropServerContextInspector;
248  friend class ::grpc::testing::ServerContextTestSpouse;
249  friend class ::grpc::ServerInterface;
250  friend class ::grpc::Server;
251  template <class W, class R>
252  friend class ::grpc::ServerAsyncReader;
253  template <class W>
254  friend class ::grpc::ServerAsyncWriter;
255  template <class W>
256  friend class ::grpc::ServerAsyncResponseWriter;
257  template <class W, class R>
258  friend class ::grpc::ServerAsyncReaderWriter;
259  template <class R>
260  friend class ::grpc::ServerReader;
261  template <class W>
262  friend class ::grpc::ServerWriter;
263  template <class W, class R>
264  friend class ::grpc::internal::ServerReaderWriterBody;
265  template <class ServiceType, class RequestType, class ResponseType>
266  friend class ::grpc::internal::RpcMethodHandler;
267  template <class ServiceType, class RequestType, class ResponseType>
268  friend class ::grpc::internal::ClientStreamingHandler;
269  template <class ServiceType, class RequestType, class ResponseType>
270  friend class ::grpc::internal::ServerStreamingHandler;
271  template <class Streamer, bool WriteNeeded>
272  friend class ::grpc::internal::TemplatedBidiStreamingHandler;
273  template <class ServiceType, class RequestType, class ResponseType>
274  friend class ::grpc::internal::CallbackUnaryHandler;
275  template <StatusCode code>
277  friend class ::grpc::ClientContext;
278 
281  ServerContext& operator=(const ServerContext&);
282 
283  class CompletionOp;
284 
285  void BeginCompletionOp(internal::Call* call, bool callback);
287  internal::CompletionQueueTag* GetCompletionOpTag();
288 
290 
291  void set_call(grpc_call* call) { call_ = call; }
292 
293  void BindDeadlineAndMetadata(gpr_timespec deadline, grpc_metadata_array* arr);
294 
295  void Clear();
296 
297  void Setup(gpr_timespec deadline);
298 
299  uint32_t initial_metadata_flags() const { return 0; }
300 
301  experimental::ServerRpcInfo* set_server_rpc_info(
302  const char* method,
303  const std::vector<
304  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>&
305  creators) {
306  if (creators.size() != 0) {
307  rpc_info_ = new experimental::ServerRpcInfo(this, method);
308  rpc_info_->RegisterInterceptors(creators);
309  }
310  return rpc_info_;
311  }
312 
313  CompletionOp* completion_op_;
314  bool has_notify_when_done_tag_;
315  void* async_notify_when_done_tag_;
316  internal::CallbackWithSuccessTag completion_tag_;
317 
318  gpr_timespec deadline_;
319  grpc_call* call_;
320  CompletionQueue* cq_;
321  bool sent_initial_metadata_;
322  mutable std::shared_ptr<const AuthContext> auth_context_;
323  mutable internal::MetadataMap client_metadata_;
324  std::multimap<grpc::string, grpc::string> initial_metadata_;
325  std::multimap<grpc::string, grpc::string> trailing_metadata_;
326 
327  bool compression_level_set_;
328  grpc_compression_level compression_level_;
329  grpc_compression_algorithm compression_algorithm_;
330 
333  pending_ops_;
334  bool has_pending_ops_;
335 
336  experimental::ServerRpcInfo* rpc_info_;
337 };
338 
339 } // namespace grpc
340 
341 #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:33
Primary implementation of CallOpSetInterface.
Definition: call_op_set.h:755
void set_compression_level(grpc_compression_level level)
Set level to be the compression level used for the server call.
Definition: server_context.h:188
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the server call will request be used.
Definition: server_context.h:202
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context.h:237
Definition: grpc_types.h:480
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:175
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:181
std::chrono::system_clock::time_point deadline() const
Return the deadline for the server call.
Definition: server_context.h:108
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:196
Definition: call_op_set.h:223
A single metadata element.
Definition: grpc_types.h:437
Definition: call_op_set.h:293
grpc_compression_algorithm
The various compression algorithms supported by gRPC.
Definition: compression_types.h:56
Definition: server_interceptor.h:45
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:33
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:102
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the server call&#39;s deadline.
Definition: server_context.h:113
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:95
CallbackWithSuccessTag can be reused multiple times, and will be used in this fashion for streaming o...
Definition: callback_common.h:116
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this server call.
Definition: server_context.h:216
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: byte_buffer.h:51
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:244
Straightforward wrapping of the C call object.
Definition: call.h:36