grpcpp/impl/codegen/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 // IWYU pragma: private, include <grpcpp/impl/rpc_service_method.h>
23 
24 #include <climits>
25 #include <functional>
26 #include <map>
27 #include <memory>
28 #include <vector>
29 
30 #include <grpc/impl/codegen/log.h>
35 
36 namespace grpc {
37 class ServerContextBase;
38 namespace internal {
41  public:
42  virtual ~MethodHandler() {}
55  Status req_status, void* handler_data,
56  std::function<void()> requester)
57  : call(c),
59  request(req),
60  status(req_status),
61  internal_data(handler_data),
62  call_requester(std::move(requester)) {}
64  Call* const call;
66  void* const request;
67  const Status status;
68  void* const internal_data;
70  };
71  virtual void RunHandler(const HandlerParameter& param) = 0;
72 
73  /* Returns a pointer to the deserialized request. \a status reflects the
74  result of deserialization. This pointer and the status should be filled in
75  a HandlerParameter and passed to RunHandler. It is illegal to access the
76  pointer after calling RunHandler. Ownership of the deserialized request is
77  retained by the handler. Returns nullptr if deserialization failed. */
78  virtual void* Deserialize(grpc_call* /*call*/, grpc_byte_buffer* req,
79  Status* /*status*/, void** /*handler_data*/) {
80  GPR_CODEGEN_ASSERT(req == nullptr);
81  return nullptr;
82  }
83 };
84 
86 class RpcServiceMethod : public RpcMethod {
87  public:
91  : RpcMethod(name, type),
92  server_tag_(nullptr),
94  handler_(handler) {}
95 
96  enum class ApiType {
97  SYNC,
98  ASYNC,
99  RAW,
100  CALL_BACK, // not CALLBACK because that is reserved in Windows
102  };
103 
104  void set_server_tag(void* tag) { server_tag_ = tag; }
105  void* server_tag() const { return server_tag_; }
107  MethodHandler* handler() const { return handler_.get(); }
108  ApiType api_type() const { return api_type_; }
111  if ((api_type_ == ApiType::SYNC) &&
112  (type == ApiType::ASYNC || type == ApiType::RAW)) {
113  // this marks this method as async
114  handler_.reset();
115  } else if (api_type_ != ApiType::SYNC) {
116  // this is not an error condition, as it allows users to declare a server
117  // like WithRawMethod_foo<AsyncService>. However since it
118  // overwrites behavior, it should be logged.
119  gpr_log(
120  GPR_INFO,
121  "You are marking method %s as '%s', even though it was "
122  "previously marked '%s'. This behavior will overwrite the original "
123  "behavior. If you expected this then ignore this message.",
125  }
126  api_type_ = type;
127  }
128 
129  private:
130  void* server_tag_;
132  std::unique_ptr<MethodHandler> handler_;
133 
135  switch (type) {
136  case ApiType::SYNC:
137  return "sync";
138  case ApiType::ASYNC:
139  return "async";
140  case ApiType::RAW:
141  return "raw";
142  case ApiType::CALL_BACK:
143  return "callback";
145  return "raw_callback";
146  default:
147  GPR_UNREACHABLE_CODE(return "unknown");
148  }
149  }
150 };
151 } // namespace internal
152 
153 } // namespace grpc
154 
155 #endif // GRPCPP_IMPL_CODEGEN_RPC_SERVICE_METHOD_H
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc::internal::RpcServiceMethod::ApiType::SYNC
@ SYNC
grpc::internal::RpcServiceMethod::handler
MethodHandler * handler() const
if MethodHandler is nullptr, then this is an async method
Definition: grpcpp/impl/codegen/rpc_service_method.h:107
grpc
Definition: grpcpp/alarm.h:33
log.h
grpc::internal::RpcServiceMethod::RpcServiceMethod
RpcServiceMethod(const char *name, RpcMethod::RpcType type, MethodHandler *handler)
Takes ownership of the handler.
Definition: grpcpp/impl/codegen/rpc_service_method.h:89
grpc::internal::MethodHandler::HandlerParameter::internal_data
void *const internal_data
Definition: grpcpp/impl/codegen/rpc_service_method.h:68
config.h
grpc::internal::RpcServiceMethod::server_tag
void * server_tag() const
Definition: grpcpp/impl/codegen/rpc_service_method.h:105
grpc::internal::RpcServiceMethod::handler_
std::unique_ptr< MethodHandler > handler_
Definition: grpcpp/impl/codegen/rpc_service_method.h:132
grpc::internal::RpcServiceMethod::ApiType::ASYNC
@ ASYNC
grpc::internal::RpcServiceMethod
Server side rpc method class.
Definition: grpcpp/impl/codegen/rpc_service_method.h:86
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc::internal::MethodHandler::HandlerParameter::server_context
grpc::ServerContextBase *const server_context
Definition: grpcpp/impl/codegen/rpc_service_method.h:65
grpc::ServerContextBase
Base class of ServerContext.
Definition: grpcpp/impl/codegen/server_context.h:126
grpc::internal::RpcMethod
Descriptor of an RPC method.
Definition: grpcpp/impl/codegen/rpc_method.h:31
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
grpc::internal::MethodHandler::Deserialize
virtual void * Deserialize(grpc_call *, grpc_byte_buffer *req, Status *, void **)
Definition: grpcpp/impl/codegen/rpc_service_method.h:78
req
static uv_connect_t req
Definition: test-connection-fail.c:30
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc::internal::MethodHandler::HandlerParameter::status
const Status status
Definition: grpcpp/impl/codegen/rpc_service_method.h:67
grpc_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
grpc_byte_buffer
Definition: grpc_types.h:43
grpc::internal::RpcServiceMethod::set_server_tag
void set_server_tag(void *tag)
Definition: grpcpp/impl/codegen/rpc_service_method.h:104
grpc::internal::RpcMethod::name
const char * name() const
Definition: grpcpp/impl/codegen/rpc_method.h:66
byte_buffer.h
GPR_UNREACHABLE_CODE
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: impl/codegen/port_platform.h:652
grpc::internal::RpcServiceMethod::server_tag_
void * server_tag_
Definition: grpcpp/impl/codegen/rpc_service_method.h:130
grpc::internal::MethodHandler::HandlerParameter::~HandlerParameter
~HandlerParameter()
Definition: grpcpp/impl/codegen/rpc_service_method.h:63
grpc::internal::MethodHandler::HandlerParameter::call_requester
const std::function< void()> call_requester
Definition: grpcpp/impl/codegen/rpc_service_method.h:69
grpc::internal::RpcServiceMethod::TypeToString
const char * TypeToString(RpcServiceMethod::ApiType type)
Definition: grpcpp/impl/codegen/rpc_service_method.h:134
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: include/grpcpp/impl/codegen/call.h:37
rpc_method.h
grpc::internal::RpcServiceMethod::ApiType
ApiType
Definition: grpcpp/impl/codegen/rpc_service_method.h:96
grpc::internal::RpcMethod::RpcType
RpcType
Definition: grpcpp/impl/codegen/rpc_method.h:33
status.h
grpc::internal::RpcServiceMethod::api_type
ApiType api_type() const
Definition: grpcpp/impl/codegen/rpc_service_method.h:108
grpc::internal::MethodHandler::HandlerParameter::call
Call *const call
Definition: grpcpp/impl/codegen/rpc_service_method.h:64
grpc::internal::MethodHandler
Base class for running an RPC handler.
Definition: grpcpp/impl/codegen/rpc_service_method.h:40
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc::internal::MethodHandler::~MethodHandler
virtual ~MethodHandler()
Definition: grpcpp/impl/codegen/rpc_service_method.h:42
grpc::internal::MethodHandler::HandlerParameter::HandlerParameter
HandlerParameter(Call *c, grpc::ServerContextBase *context, void *req, Status req_status, void *handler_data, std::function< void()> requester)
Definition: grpcpp/impl/codegen/rpc_service_method.h:54
grpc::internal::RpcServiceMethod::SetHandler
void SetHandler(MethodHandler *handler)
Definition: grpcpp/impl/codegen/rpc_service_method.h:109
grpc::internal::MethodHandler::HandlerParameter::request
void *const request
Definition: grpcpp/impl/codegen/rpc_service_method.h:66
grpc::internal::MethodHandler::HandlerParameter
Definition: grpcpp/impl/codegen/rpc_service_method.h:43
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
grpc::internal::RpcServiceMethod::SetServerApiType
void SetServerApiType(RpcServiceMethod::ApiType type)
Definition: grpcpp/impl/codegen/rpc_service_method.h:110
SYNC
@ SYNC
Definition: bloaty/third_party/zlib/inflate.h:52
grpc::internal::RpcServiceMethod::ApiType::RAW
@ RAW
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: grpcpp/impl/codegen/core_codegen_interface.h:151
grpc::internal::MethodHandler::RunHandler
virtual void RunHandler(const HandlerParameter &param)=0
internal
Definition: benchmark/test/output_test_helper.cc:20
grpc::internal::RpcServiceMethod::api_type_
ApiType api_type_
Definition: grpcpp/impl/codegen/rpc_service_method.h:131
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc::internal::RpcServiceMethod::ApiType::RAW_CALL_BACK
@ RAW_CALL_BACK
grpc::internal::RpcServiceMethod::ApiType::CALL_BACK
@ CALL_BACK


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:13