GRPC C++  1.23.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 ServerContext;
36 }
37 
38 namespace grpc {
39 namespace internal {
42  public:
43  virtual ~MethodHandler() {}
55  HandlerParameter(Call* c, ::grpc_impl::ServerContext* context, void* req,
56  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)) {}
67  void* request;
70  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:
90  RpcServiceMethod(const char* name, RpcMethod::RpcType type,
91  MethodHandler* handler)
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
102  RAW_CALL_BACK,
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";
145  case ApiType::RAW_CALL_BACK:
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
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:145
#define GPR_INFO
Definition: log.h:56
std::function< void()> call_requester
Definition: rpc_service_method.h:70
void * server_tag() const
Definition: rpc_service_method.h:106
ApiType
Definition: rpc_service_method.h:97
virtual void * Deserialize(grpc_call *call, grpc_byte_buffer *req, Status *status, void **handler_data)
Definition: rpc_service_method.h:79
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: port_platform.h:551
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
Log a message.
virtual ~MethodHandler()
Definition: rpc_service_method.h:43
Definition: async_unary_call_impl.h:302
Definition: grpc_types.h:40
::grpc_impl::ServerContext * server_context
Definition: rpc_service_method.h:66
MethodHandler * handler() const
if MethodHandler is nullptr, then this is an async method
Definition: rpc_service_method.h:108
RpcType
Definition: rpc_method.h:31
Call * call
Definition: rpc_service_method.h:65
::grpc_impl::ServerContext ServerContext
Definition: server_context.h:25
A ServerContext allows the person implementing a service handler to:
Definition: server_context_impl.h:118
void SetHandler(MethodHandler *handler)
Definition: rpc_service_method.h:110
Descriptor of an RPC method.
Definition: rpc_method.h:29
void * internal_data
Definition: rpc_service_method.h:69
HandlerParameter(Call *c, ::grpc_impl::ServerContext *context, void *req, Status req_status, void *handler_data, std::function< void()> requester)
Constructor for HandlerParameter.
Definition: rpc_service_method.h:55
ApiType api_type() const
Definition: rpc_service_method.h:109
void set_server_tag(void *tag)
Definition: rpc_service_method.h:105
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Server side rpc method class.
Definition: rpc_service_method.h:87
void SetServerApiType(RpcServiceMethod::ApiType type)
Definition: rpc_service_method.h:111
Definition: rpc_service_method.h:44
RpcServiceMethod(const char *name, RpcMethod::RpcType type, MethodHandler *handler)
Takes ownership of the handler.
Definition: rpc_service_method.h:90
~HandlerParameter()
Definition: rpc_service_method.h:64
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
Base class for running an RPC handler.
Definition: rpc_service_method.h:41
Did it work? If it didn&#39;t, why?
Definition: status.h:31
Status status
Definition: rpc_service_method.h:68
void * request
Definition: rpc_service_method.h:67
Straightforward wrapping of the C call object.
Definition: call.h:38