Go to the documentation of this file.
19 #ifndef GRPCPP_SERVER_CONTEXT_H
20 #define GRPCPP_SERVER_CONTEXT_H
28 #include <type_traits>
55 template <
class W,
class R>
61 template <
class W,
class R>
69 template <
class ServiceType,
class RequestType,
class ResponseType>
71 template <
class RequestType,
class ResponseType>
73 template <
class RequestType,
class ResponseType>
75 template <
class RequestType,
class ResponseType>
77 template <
class RequestType,
class ResponseType>
79 template <
class ServiceType,
class RequestType,
class ResponseType>
81 template <
class ResponseType>
84 template <
class ServiceType,
class RequestType,
class ResponseType,
85 class BaseRequestType,
class BaseResponseType>
89 template <
class W,
class R>
91 template <
class ServiceType,
class RequestType,
class ResponseType>
94 template <
class Streamer,
bool WriteNeeded>
96 template <grpc::StatusCode code>
113 class InteropServerContextInspector;
114 class ServerContextTestSpouse;
115 class DefaultReactorTestPeer;
118 namespace experimental {
119 class CallMetricRecorder;
120 class ServerMetricRecorder;
129 std::chrono::system_clock::time_point
deadline()
const {
230 return *client_metadata_.
map();
235 return compression_level_;
242 compression_level_set_ =
true;
243 compression_level_ = level;
256 return compression_algorithm_;
270 if (auth_context_ ==
nullptr) {
273 return auth_context_;
280 std::string
peer()
const;
295 return call_metric_recorder_;
309 has_notify_when_done_tag_ =
true;
310 async_notify_when_done_tag_ = tag;
335 if (test_unary_ !=
nullptr) {
336 return reinterpret_cast<Reactor*
>(&default_reactor_);
338 new (&default_reactor_) Reactor;
341 assert(default_reactor_used_.compare_exchange_strong(
342 old,
true, std::memory_order_relaxed));
344 default_reactor_used_.store(
true, std::memory_order_relaxed);
346 return reinterpret_cast<Reactor*
>(&default_reactor_);
365 template <
class W,
class R>
371 template <
class W,
class R>
377 template <
class W,
class R>
379 template <
class ResponseType>
383 template <
class ServiceType,
class RequestType,
class ResponseType,
384 class BaseRequestType,
class BaseResponseType>
386 template <
class ServiceType,
class RequestType,
class ResponseType>
388 template <
class ServiceType,
class RequestType,
class ResponseType>
390 template <
class Streamer,
bool WriteNeeded>
392 template <
class RequestType,
class ResponseType>
394 template <
class RequestType,
class ResponseType>
396 template <
class RequestType,
class ResponseType>
398 template <
class RequestType,
class ResponseType>
400 template <grpc::StatusCode code>
402 template <
class Base>
414 void BeginCompletionOp(
420 void set_call(
grpc_call* call,
bool call_metric_recording_enabled,
423 if (call_metric_recording_enabled) {
424 CreateCallMetricRecorder(server_metric_recorder);
430 uint32_t initial_metadata_flags()
const {
return 0; }
434 const std::vector<std::unique_ptr<
436 if (!creators.empty()) {
438 rpc_info_->RegisterInterceptors(creators);
443 void set_message_allocator_state(RpcAllocatorState* allocator_state) {
444 message_allocator_state_ = allocator_state;
447 void MaybeMarkCancelledOnRead() {
449 marked_cancelled_.store(
true, std::memory_order_release);
455 void CreateCallMetricRecorder(
456 experimental::ServerMetricRecorder* server_metric_recorder =
nullptr);
470 CompletionOp* completion_op_ =
nullptr;
471 bool has_notify_when_done_tag_ =
false;
472 void* async_notify_when_done_tag_ =
nullptr;
477 bool sent_initial_metadata_ =
false;
478 mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
480 std::multimap<std::string, std::string> initial_metadata_;
481 std::multimap<std::string, std::string> trailing_metadata_;
483 bool compression_level_set_ =
false;
490 bool has_pending_ops_ =
false;
493 RpcAllocatorState* message_allocator_state_ =
nullptr;
494 ContextAllocator* context_allocator_ =
nullptr;
495 experimental::CallMetricRecorder* call_metric_recorder_ =
nullptr;
499 void OnCancel()
override {}
500 void OnDone()
override {}
505 bool InternalInlineable()
override {
return true; }
508 void SetupTestDefaultReactor(std::function<
void(
grpc::Status)> func) {
510 test_unary_.reset(
new TestServerCallbackUnary(
this, std::move(func)));
512 bool test_status_set()
const {
513 return (test_unary_ !=
nullptr) && test_unary_->status_set();
515 grpc::Status test_status()
const {
return test_unary_->status(); }
527 status_set_.store(
true, std::memory_order_release);
529 void SendInitialMetadata()
override {}
531 bool status_set()
const {
532 return status_set_.load(std::memory_order_acquire);
537 void CallOnDone()
override {}
541 std::atomic_bool status_set_{
false};
546 typename std::aligned_storage<
sizeof(Reactor),
alignof(Reactor)>::type
548 std::atomic_bool default_reactor_used_{
false};
550 std::atomic_bool marked_cancelled_{
false};
552 std::unique_ptr<TestServerCallbackUnary> test_unary_;
673 std::is_base_of<grpc::ServerContextBase, grpc::ServerContext>::value,
674 "improper base class");
677 "improper base class");
684 #endif // GRPCPP_SERVER_CONTEXT_H
CallbackWithSuccessTag can be reused multiple times, and will be used in this fashion for streaming o...
Definition: callback_common.h:136
This class is a non owning reference to a string.
Definition: string_ref.h:41
struct census_context census_context
A Census Context is a handle used by Census to represent the current tracing and stats collection inf...
Definition: census.h:34
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Set algorithm to be the compression algorithm used for the server call.
void SetLoadReportingCosts(const std::vector< std::string > &cost_data)
Set the serialized load reporting costs in cost_data for the call.
Records server wide metrics to be reported to the client.
Definition: server_metric_recorder.h:42
virtual GenericCallbackServerContext * NewGenericCallbackServerContext()
Definition: server_context.h:661
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:571
Represents a gRPC server.
Definition: server.h:58
const struct census_context * census_context() const
Get the census context associated with this server call.
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:32
Primary implementation of CallOpSetInterface.
Definition: completion_queue.h:96
bool IsCancelled() const
Return whether this RPC failed before the server could provide its status back to the client.
Definition: server_context.h:611
void set_context_allocator(ContextAllocator *context_allocator)
Definition: server_context.h:353
Definition: call_op_set.h:286
Definition: message_allocator.h:26
GRPCAPI int grpc_call_failed_before_recv_message(const grpc_call *c)
Async server-side API for doing server streaming RPCs, where the outgoing message stream from the ser...
Definition: server_context.h:58
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: completion_queue.h:80
Definition: rpc_service_method.h:41
A wrapper class of an application provided rpc method handler.
Definition: completion_queue.h:72
Synchronous (blocking) server-side API for doing for doing a server-streaming RPCs,...
Definition: completion_queue.h:61
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level)
Definition: compression_types.h:60
grpc::ServerUnaryReactor * DefaultReactor()
Get a library-owned default unary reactor for use in minimal reaction cases.
Definition: server_context.h:332
Straightforward wrapping of the C call object.
Definition: call.h:36
Base class of ServerContext.
Definition: server_context.h:124
ContextAllocator * context_allocator() const
Definition: server_context.h:357
void BindReactor(Reactor *reactor)
Definition: server_callback.h:201
ServerContextBase()
Constructors for use by derived classes.
Definition: completion_queue.h:64
friend class grpc::testing::DefaultReactorTestPeer
Definition: server_context.h:362
friend class grpc::testing::ServerContextTestSpouse
Definition: server_context.h:361
Did it work? If it didn't, why?
Definition: status.h:35
virtual void Release(CallbackServerContext *)
Definition: server_context.h:665
Records call metrics for the purpose of load balancing.
Definition: call_metric_recorder.h:37
experimental::CallMetricRecorder * ExperimentalGetCallMetricRecorder()
Get the CallMetricRecorder object for the current RPC.
Definition: server_context.h:294
The base class of ServerCallbackUnary etc.
Definition: server_callback.h:73
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
TODO(ctiller): not sure we want to make this a permanent thing.
Synchronous (blocking) server-side API for doing client-streaming RPCs, where the incoming message st...
Definition: completion_queue.h:59
std::chrono::system_clock::time_point deadline() const
Return the deadline for the server call.
Definition: server_context.h:129
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the server call's deadline.
Definition: server_context.h:134
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:193
RpcAllocatorState * GetRpcAllocatorState()
NOTE: This is an API for advanced users who need custom allocators.
Definition: server_context.h:316
void AddInitialMetadata(const std::string &key, const std::string &value)
Add the (key, value) pair to the initial metadata associated with a server call.
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:69
Definition: server_callback_handlers.h:33
std::shared_ptr< const grpc::AuthContext > auth_context() const
Return the authentication context for this server call.
Definition: server_context.h:269
Definition: server_callback_handlers.h:442
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:249
virtual CallbackServerContext * NewCallbackServerContext()
Definition: server_context.h:659
RpcType
Definition: rpc_method.h:31
CallbackServerContext()
Public constructors are for direct use only by mocking tests.
Definition: server_context.h:615
Definition: async_generic_service.h:39
void AddTrailingMetadata(const std::string &key, const std::string &value)
Add the (key, value) pair to the initial metadata associated with a server call.
ServerRpcInfo represents the state of a particular RPC as it appears to an interceptor.
Definition: server_interceptor.h:57
Definition: server_context.h:88
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
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:228
Definition: server_interceptor.h:44
virtual ~ServerContextBase()
A wrapper class of an application provided server streaming handler.
Definition: completion_queue.h:76
Async server-side API for handling unary calls, where the single response message sent to the client ...
Definition: server_context.h:60
Definition: server_callback_handlers.h:668
A CallbackServerContext allows users to use the contents of the CallbackServerContext or GenericCallb...
Definition: server_context.h:655
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:74
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context.h:308
A wrapper class of an application provided bidi-streaming handler.
Definition: completion_queue.h:78
Definition: server_context.h:70
grpc_compression_level
Compression levels allow a party with knowledge of its peer's accepted encodings to request compressi...
Definition: compression_types.h:72
Definition: async_unary_call.h:405
friend class grpc::testing::InteropServerContextInspector
Definition: server_context.h:360
Definition: async_generic_service.h:87
std::string peer() const
Return the peer uri in a string.
Definition: server_callback.h:49
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:103
grpc::string_ref ExperimentalGetAuthority() const
EXPERIMENTAL API Returns the call's authority.
Definition: server_callback_handlers.h:251
void TryCancel() const
Cancel the Call from the server.
virtual ~ContextAllocator()
Definition: server_context.h:657
Async server-side API for doing client-streaming RPCs, where the incoming message stream from the cli...
Definition: server_context.h:56
Async server-side API for doing bidirectional streaming RPCs, where the incoming message stream comin...
Definition: server_context.h:62
Definition: server_interface.h:60
ServerContext()
Definition: server_context.h:573
Analogous to struct timespec.
Definition: time.h:48
void UnaryRunHandlerHelper(const grpc::internal::MethodHandler::HandlerParameter &, ResponseType *, grpc::Status &)
A helper function with reduced templating to do the common work needed to actually send the server re...
Definition: method_handler.h:58
Definition: server_callback.h:191
Definition: server_callback.h:697
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the server call will request be used.
Definition: server_context.h:255
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: server_context.h:287
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
virtual void Release(GenericCallbackServerContext *)
Definition: server_context.h:667
void set_compression_level(grpc_compression_level level)
Set level to be the compression level used for the server call.
Definition: server_context.h:241
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:234