Go to the documentation of this file.
19 #ifndef GRPCPP_SERVER_CONTEXT_H
20 #define GRPCPP_SERVER_CONTEXT_H
49 #include <type_traits>
57 template <
class W,
class R>
63 template <
class W,
class R>
71 template <
class ServiceType,
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 RequestType,
class ResponseType>
81 template <
class ServiceType,
class RequestType,
class ResponseType>
83 template <
class ResponseType>
86 template <
class ServiceType,
class RequestType,
class ResponseType,
87 class BaseRequestType,
class BaseResponseType>
91 template <
class W,
class R>
93 template <
class ServiceType,
class RequestType,
class ResponseType>
96 template <
class Streamer,
bool WriteNeeded>
98 template <grpc::StatusCode code>
117 template <
typename T>
119 static uint16_t
id() {
120 static const uint16_t
id =
122 delete static_cast<std::shared_ptr<T>*
>(element);
127 static void*
Wrap(std::shared_ptr<T> context) {
128 return new std::shared_ptr<T>(std::move(context));
131 static std::shared_ptr<T>
Unwrap(
void* ptr) {
132 return ptr !=
nullptr ? *
static_cast<std::shared_ptr<T>*
>(ptr) :
nullptr;
138 class InteropServerContextInspector;
139 class ServerContextTestSpouse;
140 class DefaultReactorTestPeer;
143 class ServerContextBase;
145 namespace experimental {
166 template <
typename T>
168 std::shared_ptr<T> session_context);
181 template <
typename T>
184 class CallMetricRecorder;
185 class ServerMetricRecorder;
187 template <
class RequestType>
188 class CallbackSessionHandler;
198 std::chrono::system_clock::time_point
deadline()
const {
301 return *client_metadata_.
map();
306 return compression_level_;
313 compression_level_set_ =
true;
314 compression_level_ = level;
327 return compression_algorithm_;
341 if (auth_context_ ==
nullptr) {
344 return auth_context_;
351 std::string
peer()
const;
366 return call_metric_recorder_;
370 return memory_allocator_;
384 has_notify_when_done_tag_ =
true;
385 async_notify_when_done_tag_ = tag;
410 if (test_unary_ !=
nullptr) {
411 return reinterpret_cast<Reactor*
>(&default_reactor_);
413 new (&default_reactor_) Reactor;
416 assert(default_reactor_used_.compare_exchange_strong(
417 old,
true, std::memory_order_relaxed));
419 default_reactor_used_.store(
true, std::memory_order_relaxed);
421 return reinterpret_cast<Reactor*
>(&default_reactor_);
431 sent_initial_metadata_.store(
true, std::memory_order_release);
450 template <
class W,
class R>
456 template <
class W,
class R>
462 template <
class W,
class R>
464 template <
class ResponseType>
468 template <
class ServiceType,
class RequestType,
class ResponseType,
469 class BaseRequestType,
class BaseResponseType>
471 template <
class ServiceType,
class RequestType,
class ResponseType>
473 template <
class ServiceType,
class RequestType,
class ResponseType>
475 template <
class Streamer,
bool WriteNeeded>
477 template <
class RequestType,
class ResponseType>
479 template <
class RequestType,
class ResponseType>
481 template <
class RequestType,
class ResponseType>
483 template <
class RequestType,
class ResponseType>
485 template <
class RequestType>
487 template <grpc::StatusCode code>
489 template <
class Base>
495 template <
typename T>
498 template <
typename T>
502 template <
typename T>
503 void SetSessionContext(std::shared_ptr<T> context) {
504 if (!sent_initial_metadata_.load(std::memory_order_acquire)) {
506 if (!sent_initial_metadata_.load(std::memory_order_acquire)) {
514 template <
typename T>
515 std::shared_ptr<T> GetSessionContext()
const {
516 if (sent_initial_metadata_.load(std::memory_order_acquire)) {
534 void BeginCompletionOp(
541 grpc_call* call,
bool call_metric_recording_enabled,
542 experimental::ServerMetricRecorder* server_metric_recorder,
545 if (call_metric_recording_enabled) {
546 CreateCallMetricRecorder(server_metric_recorder);
553 uint32_t initial_metadata_flags()
const {
return 0; }
557 const std::vector<std::unique_ptr<
559 if (!creators.empty()) {
561 rpc_info_->RegisterInterceptors(creators);
566 void set_message_allocator_state(RpcAllocatorState* allocator_state) {
567 message_allocator_state_ = allocator_state;
570 void MaybeMarkCancelledOnRead() {
572 marked_cancelled_.store(
true, std::memory_order_release);
578 void CreateCallMetricRecorder(
579 experimental::ServerMetricRecorder* server_metric_recorder =
nullptr);
593 CompletionOp* completion_op_ =
nullptr;
594 bool has_notify_when_done_tag_ =
false;
595 void* async_notify_when_done_tag_ =
nullptr;
600 std::atomic<bool> sent_initial_metadata_{
false};
602 mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
604 std::multimap<std::string, std::string> initial_metadata_;
605 std::multimap<std::string, std::string> trailing_metadata_;
607 bool compression_level_set_ =
false;
614 bool has_pending_ops_ =
false;
617 RpcAllocatorState* message_allocator_state_ =
nullptr;
618 ContextAllocator* context_allocator_ =
nullptr;
619 experimental::CallMetricRecorder* call_metric_recorder_ =
nullptr;
624 void OnCancel()
override {}
625 void OnDone()
override {}
630 bool InternalInlineable()
override {
return true; }
633 void SetupTestDefaultReactor(std::function<
void(
grpc::Status)> func) {
635 test_unary_.reset(
new TestServerCallbackUnary(
this, std::move(func)));
637 bool test_status_set()
const {
638 return (test_unary_ !=
nullptr) && test_unary_->status_set();
640 grpc::Status test_status()
const {
return test_unary_->status(); }
647 func_(
std::move(func)),
654 status_set_.store(
true, std::memory_order_release);
656 void SendInitialMetadata()
override {}
658 bool status_set()
const {
659 return status_set_.load(std::memory_order_acquire);
664 void CallOnDone()
override {}
666 grpc_call* call()
override {
return call_; }
671 std::atomic_bool status_set_{
false};
677 alignas(Reactor)
char default_reactor_[
sizeof(Reactor)];
678 std::atomic_bool default_reactor_used_{
false};
680 std::atomic_bool marked_cancelled_{
false};
682 std::unique_ptr<TestServerCallbackUnary> test_unary_;
800 namespace experimental {
802 template <
typename T>
804 std::shared_ptr<T> session_context) {
805 context->SetSessionContext(std::move(session_context));
808 template <
typename T>
810 return context->GetSessionContext<T>();
818 std::is_base_of<grpc::ServerContextBase, grpc::ServerContext>::value,
819 "improper base class");
822 "improper base class");
829 #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:153
This class is a non owning reference to a string.
Definition: string_ref.h:40
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:33
uint16_t ServerContextRegisterSessionContext(void(*destroy)(void *))
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.
virtual GenericCallbackServerContext * NewGenericCallbackServerContext()
Definition: server_context.h:791
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:701
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:33
Primary implementation of CallOpSetInterface.
Definition: completion_queue.h:97
bool IsCancelled() const
Return whether this RPC failed before the server could provide its status back to the client.
Definition: server_context.h:741
void set_context_allocator(ContextAllocator *context_allocator)
Definition: server_context.h:438
Definition: call_op_set.h:287
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:60
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: completion_queue.h:81
Definition: rpc_service_method.h:43
A wrapper class of an application provided rpc method handler.
Definition: completion_queue.h:73
Synchronous (blocking) server-side API for doing for doing a server-streaming RPCs,...
Definition: completion_queue.h:62
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level)
Definition: compression_types.h:61
grpc::ServerUnaryReactor * DefaultReactor()
Get a library-owned default unary reactor for use in minimal reaction cases.
Definition: server_context.h:407
Straightforward wrapping of the C call object.
Definition: call.h:34
Base class of ServerContext.
Definition: server_context.h:193
grpc_event_engine::experimental::MemoryAllocator * memory_allocator()
Definition: server_context.h:369
ContextAllocator * context_allocator() const
Definition: server_context.h:442
void BindReactor(Reactor *reactor)
Definition: server_callback.h:234
ServerContextBase()
Constructors for use by derived classes.
Definition: completion_queue.h:65
static uint16_t id()
Definition: server_context.h:119
friend class grpc::testing::DefaultReactorTestPeer
Definition: server_context.h:447
friend class grpc::testing::ServerContextTestSpouse
Definition: server_context.h:446
Did it work? If it didn't, why?
Definition: status.h:34
virtual void Release(CallbackServerContext *)
Definition: server_context.h:795
Records call metrics for the purpose of load balancing.
Definition: call_metric_recorder.h:36
experimental::CallMetricRecorder * ExperimentalGetCallMetricRecorder()
Get the CallMetricRecorder object for the current RPC.
Definition: server_context.h:365
The base class of ServerCallbackUnary etc.
Definition: server_callback.h:95
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:60
std::chrono::system_clock::time_point deadline() const
Return the deadline for the server call.
Definition: server_context.h:198
gpr_timespec raw_deadline() const
Return a gpr_timespec representation of the server call's deadline.
Definition: server_context.h:203
static void * Wrap(std::shared_ptr< T > context)
Definition: server_context.h:127
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:203
RpcAllocatorState * GetRpcAllocatorState()
NOTE: This is an API for advanced users who need custom allocators.
Definition: server_context.h:391
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:68
Definition: server_callback_handlers.h:38
std::shared_ptr< const grpc::AuthContext > auth_context() const
Return the authentication context for this server call.
Definition: server_context.h:340
Definition: server_callback_handlers.h:452
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:320
virtual CallbackServerContext * NewCallbackServerContext()
Definition: server_context.h:789
RpcType
Definition: rpc_method.h:31
CallbackServerContext()
Public constructors are for direct use only by mocking tests.
Definition: server_context.h:745
Definition: async_generic_service.h:37
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:58
Definition: server_context.h:90
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
Definition: server_context.h:118
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:299
Definition: server_callback_handlers.h:912
Definition: server_interceptor.h:45
virtual ~ServerContextBase()
A wrapper class of an application provided server streaming handler.
Definition: completion_queue.h:77
Async server-side API for handling unary calls, where the single response message sent to the client ...
Definition: server_context.h:62
Definition: server_callback_handlers.h:683
A CallbackServerContext allows users to use the contents of the CallbackServerContext or GenericCallb...
Definition: server_context.h:785
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:75
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context.h:383
A wrapper class of an application provided bidi-streaming handler.
Definition: completion_queue.h:79
Definition: server_context.h:72
void SetSessionContext(ServerContextBase *context, std::shared_ptr< T > session_context)
Set the session context.
Definition: server_context.h:803
std::shared_ptr< T > GetSessionContext(const ServerContextBase *context)
Get the session context.
Definition: server_context.h:809
grpc_compression_level
Compression levels allow a party with knowledge of its peer's accepted encodings to request compressi...
Definition: compression_types.h:73
Definition: async_unary_call.h:410
friend class grpc::testing::InteropServerContextInspector
Definition: server_context.h:445
Definition: callback_generic_service.h:36
std::string peer() const
Return the peer uri in a string.
Definition: server_callback.h:71
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:104
grpc::string_ref ExperimentalGetAuthority() const
EXPERIMENTAL API Returns the call's authority.
void MarkInitialMetadataSent()
Definition: server_context.h:424
Definition: server_callback_handlers.h:258
Definition: memory_allocator.h:33
void TryCancel() const
Cancel the Call from the server.
static std::shared_ptr< T > Unwrap(void *ptr)
Definition: server_context.h:131
virtual ~ContextAllocator()
Definition: server_context.h:787
Async server-side API for doing client-streaming RPCs, where the incoming message stream from the cli...
Definition: server_context.h:58
Async server-side API for doing bidirectional streaming RPCs, where the incoming message stream comin...
Definition: server_context.h:64
Definition: server_interface.h:59
ServerContext()
Definition: server_context.h:703
Analogous to struct timespec.
Definition: time.h:47
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:59
Definition: server_callback.h:224
void * ServerContextGetSessionContext(grpc_call *call, uint16_t id)
Definition: server_callback.h:749
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the server call will request be used.
Definition: server_context.h:326
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: server_context.h:358
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
virtual void Release(GenericCallbackServerContext *)
Definition: server_context.h:797
void set_compression_level(grpc_compression_level level)
Set level to be the compression level used for the server call.
Definition: server_context.h:312
void ServerContextSetSessionContext(grpc_call *call, uint16_t id, void *ptr)
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:305