GRPC C++  1.46.2
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 
22 // IWYU pragma: private, include <grpcpp/impl/service_type.h>
23 
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);
156  methods_[idx].get() != nullptr &&
157  "Cannot mark the method as 'async' because it has already been "
158  "marked as 'generic'.");
159  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::ASYNC);
160  }
161 
162  void MarkMethodRaw(int index) {
163  // This does not have to be a hard error, however no one has approached us
164  // with a use case yet. Please file an issue if you believe you have one.
165  size_t idx = static_cast<size_t>(index);
166  GPR_CODEGEN_ASSERT(methods_[idx].get() != nullptr &&
167  "Cannot mark the method as 'raw' because it has already "
168  "been marked as 'generic'.");
169  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::RAW);
170  }
171 
172  void MarkMethodGeneric(int index) {
173  // This does not have to be a hard error, however no one has approached us
174  // with a use case yet. Please file an issue if you believe you have one.
175  size_t idx = static_cast<size_t>(index);
177  methods_[idx]->handler() != nullptr &&
178  "Cannot mark the method as 'generic' because it has already been "
179  "marked as 'async' or 'raw'.");
180  methods_[idx].reset();
181  }
182 
183  void MarkMethodStreamed(int index, internal::MethodHandler* streamed_method) {
184  // This does not have to be a hard error, however no one has approached us
185  // with a use case yet. Please file an issue if you believe you have one.
186  size_t idx = static_cast<size_t>(index);
187  GPR_CODEGEN_ASSERT(methods_[idx] && methods_[idx]->handler() &&
188  "Cannot mark an async or generic method Streamed");
189  methods_[idx]->SetHandler(streamed_method);
190 
191  // From the server's point of view, streamed unary is a special
192  // case of BIDI_STREAMING that has 1 read and 1 write, in that order,
193  // and split server-side streaming is BIDI_STREAMING with 1 read and
194  // any number of writes, in that order.
195  methods_[idx]->SetMethodType(internal::RpcMethod::BIDI_STREAMING);
196  }
197 
198  void MarkMethodCallback(int index, internal::MethodHandler* handler) {
199  // This does not have to be a hard error, however no one has approached us
200  // with a use case yet. Please file an issue if you believe you have one.
201  size_t idx = static_cast<size_t>(index);
203  methods_[idx].get() != nullptr &&
204  "Cannot mark the method as 'callback' because it has already been "
205  "marked as 'generic'.");
206  methods_[idx]->SetHandler(handler);
207  methods_[idx]->SetServerApiType(
209  }
210 
211  void MarkMethodRawCallback(int index, internal::MethodHandler* handler) {
212  // This does not have to be a hard error, however no one has approached us
213  // with a use case yet. Please file an issue if you believe you have one.
214  size_t idx = static_cast<size_t>(index);
216  methods_[idx].get() != nullptr &&
217  "Cannot mark the method as 'raw callback' because it has already "
218  "been marked as 'generic'.");
219  methods_[idx]->SetHandler(handler);
220  methods_[idx]->SetServerApiType(
222  }
223 
225  size_t idx = static_cast<size_t>(index);
226  return methods_[idx]->handler();
227  }
228 
229  private:
230  friend class Server;
231  friend class ServerInterface;
232  ServerInterface* server_;
233  std::vector<std::unique_ptr<internal::RpcServiceMethod>> methods_;
234 };
235 
236 } // namespace grpc
237 
238 #endif // GRPCPP_IMPL_CODEGEN_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:162
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:544
grpc::Server
Represents a gRPC server.
Definition: server.h:59
rpc_service_method.h
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
status.h
grpc::Service::~Service
virtual ~Service()
Definition: service_type.h:61
grpc::Service::GetHandler
internal::MethodHandler * GetHandler(int index)
Definition: service_type.h:224
serialization_traits.h
config.h
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:37
grpc::Service
Desriptor 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:198
core_codegen_interface.h
grpc::internal::RpcMethod::BIDI_STREAMING
@ BIDI_STREAMING
Definition: rpc_method.h:37
grpc::Service::MarkMethodGeneric
void MarkMethodGeneric(int index)
Definition: service_type.h:172
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:183
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
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:315
grpc::internal::RpcServiceMethod::ApiType::ASYNC
@ ASYNC
grpc::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:436
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
server_interface.h
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:149
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:211
grpc::ServerInterface
Definition: server_interface.h:61
grpc::protobuf::Message
::google::protobuf::Message Message
Definition: config_protobuf.h:78
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