GRPC C++  1.81.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_SERVICE_TYPE_H
20 #define GRPCPP_IMPL_SERVICE_TYPE_H
21 
25 #include <grpcpp/support/config.h>
26 #include <grpcpp/support/status.h>
27 
28 #include "absl/log/absl_check.h"
29 
30 namespace grpc {
31 
32 class CompletionQueue;
33 class ServerContext;
34 class ServerInterface;
35 class Service;
36 
37 namespace experimental {
38 void SetVirtualService(Service* service);
39 namespace internal {
40 template <class RequestType>
41 class CallbackSessionHandler;
42 } // namespace internal
43 } // namespace experimental
44 
45 namespace internal {
46 class Call;
48  public:
50 
57  virtual void SendInitialMetadata(void* tag) = 0;
58 
59  private:
60  friend class grpc::ServerInterface;
61  virtual void BindCall(Call* call) = 0;
62 };
63 } // namespace internal
64 
66 class Service {
67  public:
68  Service() : server_(nullptr) {}
69  virtual ~Service() {}
70 
71  bool has_async_methods() const {
72  for (const auto& method : methods_) {
73  if (method && method->handler() == nullptr) {
74  return true;
75  }
76  }
77  return false;
78  }
79 
80  bool has_synchronous_methods() const {
81  for (const auto& method : methods_) {
82  if (method &&
83  method->api_type() == internal::RpcServiceMethod::ApiType::SYNC) {
84  return true;
85  }
86  }
87  return false;
88  }
89 
90  bool has_callback_methods() const {
91  for (const auto& method : methods_) {
92  if (method && (method->api_type() ==
94  method->api_type() ==
96  return true;
97  }
98  }
99  return false;
100  }
101 
102  bool has_generic_methods() const {
103  for (const auto& method : methods_) {
104  if (method == nullptr) {
105  return true;
106  }
107  }
108  return false;
109  }
110 
111  protected:
112  template <class Message>
113  void RequestAsyncUnary(int index, grpc::ServerContext* context,
114  Message* request,
116  grpc::CompletionQueue* call_cq,
117  grpc::ServerCompletionQueue* notification_cq,
118  void* tag) {
119  // Typecast the index to size_t for indexing into a vector
120  // while preserving the API that existed before a compiler
121  // warning was first seen (grpc/grpc#11664)
122  size_t idx = static_cast<size_t>(index);
123  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
124  notification_cq, tag, request);
125  }
127  int index, grpc::ServerContext* context,
129  grpc::CompletionQueue* call_cq,
130  grpc::ServerCompletionQueue* notification_cq, void* tag) {
131  size_t idx = static_cast<size_t>(index);
132  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
133  notification_cq, tag);
134  }
135  template <class Message>
137  int index, grpc::ServerContext* context, Message* request,
139  grpc::CompletionQueue* call_cq,
140  grpc::ServerCompletionQueue* notification_cq, void* tag) {
141  size_t idx = static_cast<size_t>(index);
142  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
143  notification_cq, tag, request);
144  }
146  int index, grpc::ServerContext* context,
148  grpc::CompletionQueue* call_cq,
149  grpc::ServerCompletionQueue* notification_cq, void* tag) {
150  size_t idx = static_cast<size_t>(index);
151  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
152  notification_cq, tag);
153  }
154 
156  methods_.emplace_back(method);
157  }
158 
159  void MarkMethodAsync(int index) {
160  // This does not have to be a hard error, however no one has approached us
161  // with a use case yet. Please file an issue if you believe you have one.
162  size_t idx = static_cast<size_t>(index);
163  ABSL_CHECK_NE(methods_[idx].get(), nullptr)
164  << "Cannot mark the method as 'async' because it has already been "
165  "marked as 'generic'.";
166  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::ASYNC);
167  }
168 
169  void MarkMethodRaw(int index) {
170  // This does not have to be a hard error, however no one has approached us
171  // with a use case yet. Please file an issue if you believe you have one.
172  size_t idx = static_cast<size_t>(index);
173  ABSL_CHECK_NE(methods_[idx].get(), nullptr)
174  << "Cannot mark the method as 'raw' because it has already "
175  "been marked as 'generic'.";
176  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::RAW);
177  }
178 
179  void MarkMethodGeneric(int index) {
180  // This does not have to be a hard error, however no one has approached us
181  // with a use case yet. Please file an issue if you believe you have one.
182  size_t idx = static_cast<size_t>(index);
183  ABSL_CHECK_NE(methods_[idx]->handler(), nullptr)
184  << "Cannot mark the method as 'generic' because it has already been "
185  "marked as 'async' or 'raw'.";
186  methods_[idx].reset();
187  }
188 
189  void MarkMethodStreamed(int index, internal::MethodHandler* streamed_method) {
190  // This does not have to be a hard error, however no one has approached us
191  // with a use case yet. Please file an issue if you believe you have one.
192  size_t idx = static_cast<size_t>(index);
193  ABSL_CHECK(methods_[idx] && methods_[idx]->handler())
194  << "Cannot mark an async or generic method Streamed";
195  methods_[idx]->SetHandler(streamed_method);
196 
197  // From the server's point of view, streamed unary is a special
198  // case of BIDI_STREAMING that has 1 read and 1 write, in that order,
199  // and split server-side streaming is BIDI_STREAMING with 1 read and
200  // any number of writes, in that order.
201  methods_[idx]->SetMethodType(internal::RpcMethod::BIDI_STREAMING);
202  }
203 
204  void MarkMethodCallback(int index, internal::MethodHandler* handler) {
205  // This does not have to be a hard error, however no one has approached us
206  // with a use case yet. Please file an issue if you believe you have one.
207  size_t idx = static_cast<size_t>(index);
208  ABSL_CHECK_NE(methods_[idx].get(), nullptr)
209  << "Cannot mark the method as 'callback' because it has already been "
210  "marked as 'generic'.";
211  methods_[idx]->SetHandler(handler);
212  methods_[idx]->SetServerApiType(
214  }
215 
216  void MarkMethodRawCallback(int index, internal::MethodHandler* handler) {
217  // This does not have to be a hard error, however no one has approached us
218  // with a use case yet. Please file an issue if you believe you have one.
219  size_t idx = static_cast<size_t>(index);
220  ABSL_CHECK_NE(methods_[idx].get(), nullptr)
221  << "Cannot mark the method as 'raw callback' because it has already "
222  "been marked as 'generic'.";
223  methods_[idx]->SetHandler(handler);
224  methods_[idx]->SetServerApiType(
226  }
227 
229  size_t idx = static_cast<size_t>(index);
230  return methods_[idx]->handler();
231  }
232 
233  private:
234  friend class Server;
235  friend class ServerInterface;
236  template <class RequestType>
238  friend void experimental::SetVirtualService(Service* service);
239  ServerInterface* server_;
240  bool is_virtual_service_ = false;
241  std::vector<std::unique_ptr<internal::RpcServiceMethod>> methods_;
242 };
243 
244 namespace experimental {
245 inline void SetVirtualService(Service* service) {
246  service->is_virtual_service_ = true;
247 }
248 } // namespace experimental
249 
250 } // namespace grpc
251 
252 #endif // GRPCPP_IMPL_SERVICE_TYPE_H
grpc::Service::RequestAsyncUnary
void RequestAsyncUnary(int index, grpc::ServerContext *context, Message *request, internal::ServerAsyncStreamingInterface *stream, grpc::CompletionQueue *call_cq, grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: service_type.h:113
grpc::Service::has_callback_methods
bool has_callback_methods() const
Definition: service_type.h:90
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:47
grpc::Service::MarkMethodRaw
void MarkMethodRaw(int index)
Definition: service_type.h:169
grpc::Service::MarkMethodAsync
void MarkMethodAsync(int index)
Definition: service_type.h:159
grpc::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:592
grpc::Server
Represents a gRPC server.
Definition: server.h:58
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::Service::~Service
virtual ~Service()
Definition: service_type.h:69
grpc::Service::GetHandler
internal::MethodHandler * GetHandler(int index)
Definition: service_type.h:228
grpc::Service::has_generic_methods
bool has_generic_methods() const
Definition: service_type.h:102
grpc::Service::RequestAsyncBidiStreaming
void RequestAsyncBidiStreaming(int index, grpc::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, grpc::CompletionQueue *call_cq, grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: service_type.h:145
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:34
rpc_service_method.h
grpc::Service
Descriptor of an RPC service and its various RPC methods.
Definition: service_type.h:66
grpc::Service::MarkMethodCallback
void MarkMethodCallback(int index, internal::MethodHandler *handler)
Definition: service_type.h:204
status.h
grpc::internal::RpcMethod::BIDI_STREAMING
@ BIDI_STREAMING
Definition: rpc_method.h:35
grpc::Service::MarkMethodGeneric
void MarkMethodGeneric(int index)
Definition: service_type.h:179
grpc::Service::AddMethod
void AddMethod(internal::RpcServiceMethod *method)
Definition: service_type.h:155
grpc::Service::has_synchronous_methods
bool has_synchronous_methods() const
Definition: service_type.h:80
grpc::Service::MarkMethodStreamed
void MarkMethodStreamed(int index, internal::MethodHandler *streamed_method)
Definition: service_type.h:189
server_interface.h
grpc::internal::RpcServiceMethod::ApiType::SYNC
@ SYNC
grpc::internal::ServerAsyncStreamingInterface::SendInitialMetadata
virtual void SendInitialMetadata(void *tag)=0
Request notification of the sending of initial metadata to the client.
grpc::Service::RequestAsyncServerStreaming
void RequestAsyncServerStreaming(int index, grpc::ServerContext *context, Message *request, internal::ServerAsyncStreamingInterface *stream, grpc::CompletionQueue *call_cq, grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: service_type.h:136
grpc::internal::MethodHandler
Base class for running an RPC handler.
Definition: rpc_service_method.h:40
grpc::experimental::internal::CallbackSessionHandler
Definition: server_callback_handlers.h:911
serialization_traits.h
grpc::ServerInterface::RequestAsyncCall
void RequestAsyncCall(internal::RpcServiceMethod *method, grpc::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, grpc::CompletionQueue *call_cq, grpc::ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:314
grpc::internal::RpcServiceMethod::ApiType::ASYNC
@ ASYNC
config.h
grpc::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:426
grpc::internal::RpcServiceMethod::ApiType::CALL_BACK
@ CALL_BACK
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:104
grpc::internal::RpcServiceMethod::ApiType::RAW_CALL_BACK
@ RAW_CALL_BACK
grpc::experimental::SetVirtualService
void SetVirtualService(Service *service)
Definition: service_type.h:245
grpc::internal::RpcServiceMethod
Server side rpc method class.
Definition: rpc_service_method.h:86
grpc::Service::has_async_methods
bool has_async_methods() const
Definition: service_type.h:71
grpc::Service::MarkMethodRawCallback
void MarkMethodRawCallback(int index, internal::MethodHandler *handler)
Definition: service_type.h:216
grpc::ServerInterface
Definition: server_interface.h:59
grpc::protobuf::Message
::google::protobuf::Message Message
Definition: config_protobuf.h:89
grpc::Service::RequestAsyncClientStreaming
void RequestAsyncClientStreaming(int index, grpc::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, grpc::CompletionQueue *call_cq, grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: service_type.h:126
grpc::internal::ServerAsyncStreamingInterface::~ServerAsyncStreamingInterface
virtual ~ServerAsyncStreamingInterface()
Definition: service_type.h:49
grpc::internal::RpcServiceMethod::ApiType::RAW
@ RAW
grpc::Service::Service
Service()
Definition: service_type.h:68