GRPC C++  1.19.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 RequestType, class ResponseType>
70 class CallbackUnaryHandler;
71 template <class RequestType, class ResponseType>
72 class CallbackClientStreamingHandler;
73 template <class RequestType, class ResponseType>
74 class CallbackServerStreamingHandler;
75 template <class RequestType, class ResponseType>
76 class CallbackBidiHandler;
77 template <class Streamer, bool WriteNeeded>
78 class TemplatedBidiStreamingHandler;
79 template <StatusCode code>
80 class ErrorMethodHandler;
81 class Call;
82 class ServerReactor;
83 } // namespace internal
84 
85 class CompletionQueue;
86 class Server;
87 class ServerInterface;
88 
89 namespace testing {
90 class InteropServerContextInspector;
91 class ServerContextTestSpouse;
92 } // namespace testing
93 
110  public:
111  ServerContext(); // for async calls
112  ~ServerContext();
113 
115  std::chrono::system_clock::time_point deadline() const {
116  return Timespec2Timepoint(deadline_);
117  }
118 
120  gpr_timespec raw_deadline() const { return deadline_; }
121 
141  void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
142 
162  void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
163 
167  bool IsCancelled() const;
168 
185  void TryCancel() const;
186 
196  const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata()
197  const {
198  return *client_metadata_.map();
199  }
200 
203  return compression_level_;
204  }
205 
210  compression_level_set_ = true;
211  compression_level_ = level;
212  }
213 
217  bool compression_level_set() const { return compression_level_set_; }
218 
224  return compression_algorithm_;
225  }
229  void set_compression_algorithm(grpc_compression_algorithm algorithm);
230 
232  void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data);
233 
237  std::shared_ptr<const AuthContext> auth_context() const {
238  if (auth_context_.get() == nullptr) {
239  auth_context_ = CreateAuthContext(call_);
240  }
241  return auth_context_;
242  }
243 
248  grpc::string peer() const;
249 
251  const struct census_context* census_context() const;
252 
258  void AsyncNotifyWhenDone(void* tag) {
259  has_notify_when_done_tag_ = true;
260  async_notify_when_done_tag_ = tag;
261  }
262 
265  grpc_call* c_call() { return call_; }
266 
267  private:
268  friend class ::grpc::testing::InteropServerContextInspector;
269  friend class ::grpc::testing::ServerContextTestSpouse;
270  friend class ::grpc::ServerInterface;
271  friend class ::grpc::Server;
272  template <class W, class R>
273  friend class ::grpc::ServerAsyncReader;
274  template <class W>
275  friend class ::grpc::ServerAsyncWriter;
276  template <class W>
277  friend class ::grpc::ServerAsyncResponseWriter;
278  template <class W, class R>
279  friend class ::grpc::ServerAsyncReaderWriter;
280  template <class R>
281  friend class ::grpc::ServerReader;
282  template <class W>
283  friend class ::grpc::ServerWriter;
284  template <class W, class R>
285  friend class ::grpc::internal::ServerReaderWriterBody;
286  template <class ServiceType, class RequestType, class ResponseType>
287  friend class ::grpc::internal::RpcMethodHandler;
288  template <class ServiceType, class RequestType, class ResponseType>
289  friend class ::grpc::internal::ClientStreamingHandler;
290  template <class ServiceType, class RequestType, class ResponseType>
291  friend class ::grpc::internal::ServerStreamingHandler;
292  template <class Streamer, bool WriteNeeded>
293  friend class ::grpc::internal::TemplatedBidiStreamingHandler;
294  template <class RequestType, class ResponseType>
295  friend class ::grpc::internal::CallbackUnaryHandler;
296  template <class RequestType, class ResponseType>
297  friend class ::grpc::internal::CallbackClientStreamingHandler;
298  template <class RequestType, class ResponseType>
299  friend class ::grpc::internal::CallbackServerStreamingHandler;
300  template <class RequestType, class ResponseType>
301  friend class ::grpc::internal::CallbackBidiHandler;
302  template <StatusCode code>
304  friend class ::grpc::ClientContext;
305 
308  ServerContext& operator=(const ServerContext&);
309 
310  class CompletionOp;
311 
312  void BeginCompletionOp(internal::Call* call,
313  std::function<void(bool)> callback,
314  internal::ServerReactor* reactor);
316  internal::CompletionQueueTag* GetCompletionOpTag();
317 
319 
320  void set_call(grpc_call* call) { call_ = call; }
321 
322  void BindDeadlineAndMetadata(gpr_timespec deadline, grpc_metadata_array* arr);
323 
324  void Clear();
325 
326  void Setup(gpr_timespec deadline);
327 
328  uint32_t initial_metadata_flags() const { return 0; }
329 
330  experimental::ServerRpcInfo* set_server_rpc_info(
331  const char* method, internal::RpcMethod::RpcType type,
332  const std::vector<
333  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>&
334  creators) {
335  if (creators.size() != 0) {
336  rpc_info_ = new experimental::ServerRpcInfo(this, method, type);
337  rpc_info_->RegisterInterceptors(creators);
338  }
339  return rpc_info_;
340  }
341 
342  CompletionOp* completion_op_;
343  bool has_notify_when_done_tag_;
344  void* async_notify_when_done_tag_;
345  internal::CallbackWithSuccessTag completion_tag_;
346 
347  gpr_timespec deadline_;
348  grpc_call* call_;
349  CompletionQueue* cq_;
350  bool sent_initial_metadata_;
351  mutable std::shared_ptr<const AuthContext> auth_context_;
352  mutable internal::MetadataMap client_metadata_;
353  std::multimap<grpc::string, grpc::string> initial_metadata_;
354  std::multimap<grpc::string, grpc::string> trailing_metadata_;
355 
356  bool compression_level_set_;
357  grpc_compression_level compression_level_;
358  grpc_compression_algorithm compression_algorithm_;
359 
362  pending_ops_;
363  bool has_pending_ops_;
364 
365  experimental::ServerRpcInfo* rpc_info_;
366 };
367 
368 } // namespace grpc
369 
370 #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:828
void set_compression_level(grpc_compression_level level)
Set level to be the compression level used for the server call.
Definition: server_context.h:209
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the server call will request be used.
Definition: server_context.h:223
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context.h:258
Definition: grpc_types.h:497
grpc_compression_level
Compression levels allow a party with knowledge of its peer&#39;s accepted encodings to request compressi...
Definition: compression_types.h:71
RpcType
Definition: rpc_method.h:31
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:196
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:202
std::chrono::system_clock::time_point deadline() const
Return the deadline for the server call.
Definition: server_context.h:115
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:217
Definition: call_op_set.h:224
A single metadata element.
Definition: grpc_types.h:454
Definition: call_op_set.h:294
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level) ...
Definition: compression_types.h:57
ServerRpcInfo represents the state of a particular RPC as it appears to an interceptor.
Definition: server_interceptor.h:58
Definition: server_callback.h:40
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:109
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the server call&#39;s deadline.
Definition: server_context.h:120
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:97
CallbackWithSuccessTag can be reused multiple times, and will be used in this fashion for streaming o...
Definition: callback_common.h:132
std::shared_ptr< const AuthContext > auth_context() const
Return the authentication context for this server call.
Definition: server_context.h:237
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: byte_buffer.h:53
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:265
Straightforward wrapping of the C call object.
Definition: call.h:36