GRPC C++  1.62.0
rpc_service_method.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPCPP_IMPL_RPC_SERVICE_METHOD_H
20 #define GRPCPP_IMPL_RPC_SERVICE_METHOD_H
21 
22 #include <climits>
23 #include <functional>
24 #include <map>
25 #include <memory>
26 #include <vector>
27 
28 #include <grpc/support/log.h>
29 #include <grpcpp/impl/rpc_method.h>
31 #include <grpcpp/support/config.h>
32 #include <grpcpp/support/status.h>
33 
34 namespace grpc {
35 class ServerContextBase;
36 namespace internal {
39  public:
40  virtual ~MethodHandler() {}
53  Status req_status, void* handler_data,
54  std::function<void()> requester)
55  : call(c),
56  server_context(context),
57  request(req),
58  status(req_status),
59  internal_data(handler_data),
60  call_requester(std::move(requester)) {}
62  Call* const call;
64  void* const request;
65  const Status status;
66  void* const internal_data;
67  const std::function<void()> call_requester;
68  };
69  virtual void RunHandler(const HandlerParameter& param) = 0;
70 
71  // Returns a pointer to the deserialized request. \a status reflects the
72  // result of deserialization. This pointer and the status should be filled in
73  // a HandlerParameter and passed to RunHandler. It is illegal to access the
74  // pointer after calling RunHandler. Ownership of the deserialized request is
75  // retained by the handler. Returns nullptr if deserialization failed.
76  virtual void* Deserialize(grpc_call* /*call*/, grpc_byte_buffer* req,
77  Status* /*status*/, void** /*handler_data*/) {
78  GPR_ASSERT(req == nullptr);
79  return nullptr;
80  }
81 };
82 
84 class RpcServiceMethod : public RpcMethod {
85  public:
89  : RpcMethod(name, type),
90  server_tag_(nullptr),
91  api_type_(ApiType::SYNC),
92  handler_(handler) {}
93 
94  enum class ApiType {
95  SYNC,
96  ASYNC,
97  RAW,
98  CALL_BACK, // not CALLBACK because that is reserved in Windows
100  };
101 
102  void set_server_tag(void* tag) { server_tag_ = tag; }
103  void* server_tag() const { return server_tag_; }
105  MethodHandler* handler() const { return handler_.get(); }
106  ApiType api_type() const { return api_type_; }
107  void SetHandler(MethodHandler* handler) { handler_.reset(handler); }
109  if ((api_type_ == ApiType::SYNC) &&
110  (type == ApiType::ASYNC || type == ApiType::RAW)) {
111  // this marks this method as async
112  handler_.reset();
113  } else if (api_type_ != ApiType::SYNC) {
114  // this is not an error condition, as it allows users to declare a server
115  // like WithRawMethod_foo<AsyncService>. However since it
116  // overwrites behavior, it should be logged.
117  gpr_log(
118  GPR_INFO,
119  "You are marking method %s as '%s', even though it was "
120  "previously marked '%s'. This behavior will overwrite the original "
121  "behavior. If you expected this then ignore this message.",
122  name(), TypeToString(api_type_), TypeToString(type));
123  }
124  api_type_ = type;
125  }
126 
127  private:
128  void* server_tag_;
129  ApiType api_type_;
130  std::unique_ptr<MethodHandler> handler_;
131 
132  const char* TypeToString(RpcServiceMethod::ApiType type) {
133  switch (type) {
134  case ApiType::SYNC:
135  return "sync";
136  case ApiType::ASYNC:
137  return "async";
138  case ApiType::RAW:
139  return "raw";
140  case ApiType::CALL_BACK:
141  return "callback";
143  return "raw_callback";
144  default:
145  GPR_UNREACHABLE_CODE(return "unknown");
146  }
147  }
148 };
149 } // namespace internal
150 
151 } // namespace grpc
152 
153 #endif // GRPCPP_IMPL_RPC_SERVICE_METHOD_H
grpc::internal::RpcServiceMethod::SetServerApiType
void SetServerApiType(RpcServiceMethod::ApiType type)
Definition: rpc_service_method.h:108
grpc::internal::RpcServiceMethod::ApiType
ApiType
Definition: rpc_service_method.h:94
grpc::internal::MethodHandler::HandlerParameter::status
const Status status
Definition: rpc_service_method.h:65
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::internal::MethodHandler::HandlerParameter
Definition: rpc_service_method.h:41
grpc::internal::RpcServiceMethod::RpcServiceMethod
RpcServiceMethod(const char *name, RpcMethod::RpcType type, MethodHandler *handler)
Takes ownership of the handler.
Definition: rpc_service_method.h:87
grpc::internal::MethodHandler::HandlerParameter::request
void *const request
Definition: rpc_service_method.h:64
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:36
GPR_ASSERT
#define GPR_ASSERT(x)
abort() the process if x is zero, having written a line to the log.
Definition: log.h:95
grpc::ServerContextBase
Base class of ServerContext.
Definition: server_context.h:124
status.h
rpc_method.h
GPR_UNREACHABLE_CODE
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: port_platform.h:710
grpc::internal::MethodHandler::HandlerParameter::call
Call *const call
Definition: rpc_service_method.h:62
grpc::internal::MethodHandler::Deserialize
virtual void * Deserialize(grpc_call *, grpc_byte_buffer *req, Status *, void **)
Definition: rpc_service_method.h:76
grpc::internal::RpcServiceMethod::handler
MethodHandler * handler() const
if MethodHandler is nullptr, then this is an async method
Definition: rpc_service_method.h:105
grpc::internal::RpcMethod::name
const char * name() const
Definition: rpc_method.h:64
grpc::internal::RpcServiceMethod::SetHandler
void SetHandler(MethodHandler *handler)
Definition: rpc_service_method.h:107
grpc::internal::MethodHandler::HandlerParameter::call_requester
const std::function< void()> call_requester
Definition: rpc_service_method.h:67
grpc::internal::MethodHandler::RunHandler
virtual void RunHandler(const HandlerParameter &param)=0
GPR_INFO
#define GPR_INFO
Definition: log.h:54
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:35
grpc::internal::RpcServiceMethod::api_type
ApiType api_type() const
Definition: rpc_service_method.h:106
log.h
grpc::internal::MethodHandler::HandlerParameter::~HandlerParameter
~HandlerParameter()
Definition: rpc_service_method.h:61
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
grpc_byte_buffer
Definition: grpc_types.h:43
grpc::internal::MethodHandler::HandlerParameter::HandlerParameter
HandlerParameter(Call *c, grpc::ServerContextBase *context, void *req, Status req_status, void *handler_data, std::function< void()> requester)
Constructor for HandlerParameter.
Definition: rpc_service_method.h:52
grpc::internal::RpcServiceMethod::ApiType::SYNC
@ SYNC
grpc::internal::RpcServiceMethod::server_tag
void * server_tag() const
Definition: rpc_service_method.h:103
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc::internal::MethodHandler
Base class for running an RPC handler.
Definition: rpc_service_method.h:38
grpc::internal::RpcServiceMethod::ApiType::ASYNC
@ ASYNC
grpc::internal::MethodHandler::HandlerParameter::server_context
grpc::ServerContextBase *const server_context
Definition: rpc_service_method.h:63
config.h
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
Log a message.
byte_buffer.h
std
Definition: async_unary_call.h:405
grpc::internal::RpcServiceMethod::ApiType::CALL_BACK
@ CALL_BACK
grpc::internal::RpcServiceMethod::ApiType::RAW_CALL_BACK
@ RAW_CALL_BACK
grpc::internal::RpcServiceMethod::set_server_tag
void set_server_tag(void *tag)
Definition: rpc_service_method.h:102
grpc::internal::MethodHandler::HandlerParameter::internal_data
void *const internal_data
Definition: rpc_service_method.h:66
grpc::internal::RpcServiceMethod
Server side rpc method class.
Definition: rpc_service_method.h:84
grpc::internal::RpcMethod
Descriptor of an RPC method.
Definition: rpc_method.h:29
grpc::internal::RpcServiceMethod::ApiType::RAW
@ RAW
grpc::internal::MethodHandler::~MethodHandler
virtual ~MethodHandler()
Definition: rpc_service_method.h:40