Go to the documentation of this file.
19 #ifndef GRPCPP_SUPPORT_METHOD_HANDLER_H
20 #define GRPCPP_SUPPORT_METHOD_HANDLER_H
27 #include "absl/log/absl_check.h"
40 template <
class Callable>
42 #if GRPC_ALLOW_EXCEPTIONS
47 "Unexpected error in RPC handling");
49 #else // GRPC_ALLOW_EXCEPTIONS
51 #endif // GRPC_ALLOW_EXCEPTIONS
58 template <
class ResponseType>
74 ops.ServerSendStatus(¶m.
server_context->trailing_metadata_, status);
76 param.
call->
cq()->Pluck(&ops);
81 template <
class RequestType>
83 RequestType* request) {
91 request->~RequestType();
96 template <
class ServiceType,
class RequestType,
class ResponseType,
97 class BaseRequestType = RequestType,
98 class BaseResponseType = ResponseType>
103 const RequestType*, ResponseType*)>
105 ServiceType* service)
106 : func_(func), service_(service) {}
113 return func_(service_,
115 static_cast<RequestType*
>(param.request), &rsp);
117 static_cast<RequestType*
>(param.request)->~RequestType();
127 static_cast<BaseRequestType*
>(request));
133 const RequestType*, ResponseType*)>
136 ServiceType* service_;
140 template <
class ServiceType,
class RequestType,
class ResponseType>
147 ServiceType* service)
148 : func_(func), service_(service) {}
156 return func_(service_,
165 if (!param.server_context->sent_initial_metadata_) {
166 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
167 param.server_context->initial_metadata_flags());
168 if (param.server_context->compression_level_set()) {
169 ops.set_compression_level(param.server_context->compression_level());
174 ops.SendMessagePtr(&rsp, param.server_context->memory_allocator());
176 ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status);
177 param.call->PerformOps(&ops);
178 param.call->cq()->Pluck(&ops);
185 ServiceType* service_;
189 template <
class ServiceType,
class RequestType,
class ResponseType>
196 ServiceType* service)
197 : func_(func), service_(service) {}
205 return func_(service_,
207 static_cast<RequestType*
>(param.request), &writer);
209 static_cast<RequestType*
>(param.request)->~RequestType();
215 if (!param.server_context->sent_initial_metadata_) {
216 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
217 param.server_context->initial_metadata_flags());
218 if (param.server_context->compression_level_set()) {
219 ops.set_compression_level(param.server_context->compression_level());
223 param.call->PerformOps(&ops);
224 if (param.server_context->has_pending_ops_) {
225 param.call->cq()->Pluck(¶m.server_context->pending_ops_);
227 param.call->cq()->Pluck(&ops);
241 request->~RequestType();
249 ServiceType* service_;
259 template <
class Streamer,
bool WriteNeeded>
264 : func_(func), write_needed_(WriteNeeded) {}
267 Streamer stream(param.call,
277 if (!param.server_context->sent_initial_metadata_) {
278 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
279 param.server_context->initial_metadata_flags());
280 if (param.server_context->compression_level_set()) {
281 ops.set_compression_level(param.server_context->compression_level());
283 if (write_needed_ && status.
ok()) {
287 "Service did not provide response message");
290 ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status);
291 param.call->PerformOps(&ops);
292 if (param.server_context->has_pending_ops_) {
293 param.call->cq()->Pluck(¶m.server_context->pending_ops_);
295 param.call->cq()->Pluck(&ops);
300 const bool write_needed_;
303 template <
class ServiceType,
class RequestType,
class ResponseType>
304 class BidiStreamingHandler
305 :
public TemplatedBidiStreamingHandler<
306 ServerReaderWriter<ResponseType, RequestType>, false> {
312 ServiceType* service)
319 return func(service, ctx, streamer);
323 template <
class RequestType,
class ResponseType>
326 ServerUnaryStreamer<RequestType, ResponseType>, true> {
338 template <
class RequestType,
class ResponseType>
341 ServerSplitStreamer<RequestType, ResponseType>, false> {
355 template <grpc::StatusCode code>
362 const std::string& message, T* ops) {
364 if (!context->sent_initial_metadata_) {
365 ops->SendInitialMetadata(&context->initial_metadata_,
366 context->initial_metadata_flags());
370 context->sent_initial_metadata_ =
true;
372 ops->ServerSendStatus(&context->trailing_metadata_, status);
379 FillOps(param.server_context, message_, &ops);
380 param.call->PerformOps(&ops);
381 param.call->cq()->Pluck(&ops);
387 if (req !=
nullptr) {
394 const std::string message_;
397 typedef ErrorMethodHandler<grpc::StatusCode::UNIMPLEMENTED>
405 #endif // GRPCPP_SUPPORT_METHOD_HANDLER_H
grpc::CompletionQueue * cq() const
Definition: call.h:71
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:150
ServerStreamingHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, const RequestType *, ServerWriter< ResponseType > *)> func, ServiceType *service)
Definition: method_handler.h:192
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:586
Definition: call_op_set.h:658
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:289
TemplatedBidiStreamingHandler(std::function< grpc::Status(grpc::ServerContext *, Streamer *)> func)
Definition: method_handler.h:262
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:199
Synchronous (blocking) server-side API for doing for doing a server-streaming RPCs,...
Definition: completion_queue.h:62
Definition: method_handler.h:339
ClientStreamingHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, ServerReader< RequestType > *, ResponseType *)> func, ServiceType *service)
Definition: method_handler.h:143
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:266
RpcMethodHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, const RequestType *, ResponseType *)> func, ServiceType *service)
Definition: method_handler.h:101
Base class of ServerContext.
Definition: server_context.h:124
void * Deserialize(grpc_call *call, grpc_byte_buffer *req, grpc::Status *status, void **) final
Definition: method_handler.h:122
grpc_event_engine::experimental::MemoryAllocator * memory_allocator()
Definition: server_context.h:300
void ServerSendStatus(std::multimap< std::string, std::string > *trailing_metadata, const Status &status)
Definition: call_op_set.h:662
Call *const call
Definition: rpc_service_method.h:64
bool ok() const
Is the status OK?
Definition: status.h:125
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:400
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:82
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:59
BidiStreamingHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, ServerReaderWriter< ResponseType, RequestType > *)> func, ServiceType *service)
Definition: method_handler.h:308
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
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
void PerformOps(CallOpSetInterface *ops)
Definition: call.h:66
ErrorMethodHandler(const std::string &message)
Definition: method_handler.h:358
Definition: async_unary_call.h:410
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:375
StreamedUnaryHandler(std::function< grpc::Status(grpc::ServerContext *, ServerUnaryStreamer< RequestType, ResponseType > *)> func)
Definition: method_handler.h:328
static void FillOps(grpc::ServerContextBase *context, const std::string &message, T *ops)
Definition: method_handler.h:361
::grpc::Status CatchingFunctionHandler(Callable &&handler)
Definition: method_handler.h:41
A class to represent a flow-controlled unary call.
Definition: sync_stream.h:835
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:108
@ UNKNOWN
Unknown error.
Definition: status_code_enum.h:37
ErrorMethodHandler< grpc::StatusCode::UNIMPLEMENTED > UnknownMethodHandler
Definition: method_handler.h:398
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:143
Definition: method_handler.h:324
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
::absl::Status Status
Definition: config_protobuf.h:107
SplitServerStreamingHandler(std::function< grpc::Status(grpc::ServerContext *, ServerSplitStreamer< RequestType, ResponseType > *)> func)
Definition: method_handler.h:343
void * Deserialize(grpc_call *, grpc_byte_buffer *req, grpc::Status *, void **) final
Definition: method_handler.h:384
void * Deserialize(grpc_call *call, grpc_byte_buffer *req, grpc::Status *status, void **) final
Definition: method_handler.h:230
@ 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:236