GRPC C++  1.22.0-dev
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 {
35 class ServerContext;
36 
37 namespace internal {
40  public:
41  virtual ~MethodHandler() {}
53  HandlerParameter(Call* c, ServerContext* context, void* req,
54  Status req_status, void* handler_data,
55  std::function<void()> requester)
56  : call(c),
57  server_context(context),
58  request(req),
59  status(req_status),
60  internal_data(handler_data),
61  call_requester(std::move(requester)) {}
65  void* request;
68  std::function<void()> call_requester;
69  };
70  virtual void RunHandler(const HandlerParameter& param) = 0;
71 
72  /* Returns a pointer to the deserialized request. \a status reflects the
73  result of deserialization. This pointer and the status should be filled in
74  a HandlerParameter and passed to RunHandler. It is illegal to access the
75  pointer after calling RunHandler. Ownership of the deserialized request is
76  retained by the handler. Returns nullptr if deserialization failed. */
78  Status* status, void** handler_data) {
79  GPR_CODEGEN_ASSERT(req == nullptr);
80  return nullptr;
81  }
82 };
83 
85 class RpcServiceMethod : public RpcMethod {
86  public:
88  RpcServiceMethod(const char* name, RpcMethod::RpcType type,
89  MethodHandler* handler)
90  : RpcMethod(name, type),
91  server_tag_(nullptr),
92  api_type_(ApiType::SYNC),
93  handler_(handler) {}
94 
95  enum class ApiType {
96  SYNC,
97  ASYNC,
98  RAW,
99  CALL_BACK, // not CALLBACK because that is reserved in Windows
100  RAW_CALL_BACK,
101  };
102 
103  void set_server_tag(void* tag) { server_tag_ = tag; }
104  void* server_tag() const { return server_tag_; }
106  MethodHandler* handler() const { return handler_.get(); }
107  ApiType api_type() const { return api_type_; }
108  void SetHandler(MethodHandler* handler) { handler_.reset(handler); }
110  if ((api_type_ == ApiType::SYNC) &&
111  (type == ApiType::ASYNC || type == ApiType::RAW)) {
112  // this marks this method as async
113  handler_.reset();
114  } else if (api_type_ != ApiType::SYNC) {
115  // this is not an error condition, as it allows users to declare a server
116  // like WithRawMethod_foo<AsyncService>. However since it
117  // overwrites behavior, it should be logged.
118  gpr_log(
119  GPR_INFO,
120  "You are marking method %s as '%s', even though it was "
121  "previously marked '%s'. This behavior will overwrite the original "
122  "behavior. If you expected this then ignore this message.",
123  name(), TypeToString(api_type_), TypeToString(type));
124  }
125  api_type_ = type;
126  }
127 
128  private:
129  void* server_tag_;
130  ApiType api_type_;
131  std::unique_ptr<MethodHandler> handler_;
132 
133  const char* TypeToString(RpcServiceMethod::ApiType type) {
134  switch (type) {
135  case ApiType::SYNC:
136  return "sync";
137  case ApiType::ASYNC:
138  return "async";
139  case ApiType::RAW:
140  return "raw";
141  case ApiType::CALL_BACK:
142  return "callback";
143  case ApiType::RAW_CALL_BACK:
144  return "raw_callback";
145  default:
146  GPR_UNREACHABLE_CODE(return "unknown");
147  }
148  }
149 };
150 } // namespace internal
151 
152 } // namespace grpc
153 
154 #endif // GRPCPP_IMPL_CODEGEN_RPC_SERVICE_METHOD_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
ServerContext * server_context
Definition: rpc_service_method.h:64
#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:68
void * server_tag() const
Definition: rpc_service_method.h:104
ApiType
Definition: rpc_service_method.h:95
virtual void * Deserialize(grpc_call *call, grpc_byte_buffer *req, Status *status, void **handler_data)
Definition: rpc_service_method.h:77
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: port_platform.h:539
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:41
Definition: async_unary_call.h:303
Definition: grpc_types.h:40
MethodHandler * handler() const
if MethodHandler is nullptr, then this is an async method
Definition: rpc_service_method.h:106
RpcType
Definition: rpc_method.h:31
Call * call
Definition: rpc_service_method.h:63
HandlerParameter(Call *c, ServerContext *context, void *req, Status req_status, void *handler_data, std::function< void()> requester)
Constructor for HandlerParameter.
Definition: rpc_service_method.h:53
void SetHandler(MethodHandler *handler)
Definition: rpc_service_method.h:108
Descriptor of an RPC method.
Definition: rpc_method.h:29
void * internal_data
Definition: rpc_service_method.h:67
ApiType api_type() const
Definition: rpc_service_method.h:107
void set_server_tag(void *tag)
Definition: rpc_service_method.h:103
virtual void RunHandler(const HandlerParameter &param)=0
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:85
void SetServerApiType(RpcServiceMethod::ApiType type)
Definition: rpc_service_method.h:109
Definition: rpc_service_method.h:42
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:114
RpcServiceMethod(const char *name, RpcMethod::RpcType type, MethodHandler *handler)
Takes ownership of the handler.
Definition: rpc_service_method.h:88
~HandlerParameter()
Definition: rpc_service_method.h:62
Base class for running an RPC handler.
Definition: rpc_service_method.h:39
Did it work? If it didn&#39;t, why?
Definition: status.h:31
Status status
Definition: rpc_service_method.h:66
void * request
Definition: rpc_service_method.h:65
Straightforward wrapping of the C call object.
Definition: call.h:38