GRPC C++  1.39.1
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 {
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);
154  methods_[idx].get() != nullptr &&
155  "Cannot mark the method as 'async' because it has already been "
156  "marked as 'generic'.");
157  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::ASYNC);
158  }
159 
160  void MarkMethodRaw(int index) {
161  // This does not have to be a hard error, however no one has approached us
162  // with a use case yet. Please file an issue if you believe you have one.
163  size_t idx = static_cast<size_t>(index);
164  GPR_CODEGEN_ASSERT(methods_[idx].get() != nullptr &&
165  "Cannot mark the method as 'raw' because it has already "
166  "been marked as 'generic'.");
167  methods_[idx]->SetServerApiType(internal::RpcServiceMethod::ApiType::RAW);
168  }
169 
170  void MarkMethodGeneric(int index) {
171  // This does not have to be a hard error, however no one has approached us
172  // with a use case yet. Please file an issue if you believe you have one.
173  size_t idx = static_cast<size_t>(index);
175  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  GPR_CODEGEN_ASSERT(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);
201  methods_[idx].get() != nullptr &&
202  "Cannot mark the method as 'callback' because it has already been "
203  "marked as 'generic'.");
204  methods_[idx]->SetHandler(handler);
205  methods_[idx]->SetServerApiType(
207  }
208 
209  void MarkMethodRawCallback(int index, internal::MethodHandler* handler) {
210  // This does not have to be a hard error, however no one has approached us
211  // with a use case yet. Please file an issue if you believe you have one.
212  size_t idx = static_cast<size_t>(index);
214  methods_[idx].get() != nullptr &&
215  "Cannot mark the method as 'raw callback' because it has already "
216  "been marked as 'generic'.");
217  methods_[idx]->SetHandler(handler);
218  methods_[idx]->SetServerApiType(
220  }
221 
223  size_t idx = static_cast<size_t>(index);
224  return methods_[idx]->handler();
225  }
226 
227  private:
228  friend class Server;
229  friend class ServerInterface;
230  ServerInterface* server_;
231  std::vector<std::unique_ptr<internal::RpcServiceMethod>> methods_;
232 };
233 
234 } // namespace grpc
235 
236 #endif // GRPCPP_IMPL_CODEGEN_SERVICE_TYPE_H
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:102
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:429
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:538
Represents a gRPC server.
Definition: server.h:59
Definition: server_interface.h:59
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
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:56
void MarkMethodGeneric(int index)
Definition: service_type.h:170
internal::MethodHandler * GetHandler(int index)
Definition: service_type.h:222
bool has_callback_methods() const
Definition: service_type.h:80
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
void MarkMethodStreamed(int index, internal::MethodHandler *streamed_method)
Definition: service_type.h:181
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
void MarkMethodRawCallback(int index, internal::MethodHandler *handler)
Definition: service_type.h:209
void MarkMethodCallback(int index, internal::MethodHandler *handler)
Definition: service_type.h:196
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
Service()
Definition: service_type.h:58
void MarkMethodRaw(int index)
Definition: service_type.h:160
bool has_generic_methods() const
Definition: service_type.h:92
void AddMethod(internal::RpcServiceMethod *method)
Definition: service_type.h:145
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
virtual ~Service()
Definition: service_type.h:59
bool has_synchronous_methods() const
Definition: service_type.h:70
void MarkMethodAsync(int index)
Definition: service_type.h:149
bool has_async_methods() const
Definition: service_type.h:61
Straightforward wrapping of the C call object.
Definition: call.h:35
Base class for running an RPC handler.
Definition: rpc_service_method.h:38
@ BIDI_STREAMING
Definition: rpc_method.h:35
Server side rpc method class.
Definition: rpc_service_method.h:84
virtual void SendInitialMetadata(void *tag)=0
Request notification of the sending of initial metadata to the client.
virtual ~ServerAsyncStreamingInterface()
Definition: service_type.h:39
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
::google::protobuf::Message Message
Definition: config_protobuf.h:76
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33