GRPC C++  1.62.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 
22 #include <grpc/support/log.h>
26 #include <grpcpp/support/config.h>
27 #include <grpcpp/support/status.h>
28 
29 namespace grpc {
30 
31 class CompletionQueue;
32 class ServerContext;
33 class ServerInterface;
34 
35 namespace internal {
36 class Call;
38  public:
40 
47  virtual void SendInitialMetadata(void* tag) = 0;
48 
49  private:
50  friend class grpc::ServerInterface;
51  virtual void BindCall(Call* call) = 0;
52 };
53 } // namespace internal
54 
56 class Service {
57  public:
58  Service() : server_(nullptr) {}
59  virtual ~Service() {}
60 
61  bool has_async_methods() const {
62  for (const auto& method : methods_) {
63  if (method && method->handler() == nullptr) {
64  return true;
65  }
66  }
67  return false;
68  }
69 
70  bool has_synchronous_methods() const {
71  for (const auto& method : methods_) {
72  if (method &&
73  method->api_type() == internal::RpcServiceMethod::ApiType::SYNC) {
74  return true;
75  }
76  }
77  return false;
78  }
79 
80  bool has_callback_methods() const {
81  for (const auto& method : methods_) {
82  if (method && (method->api_type() ==
84  method->api_type() ==
86  return true;
87  }
88  }
89  return false;
90  }
91 
92  bool has_generic_methods() const {
93  for (const auto& method : methods_) {
94  if (method == nullptr) {
95  return true;
96  }
97  }
98  return false;
99  }
100 
101  protected:
102  template <class Message>
103  void RequestAsyncUnary(int index, grpc::ServerContext* context,
104  Message* request,
106  grpc::CompletionQueue* call_cq,
107  grpc::ServerCompletionQueue* notification_cq,
108  void* tag) {
109  // Typecast the index to size_t for indexing into a vector
110  // while preserving the API that existed before a compiler
111  // warning was first seen (grpc/grpc#11664)
112  size_t idx = static_cast<size_t>(index);
113  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
114  notification_cq, tag, request);
115  }
117  int index, grpc::ServerContext* context,
119  grpc::CompletionQueue* call_cq,
120  grpc::ServerCompletionQueue* notification_cq, void* tag) {
121  size_t idx = static_cast<size_t>(index);
122  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
123  notification_cq, tag);
124  }
125  template <class Message>
127  int index, grpc::ServerContext* context, Message* request,
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, request);
134  }
136  int index, grpc::ServerContext* context,
138  grpc::CompletionQueue* call_cq,
139  grpc::ServerCompletionQueue* notification_cq, void* tag) {
140  size_t idx = static_cast<size_t>(index);
141  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
142  notification_cq, tag);
143  }
144 
146  methods_.emplace_back(method);
147  }
148 
149  void MarkMethodAsync(int index) {
150  // This does not have to be a hard error, however no one has approached us
151  // with a use case yet. Please file an issue if you believe you have one.
152  size_t idx = static_cast<size_t>(index);
153  GPR_ASSERT(methods_[idx].get() != nullptr &&
154  "Cannot mark the method as 'async' because it has already been "
155  "marked as 'generic'.");
156  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::ASYNC);
157  }
158 
159  void MarkMethodRaw(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  GPR_ASSERT(methods_[idx].get() != nullptr &&
164  "Cannot mark the method as 'raw' because it has already "
165  "been marked as 'generic'.");
166  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::RAW);
167  }
168 
169  void MarkMethodGeneric(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  GPR_ASSERT(
174  methods_[idx]->handler() != nullptr &&
175  "Cannot mark the method as 'generic' because it has already been "
176  "marked as 'async' or 'raw'.");
177  methods_[idx].reset();
178  }
179 
180  void MarkMethodStreamed(int index, internal::MethodHandler* streamed_method) {
181  // This does not have to be a hard error, however no one has approached us
182  // with a use case yet. Please file an issue if you believe you have one.
183  size_t idx = static_cast<size_t>(index);
184  GPR_ASSERT(methods_[idx] && methods_[idx]->handler() &&
185  "Cannot mark an async or generic method Streamed");
186  methods_[idx]->SetHandler(streamed_method);
187 
188  // From the server's point of view, streamed unary is a special
189  // case of BIDI_STREAMING that has 1 read and 1 write, in that order,
190  // and split server-side streaming is BIDI_STREAMING with 1 read and
191  // any number of writes, in that order.
192  methods_[idx]->SetMethodType(internal::RpcMethod::BIDI_STREAMING);
193  }
194 
195  void MarkMethodCallback(int index, internal::MethodHandler* handler) {
196  // This does not have to be a hard error, however no one has approached us
197  // with a use case yet. Please file an issue if you believe you have one.
198  size_t idx = static_cast<size_t>(index);
199  GPR_ASSERT(
200  methods_[idx].get() != nullptr &&
201  "Cannot mark the method as 'callback' because it has already been "
202  "marked as 'generic'.");
203  methods_[idx]->SetHandler(handler);
204  methods_[idx]->SetServerApiType(
206  }
207 
208  void MarkMethodRawCallback(int index, internal::MethodHandler* handler) {
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_ASSERT(
213  methods_[idx].get() != nullptr &&
214  "Cannot mark the method as 'raw callback' because it has already "
215  "been marked as 'generic'.");
216  methods_[idx]->SetHandler(handler);
217  methods_[idx]->SetServerApiType(
219  }
220 
222  size_t idx = static_cast<size_t>(index);
223  return methods_[idx]->handler();
224  }
225 
226  private:
227  friend class Server;
228  friend class ServerInterface;
229  ServerInterface* server_;
230  std::vector<std::unique_ptr<internal::RpcServiceMethod>> methods_;
231 };
232 
233 } // namespace grpc
234 
235 #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:103
grpc::Service::has_callback_methods
bool has_callback_methods() const
Definition: service_type.h:80
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:37
grpc::Service::MarkMethodRaw
void MarkMethodRaw(int index)
Definition: service_type.h:159
grpc::Service::MarkMethodAsync
void MarkMethodAsync(int index)
Definition: service_type.h:149
grpc::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:572
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:59
grpc::Service::GetHandler
internal::MethodHandler * GetHandler(int index)
Definition: service_type.h:221
grpc::Service::has_generic_methods
bool has_generic_methods() const
Definition: service_type.h:92
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:135
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:36
GPR_ASSERT
#define GPR_ASSERT(x)
abort() the process if x is zero, having written a line to the log.
Definition: log.h:95
rpc_service_method.h
grpc::Service
Descriptor of an RPC service and its various RPC methods.
Definition: service_type.h:56
grpc::Service::MarkMethodCallback
void MarkMethodCallback(int index, internal::MethodHandler *handler)
Definition: service_type.h:195
status.h
log.h
grpc::internal::RpcMethod::BIDI_STREAMING
@ BIDI_STREAMING
Definition: rpc_method.h:35
grpc::Service::MarkMethodGeneric
void MarkMethodGeneric(int index)
Definition: service_type.h:169
grpc::Service::AddMethod
void AddMethod(internal::RpcServiceMethod *method)
Definition: service_type.h:145
grpc::Service::has_synchronous_methods
bool has_synchronous_methods() const
Definition: service_type.h:70
grpc::Service::MarkMethodStreamed
void MarkMethodStreamed(int index, internal::MethodHandler *streamed_method)
Definition: service_type.h:180
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:126
grpc::internal::MethodHandler
Base class for running an RPC handler.
Definition: rpc_service_method.h:38
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:316
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:425
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:103
grpc::internal::RpcServiceMethod::ApiType::RAW_CALL_BACK
@ RAW_CALL_BACK
grpc::internal::RpcServiceMethod
Server side rpc method class.
Definition: rpc_service_method.h:84
grpc::Service::has_async_methods
bool has_async_methods() const
Definition: service_type.h:61
grpc::Service::MarkMethodRawCallback
void MarkMethodRawCallback(int index, internal::MethodHandler *handler)
Definition: service_type.h:208
grpc::ServerInterface
Definition: server_interface.h:60
grpc::protobuf::Message
::google::protobuf::Message Message
Definition: config_protobuf.h:82
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:116
grpc::internal::ServerAsyncStreamingInterface::~ServerAsyncStreamingInterface
virtual ~ServerAsyncStreamingInterface()
Definition: service_type.h:39
grpc::internal::RpcServiceMethod::ApiType::RAW
@ RAW
grpc::Service::Service
Service()
Definition: service_type.h:58