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>
72 status = ops.SendMessagePtr(rsp);
74 ops.ServerSendStatus(¶m.
server_context->trailing_metadata_, status);
76 param.
call->
cq()->Pluck(&ops);
81 template <
class RequestType>
83 RequestType* request) {
87 &buf,
static_cast<RequestType*
>(request));
92 request->~RequestType();
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 static_cast<BaseRequestType*
>(request));
134 const RequestType*, ResponseType*)>
137 ServiceType* service_;
141 template <
class ServiceType,
class RequestType,
class ResponseType>
148 ServiceType* service)
149 : func_(func), service_(service) {}
157 return func_(service_,
166 if (!param.server_context->sent_initial_metadata_) {
167 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
168 param.server_context->initial_metadata_flags());
169 if (param.server_context->compression_level_set()) {
170 ops.set_compression_level(param.server_context->compression_level());
174 status = ops.SendMessagePtr(&rsp);
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);
242 request->~RequestType();
250 ServiceType* service_;
260 template <
class Streamer,
bool WriteNeeded>
265 : func_(func), write_needed_(WriteNeeded) {}
268 Streamer stream(param.call,
278 if (!param.server_context->sent_initial_metadata_) {
279 ops.SendInitialMetadata(¶m.server_context->initial_metadata_,
280 param.server_context->initial_metadata_flags());
281 if (param.server_context->compression_level_set()) {
282 ops.set_compression_level(param.server_context->compression_level());
284 if (write_needed_ && status.
ok()) {
288 "Service did not provide response message");
291 ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status);
292 param.call->PerformOps(&ops);
293 if (param.server_context->has_pending_ops_) {
294 param.call->cq()->Pluck(¶m.server_context->pending_ops_);
296 param.call->cq()->Pluck(&ops);
301 const bool write_needed_;
304 template <
class ServiceType,
class RequestType,
class ResponseType>
305 class BidiStreamingHandler
306 :
public TemplatedBidiStreamingHandler<
307 ServerReaderWriter<ResponseType, RequestType>, false> {
313 ServiceType* service)
320 return func(service, ctx, streamer);
324 template <
class RequestType,
class ResponseType>
327 ServerUnaryStreamer<RequestType, ResponseType>, true> {
339 template <
class RequestType,
class ResponseType>
342 ServerSplitStreamer<RequestType, ResponseType>, false> {
356 template <grpc::StatusCode code>
363 const std::string& message, T* ops) {
365 if (!context->sent_initial_metadata_) {
366 ops->SendInitialMetadata(&context->initial_metadata_,
367 context->initial_metadata_flags());
371 context->sent_initial_metadata_ =
true;
373 ops->ServerSendStatus(&context->trailing_metadata_, status);
380 FillOps(param.server_context, message_, &ops);
381 param.call->PerformOps(&ops);
382 param.call->cq()->Pluck(&ops);
388 if (req !=
nullptr) {
395 const std::string message_;
398 typedef ErrorMethodHandler<grpc::StatusCode::UNIMPLEMENTED>
406 #endif // GRPCPP_SUPPORT_METHOD_HANDLER_H
grpc::CompletionQueue * cq() const
Definition: call.h:71
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:151
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: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:97
Definition: call_op_set.h:288
TemplatedBidiStreamingHandler(std::function< grpc::Status(grpc::ServerContext *, Streamer *)> func)
Definition: method_handler.h:263
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:340
ClientStreamingHandler(std::function< grpc::Status(ServiceType *, grpc::ServerContext *, ServerReader< RequestType > *, ResponseType *)> func, ServiceType *service)
Definition: method_handler.h:144
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:267
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:124
void * Deserialize(grpc_call *call, grpc_byte_buffer *req, grpc::Status *status, void **) final
Definition: method_handler.h:123
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:893
Did it work? If it didn't, why?
Definition: status.h:34
ErrorMethodHandler< grpc::StatusCode::RESOURCE_EXHAUSTED > ResourceExhaustedHandler
Definition: method_handler.h:401
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:309
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:786
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:359
Definition: async_unary_call.h:406
void RunHandler(const HandlerParameter ¶m) final
Definition: method_handler.h:376
StreamedUnaryHandler(std::function< grpc::Status(grpc::ServerContext *, ServerUnaryStreamer< RequestType, ResponseType > *)> func)
Definition: method_handler.h:329
static void FillOps(grpc::ServerContextBase *context, const std::string &message, T *ops)
Definition: method_handler.h:362
::grpc::Status CatchingFunctionHandler(Callable &&handler)
Definition: method_handler.h:41
A class to represent a flow-controlled unary call.
Definition: sync_stream.h:827
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:399
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:325
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:344
void * Deserialize(grpc_call *, grpc_byte_buffer *req, grpc::Status *, void **) final
Definition: method_handler.h:385
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