GRPC C++  1.17.0
server_interface.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_SERVER_INTERFACE_H
20 #define GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
21 
30 
31 namespace grpc {
32 
33 class AsyncGenericService;
34 class Channel;
35 class GenericServerContext;
36 class ServerCompletionQueue;
37 class ServerContext;
38 class ServerCredentials;
39 class Service;
40 
41 extern CoreCodegenInterface* g_core_codegen_interface;
42 
46 namespace internal {
47 class ServerAsyncStreamingInterface;
48 } // namespace internal
49 
51  public:
52  virtual ~ServerInterface() {}
53 
86  template <class T>
87  void Shutdown(const T& deadline) {
88  ShutdownInternal(TimePoint<T>(deadline).raw_time());
89  }
90 
96  void Shutdown() {
97  ShutdownInternal(
98  g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_MONOTONIC));
99  }
100 
105  virtual void Wait() = 0;
106 
107  protected:
108  friend class ::grpc::Service;
109 
112  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
113 
116  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
117 
129  virtual int AddListeningPort(const grpc::string& addr,
130  ServerCredentials* creds) = 0;
131 
138  virtual void Start(ServerCompletionQueue** cqs, size_t num_cqs) = 0;
139 
140  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
141 
142  virtual int max_receive_message_size() const = 0;
143 
144  virtual grpc_server* server() = 0;
145 
146  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
147  internal::Call* call) = 0;
148 
150  public:
153  CompletionQueue* call_cq,
154  ServerCompletionQueue* notification_cq, void* tag,
155  bool delete_on_finalize);
156  virtual ~BaseAsyncRequest();
157 
158  bool FinalizeResult(void** tag, bool* status) override;
159 
160  private:
161  void ContinueFinalizeResultAfterInterception();
162 
163  protected:
169  void* const tag_;
175  };
176 
178  public:
181  CompletionQueue* call_cq,
182  ServerCompletionQueue* notification_cq, void* tag,
183  const char* name);
184 
185  virtual bool FinalizeResult(void** tag, bool* status) override {
186  /* If we are done intercepting, then there is nothing more for us to do */
187  if (done_intercepting_) {
188  return BaseAsyncRequest::FinalizeResult(tag, status);
189  }
190  call_wrapper_ = internal::Call(
191  call_, server_, call_cq_, server_->max_receive_message_size(),
192  context_->set_server_rpc_info(name_,
193  *server_->interceptor_creators()));
194  return BaseAsyncRequest::FinalizeResult(tag, status);
195  }
196 
197  protected:
198  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
199  ServerCompletionQueue* notification_cq);
200  const char* name_;
201  };
202 
204  public:
206  ServerInterface* server, ServerContext* context,
208  CompletionQueue* call_cq,
209  ServerCompletionQueue* notification_cq, void* tag)
210  : RegisteredAsyncRequest(server, context, stream, call_cq,
211  notification_cq, tag,
212  registered_method->name()) {
213  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
214  }
215 
216  // uses RegisteredAsyncRequest::FinalizeResult
217  };
218 
219  template <class Message>
221  public:
223  ServerInterface* server, ServerContext* context,
225  CompletionQueue* call_cq,
226  ServerCompletionQueue* notification_cq, void* tag,
227  Message* request)
228  : RegisteredAsyncRequest(server, context, stream, call_cq,
229  notification_cq, tag,
230  registered_method->name()),
231  registered_method_(registered_method),
232  server_(server),
233  context_(context),
234  stream_(stream),
235  call_cq_(call_cq),
236  notification_cq_(notification_cq),
237  tag_(tag),
238  request_(request) {
239  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
240  notification_cq);
241  }
242 
244  payload_.Release(); // We do not own the payload_
245  }
246 
247  bool FinalizeResult(void** tag, bool* status) override {
248  /* If we are done intercepting, then there is nothing more for us to do */
249  if (done_intercepting_) {
250  return RegisteredAsyncRequest::FinalizeResult(tag, status);
251  }
252  if (*status) {
253  if (!payload_.Valid() || !SerializationTraits<Message>::Deserialize(
254  payload_.bbuf_ptr(), request_)
255  .ok()) {
256  // If deserialization fails, we cancel the call and instantiate
257  // a new instance of ourselves to request another call. We then
258  // return false, which prevents the call from being returned to
259  // the application.
260  g_core_codegen_interface->grpc_call_cancel_with_status(
261  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
262  g_core_codegen_interface->grpc_call_unref(call_);
263  new PayloadAsyncRequest(registered_method_, server_, context_,
264  stream_, call_cq_, notification_cq_, tag_,
265  request_);
266  delete this;
267  return false;
268  }
269  }
270  /* Set interception point for recv message */
271  interceptor_methods_.AddInterceptionHookPoint(
273  interceptor_methods_.SetRecvMessage(request_);
274  return RegisteredAsyncRequest::FinalizeResult(tag, status);
275  }
276 
277  private:
278  internal::RpcServiceMethod* const registered_method_;
279  ServerInterface* const server_;
280  ServerContext* const context_;
282  CompletionQueue* const call_cq_;
283 
284  ServerCompletionQueue* const notification_cq_;
285  void* const tag_;
286  Message* const request_;
287  ByteBuffer payload_;
288  };
289 
291  public:
294  CompletionQueue* call_cq,
295  ServerCompletionQueue* notification_cq, void* tag,
296  bool delete_on_finalize);
297 
298  bool FinalizeResult(void** tag, bool* status) override;
299 
300  private:
301  grpc_call_details call_details_;
302  };
303 
304  template <class Message>
306  ServerContext* context,
308  CompletionQueue* call_cq,
309  ServerCompletionQueue* notification_cq, void* tag,
310  Message* message) {
311  GPR_CODEGEN_ASSERT(method);
312  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
313  notification_cq, tag, message);
314  }
315 
317  ServerContext* context,
319  CompletionQueue* call_cq,
320  ServerCompletionQueue* notification_cq, void* tag) {
321  GPR_CODEGEN_ASSERT(method);
322  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
323  notification_cq, tag);
324  }
325 
328  CompletionQueue* call_cq,
329  ServerCompletionQueue* notification_cq,
330  void* tag) {
331  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
332  tag, true);
333  }
334 
335  private:
336  // EXPERIMENTAL
337  // Getter method for the vector of interceptor factory objects.
338  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
339  // and adding a new pure method to an interface would be a breaking change
340  // (even though this is private and non-API)
341  virtual std::vector<
342  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
343  interceptor_creators() {
344  return nullptr;
345  }
346 
347  // EXPERIMENTAL
348  // A method to get the callbackable completion queue associated with this
349  // server. If the return value is nullptr, this server doesn't support
350  // callback operations.
351  // TODO(vjpai): Consider a better default like using a global CQ
352  // Returns nullptr (rather than being pure) since this is a post-1.0 method
353  // and adding a new pure method to an interface would be a breaking change
354  // (even though this is private and non-API)
355  virtual CompletionQueue* CallbackCQ() { return nullptr; }
356 };
357 
358 } // namespace grpc
359 
360 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
virtual ~ServerInterface()
Definition: server_interface.h:52
Definition: server_interface.h:203
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:326
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:141
void Shutdown()
Shutdown the server without a deadline and forced cancellation.
Definition: server_interface.h:96
std::string string
Definition: config.h:35
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
void * server_tag() const
Definition: rpc_service_method.h:100
Definition: server_interface.h:220
ServerInterface *const server_
Definition: server_interface.h:164
ServerCompletionQueue *const notification_cq_
Definition: server_interface.h:168
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:58
Definition: server_interface.h:177
internal::InterceptorBatchMethodsImpl interceptor_methods_
Definition: server_interface.h:173
bool done_intercepting_
Definition: server_interface.h:174
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:305
Monotonic clock.
Definition: gpr_types.h:33
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:65
Definition: grpc_types.h:486
Definition: grpc_types.h:40
Definition: async_generic_service.h:65
If you are trying to use CompletionQueue::AsyncNext with a time class that isn&#39;t either gpr_timespec ...
Definition: time.h:40
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
internal::Call call_wrapper_
Definition: server_interface.h:172
PayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:222
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:35
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:166
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:33
const bool delete_on_finalize_
Definition: server_interface.h:170
Server side rpc method class.
Definition: rpc_service_method.h:81
CoreCodegenInterface * g_core_codegen_interface
Definition: call_op_set.h:50
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:316
Definition: interceptor_common.h:36
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:102
Definition: server_interface.h:50
grpc_call * call_
Definition: server_interface.h:171
CompletionQueue *const call_cq_
Definition: server_interface.h:167
const char * name_
Definition: server_interface.h:200
Definition: async_generic_service.h:35
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:95
bool FinalizeResult(void **tag, bool *status) override
FinalizeResult must be called before informing user code that the operation bound to the underlying c...
Definition: server_interface.h:247
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:87
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call_op_set_interface.h:34
~PayloadAsyncRequest()
Definition: server_interface.h:243
ServerContext *const context_
Definition: server_interface.h:165
Internal errors.
Definition: status.h:127
Analogous to struct timespec.
Definition: gpr_types.h:47
Definition: server_interface.h:290
void *const tag_
Definition: server_interface.h:169
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:374
This is an interface that Channel and Server implement to allow them to hook performing ops...
Definition: call_hook.h:30
NoPayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:205
A sequence of bytes.
Definition: byte_buffer.h:62
Definition: server_interface.h:149
virtual bool FinalizeResult(void **tag, bool *status) override
FinalizeResult must be called before informing user code that the operation bound to the underlying c...
Definition: server_interface.h:185
Straightforward wrapping of the C call object.
Definition: call.h:36
::google::protobuf::Message Message
Definition: config_protobuf.h:78