Go to the documentation of this file.
19 #ifndef GRPCPP_SUPPORT_METHOD_HANDLER_H
20 #define GRPCPP_SUPPORT_METHOD_HANDLER_H
22 #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>
73 status = ops.SendMessagePtr(rsp);
75 ops.ServerSendStatus(¶m.
server_context->trailing_metadata_, status);
77 param.
call->
cq()->Pluck(&ops);
82 template <
class RequestType>
84 RequestType* request) {
88 &buf,
static_cast<RequestType*
>(request));
93 request->~RequestType();
98 template <
class ServiceType,
class RequestType,
class ResponseType,
99 class BaseRequestType = RequestType,
100 class BaseResponseType = ResponseType>
105 const RequestType*, ResponseType*)>
107 ServiceType* service)
108 : func_(func), service_(service) {}
115 return func_(service_,
117 static_cast<RequestType*
>(param.request), &rsp);
119 static_cast<RequestType*
>(param.request)->~RequestType();
129 static_cast<BaseRequestType*
>(request));
135 const RequestType*, ResponseType*)>
138 ServiceType* service_;
142 template <
class ServiceType,
class RequestType,
class ResponseType>
149 ServiceType* service)
150 : func_(func), service_(service) {}
158 return func_(service_,
167 if (!param.server_context->sent_initial_metadata_) {
168 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
169 param.server_context->initial_metadata_flags());
170 if (param.server_context->compression_level_set()) {
171 ops.set_compression_level(param.server_context->compression_level());
175 status = ops.SendMessagePtr(&rsp);
177 ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status);
178 param.call->PerformOps(&ops);
179 param.call->cq()->Pluck(&ops);
186 ServiceType* service_;
190 template <
class ServiceType,
class RequestType,
class ResponseType>
197 ServiceType* service)
198 : func_(func), service_(service) {}
206 return func_(service_,
208 static_cast<RequestType*
>(param.request), &writer);
210 static_cast<RequestType*
>(param.request)->~RequestType();
216 if (!param.server_context->sent_initial_metadata_) {
217 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
218 param.server_context->initial_metadata_flags());
219 if (param.server_context->compression_level_set()) {
220 ops.set_compression_level(param.server_context->compression_level());
224 param.call->PerformOps(&ops);
225 if (param.server_context->has_pending_ops_) {
226 param.call->cq()->Pluck(¶m.server_context->pending_ops_);
228 param.call->cq()->Pluck(&ops);
243 request->~RequestType();
251 ServiceType* service_;
261 template <
class Streamer,
bool WriteNeeded>
266 : func_(func), write_needed_(WriteNeeded) {}
269 Streamer stream(param.call,
279 if (!param.server_context->sent_initial_metadata_) {
280 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
281 param.server_context->initial_metadata_flags());
282 if (param.server_context->compression_level_set()) {
283 ops.set_compression_level(param.server_context->compression_level());
285 if (write_needed_ && status.
ok()) {
289 "Service did not provide response message");
292 ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status);
293 param.call->PerformOps(&ops);
294 if (param.server_context->has_pending_ops_) {
295 param.call->cq()->Pluck(¶m.server_context->pending_ops_);
297 param.call->cq()->Pluck(&ops);
302 const bool write_needed_;
305 template <
class ServiceType,
class RequestType,
class ResponseType>
306 class BidiStreamingHandler
307 :
public TemplatedBidiStreamingHandler<
308 ServerReaderWriter<ResponseType, RequestType>, false> {
314 ServiceType* service)
321 return func(service, ctx, streamer);
325 template <
class RequestType,
class ResponseType>
328 ServerUnaryStreamer<RequestType, ResponseType>, true> {
340 template <
class RequestType,
class ResponseType>
343 ServerSplitStreamer<RequestType, ResponseType>, false> {
357 template <grpc::StatusCode code>
364 const std::string& message, T* ops) {
366 if (!context->sent_initial_metadata_) {
367 ops->SendInitialMetadata(&context->initial_metadata_,
368 context->initial_metadata_flags());
372 context->sent_initial_metadata_ =
true;
374 ops->ServerSendStatus(&context->trailing_metadata_, status);
381 FillOps(param.server_context, message_, &ops);
382 param.call->PerformOps(&ops);
383 param.call->cq()->Pluck(&ops);
389 if (req !=
nullptr) {
396 const std::string message_;
399 typedef ErrorMethodHandler<grpc::StatusCode::UNIMPLEMENTED>
407 #endif // GRPCPP_SUPPORT_METHOD_HANDLER_H
grpc::CompletionQueue * cq() const
Definition: call.h:71
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:152
ServerStreamingHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, const RequestType *, ServerWriter< ResponseType > *)> func, ServiceType *service)
Definition: method_handler.h:193
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:578
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:98
Definition: call_op_set.h:288
TemplatedBidiStreamingHandler(std::function< grpc::Status(grpc::ServerContext *, Streamer *)> func)
Definition: method_handler.h:264
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
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:200
Synchronous (blocking) server-side API for doing for doing a server-streaming RPCs,...
Definition: completion_queue.h:63
Definition: method_handler.h:341
ClientStreamingHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, ServerReader< RequestType > *, ResponseType *)> func, ServiceType *service)
Definition: method_handler.h:145
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:268
RpcMethodHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, const RequestType *, ResponseType *)> func, ServiceType *service)
Definition: method_handler.h:103
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:124
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:125
A class to represent a flow-controlled server-side streaming call.
Definition: sync_stream.h:894
Did it work? If it didn't, why?
Definition: status.h:34
ErrorMethodHandler< grpc::StatusCode::RESOURCE_EXHAUSTED > ResourceExhaustedHandler
Definition: method_handler.h:402
Synchronous (blocking) server-side API for doing client-streaming RPCs, where the incoming message st...
Definition: completion_queue.h:61
void * UnaryDeserializeHelper(grpc_byte_buffer *, grpc::Status *, RequestType *)
A helper function with reduced templating to do deserializing.
Definition: method_handler.h:83
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:69
Definition: grpc_types.h:42
A sequence of bytes.
Definition: byte_buffer.h:60
BidiStreamingHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, ServerReaderWriter< ResponseType, RequestType > *)> func, ServiceType *service)
Definition: method_handler.h:310
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
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
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:787
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:80
void PerformOps(CallOpSetInterface *ops)
Definition: call.h:66
ErrorMethodHandler(const std::string &message)
Definition: method_handler.h:360
Definition: async_unary_call.h:407
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:377
StreamedUnaryHandler(std::function< grpc::Status(grpc::ServerContext *, ServerUnaryStreamer< RequestType, ResponseType > *)> func)
Definition: method_handler.h:330
static void FillOps(grpc::ServerContextBase *context, const std::string &message, T *ops)
Definition: method_handler.h:363
::grpc::Status CatchingFunctionHandler(Callable &&handler)
Definition: method_handler.h:42
A class to represent a flow-controlled unary call.
Definition: sync_stream.h:828
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:110
@ UNKNOWN
Unknown error.
Definition: status_code_enum.h:37
ErrorMethodHandler< grpc::StatusCode::UNIMPLEMENTED > UnknownMethodHandler
Definition: method_handler.h:400
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:144
Definition: method_handler.h:326
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:106
SplitServerStreamingHandler(std::function< grpc::Status(grpc::ServerContext *, ServerSplitStreamer< RequestType, ResponseType > *)> func)
Definition: method_handler.h:345
void * Deserialize(grpc_call *, grpc_byte_buffer *req, grpc::Status *, void **) final
Definition: method_handler.h:386
void * Deserialize(grpc_call *call, grpc_byte_buffer *req, grpc::Status *status, void **) final
Definition: method_handler.h:231
@ 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