GRPC C++  1.16.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() {}
44  : call(c), server_context(context) {
45  request.set_buffer(req);
46  }
50  // Handler required to destroy these contents
52  };
53  virtual void RunHandler(const HandlerParameter& param) = 0;
54 };
55 
57 class RpcServiceMethod : public RpcMethod {
58  public:
60  RpcServiceMethod(const char* name, RpcMethod::RpcType type,
61  MethodHandler* handler)
62  : RpcMethod(name, type),
63  server_tag_(nullptr),
64  async_type_(AsyncType::UNSET),
65  handler_(handler) {}
66 
67  enum class AsyncType {
68  UNSET,
69  ASYNC,
70  RAW,
71  };
72 
73  void set_server_tag(void* tag) { server_tag_ = tag; }
74  void* server_tag() const { return server_tag_; }
76  MethodHandler* handler() const { return handler_.get(); }
77  void SetHandler(MethodHandler* handler) { handler_.reset(handler); }
79  if (async_type_ == AsyncType::UNSET) {
80  // this marks this method as async
81  handler_.reset();
82  } else {
83  // this is not an error condition, as it allows users to declare a server
84  // like WithRawMethod_foo<AsyncService>. However since it
85  // overwrites behavior, it should be logged.
86  gpr_log(
87  GPR_INFO,
88  "You are marking method %s as '%s', even though it was "
89  "previously marked '%s'. This behavior will overwrite the original "
90  "behavior. If you expected this then ignore this message.",
91  name(), TypeToString(async_type_), TypeToString(type));
92  }
93  async_type_ = type;
94  }
95 
96  private:
97  void* server_tag_;
98  AsyncType async_type_;
99  std::unique_ptr<MethodHandler> handler_;
100 
101  const char* TypeToString(RpcServiceMethod::AsyncType type) {
102  switch (type) {
103  case AsyncType::UNSET:
104  return "unset";
105  case AsyncType::ASYNC:
106  return "async";
107  case AsyncType::RAW:
108  return "raw";
109  default:
110  GPR_UNREACHABLE_CODE(return "unknown");
111  }
112  }
113 };
114 } // namespace internal
115 
116 } // namespace grpc
117 
118 #endif // GRPCPP_IMPL_CODEGEN_RPC_SERVICE_METHOD_H
ServerContext * server_context
Definition: rpc_service_method.h:49
#define GPR_INFO
Definition: log.h:56
void * server_tag() const
Definition: rpc_service_method.h:74
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: port_platform.h:498
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
HandlerParameter(Call *c, ServerContext *context, grpc_byte_buffer *req)
Definition: rpc_service_method.h:43
Definition: grpc_types.h:40
MethodHandler * handler() const
if MethodHandler is nullptr, then this is an async method
Definition: rpc_service_method.h:76
RpcType
Definition: rpc_method.h:31
Call * call
Definition: rpc_service_method.h:48
void SetHandler(MethodHandler *handler)
Definition: rpc_service_method.h:77
Descriptor of an RPC method.
Definition: rpc_method.h:29
void set_server_tag(void *tag)
Definition: rpc_service_method.h:73
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:118
virtual void RunHandler(const HandlerParameter &param)=0
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
ByteBuffer request
Definition: rpc_service_method.h:51
Server side rpc method class.
Definition: rpc_service_method.h:57
Definition: rpc_service_method.h:42
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:97
RpcServiceMethod(const char *name, RpcMethod::RpcType type, MethodHandler *handler)
Takes ownership of the handler.
Definition: rpc_service_method.h:60
~HandlerParameter()
Definition: rpc_service_method.h:47
void SetServerAsyncType(RpcServiceMethod::AsyncType type)
Definition: rpc_service_method.h:78
AsyncType
Definition: rpc_service_method.h:67
Base class for running an RPC handler.
Definition: rpc_service_method.h:39
A sequence of bytes.
Definition: byte_buffer.h:55
Straightforward wrapping of the C call object.
Definition: call.h:668