Go to the documentation of this file.
19 #ifndef GRPCPP_SUPPORT_METHOD_HANDLER_H
20 #define GRPCPP_SUPPORT_METHOD_HANDLER_H
27 #include "absl/base/optimization.h"
28 #include "absl/log/absl_check.h"
41 template <
class Callable>
43 #if GRPC_ALLOW_EXCEPTIONS
48 "Unexpected error in RPC handling");
50 #else // GRPC_ALLOW_EXCEPTIONS
52 #endif // GRPC_ALLOW_EXCEPTIONS
59 template <
class ResponseType>
75 ops.ServerSendStatus(¶m.
server_context->trailing_metadata_, status);
76 ops.FillOps(param.
call);
77 param.
call->
cq()->Pluck(&ops);
83 template <
class RequestType>
85 RequestType* request) {
97 template <
class ServiceType,
class RequestType,
class ResponseType,
98 class BaseRequestType = RequestType,
99 class BaseResponseType = ResponseType>
104 const RequestType*, ResponseType*)>
106 ServiceType* service)
107 : func_(func), service_(service) {}
114 return func_(service_,
116 static_cast<RequestType*
>(param.request), &rsp);
118 static_cast<RequestType*
>(param.request)->~RequestType();
128 req, status,
static_cast<BaseRequestType*
>(request));
129 if (ABSL_PREDICT_FALSE(result ==
nullptr)) {
130 request->~RequestType();
138 const RequestType*, ResponseType*)>
141 ServiceType* service_;
145 template <
class ServiceType,
class RequestType,
class ResponseType>
152 ServiceType* service)
153 : func_(func), service_(service) {}
161 return func_(service_,
170 if (!param.server_context->sent_initial_metadata_) {
171 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
172 param.server_context->initial_metadata_flags());
173 if (param.server_context->compression_level_set()) {
174 ops.set_compression_level(param.server_context->compression_level());
179 ops.SendMessagePtr(&rsp, param.server_context->memory_allocator());
181 ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status);
182 ops.FillOps(param.call);
183 param.call->cq()->Pluck(&ops);
190 ServiceType* service_;
194 template <
class ServiceType,
class RequestType,
class ResponseType>
201 ServiceType* service)
202 : func_(func), service_(service) {}
210 return func_(service_,
212 static_cast<RequestType*
>(param.request), &writer);
214 static_cast<RequestType*
>(param.request)->~RequestType();
220 if (!param.server_context->sent_initial_metadata_) {
221 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
222 param.server_context->initial_metadata_flags());
223 if (param.server_context->compression_level_set()) {
224 ops.set_compression_level(param.server_context->compression_level());
228 ops.FillOps(param.call);
229 if (param.server_context->has_pending_ops_) {
230 param.call->cq()->Pluck(¶m.server_context->pending_ops_);
232 param.call->cq()->Pluck(&ops);
246 request->~RequestType();
254 ServiceType* service_;
264 template <
class Streamer,
bool WriteNeeded>
269 : func_(func), write_needed_(WriteNeeded) {}
272 Streamer stream(param.call,
282 if (!param.server_context->sent_initial_metadata_) {
283 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
284 param.server_context->initial_metadata_flags());
285 if (param.server_context->compression_level_set()) {
286 ops.set_compression_level(param.server_context->compression_level());
288 if (write_needed_ && status.
ok()) {
292 "Service did not provide response message");
295 ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status);
296 ops.FillOps(param.call);
297 if (param.server_context->has_pending_ops_) {
298 param.call->cq()->Pluck(¶m.server_context->pending_ops_);
300 param.call->cq()->Pluck(&ops);
305 const bool write_needed_;
308 template <
class ServiceType,
class RequestType,
class ResponseType>
309 class BidiStreamingHandler
310 :
public TemplatedBidiStreamingHandler<
311 ServerReaderWriter<ResponseType, RequestType>, false> {
317 ServiceType* service)
324 return func(service, ctx, streamer);
328 template <
class RequestType,
class ResponseType>
331 ServerUnaryStreamer<RequestType, ResponseType>, true> {
343 template <
class RequestType,
class ResponseType>
346 ServerSplitStreamer<RequestType, ResponseType>, false> {
360 template <grpc::StatusCode code>
367 const std::string& message, T* ops) {
369 if (!context->sent_initial_metadata_) {
370 ops->SendInitialMetadata(&context->initial_metadata_,
371 context->initial_metadata_flags());
377 ops->ServerSendStatus(&context->trailing_metadata_, status);
384 FillOps(param.server_context, message_, &ops);
385 ops.FillOps(param.call);
386 param.call->cq()->Pluck(&ops);
392 if (req !=
nullptr) {
399 const std::string message_;
402 typedef ErrorMethodHandler<grpc::StatusCode::UNIMPLEMENTED>
410 #endif // GRPCPP_SUPPORT_METHOD_HANDLER_H
grpc::CompletionQueue * cq() const
Definition: call.h:56
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:155
ServerStreamingHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, const RequestType *, ServerWriter< ResponseType > *)> func, ServiceType *service)
Definition: method_handler.h:197
GRPCAPI void * grpc_call_arena_alloc(grpc_call *call, size_t size)
Allocate memory in the grpc_call arena: this memory is automatically discarded at call completion.
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:701
Definition: call_op_set.h:656
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
auto Deserialize(BufferPtr buffer, Message *msg)
Definition: serialization_traits.h:120
Definition: call_op_set.h:287
TemplatedBidiStreamingHandler(std::function< grpc::Status(grpc::ServerContext *, Streamer *)> func)
Definition: method_handler.h:267
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
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:204
Synchronous (blocking) server-side API for doing for doing a server-streaming RPCs,...
Definition: completion_queue.h:62
Definition: method_handler.h:344
ClientStreamingHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, ServerReader< RequestType > *, ResponseType *)> func, ServiceType *service)
Definition: method_handler.h:148
GRPCAPI void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)
Destroys byte_buffer deallocating all its memory.
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:271
RpcMethodHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, const RequestType *, ResponseType *)> func, ServiceType *service)
Definition: method_handler.h:102
Base class of ServerContext.
Definition: server_context.h:193
void * Deserialize(grpc_call *call, grpc_byte_buffer *req, grpc::Status *status, void **) final
Definition: method_handler.h:123
grpc_event_engine::experimental::MemoryAllocator * memory_allocator()
Definition: server_context.h:369
void ServerSendStatus(std::multimap< std::string, std::string > *trailing_metadata, const Status &status)
Definition: call_op_set.h:660
Call *const call
Definition: rpc_service_method.h:64
bool ok() const
Is the status OK?
Definition: status.h:124
A class to represent a flow-controlled server-side streaming call.
Definition: sync_stream.h:901
Did it work? If it didn't, why?
Definition: status.h:34
ErrorMethodHandler< grpc::StatusCode::RESOURCE_EXHAUSTED > ResourceExhaustedHandler
Definition: method_handler.h:405
Synchronous (blocking) server-side API for doing client-streaming RPCs, where the incoming message st...
Definition: completion_queue.h:60
void * UnaryDeserializeHelper(grpc_byte_buffer *, grpc::Status *, RequestType *)
A helper function with reduced templating to do deserializing.
Definition: method_handler.h:84
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:68
Definition: grpc_types.h:41
A sequence of bytes.
Definition: byte_buffer.h:67
BidiStreamingHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, ServerReaderWriter< ResponseType, RequestType > *)> func, ServiceType *service)
Definition: method_handler.h:313
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
Base class for running an RPC handler.
Definition: rpc_service_method.h:40
Synchronous (blocking) server-side API for a bidirectional streaming call, where the incoming message...
Definition: sync_stream.h:794
grpc::ServerContextBase *const server_context
Definition: rpc_service_method.h:65
A wrapper class of an application provided bidi-streaming handler.
Definition: completion_queue.h:79
ErrorMethodHandler(const std::string &message)
Definition: method_handler.h:363
Definition: async_unary_call.h:410
void MarkInitialMetadataSent()
Definition: server_context.h:424
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:380
StreamedUnaryHandler(std::function< grpc::Status(grpc::ServerContext *, ServerUnaryStreamer< RequestType, ResponseType > *)> func)
Definition: method_handler.h:333
static void FillOps(grpc::ServerContextBase *context, const std::string &message, T *ops)
Definition: method_handler.h:366
::grpc::Status CatchingFunctionHandler(Callable &&handler)
Definition: method_handler.h:42
A class to represent a flow-controlled unary call.
Definition: sync_stream.h:835
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:109
@ UNKNOWN
Unknown error.
Definition: status_code_enum.h:37
ErrorMethodHandler< grpc::StatusCode::UNIMPLEMENTED > UnknownMethodHandler
Definition: method_handler.h:403
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:151
Definition: method_handler.h:329
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
::absl::Status Status
Definition: config_protobuf.h:107
SplitServerStreamingHandler(std::function< grpc::Status(grpc::ServerContext *, ServerSplitStreamer< RequestType, ResponseType > *)> func)
Definition: method_handler.h:348
void * Deserialize(grpc_call *, grpc_byte_buffer *req, grpc::Status *, void **) final
Definition: method_handler.h:389
void * Deserialize(grpc_call *call, grpc_byte_buffer *req, grpc::Status *status, void **) final
Definition: method_handler.h:235
@ INTERNAL
Internal errors.
Definition: status_code_enum.h:121
grpc_compression_level compression_level() const
Return the compression algorithm to be used by the server call.
Definition: server_context.h:305