Go to the documentation of this file.
19 #ifndef GRPCPP_SERVER_CONTEXT_H
20 #define GRPCPP_SERVER_CONTEXT_H
26 #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 {
232 return *client_metadata_.
map();
237 return compression_level_;
244 compression_level_set_ =
true;
245 compression_level_ = level;
258 return compression_algorithm_;
272 if (auth_context_ ==
nullptr) {
275 return auth_context_;
282 std::string
peer()
const;
297 return call_metric_recorder_;
311 has_notify_when_done_tag_ =
true;
312 async_notify_when_done_tag_ = tag;
337 if (test_unary_ !=
nullptr) {
338 return reinterpret_cast<Reactor*
>(&default_reactor_);
340 new (&default_reactor_) Reactor;
343 assert(default_reactor_used_.compare_exchange_strong(
344 old,
true, std::memory_order_relaxed));
346 default_reactor_used_.store(
true, std::memory_order_relaxed);
348 return reinterpret_cast<Reactor*
>(&default_reactor_);
367 template <
class W,
class R>
373 template <
class W,
class R>
379 template <
class W,
class R>
381 template <
class ResponseType>
385 template <
class ServiceType,
class RequestType,
class ResponseType,
386 class BaseRequestType,
class BaseResponseType>
388 template <
class ServiceType,
class RequestType,
class ResponseType>
390 template <
class ServiceType,
class RequestType,
class ResponseType>
392 template <
class Streamer,
bool WriteNeeded>
394 template <
class RequestType,
class ResponseType>
396 template <
class RequestType,
class ResponseType>
398 template <
class RequestType,
class ResponseType>
400 template <
class RequestType,
class ResponseType>
402 template <grpc::StatusCode code>
404 template <
class Base>
416 void BeginCompletionOp(
422 void set_call(
grpc_call* call,
bool call_metric_recording_enabled,
425 if (call_metric_recording_enabled) {
426 CreateCallMetricRecorder(server_metric_recorder);
432 uint32_t initial_metadata_flags()
const {
return 0; }
436 const std::vector<std::unique_ptr<
438 if (!creators.empty()) {
440 rpc_info_->RegisterInterceptors(creators);
445 void set_message_allocator_state(RpcAllocatorState* allocator_state) {
446 message_allocator_state_ = allocator_state;
449 void MaybeMarkCancelledOnRead() {
451 marked_cancelled_.store(
true, std::memory_order_release);
457 void CreateCallMetricRecorder(
458 experimental::ServerMetricRecorder* server_metric_recorder =
nullptr);
472 CompletionOp* completion_op_ =
nullptr;
473 bool has_notify_when_done_tag_ =
false;
474 void* async_notify_when_done_tag_ =
nullptr;
479 bool sent_initial_metadata_ =
false;
480 mutable std::shared_ptr<const grpc::AuthContext> auth_context_;
482 std::multimap<std::string, std::string> initial_metadata_;
483 std::multimap<std::string, std::string> trailing_metadata_;
485 bool compression_level_set_ =
false;
492 bool has_pending_ops_ =
false;
495 RpcAllocatorState* message_allocator_state_ =
nullptr;
496 ContextAllocator* context_allocator_ =
nullptr;
497 experimental::CallMetricRecorder* call_metric_recorder_ =
nullptr;
501 void OnCancel()
override {}
502 void OnDone()
override {}
507 bool InternalInlineable()
override {
return true; }
510 void SetupTestDefaultReactor(std::function<
void(
grpc::Status)> func) {
512 test_unary_.reset(
new TestServerCallbackUnary(
this, std::move(func)));
514 bool test_status_set()
const {
515 return (test_unary_ !=
nullptr) && test_unary_->status_set();
517 grpc::Status test_status()
const {
return test_unary_->status(); }
524 func_(
std::move(func)),
531 status_set_.store(
true, std::memory_order_release);
533 void SendInitialMetadata()
override {}
535 bool status_set()
const {
536 return status_set_.load(std::memory_order_acquire);
541 void CallOnDone()
override {}
543 grpc_call* call()
override {
return call_; }
548 std::atomic_bool status_set_{
false};
554 alignas(Reactor)
char default_reactor_[
sizeof(Reactor)];
555 std::atomic_bool default_reactor_used_{
false};
557 std::atomic_bool marked_cancelled_{
false};
559 std::unique_ptr<TestServerCallbackUnary> test_unary_;
680 std::is_base_of<grpc::ServerContextBase, grpc::ServerContext>::value,
681 "improper base class");
684 "improper base class");
691 #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:138
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:33
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:668
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:578
Represents a gRPC server.
Definition: server.h:57
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:98
bool IsCancelled() const
Return whether this RPC failed before the server could provide its status back to the client.
Definition: server_context.h:618
void set_context_allocator(ContextAllocator *context_allocator)
Definition: server_context.h:355
Definition: call_op_set.h:288
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:82
Definition: rpc_service_method.h:43
A wrapper class of an application provided rpc method handler.
Definition: completion_queue.h:74
Synchronous (blocking) server-side API for doing for doing a server-streaming RPCs,...
Definition: completion_queue.h:63
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:334
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:359
void BindReactor(Reactor *reactor)
Definition: server_callback.h:210
ServerContextBase()
Constructors for use by derived classes.
Definition: completion_queue.h:66
friend class grpc::testing::DefaultReactorTestPeer
Definition: server_context.h:364
friend class grpc::testing::ServerContextTestSpouse
Definition: server_context.h:363
Did it work? If it didn't, why?
Definition: status.h:34
virtual void Release(CallbackServerContext *)
Definition: server_context.h:672
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:296
The base class of ServerCallbackUnary etc.
Definition: server_callback.h:76
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:61
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:195
RpcAllocatorState * GetRpcAllocatorState()
NOTE: This is an API for advanced users who need custom allocators.
Definition: server_context.h:318
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:36
std::shared_ptr< const grpc::AuthContext > auth_context() const
Return the authentication context for this server call.
Definition: server_context.h:271
Definition: server_callback_handlers.h:449
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:251
virtual CallbackServerContext * NewCallbackServerContext()
Definition: server_context.h:666
RpcType
Definition: rpc_method.h:31
CallbackServerContext()
Public constructors are for direct use only by mocking tests.
Definition: server_context.h:622
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:59
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:230
Definition: server_interceptor.h:46
virtual ~ServerContextBase()
A wrapper class of an application provided server streaming handler.
Definition: completion_queue.h:78
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:677
A CallbackServerContext allows users to use the contents of the CallbackServerContext or GenericCallb...
Definition: server_context.h:662
A wrapper class of an application provided client streaming handler.
Definition: completion_queue.h:76
void AsyncNotifyWhenDone(void *tag)
Async only.
Definition: server_context.h:310
A wrapper class of an application provided bidi-streaming handler.
Definition: completion_queue.h:80
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:407
friend class grpc::testing::InteropServerContextInspector
Definition: server_context.h:362
Definition: callback_generic_service.h:36
std::string peer() const
Return the peer uri in a string.
Definition: server_callback.h:52
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:105
grpc::string_ref ExperimentalGetAuthority() const
EXPERIMENTAL API Returns the call's authority.
Definition: server_callback_handlers.h:256
void TryCancel() const
Cancel the Call from the server.
virtual ~ContextAllocator()
Definition: server_context.h:664
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:61
ServerContext()
Definition: server_context.h:580
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:60
Definition: server_callback.h:200
Definition: server_callback.h:706
grpc_compression_algorithm compression_algorithm() const
Return the compression algorithm the server call will request be used.
Definition: server_context.h:257
grpc_call * c_call()
Should be used for framework-level extensions only.
Definition: server_context.h:289
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
virtual void Release(GenericCallbackServerContext *)
Definition: server_context.h:674
void set_compression_level(grpc_compression_level level)
Set level to be the compression level used for the server call.
Definition: server_context.h:243
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:236