GRPC C++  1.26.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 
22 #include <atomic>
23 #include <map>
24 #include <memory>
25 #include <vector>
26 
28 
43 
44 struct grpc_metadata;
45 struct grpc_call;
46 struct census_context;
47 
48 namespace grpc_impl {
49 class ClientContext;
50 class CompletionQueue;
51 class Server;
52 template <class W, class R>
53 class ServerAsyncReader;
54 template <class W>
55 class ServerAsyncWriter;
56 template <class W>
58 template <class W, class R>
60 template <class R>
61 class ServerReader;
62 template <class W>
63 class ServerWriter;
64 
65 namespace internal {
66 template <class ServiceType, class RequestType, class ResponseType>
68 template <class RequestType, class ResponseType>
69 class CallbackUnaryHandler;
70 template <class RequestType, class ResponseType>
71 class CallbackClientStreamingHandler;
72 template <class RequestType, class ResponseType>
73 class CallbackServerStreamingHandler;
74 template <class RequestType, class ResponseType>
75 class CallbackBidiHandler;
76 template <class ServiceType, class RequestType, class ResponseType>
78 template <class ServiceType, class RequestType, class ResponseType>
79 class RpcMethodHandler;
80 template <class Base>
81 class FinishOnlyReactor;
82 template <class W, class R>
83 class ServerReaderWriterBody;
84 template <class ServiceType, class RequestType, class ResponseType>
86 class ServerReactor;
87 template <class Streamer, bool WriteNeeded>
89 template <::grpc::StatusCode code>
90 class ErrorMethodHandler;
91 } // namespace internal
92 
93 } // namespace grpc_impl
94 namespace grpc {
95 class GenericServerContext;
96 class ServerInterface;
97 
98 namespace experimental {
99 class GenericCallbackServerContext;
100 } // namespace experimental
101 
102 namespace internal {
103 class Call;
104 } // namespace internal
105 
106 namespace testing {
107 class InteropServerContextInspector;
108 class ServerContextTestSpouse;
109 class DefaultReactorTestPeer;
110 } // namespace testing
111 
112 } // namespace grpc
113 
114 namespace grpc_impl {
115 
118  public:
119  virtual ~ServerContextBase();
120 
122  std::chrono::system_clock::time_point deadline() const {
124  }
125 
127  gpr_timespec raw_deadline() const { return deadline_; }
128 
148  void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
149 
169  void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
170 
174  bool IsCancelled() const;
175 
192  void TryCancel() const;
193 
203  const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata()
204  const {
205  return *client_metadata_.map();
206  }
207 
210  return compression_level_;
211  }
212 
217  compression_level_set_ = true;
218  compression_level_ = level;
219  }
220 
224  bool compression_level_set() const { return compression_level_set_; }
225 
231  return compression_algorithm_;
232  }
236  void set_compression_algorithm(grpc_compression_algorithm algorithm);
237 
239  void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data);
240 
244  std::shared_ptr<const ::grpc::AuthContext> auth_context() const {
245  if (auth_context_.get() == nullptr) {
246  auth_context_ = ::grpc::CreateAuthContext(call_);
247  }
248  return auth_context_;
249  }
250 
255  grpc::string peer() const;
256 
258  const struct census_context* census_context() const;
259 
262  grpc_call* c_call() { return call_; }
263 
264  protected:
270  void AsyncNotifyWhenDone(void* tag) {
271  has_notify_when_done_tag_ = true;
272  async_notify_when_done_tag_ = tag;
273  }
274 
280  return message_allocator_state_;
281  }
282 
300  auto reactor = &default_reactor_;
301  default_reactor_used_.store(true, std::memory_order_relaxed);
302  return reactor;
303  }
304 
308 
309  private:
310  friend class ::grpc::testing::InteropServerContextInspector;
311  friend class ::grpc::testing::ServerContextTestSpouse;
312  friend class ::grpc::testing::DefaultReactorTestPeer;
313  friend class ::grpc::ServerInterface;
315  template <class W, class R>
317  template <class W>
319  template <class W>
321  template <class W, class R>
323  template <class R>
325  template <class W>
327  template <class W, class R>
328  friend class ::grpc_impl::internal::ServerReaderWriterBody;
329  template <class ServiceType, class RequestType, class ResponseType>
331  template <class ServiceType, class RequestType, class ResponseType>
333  template <class ServiceType, class RequestType, class ResponseType>
335  template <class Streamer, bool WriteNeeded>
337  template <class RequestType, class ResponseType>
338  friend class ::grpc_impl::internal::CallbackUnaryHandler;
339  template <class RequestType, class ResponseType>
340  friend class ::grpc_impl::internal::CallbackClientStreamingHandler;
341  template <class RequestType, class ResponseType>
342  friend class ::grpc_impl::internal::CallbackServerStreamingHandler;
343  template <class RequestType, class ResponseType>
344  friend class ::grpc_impl::internal::CallbackBidiHandler;
345  template <::grpc::StatusCode code>
347  template <class Base>
348  friend class ::grpc_impl::internal::FinishOnlyReactor;
350  friend class ::grpc::GenericServerContext;
351  friend class ::grpc::experimental::GenericCallbackServerContext;
352 
355  ServerContextBase& operator=(const ServerContextBase&);
356 
357  class CompletionOp;
358 
359  void BeginCompletionOp(
360  ::grpc::internal::Call* call, std::function<void(bool)> callback,
361  ::grpc_impl::internal::ServerCallbackCall* callback_controller);
363  ::grpc::internal::CompletionQueueTag* GetCompletionOpTag();
364 
365  void set_call(grpc_call* call) { call_ = call; }
366 
367  void BindDeadlineAndMetadata(gpr_timespec deadline, grpc_metadata_array* arr);
368 
369  void Clear();
370 
371  void Setup(gpr_timespec deadline);
372 
373  uint32_t initial_metadata_flags() const { return 0; }
374 
375  ::grpc::experimental::ServerRpcInfo* set_server_rpc_info(
376  const char* method, ::grpc::internal::RpcMethod::RpcType type,
377  const std::vector<std::unique_ptr<
379  if (creators.size() != 0) {
380  rpc_info_ = new ::grpc::experimental::ServerRpcInfo(this, method, type);
381  rpc_info_->RegisterInterceptors(creators);
382  }
383  return rpc_info_;
384  }
385 
386  void set_message_allocator_state(
387  ::grpc::experimental::RpcAllocatorState* allocator_state) {
388  message_allocator_state_ = allocator_state;
389  }
390 
391  CompletionOp* completion_op_;
392  bool has_notify_when_done_tag_;
393  void* async_notify_when_done_tag_;
395 
396  gpr_timespec deadline_;
397  grpc_call* call_;
399  bool sent_initial_metadata_;
400  mutable std::shared_ptr<const ::grpc::AuthContext> auth_context_;
401  mutable ::grpc::internal::MetadataMap client_metadata_;
402  std::multimap<grpc::string, grpc::string> initial_metadata_;
403  std::multimap<grpc::string, grpc::string> trailing_metadata_;
404 
405  bool compression_level_set_;
406  grpc_compression_level compression_level_;
407  grpc_compression_algorithm compression_algorithm_;
408 
411  pending_ops_;
412  bool has_pending_ops_;
413 
415  ::grpc::experimental::RpcAllocatorState* message_allocator_state_ = nullptr;
416 
417  class Reactor : public ServerUnaryReactor {
418  public:
419  void OnCancel() override {}
420  void OnDone() override {}
421  // Override InternalInlineable for this class since its reactions are
422  // trivial and thus do not need to be run from the executor (triggering a
423  // thread hop). This should only be used by internal reactors (thus the
424  // name) and not by user application code.
425  bool InternalInlineable() override { return true; }
426  };
427 
428  void SetupTestDefaultReactor(std::function<void(::grpc::Status)> func) {
429  test_unary_.reset(new TestServerCallbackUnary(this, std::move(func)));
430  }
431  bool test_status_set() const {
432  return (test_unary_ != nullptr) && test_unary_->status_set();
433  }
434  ::grpc::Status test_status() const { return test_unary_->status(); }
435 
436  class TestServerCallbackUnary : public ::grpc_impl::ServerCallbackUnary {
437  public:
438  TestServerCallbackUnary(ServerContextBase* ctx,
439  std::function<void(::grpc::Status)> func)
440  : reactor_(&ctx->default_reactor_), func_(std::move(func)) {
441  this->BindReactor(reactor_);
442  }
443  void Finish(::grpc::Status s) override {
444  status_ = s;
445  func_(std::move(s));
446  status_set_.store(true, std::memory_order_release);
447  }
448  void SendInitialMetadata() override {}
449 
450  bool status_set() const {
451  return status_set_.load(std::memory_order_acquire);
452  }
453  ::grpc::Status status() const { return status_; }
454 
455  private:
456  void MaybeDone() override {}
457  ::grpc_impl::internal::ServerReactor* reactor() override {
458  return reactor_;
459  }
460 
461  ::grpc_impl::ServerUnaryReactor* const reactor_;
462  std::atomic_bool status_set_{false};
463  ::grpc::Status status_;
464  const std::function<void(::grpc::Status s)> func_;
465  };
466 
467  Reactor default_reactor_;
468  std::atomic_bool default_reactor_used_{false};
469  std::unique_ptr<TestServerCallbackUnary> test_unary_;
470 };
471 
489  public:
490  ServerContext() {} // for async calls
491 
509 
510  // Sync/CQ-based Async ServerContext only
512 
513  private:
514  // Constructor for internal use by server only
517  : ServerContextBase(deadline, arr) {}
518 
519  // CallbackServerContext only
522 
524  ServerContext(const ServerContext&) = delete;
525  ServerContext& operator=(const ServerContext&) = delete;
526 };
527 
529  public:
533 
551 
552  // CallbackServerContext only
555 
556  private:
557  // Sync/CQ-based Async ServerContext only
559 
562  CallbackServerContext& operator=(const CallbackServerContext&) = delete;
563 };
564 
565 } // namespace grpc_impl
566 
567 static_assert(std::is_base_of<::grpc_impl::ServerContextBase,
568  ::grpc_impl::ServerContext>::value,
569  "improper base class");
570 static_assert(std::is_base_of<::grpc_impl::ServerContextBase,
572  "improper base class");
573 static_assert(sizeof(::grpc_impl::ServerContextBase) ==
574  sizeof(::grpc_impl::ServerContext),
575  "wrong size");
576 static_assert(sizeof(::grpc_impl::ServerContextBase) ==
578  "wrong size");
579 
580 #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)
Definition: server_context_impl.h:528
::grpc_impl::ClientContext ClientContext
Definition: client_context.h:26
::grpc_impl::internal::ErrorMethodHandler< code > ErrorMethodHandler
Definition: method_handler.h:62
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:203
bool IsCancelled() const
IsCancelled is always safe to call when using sync or callback API.
void AddInitialMetadata(const grpc::string &key, const grpc::string &value)
Add the (key, value) pair to the initial metadata associated with a server call.
::grpc_impl::ServerContextBase ServerContextBase
Definition: server_context.h:30
std::string string
Definition: config.h:35
::grpc_impl::internal::ClientStreamingHandler< ServiceType, RequestType, ResponseType > ClientStreamingHandler
Definition: method_handler.h:41
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context_impl.h:270
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context_impl.h:209
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the server call&#39;s deadline.
Definition: server_context_impl.h:127
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
::grpc_impl::internal::TemplatedBidiStreamingHandler< Streamer, WriteNeeded > TemplatedBidiStreamingHandler
Definition: method_handler.h:50
::grpc_impl::Server Server
Definition: server.h:26
Definition: server_callback_impl.h:151
Primary implementation of CallOpSetInterface.
Definition: call_op_set.h:824
CallbackServerContext()
Public constructors are for direct use only by mocking tests.
Definition: server_context_impl.h:532
::grpc_impl::internal::BidiStreamingHandler< ServiceType, RequestType, ResponseType > BidiStreamingHandler
Definition: method_handler.h:31
::grpc_impl::ServerAsyncResponseWriter< W > ServerAsyncResponseWriter
Definition: async_unary_call.h:34
Definition: async_unary_call_impl.h:301
const struct census_context * census_context() const
Get the census context associated with this server call.
Definition: grpc_types.h:529
::grpc_impl::ServerAsyncWriter< W > ServerAsyncWriter
Definition: async_stream.h:69
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:26
A ServerContext or CallbackServerContext allows the code implementing a service handler to: ...
Definition: server_context_impl.h:488
::grpc::experimental::RpcAllocatorState * GetRpcAllocatorState()
NOTE: This is an API for advanced users who need custom allocators.
Definition: server_context_impl.h:279
The base class of ServerCallbackUnary etc.
Definition: server_callback_impl.h:72
std::chrono::system_clock::time_point deadline() const
Return the deadline for the server call.
Definition: server_context_impl.h:122
grpc::string peer() const
Return the peer uri in a string.
Base class of ServerContext. Experimental until callback API is final.
Definition: server_context_impl.h:117
Definition: call_op_set.h:216
::grpc_impl::ServerReader< R > ServerReader
Definition: sync_stream.h:75
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
::grpc_impl::ServerUnaryReactor * DefaultReactor()
Get a library-owned default unary reactor for use in minimal reaction cases.
Definition: server_context_impl.h:299
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the server call will request be used.
Definition: server_context_impl.h:230
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: server_context_impl.h:262
A single metadata element.
Definition: grpc_types.h:485
Definition: call_op_set.h:286
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
::grpc_impl::CallbackServerContext CallbackServerContext
Definition: server_context.h:31
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
void AddTrailingMetadata(const grpc::string &key, const grpc::string &value)
Add the (key, value) pair to the initial metadata associated with a server call.
::grpc_impl::internal::RpcMethodHandler< ServiceType, RequestType, ResponseType > RpcMethodHandler
Definition: method_handler.h:36
void SetLoadReportingCosts(const std::vector< grpc::string > &cost_data)
Set the serialized load reporting costs in cost_data for the call.
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:224
::grpc_impl::internal::ServerStreamingHandler< ServiceType, RequestType, ResponseType > ServerStreamingHandler
Definition: method_handler.h:46
Definition: server_callback_impl.h:660
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:216
ServerContext()
Definition: server_context_impl.h:490
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
::grpc_impl::ServerAsyncReaderWriter< W, R > ServerAsyncReaderWriter
Definition: async_stream.h:76
CallbackWithSuccessTag can be reused multiple times, and will be used in this fashion for streaming o...
Definition: callback_common.h:136
::grpc_impl::ServerAsyncReader< W, R > ServerAsyncReader
Definition: async_stream.h:63
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:100
Did it work? If it didn&#39;t, why?
Definition: status.h:31
Analogous to struct timespec.
Definition: gpr_types.h:47
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
Definition: message_allocator.h:27
::grpc_impl::ServerWriter< W > ServerWriter
Definition: sync_stream.h:81
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Set algorithm to be the compression algorithm used for the server call.
std::shared_ptr< const ::grpc::AuthContext > auth_context() const
Return the authentication context for this server call.
Definition: server_context_impl.h:244
Definition: server_callback_impl.h:48
void TryCancel() const
Cancel the Call from the server.
Straightforward wrapping of the C call object.
Definition: call.h:38