GRPC C++  1.30.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_CODEGEN_RPC_SERVICE_METHOD_H
20 #define GRPCPP_IMPL_CODEGEN_RPC_SERVICE_METHOD_H
21 
22 #include <climits>
23 #include <functional>
24 #include <map>
25 #include <memory>
26 #include <vector>
27 
28 #include <grpc/impl/codegen/log.h>
33 
34 namespace grpc_impl {
35 class ServerContextBase;
36 } // namespace grpc_impl
37 
38 namespace grpc {
39 namespace internal {
42  public:
43  virtual ~MethodHandler() {}
56  void* req, Status req_status, void* handler_data,
57  std::function<void()> requester)
58  : call(c),
59  server_context(context),
60  request(req),
61  status(req_status),
62  internal_data(handler_data),
63  call_requester(std::move(requester)) {}
65  Call* const call;
67  void* const request;
68  const Status status;
69  void* const internal_data;
70  const std::function<void()> call_requester;
71  };
72  virtual void RunHandler(const HandlerParameter& param) = 0;
73 
74  /* Returns a pointer to the deserialized request. \a status reflects the
75  result of deserialization. This pointer and the status should be filled in
76  a HandlerParameter and passed to RunHandler. It is illegal to access the
77  pointer after calling RunHandler. Ownership of the deserialized request is
78  retained by the handler. Returns nullptr if deserialization failed. */
79  virtual void* Deserialize(grpc_call* /*call*/, grpc_byte_buffer* req,
80  Status* /*status*/, void** /*handler_data*/) {
81  GPR_CODEGEN_ASSERT(req == nullptr);
82  return nullptr;
83  }
84 };
85 
87 class RpcServiceMethod : public RpcMethod {
88  public:
92  : RpcMethod(name, type),
93  server_tag_(nullptr),
94  api_type_(ApiType::SYNC),
95  handler_(handler) {}
96 
97  enum class ApiType {
98  SYNC,
99  ASYNC,
100  RAW,
101  CALL_BACK, // not CALLBACK because that is reserved in Windows
103  };
104 
105  void set_server_tag(void* tag) { server_tag_ = tag; }
106  void* server_tag() const { return server_tag_; }
108  MethodHandler* handler() const { return handler_.get(); }
109  ApiType api_type() const { return api_type_; }
110  void SetHandler(MethodHandler* handler) { handler_.reset(handler); }
112  if ((api_type_ == ApiType::SYNC) &&
113  (type == ApiType::ASYNC || type == ApiType::RAW)) {
114  // this marks this method as async
115  handler_.reset();
116  } else if (api_type_ != ApiType::SYNC) {
117  // this is not an error condition, as it allows users to declare a server
118  // like WithRawMethod_foo<AsyncService>. However since it
119  // overwrites behavior, it should be logged.
120  gpr_log(
121  GPR_INFO,
122  "You are marking method %s as '%s', even though it was "
123  "previously marked '%s'. This behavior will overwrite the original "
124  "behavior. If you expected this then ignore this message.",
125  name(), TypeToString(api_type_), TypeToString(type));
126  }
127  api_type_ = type;
128  }
129 
130  private:
131  void* server_tag_;
132  ApiType api_type_;
133  std::unique_ptr<MethodHandler> handler_;
134 
135  const char* TypeToString(RpcServiceMethod::ApiType type) {
136  switch (type) {
137  case ApiType::SYNC:
138  return "sync";
139  case ApiType::ASYNC:
140  return "async";
141  case ApiType::RAW:
142  return "raw";
143  case ApiType::CALL_BACK:
144  return "callback";
146  return "raw_callback";
147  default:
148  GPR_UNREACHABLE_CODE(return "unknown");
149  }
150  }
151 };
152 } // namespace internal
153 
154 } // namespace grpc
155 
156 #endif // GRPCPP_IMPL_CODEGEN_RPC_SERVICE_METHOD_H
grpc::internal::RpcServiceMethod::SetServerApiType
void SetServerApiType(RpcServiceMethod::ApiType type)
Definition: rpc_service_method.h:111
grpc::internal::RpcServiceMethod::ApiType
ApiType
Definition: rpc_service_method.h:97
log.h
GPR_INFO
#define GPR_INFO
Definition: log.h:56
grpc::internal::MethodHandler::HandlerParameter::server_context
::grpc_impl::ServerContextBase *const server_context
Definition: rpc_service_method.h:66
grpc::internal::MethodHandler::HandlerParameter::status
const Status status
Definition: rpc_service_method.h:68
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
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.
status.h
grpc::internal::MethodHandler::HandlerParameter
Definition: rpc_service_method.h:44
grpc::internal::RpcServiceMethod::RpcServiceMethod
RpcServiceMethod(const char *name, RpcMethod::RpcType type, MethodHandler *handler)
Takes ownership of the handler.
Definition: rpc_service_method.h:90
grpc::internal::MethodHandler::HandlerParameter::request
void *const request
Definition: rpc_service_method.h:67
config.h
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:38
grpc_impl::ServerContextBase
Base class of ServerContext. Experimental until callback API is final.
Definition: server_context_impl.h:123
grpc::internal::MethodHandler::HandlerParameter::call
Call *const call
Definition: rpc_service_method.h:65
byte_buffer.h
grpc::internal::MethodHandler::Deserialize
virtual void * Deserialize(grpc_call *, grpc_byte_buffer *req, Status *, void **)
Definition: rpc_service_method.h:79
grpc::internal::RpcServiceMethod::handler
MethodHandler * handler() const
if MethodHandler is nullptr, then this is an async method
Definition: rpc_service_method.h:108
grpc::internal::RpcMethod::name
const char * name() const
Definition: rpc_method.h:47
grpc::internal::RpcServiceMethod::SetHandler
void SetHandler(MethodHandler *handler)
Definition: rpc_service_method.h:110
grpc::internal::MethodHandler::HandlerParameter::call_requester
const std::function< void()> call_requester
Definition: rpc_service_method.h:70
grpc::internal::MethodHandler::RunHandler
virtual void RunHandler(const HandlerParameter &param)=0
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc::internal::RpcServiceMethod::api_type
ApiType api_type() const
Definition: rpc_service_method.h:109
grpc::internal::MethodHandler::HandlerParameter::~HandlerParameter
~HandlerParameter()
Definition: rpc_service_method.h:64
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
grpc_byte_buffer
Definition: grpc_types.h:40
grpc::internal::MethodHandler::HandlerParameter::HandlerParameter
HandlerParameter(Call *c, ::grpc_impl::ServerContextBase *context, void *req, Status req_status, void *handler_data, std::function< void()> requester)
Constructor for HandlerParameter.
Definition: rpc_service_method.h:55
GPR_UNREACHABLE_CODE
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: port_platform.h:602
grpc::internal::RpcServiceMethod::ApiType::SYNC
rpc_method.h
grpc::internal::RpcServiceMethod::server_tag
void * server_tag() const
Definition: rpc_service_method.h:106
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:41
grpc::experimental::ServerContextBase
::grpc_impl::ServerContextBase ServerContextBase
Definition: server_context.h:36
grpc::internal::RpcServiceMethod::ApiType::ASYNC
std
Definition: async_unary_call_impl.h:301
grpc::internal::RpcServiceMethod::ApiType::CALL_BACK
grpc_impl
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
grpc::internal::RpcServiceMethod::ApiType::RAW_CALL_BACK
grpc::internal::RpcServiceMethod::set_server_tag
void set_server_tag(void *tag)
Definition: rpc_service_method.h:105
grpc::internal::MethodHandler::HandlerParameter::internal_data
void *const internal_data
Definition: rpc_service_method.h:69
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
grpc::internal::RpcServiceMethod
Server side rpc method class.
Definition: rpc_service_method.h:87
grpc::internal::RpcMethod
Descriptor of an RPC method.
Definition: rpc_method.h:29
grpc::internal::RpcServiceMethod::ApiType::RAW
grpc::internal::MethodHandler::~MethodHandler
virtual ~MethodHandler()
Definition: rpc_service_method.h:43