GRPC C++  1.22.0
server_context_impl.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2019 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_IMPL_H
20 #define GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_H
21 #include <map>
22 #include <memory>
23 #include <vector>
24 
26 
38 
39 struct grpc_metadata;
40 struct grpc_call;
41 struct census_context;
42 
43 namespace grpc_impl {
44 class ClientContext;
45 class CompletionQueue;
46 class Server;
47 } // namespace grpc_impl
48 namespace grpc {
49 class GenericServerContext;
50 class ServerInterface;
51 template <class W, class R>
52 class ServerAsyncReader;
53 template <class W>
54 class ServerAsyncWriter;
55 template <class W>
56 class ServerAsyncResponseWriter;
57 template <class W, class R>
58 class ServerAsyncReaderWriter;
59 template <class R>
60 class ServerReader;
61 template <class W>
62 class ServerWriter;
63 
64 namespace internal {
65 template <class W, class R>
66 class ServerReaderWriterBody;
67 template <class ServiceType, class RequestType, class ResponseType>
68 class RpcMethodHandler;
69 template <class ServiceType, class RequestType, class ResponseType>
70 class ClientStreamingHandler;
71 template <class ServiceType, class RequestType, class ResponseType>
72 class ServerStreamingHandler;
73 template <class ServiceType, class RequestType, class ResponseType>
74 class BidiStreamingHandler;
75 template <class RequestType, class ResponseType>
76 class CallbackUnaryHandler;
77 template <class RequestType, class ResponseType>
78 class CallbackClientStreamingHandler;
79 template <class RequestType, class ResponseType>
80 class CallbackServerStreamingHandler;
81 template <class RequestType, class ResponseType>
82 class CallbackBidiHandler;
83 template <class Streamer, bool WriteNeeded>
84 class TemplatedBidiStreamingHandler;
85 template <StatusCode code>
86 class ErrorMethodHandler;
87 class Call;
88 class ServerReactor;
89 } // namespace internal
90 
91 class ServerInterface;
92 namespace testing {
93 class InteropServerContextInspector;
94 class ServerContextTestSpouse;
95 } // namespace testing
96 } // namespace grpc
97 
98 namespace grpc_impl {
115  public:
116  ServerContext(); // for async calls
117  ~ServerContext();
118 
120  std::chrono::system_clock::time_point deadline() const {
122  }
123 
125  gpr_timespec raw_deadline() const { return deadline_; }
126 
146  void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
147 
167  void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
168 
172  bool IsCancelled() const;
173 
190  void TryCancel() const;
191 
201  const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata()
202  const {
203  return *client_metadata_.map();
204  }
205 
208  return compression_level_;
209  }
210 
215  compression_level_set_ = true;
216  compression_level_ = level;
217  }
218 
222  bool compression_level_set() const { return compression_level_set_; }
223 
229  return compression_algorithm_;
230  }
234  void set_compression_algorithm(grpc_compression_algorithm algorithm);
235 
237  void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data);
238 
242  std::shared_ptr<const ::grpc::AuthContext> auth_context() const {
243  if (auth_context_.get() == nullptr) {
244  auth_context_ = ::grpc::CreateAuthContext(call_);
245  }
246  return auth_context_;
247  }
248 
253  grpc::string peer() const;
254 
256  const struct census_context* census_context() const;
257 
263  void AsyncNotifyWhenDone(void* tag) {
264  has_notify_when_done_tag_ = true;
265  async_notify_when_done_tag_ = tag;
266  }
267 
270  grpc_call* c_call() { return call_; }
271 
272  private:
273  friend class ::grpc::testing::InteropServerContextInspector;
274  friend class ::grpc::testing::ServerContextTestSpouse;
275  friend class ::grpc::ServerInterface;
277  template <class W, class R>
278  friend class ::grpc::ServerAsyncReader;
279  template <class W>
280  friend class ::grpc::ServerAsyncWriter;
281  template <class W>
282  friend class ::grpc::ServerAsyncResponseWriter;
283  template <class W, class R>
284  friend class ::grpc::ServerAsyncReaderWriter;
285  template <class R>
286  friend class ::grpc::ServerReader;
287  template <class W>
288  friend class ::grpc::ServerWriter;
289  template <class W, class R>
290  friend class ::grpc::internal::ServerReaderWriterBody;
291  template <class ServiceType, class RequestType, class ResponseType>
292  friend class ::grpc::internal::RpcMethodHandler;
293  template <class ServiceType, class RequestType, class ResponseType>
294  friend class ::grpc::internal::ClientStreamingHandler;
295  template <class ServiceType, class RequestType, class ResponseType>
296  friend class ::grpc::internal::ServerStreamingHandler;
297  template <class Streamer, bool WriteNeeded>
298  friend class ::grpc::internal::TemplatedBidiStreamingHandler;
299  template <class RequestType, class ResponseType>
300  friend class ::grpc::internal::CallbackUnaryHandler;
301  template <class RequestType, class ResponseType>
302  friend class ::grpc::internal::CallbackClientStreamingHandler;
303  template <class RequestType, class ResponseType>
304  friend class ::grpc::internal::CallbackServerStreamingHandler;
305  template <class RequestType, class ResponseType>
306  friend class ::grpc::internal::CallbackBidiHandler;
307  template <::grpc::StatusCode code>
308  friend class ::grpc::internal::ErrorMethodHandler;
310  friend class ::grpc::GenericServerContext;
311 
314  ServerContext& operator=(const ServerContext&);
315 
316  class CompletionOp;
317 
318  void BeginCompletionOp(::grpc::internal::Call* call,
319  std::function<void(bool)> callback,
320  ::grpc::internal::ServerReactor* reactor);
322  ::grpc::internal::CompletionQueueTag* GetCompletionOpTag();
323 
325 
326  void set_call(grpc_call* call) { call_ = call; }
327 
328  void BindDeadlineAndMetadata(gpr_timespec deadline, grpc_metadata_array* arr);
329 
330  void Clear();
331 
332  void Setup(gpr_timespec deadline);
333 
334  uint32_t initial_metadata_flags() const { return 0; }
335 
336  void SetCancelCallback(std::function<void()> callback);
337  void ClearCancelCallback();
338 
339  ::grpc::experimental::ServerRpcInfo* set_server_rpc_info(
340  const char* method, ::grpc::internal::RpcMethod::RpcType type,
341  const std::vector<std::unique_ptr<
343  if (creators.size() != 0) {
344  rpc_info_ = new ::grpc::experimental::ServerRpcInfo(this, method, type);
345  rpc_info_->RegisterInterceptors(creators);
346  }
347  return rpc_info_;
348  }
349 
350  CompletionOp* completion_op_;
351  bool has_notify_when_done_tag_;
352  void* async_notify_when_done_tag_;
354 
355  gpr_timespec deadline_;
356  grpc_call* call_;
358  bool sent_initial_metadata_;
359  mutable std::shared_ptr<const ::grpc::AuthContext> auth_context_;
360  mutable ::grpc::internal::MetadataMap client_metadata_;
361  std::multimap<grpc::string, grpc::string> initial_metadata_;
362  std::multimap<grpc::string, grpc::string> trailing_metadata_;
363 
364  bool compression_level_set_;
365  grpc_compression_level compression_level_;
366  grpc_compression_algorithm compression_algorithm_;
367 
370  pending_ops_;
371  bool has_pending_ops_;
372 
374 };
375 } // namespace grpc_impl
376 #endif // GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_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)
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_impl.h:201
::grpc_impl::ClientContext ClientContext
Definition: client_context.h:26
std::string string
Definition: config.h:35
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context_impl.h:263
::grpc_impl::Server Server
Definition: server.h:26
Primary implementation of CallOpSetInterface.
Definition: call_op_set.h:821
Definition: grpc_types.h:504
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
::grpc_impl::ServerContext ServerContext
Definition: server_context.h:25
A ServerContext allows the person implementing a service handler to:
Definition: server_context_impl.h:114
std::shared_ptr< const ::grpc::AuthContext > auth_context() const
Return the authentication context for this server call.
Definition: server_context_impl.h:242
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: server_context_impl.h:270
Definition: call_op_set.h:218
void set_compression_level(grpc_compression_level level)
Set level to be the compression level used for the server call.
Definition: server_context_impl.h:214
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
std::chrono::system_clock::time_point deadline() const
Return the deadline for the server call.
Definition: server_context_impl.h:120
A single metadata element.
Definition: grpc_types.h:460
Definition: call_op_set.h:288
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:60
Definition: server_callback.h:49
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context_impl.h:207
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the server call will request be used.
Definition: server_context_impl.h:228
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the server call&#39;s deadline.
Definition: server_context_impl.h:125
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
CallbackWithSuccessTag can be reused multiple times, and will be used in this fashion for streaming o...
Definition: callback_common.h:132
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.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_impl.h:222
Analogous to struct timespec.
Definition: gpr_types.h:47
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
Straightforward wrapping of the C call object.
Definition: call.h:38