GRPC C++  1.26.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() ==
87  internal::RpcServiceMethod::ApiType::CALL_BACK ||
88  method->api_type() ==
89  internal::RpcServiceMethod::ApiType::RAW_CALL_BACK)) {
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  // This does not have to be a hard error, however no one has approached us
113  // with a use case yet. Please file an issue if you believe you have one.
114  size_t idx = static_cast<size_t>(index);
116  service_->methods_[idx].get() != nullptr &&
117  "Cannot mark the method as 'callback' because it has already been "
118  "marked as 'generic'.");
119  service_->methods_[idx]->SetHandler(handler);
120  service_->methods_[idx]->SetServerApiType(
121  internal::RpcServiceMethod::ApiType::CALL_BACK);
122  }
123 
124  void MarkMethodRawCallback(int index, internal::MethodHandler* handler) {
125  // This does not have to be a hard error, however no one has approached us
126  // with a use case yet. Please file an issue if you believe you have one.
127  size_t idx = static_cast<size_t>(index);
129  service_->methods_[idx].get() != nullptr &&
130  "Cannot mark the method as 'raw callback' because it has already "
131  "been marked as 'generic'.");
132  service_->methods_[idx]->SetHandler(handler);
133  service_->methods_[idx]->SetServerApiType(
134  internal::RpcServiceMethod::ApiType::RAW_CALL_BACK);
135  }
136 
138  size_t idx = static_cast<size_t>(index);
139  return service_->methods_[idx]->handler();
140  }
141 
142  private:
143  Service* service_;
144  };
145 
147 
148  template <class Message>
149  void RequestAsyncUnary(int index, ::grpc_impl::ServerContext* context,
150  Message* request,
152  ::grpc_impl::CompletionQueue* call_cq,
153  ::grpc_impl::ServerCompletionQueue* notification_cq,
154  void* tag) {
155  // Typecast the index to size_t for indexing into a vector
156  // while preserving the API that existed before a compiler
157  // warning was first seen (grpc/grpc#11664)
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  template <class Message>
173  int index, ::grpc_impl::ServerContext* context, Message* request,
175  ::grpc_impl::CompletionQueue* call_cq,
176  ::grpc_impl::ServerCompletionQueue* notification_cq, void* tag) {
177  size_t idx = static_cast<size_t>(index);
178  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
179  notification_cq, tag, request);
180  }
182  int index, ::grpc_impl::ServerContext* context,
184  ::grpc_impl::CompletionQueue* call_cq,
185  ::grpc_impl::ServerCompletionQueue* notification_cq, void* tag) {
186  size_t idx = static_cast<size_t>(index);
187  server_->RequestAsyncCall(methods_[idx].get(), context, stream, call_cq,
188  notification_cq, tag);
189  }
190 
192  methods_.emplace_back(method);
193  }
194 
195  void MarkMethodAsync(int index) {
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);
200  methods_[idx].get() != nullptr &&
201  "Cannot mark the method as 'async' because it has already been "
202  "marked as 'generic'.");
203  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::ASYNC);
204  }
205 
206  void MarkMethodRaw(int index) {
207  // This does not have to be a hard error, however no one has approached us
208  // with a use case yet. Please file an issue if you believe you have one.
209  size_t idx = static_cast<size_t>(index);
210  GPR_CODEGEN_ASSERT(methods_[idx].get() != nullptr &&
211  "Cannot mark the method as 'raw' because it has already "
212  "been marked as 'generic'.");
213  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::RAW);
214  }
215 
216  void MarkMethodGeneric(int index) {
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);
221  methods_[idx]->handler() != nullptr &&
222  "Cannot mark the method as 'generic' because it has already been "
223  "marked as 'async' or 'raw'.");
224  methods_[idx].reset();
225  }
226 
227  void MarkMethodStreamed(int index, internal::MethodHandler* streamed_method) {
228  // This does not have to be a hard error, however no one has approached us
229  // with a use case yet. Please file an issue if you believe you have one.
230  size_t idx = static_cast<size_t>(index);
231  GPR_CODEGEN_ASSERT(methods_[idx] && methods_[idx]->handler() &&
232  "Cannot mark an async or generic method Streamed");
233  methods_[idx]->SetHandler(streamed_method);
234 
235  // From the server's point of view, streamed unary is a special
236  // case of BIDI_STREAMING that has 1 read and 1 write, in that order,
237  // and split server-side streaming is BIDI_STREAMING with 1 read and
238  // any number of writes, in that order.
239  methods_[idx]->SetMethodType(internal::RpcMethod::BIDI_STREAMING);
240  }
241 
242  private:
243  friend class grpc_impl::Server;
244  friend class ServerInterface;
245  ServerInterface* server_;
246  std::vector<std::unique_ptr<internal::RpcServiceMethod>> methods_;
247 };
248 
249 } // namespace grpc
250 
251 #endif // GRPCPP_IMPL_CODEGEN_SERVICE_TYPE_H
void MarkMethodGeneric(int index)
Definition: service_type.h:216
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
Represents a gRPC server.
Definition: server_impl.h:62
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:162
void AddMethod(internal::RpcServiceMethod *method)
Definition: service_type.h:191
void MarkMethodStreamed(int index, internal::MethodHandler *streamed_method)
Definition: service_type.h:227
Definition: service_type.h:107
bool has_synchronous_methods() const
Definition: service_type.h:74
::grpc_impl::Server Server
Definition: server.h:26
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:60
experimental_type(Service *service)
Definition: service_type.h:109
::grpc_impl::ServerContext ServerContext
Definition: server_context.h:26
A ServerContext or CallbackServerContext allows the code implementing a service handler to: ...
Definition: server_context_impl.h:488
void MarkMethodAsync(int index)
Definition: service_type.h:195
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
virtual ~ServerAsyncStreamingInterface()
Definition: service_type.h:43
bool has_async_methods() const
Definition: service_type.h:65
experimental_type experimental()
Definition: service_type.h:146
void MarkMethodCallback(int index, internal::MethodHandler *handler)
Definition: service_type.h:111
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:149
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
internal::MethodHandler * GetHandler(int index)
Definition: service_type.h:137
Server side rpc method class.
Definition: rpc_service_method.h:87
bool has_generic_methods() const
Definition: service_type.h:96
bool has_callback_methods() const
Definition: service_type.h:84
Service()
Definition: service_type.h:62
void MarkMethodRaw(int index)
Definition: service_type.h:206
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:181
Definition: server_interface.h:59
virtual ~Service()
Definition: service_type.h:63
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:172
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
Base class for running an RPC handler.
Definition: rpc_service_method.h:41
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:100
void MarkMethodRawCallback(int index, internal::MethodHandler *handler)
Definition: service_type.h:124
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue_impl.h:389
Straightforward wrapping of the C call object.
Definition: call.h:38
::google::protobuf::Message Message
Definition: config_protobuf.h:75