Go to the documentation of this file.
19 #ifndef GRPCPP_SUPPORT_METHOD_HANDLER_H
20 #define GRPCPP_SUPPORT_METHOD_HANDLER_H
39 template <
class Callable>
41 #if GRPC_ALLOW_EXCEPTIONS
46 "Unexpected error in RPC handling");
48 #else // GRPC_ALLOW_EXCEPTIONS
50 #endif // GRPC_ALLOW_EXCEPTIONS
57 template <
class ResponseType>
71 status = ops.SendMessagePtr(rsp);
73 ops.ServerSendStatus(¶m.
server_context->trailing_metadata_, status);
75 param.
call->
cq()->Pluck(&ops);
80 template <
class RequestType>
82 RequestType* request) {
86 &buf,
static_cast<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());
173 status = ops.SendMessagePtr(&rsp);
175 ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status);
176 param.call->PerformOps(&ops);
177 param.call->cq()->Pluck(&ops);
184 ServiceType* service_;
188 template <
class ServiceType,
class RequestType,
class ResponseType>
195 ServiceType* service)
196 : func_(func), service_(service) {}
204 return func_(service_,
206 static_cast<RequestType*
>(param.request), &writer);
208 static_cast<RequestType*
>(param.request)->~RequestType();
214 if (!param.server_context->sent_initial_metadata_) {
215 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
216 param.server_context->initial_metadata_flags());
217 if (param.server_context->compression_level_set()) {
218 ops.set_compression_level(param.server_context->compression_level());
222 param.call->PerformOps(&ops);
223 if (param.server_context->has_pending_ops_) {
224 param.call->cq()->Pluck(¶m.server_context->pending_ops_);
226 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:191
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:572
Definition: call_op_set.h:654
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:96
Definition: call_op_set.h:286
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:80
Definition: rpc_service_method.h:41
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:198
Synchronous (blocking) server-side API for doing for doing a server-streaming RPCs,...
Definition: completion_queue.h:61
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.
#define GPR_ASSERT(x)
abort() the process if x is zero, having written a line to the log.
Definition: log.h:95
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
void ServerSendStatus(std::multimap< std::string, std::string > *trailing_metadata, const Status &status)
Definition: call_op_set.h:658
Call *const call
Definition: rpc_service_method.h:62
bool ok() const
Is the status OK?
Definition: status.h:126
A class to represent a flow-controlled server-side streaming call.
Definition: sync_stream.h:892
Did it work? If it didn't, why?
Definition: status.h:35
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:59
void * UnaryDeserializeHelper(grpc_byte_buffer *, grpc::Status *, RequestType *)
A helper function with reduced templating to do deserializing.
Definition: method_handler.h:81
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
Definition: grpc_types.h:43
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: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
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:38
Synchronous (blocking) server-side API for a bidirectional streaming call, where the incoming message...
Definition: sync_stream.h:785
grpc::ServerContextBase *const server_context
Definition: rpc_service_method.h:63
A wrapper class of an application provided bidi-streaming handler.
Definition: completion_queue.h:78
void PerformOps(CallOpSetInterface *ops)
Definition: call.h:66
ErrorMethodHandler(const std::string &message)
Definition: method_handler.h:358
Definition: async_unary_call.h:405
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:40
A class to represent a flow-controlled unary call.
Definition: sync_stream.h:826
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:144
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:58
::absl::Status Status
Definition: config_protobuf.h:97
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:229
@ 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