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