GRPC C++  1.35.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 
23 
33 
34 namespace grpc {
35 
36 class AsyncGenericService;
37 class Channel;
38 class CompletionQueue;
39 class GenericServerContext;
40 class ServerCompletionQueue;
41 class ServerCredentials;
42 class Service;
43 
44 extern CoreCodegenInterface* g_core_codegen_interface;
45 
49 namespace internal {
50 class ServerAsyncStreamingInterface;
51 } // namespace internal
52 
53 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
54 namespace experimental {
55 #endif
56 class CallbackGenericService;
57 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
58 } // namespace experimental
59 #endif
60 
61 namespace experimental {
62 class ServerInterceptorFactoryInterface;
63 } // namespace experimental
64 
66  public:
67  ~ServerInterface() override {}
68 
101  template <class T>
102  void Shutdown(const T& deadline) {
103  ShutdownInternal(TimePoint<T>(deadline).raw_time());
104  }
105 
111  void Shutdown() {
114  }
115 
120  virtual void Wait() = 0;
121 
122  protected:
123  friend class ::grpc::Service;
124 
127  virtual bool RegisterService(const std::string* host, Service* service) = 0;
128 
131  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
132 
133 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
134 
138  virtual void RegisterCallbackGenericService(CallbackGenericService*
139  /*service*/) {}
140 #else
141  class experimental_registration_interface {
145  public:
149  experimental::CallbackGenericService* /*service*/) {}
150  };
151 
157  return nullptr;
158  }
159 #endif
160 
172  virtual int AddListeningPort(const std::string& addr,
173  ServerCredentials* creds) = 0;
174 
181  virtual void Start(::grpc::ServerCompletionQueue** cqs, size_t num_cqs) = 0;
182 
183  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
184 
185  virtual int max_receive_message_size() const = 0;
186 
187  virtual grpc_server* server() = 0;
188 
190  internal::Call* call) override = 0;
191 
193  public:
196  ::grpc::CompletionQueue* call_cq,
197  ::grpc::ServerCompletionQueue* notification_cq, void* tag,
198  bool delete_on_finalize);
199  ~BaseAsyncRequest() override;
200 
201  bool FinalizeResult(void** tag, bool* status) override;
202 
203  private:
204  void ContinueFinalizeResultAfterInterception();
205 
206  protected:
212  void* const tag_;
218  };
219 
222  public:
224  ::grpc::ServerContext* context,
226  ::grpc::CompletionQueue* call_cq,
227  ::grpc::ServerCompletionQueue* notification_cq,
228  void* tag, const char* name,
230 
231  bool FinalizeResult(void** tag, bool* status) override {
232  /* If we are done intercepting, then there is nothing more for us to do */
233  if (done_intercepting_) {
234  return BaseAsyncRequest::FinalizeResult(tag, status);
235  }
238  context_->set_server_rpc_info(name_, type_,
239  *server_->interceptor_creators()));
240  return BaseAsyncRequest::FinalizeResult(tag, status);
241  }
242 
243  protected:
244  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
245  ::grpc::ServerCompletionQueue* notification_cq);
246  const char* name_;
248  };
249 
251  public:
254  ::grpc::ServerContext* context,
256  ::grpc::CompletionQueue* call_cq,
257  ::grpc::ServerCompletionQueue* notification_cq,
258  void* tag)
260  server, context, stream, call_cq, notification_cq, tag,
261  registered_method->name(), registered_method->method_type()) {
262  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
263  }
264 
265  // uses RegisteredAsyncRequest::FinalizeResult
266  };
267 
268  template <class Message>
270  public:
274  ::grpc::CompletionQueue* call_cq,
275  ::grpc::ServerCompletionQueue* notification_cq,
276  void* tag, Message* request)
278  server, context, stream, call_cq, notification_cq, tag,
279  registered_method->name(), registered_method->method_type()),
280  registered_method_(registered_method),
281  request_(request) {
282  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
283  notification_cq);
284  }
285 
286  ~PayloadAsyncRequest() override {
287  payload_.Release(); // We do not own the payload_
288  }
289 
290  bool FinalizeResult(void** tag, bool* status) override {
291  /* If we are done intercepting, then there is nothing more for us to do */
292  if (done_intercepting_) {
293  return RegisteredAsyncRequest::FinalizeResult(tag, status);
294  }
295  if (*status) {
297  payload_.bbuf_ptr(), request_)
298  .ok()) {
299  // If deserialization fails, we cancel the call and instantiate
300  // a new instance of ourselves to request another call. We then
301  // return false, which prevents the call from being returned to
302  // the application.
304  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
306  new PayloadAsyncRequest(registered_method_, server_, context_,
308  request_);
309  delete this;
310  return false;
311  }
312  }
313  /* Set interception point for recv message */
316  interceptor_methods_.SetRecvMessage(request_, nullptr);
317  return RegisteredAsyncRequest::FinalizeResult(tag, status);
318  }
319 
320  private:
321  internal::RpcServiceMethod* const registered_method_;
322  Message* const request_;
323  ByteBuffer payload_;
324  };
325 
327  public:
330  ::grpc::CompletionQueue* call_cq,
331  ::grpc::ServerCompletionQueue* notification_cq,
332  void* tag, bool delete_on_finalize);
333 
334  bool FinalizeResult(void** tag, bool* status) override;
335 
336  private:
337  grpc_call_details call_details_;
338  };
339 
340  template <class Message>
342  ::grpc::ServerContext* context,
344  ::grpc::CompletionQueue* call_cq,
345  ::grpc::ServerCompletionQueue* notification_cq,
346  void* tag, Message* message) {
347  GPR_CODEGEN_ASSERT(method);
348  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
349  notification_cq, tag, message);
350  }
351 
353  ::grpc::ServerContext* context,
355  ::grpc::CompletionQueue* call_cq,
356  ::grpc::ServerCompletionQueue* notification_cq,
357  void* tag) {
358  GPR_CODEGEN_ASSERT(method);
359  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
360  notification_cq, tag);
361  }
362 
365  ::grpc::CompletionQueue* call_cq,
366  ::grpc::ServerCompletionQueue* notification_cq,
367  void* tag) {
368  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
369  tag, true);
370  }
371 
372  private:
373  // EXPERIMENTAL
374  // Getter method for the vector of interceptor factory objects.
375  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
376  // and adding a new pure method to an interface would be a breaking change
377  // (even though this is private and non-API)
378  virtual std::vector<
379  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
380  interceptor_creators() {
381  return nullptr;
382  }
383 
384  // EXPERIMENTAL
385  // A method to get the callbackable completion queue associated with this
386  // server. If the return value is nullptr, this server doesn't support
387  // callback operations.
388  // TODO(vjpai): Consider a better default like using a global CQ
389  // Returns nullptr (rather than being pure) since this is a post-1.0 method
390  // and adding a new pure method to an interface would be a breaking change
391  // (even though this is private and non-API)
392  virtual ::grpc::CompletionQueue* CallbackCQ() { return nullptr; }
393 };
394 
395 } // namespace grpc
396 
397 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
grpc::ServerInterface::BaseAsyncRequest
Definition: server_interface.h:192
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:341
grpc::ServerInterface::RegisteredAsyncRequest
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:221
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:37
grpc::ServerInterface::BaseAsyncRequest::done_intercepting_
bool done_intercepting_
Definition: server_interface.h:217
GRPC_STATUS_INTERNAL
@ GRPC_STATUS_INTERNAL
Internal errors.
Definition: status.h:127
grpc::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:538
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
grpc::ServerInterface::experimental_registration
virtual experimental_registration_interface * experimental_registration()
NOTE: The function experimental_registration() is not stable public API.
Definition: server_interface.h:156
interceptor_common.h
grpc::ServerInterface::~ServerInterface
~ServerInterface() override
Definition: server_interface.h:67
grpc::ServerInterface::Shutdown
void Shutdown()
Shutdown the server without a deadline and forced cancellation.
Definition: server_interface.h:111
grpc::experimental::InterceptionHookPoints::POST_RECV_MESSAGE
@ POST_RECV_MESSAGE
grpc::internal::CallHook
This is an interface that Channel and Server implement to allow them to hook performing ops.
Definition: call_hook.h:30
grpc_call_details
Definition: grpc_types.h:554
grpc::ServerInterface::BaseAsyncRequest::call_cq_
::grpc::CompletionQueue *const call_cq_
Definition: server_interface.h:210
grpc::ServerInterface::RegisteredAsyncRequest::name_
const char * name_
Definition: server_interface.h:246
grpc::ServerCredentials
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:70
grpc::ServerInterface::PayloadAsyncRequest::~PayloadAsyncRequest
~PayloadAsyncRequest() override
Definition: server_interface.h:286
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:35
grpc::ServerInterface::Start
virtual void Start(::grpc::ServerCompletionQueue **cqs, size_t num_cqs)=0
Start the server.
grpc::ServerInterface::PayloadAsyncRequest
Definition: server_interface.h:269
grpc::ServerInterface::ShutdownInternal
virtual void ShutdownInternal(gpr_timespec deadline)=0
grpc::Service
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:56
core_codegen_interface.h
grpc::ServerInterface::RegisterService
virtual bool RegisterService(const std::string *host, Service *service)=0
Register a service.
grpc::CoreCodegenInterface::grpc_call_cancel_with_status
virtual grpc_call_error grpc_call_cancel_with_status(grpc_call *call, grpc_status_code status, const char *description, void *reserved)=0
byte_buffer.h
grpc::ServerInterface::BaseAsyncRequest::~BaseAsyncRequest
~BaseAsyncRequest() override
grpc::ServerInterface::BaseAsyncRequest::tag_
void *const tag_
Definition: server_interface.h:212
grpc::CoreCodegenInterface::gpr_inf_future
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
grpc_types.h
grpc::ServerInterface::BaseAsyncRequest::call_wrapper_
internal::Call call_wrapper_
Definition: server_interface.h:215
grpc::ServerInterface::experimental_registration_interface::~experimental_registration_interface
virtual ~experimental_registration_interface()
Definition: server_interface.h:146
grpc::ServerInterface::GenericAsyncRequest::FinalizeResult
bool FinalizeResult(void **tag, bool *status) override
FinalizeResult must be called before informing user code that the operation bound to the underlying c...
grpc::ServerInterface::BaseAsyncRequest::FinalizeResult
bool FinalizeResult(void **tag, bool *status) override
FinalizeResult must be called before informing user code that the operation bound to the underlying c...
grpc::ServerInterface::PayloadAsyncRequest::PayloadAsyncRequest
PayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ::grpc::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:271
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
grpc_byte_buffer
Definition: grpc_types.h:40
grpc::internal::CallOpSetInterface
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
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:61
grpc::ServerInterface::experimental_registration_interface
NOTE: class experimental_registration_interface is not part of the public API of this class TODO(vjpa...
Definition: server_interface.h:144
completion_queue_tag.h
grpc::ServerInterface::BaseAsyncRequest::delete_on_finalize_
const bool delete_on_finalize_
Definition: server_interface.h:213
grpc::ServerInterface::RegisterAsyncGenericService
virtual void RegisterAsyncGenericService(AsyncGenericService *service)=0
Register a generic service.
grpc::ServerInterface::server
virtual grpc_server * server()=0
grpc::ServerInterface::RequestAsyncCall
void RequestAsyncCall(internal::RpcServiceMethod *method, ::grpc::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:352
grpc::ServerInterface::max_receive_message_size
virtual int max_receive_message_size() const =0
grpc::ServerInterface::PerformOpsOnCall
void PerformOpsOnCall(internal::CallOpSetInterface *ops, internal::Call *call) override=0
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Monotonic clock.
Definition: gpr_types.h:33
grpc_server
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:65
grpc::ServerInterface::NoPayloadAsyncRequest
Definition: server_interface.h:250
grpc::ServerInterface::Shutdown
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:102
grpc::SerializationTraits
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
grpc::ServerInterface::BaseAsyncRequest::call_
grpc_call * call_
Definition: server_interface.h:214
grpc::experimental::CallbackGenericService
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:106
grpc::AsyncGenericService
Definition: async_generic_service.h:68
grpc::ServerInterface::RegisteredAsyncRequest::RegisteredAsyncRequest
RegisteredAsyncRequest(ServerInterface *server, ::grpc::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag, const char *name, internal::RpcMethod::RpcType type)
grpc::ServerInterface::BaseAsyncRequest::server_
ServerInterface *const server_
Definition: server_interface.h:207
grpc::internal::RpcServiceMethod::server_tag
void * server_tag() const
Definition: rpc_service_method.h:103
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc::GenericServerContext
Definition: async_generic_service.h:39
grpc::internal::CompletionQueueTag
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
grpc::internal::InterceptorBatchMethodsImpl::SetRecvMessage
void SetRecvMessage(void *message, bool *hijacked_recv_message_failed)
Definition: interceptor_common.h:168
grpc::CoreCodegenInterface::grpc_call_unref
virtual void grpc_call_unref(grpc_call *call)=0
grpc::ServerInterface::RegisteredAsyncRequest::type_
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:247
grpc::ServerInterface::GenericAsyncRequest::GenericAsyncRequest
GenericAsyncRequest(ServerInterface *server, GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag, bool delete_on_finalize)
grpc::ServerInterface::experimental_registration_interface::RegisterCallbackGenericService
virtual void RegisterCallbackGenericService(experimental::CallbackGenericService *)
May not be abstract since this is a post-1.0 API addition.
Definition: server_interface.h:148
grpc::ServerInterface::RequestAsyncGenericCall
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:363
grpc::ServerInterface::AddListeningPort
virtual int AddListeningPort(const std::string &addr, ServerCredentials *creds)=0
Tries to bind server to the given addr.
grpc::ServerInterface::RegisteredAsyncRequest::FinalizeResult
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:231
grpc::ServerInterface::RegisteredAsyncRequest::IssueRequest
void IssueRequest(void *registered_method, grpc_byte_buffer **payload, ::grpc::ServerCompletionQueue *notification_cq)
port_platform.h
grpc::ServerInterface::BaseAsyncRequest::BaseAsyncRequest
BaseAsyncRequest(ServerInterface *server, ::grpc::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag, bool delete_on_finalize)
grpc::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:426
call.h
server_context.h
grpc::ServerInterface::BaseAsyncRequest::context_
::grpc::ServerContext *const context_
Definition: server_interface.h:208
call_hook.h
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:102
grpc::ServerInterface::GenericAsyncRequest
Definition: server_interface.h:326
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:157
grpc::ServerInterface::BaseAsyncRequest::stream_
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:209
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue.h:96
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
grpc::internal::RpcServiceMethod
Server side rpc method class.
Definition: rpc_service_method.h:84
grpc::ServerInterface::NoPayloadAsyncRequest::NoPayloadAsyncRequest
NoPayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ::grpc::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:252
grpc::ByteBuffer::Release
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:140
grpc::ServerInterface
Definition: server_interface.h:65
gpr_timespec
Analogous to struct timespec.
Definition: gpr_types.h:47
grpc::protobuf::Message
::google::protobuf::Message Message
Definition: config_protobuf.h:76
grpc::ServerInterface::Wait
virtual void Wait()=0
Block waiting for all work to complete.
grpc::ServerInterface::BaseAsyncRequest::interceptor_methods_
internal::InterceptorBatchMethodsImpl interceptor_methods_
Definition: server_interface.h:216
grpc::ServerInterface::PayloadAsyncRequest::FinalizeResult
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:290
grpc::internal::InterceptorBatchMethodsImpl
Definition: interceptor_common.h:36
grpc::TimePoint
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:40
grpc::internal::InterceptorBatchMethodsImpl::AddInterceptionHookPoint
void AddInterceptionHookPoint(experimental::InterceptionHookPoints type)
Definition: interceptor_common.h:78
grpc::ServerInterface::BaseAsyncRequest::notification_cq_
::grpc::ServerCompletionQueue *const notification_cq_
Definition: server_interface.h:211