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