GRPC C++  1.30.0
service_type.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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_SERVICE_TYPE_H
20 #define GRPCPP_IMPL_CODEGEN_SERVICE_TYPE_H
21 
28 
29 namespace grpc_impl {
30 
31 class Server;
32 class CompletionQueue;
33 class ServerContext;
34 } // namespace grpc_impl
35 namespace grpc {
36 
37 class ServerInterface;
38 
39 namespace internal {
40 class Call;
42  public:
44 
51  virtual void SendInitialMetadata(void* tag) = 0;
52 
53  private:
54  friend class ::grpc::ServerInterface;
55  virtual void BindCall(Call* call) = 0;
56 };
57 } // namespace internal
58 
60 class Service {
61  public:
62  Service() : server_(nullptr) {}
63  virtual ~Service() {}
64 
65  bool has_async_methods() const {
66  for (const auto& method : methods_) {
67  if (method && method->handler() == nullptr) {
68  return true;
69  }
70  }
71  return false;
72  }
73 
74  bool has_synchronous_methods() const {
75  for (const auto& method : methods_) {
76  if (method &&
77  method->api_type() == internal::RpcServiceMethod::ApiType::SYNC) {
78  return true;
79  }
80  }
81  return false;
82  }
83 
84  bool has_callback_methods() const {
85  for (const auto& method : methods_) {
86  if (method && (method->api_type() ==
88  method->api_type() ==
90  return true;
91  }
92  }
93  return false;
94  }
95 
96  bool has_generic_methods() const {
97  for (const auto& method : methods_) {
98  if (method.get() == nullptr) {
99  return true;
100  }
101  }
102  return false;
103  }
104 
105  protected:
106  // TODO(vjpai): Promote experimental contents once callback API is accepted
108  public:
109  explicit experimental_type(Service* service) : service_(service) {}
110 
111  void MarkMethodCallback(int index, internal::MethodHandler* handler) {
112  service_->MarkMethodCallbackInternal(index, handler);
113  }
114 
115  void MarkMethodRawCallback(int index, internal::MethodHandler* handler) {
116  service_->MarkMethodRawCallbackInternal(index, handler);
117  }
118 
120  return service_->GetHandlerInternal(index);
121  }
122 
123  private:
124  Service* service_;
125  };
126 
128 
129  template <class Message>
130  void RequestAsyncUnary(int index, ::grpc_impl::ServerContext* context,
131  Message* request,
133  ::grpc_impl::CompletionQueue* call_cq,
134  ::grpc_impl::ServerCompletionQueue* notification_cq,
135  void* tag) {
136  // Typecast the index to size_t for indexing into a vector
137  // while preserving the API that existed before a compiler
138  // warning was first seen (grpc/grpc#11664)
139  size_t idx = static_cast<size_t>(index);
140  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
141  notification_cq, tag, request);
142  }
144  int index, ::grpc_impl::ServerContext* context,
146  ::grpc_impl::CompletionQueue* call_cq,
147  ::grpc_impl::ServerCompletionQueue* notification_cq, void* tag) {
148  size_t idx = static_cast<size_t>(index);
149  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
150  notification_cq, tag);
151  }
152  template <class Message>
154  int index, ::grpc_impl::ServerContext* context, Message* request,
156  ::grpc_impl::CompletionQueue* call_cq,
157  ::grpc_impl::ServerCompletionQueue* notification_cq, void* tag) {
158  size_t idx = static_cast<size_t>(index);
159  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
160  notification_cq, tag, request);
161  }
163  int index, ::grpc_impl::ServerContext* context,
165  ::grpc_impl::CompletionQueue* call_cq,
166  ::grpc_impl::ServerCompletionQueue* notification_cq, void* tag) {
167  size_t idx = static_cast<size_t>(index);
168  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
169  notification_cq, tag);
170  }
171 
173  methods_.emplace_back(method);
174  }
175 
176  void MarkMethodAsync(int index) {
177  // This does not have to be a hard error, however no one has approached us
178  // with a use case yet. Please file an issue if you believe you have one.
179  size_t idx = static_cast<size_t>(index);
181  methods_[idx].get() != nullptr &&
182  "Cannot mark the method as 'async' because it has already been "
183  "marked as 'generic'.");
184  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::ASYNC);
185  }
186 
187  void MarkMethodRaw(int index) {
188  // This does not have to be a hard error, however no one has approached us
189  // with a use case yet. Please file an issue if you believe you have one.
190  size_t idx = static_cast<size_t>(index);
191  GPR_CODEGEN_ASSERT(methods_[idx].get() != nullptr &&
192  "Cannot mark the method as 'raw' because it has already "
193  "been marked as 'generic'.");
194  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::RAW);
195  }
196 
197  void MarkMethodGeneric(int index) {
198  // This does not have to be a hard error, however no one has approached us
199  // with a use case yet. Please file an issue if you believe you have one.
200  size_t idx = static_cast<size_t>(index);
202  methods_[idx]->handler() != nullptr &&
203  "Cannot mark the method as 'generic' because it has already been "
204  "marked as 'async' or 'raw'.");
205  methods_[idx].reset();
206  }
207 
208  void MarkMethodStreamed(int index, internal::MethodHandler* streamed_method) {
209  // This does not have to be a hard error, however no one has approached us
210  // with a use case yet. Please file an issue if you believe you have one.
211  size_t idx = static_cast<size_t>(index);
212  GPR_CODEGEN_ASSERT(methods_[idx] && methods_[idx]->handler() &&
213  "Cannot mark an async or generic method Streamed");
214  methods_[idx]->SetHandler(streamed_method);
215 
216  // From the server's point of view, streamed unary is a special
217  // case of BIDI_STREAMING that has 1 read and 1 write, in that order,
218  // and split server-side streaming is BIDI_STREAMING with 1 read and
219  // any number of writes, in that order.
220  methods_[idx]->SetMethodType(internal::RpcMethod::BIDI_STREAMING);
221  }
222 
223 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
224  void MarkMethodCallback(int index, internal::MethodHandler* handler) {
225  MarkMethodCallbackInternal(index, handler);
226  }
227 
228  void MarkMethodRawCallback(int index, internal::MethodHandler* handler) {
229  MarkMethodRawCallbackInternal(index, handler);
230  }
231 
232  internal::MethodHandler* GetHandler(int index) {
233  return GetHandlerInternal(index);
234  }
235 #endif
236  private:
237  // TODO(vjpai): migrate the Internal functions to mainline functions once
238  // callback API is fully de-experimental
239  void MarkMethodCallbackInternal(int index, internal::MethodHandler* handler) {
240  // This does not have to be a hard error, however no one has approached us
241  // with a use case yet. Please file an issue if you believe you have one.
242  size_t idx = static_cast<size_t>(index);
244  methods_[idx].get() != nullptr &&
245  "Cannot mark the method as 'callback' because it has already been "
246  "marked as 'generic'.");
247  methods_[idx]->SetHandler(handler);
248  methods_[idx]->SetServerApiType(
250  }
251 
252  void MarkMethodRawCallbackInternal(int index,
253  internal::MethodHandler* handler) {
254  // This does not have to be a hard error, however no one has approached us
255  // with a use case yet. Please file an issue if you believe you have one.
256  size_t idx = static_cast<size_t>(index);
258  methods_[idx].get() != nullptr &&
259  "Cannot mark the method as 'raw callback' because it has already "
260  "been marked as 'generic'.");
261  methods_[idx]->SetHandler(handler);
262  methods_[idx]->SetServerApiType(
264  }
265 
266  internal::MethodHandler* GetHandlerInternal(int index) {
267  size_t idx = static_cast<size_t>(index);
268  return methods_[idx]->handler();
269  }
270 
271  friend class grpc_impl::Server;
272  friend class ServerInterface;
273  ServerInterface* server_;
274  std::vector<std::unique_ptr<internal::RpcServiceMethod>> methods_;
275 };
276 
277 } // namespace grpc
278 
279 #endif // GRPCPP_IMPL_CODEGEN_SERVICE_TYPE_H
grpc::Service::RequestAsyncServerStreaming
void RequestAsyncServerStreaming(int index, ::grpc_impl::ServerContext *context, Message *request, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: service_type.h:153
grpc::Service::has_callback_methods
bool has_callback_methods() const
Definition: service_type.h:84
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:41
grpc::Service::MarkMethodRaw
void MarkMethodRaw(int index)
Definition: service_type.h:187
grpc::Service::MarkMethodAsync
void MarkMethodAsync(int index)
Definition: service_type.h:176
rpc_service_method.h
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
status.h
grpc::Service::~Service
virtual ~Service()
Definition: service_type.h:63
grpc::Service::experimental_type::MarkMethodCallback
void MarkMethodCallback(int index, internal::MethodHandler *handler)
Definition: service_type.h:111
grpc::CompletionQueue
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
grpc::Service::experimental_type::GetHandler
internal::MethodHandler * GetHandler(int index)
Definition: service_type.h:119
serialization_traits.h
config.h
grpc::Service::experimental
experimental_type experimental()
Definition: service_type.h:127
grpc::Service::has_generic_methods
bool has_generic_methods() const
Definition: service_type.h:96
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:38
grpc::Service
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:60
core_codegen_interface.h
grpc::Server
::grpc_impl::Server Server
Definition: server.h:26
grpc::Service::experimental_type::MarkMethodRawCallback
void MarkMethodRawCallback(int index, internal::MethodHandler *handler)
Definition: service_type.h:115
grpc_impl::Server
Represents a gRPC server.
Definition: server_impl.h:64
grpc_impl::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue_impl.h:424
grpc_impl::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context_impl.h:510
grpc::internal::RpcMethod::BIDI_STREAMING
Definition: rpc_method.h:35
grpc::Service::MarkMethodGeneric
void MarkMethodGeneric(int index)
Definition: service_type.h:197
grpc::ServerContext
::grpc_impl::ServerContext ServerContext
Definition: server_context.h:26
grpc::Service::AddMethod
void AddMethod(internal::RpcServiceMethod *method)
Definition: service_type.h:172
grpc::Service::has_synchronous_methods
bool has_synchronous_methods() const
Definition: service_type.h:74
grpc::Service::MarkMethodStreamed
void MarkMethodStreamed(int index, internal::MethodHandler *streamed_method)
Definition: service_type.h:208
grpc::internal::RpcServiceMethod::ApiType::SYNC
grpc::Service::RequestAsyncClientStreaming
void RequestAsyncClientStreaming(int index, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: service_type.h:143
grpc::internal::ServerAsyncStreamingInterface::SendInitialMetadata
virtual void SendInitialMetadata(void *tag)=0
Request notification of the sending of initial metadata to the client.
grpc::internal::MethodHandler
Base class for running an RPC handler.
Definition: rpc_service_method.h:41
grpc::Service::experimental_type::experimental_type
experimental_type(Service *service)
Definition: service_type.h:109
grpc::Service::experimental_type
Definition: service_type.h:107
grpc_impl::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue_impl.h:103
grpc::internal::RpcServiceMethod::ApiType::ASYNC
grpc::internal::RpcServiceMethod::ApiType::CALL_BACK
grpc_impl
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
grpc::internal::RpcServiceMethod::ApiType::RAW_CALL_BACK
server_interface.h
grpc::ServerInterface::RequestAsyncCall
void RequestAsyncCall(internal::RpcServiceMethod *method, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:347
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
grpc::internal::RpcServiceMethod
Server side rpc method class.
Definition: rpc_service_method.h:87
grpc::Service::has_async_methods
bool has_async_methods() const
Definition: service_type.h:65
grpc::ServerInterface
Definition: server_interface.h:68
grpc::protobuf::Message
::google::protobuf::Message Message
Definition: config_protobuf.h:75
grpc::internal::ServerAsyncStreamingInterface::~ServerAsyncStreamingInterface
virtual ~ServerAsyncStreamingInterface()
Definition: service_type.h:43
grpc::internal::RpcServiceMethod::ApiType::RAW
grpc::Service::RequestAsyncBidiStreaming
void RequestAsyncBidiStreaming(int index, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: service_type.h:162
grpc::Service::Service
Service()
Definition: service_type.h:62
grpc::Service::RequestAsyncUnary
void RequestAsyncUnary(int index, ::grpc_impl::ServerContext *context, Message *request, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: service_type.h:130