GRPC C++  1.32.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  virtual ~ServerInterface() {}
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) = 0;
191 
193  public:
195  ::grpc_impl::ServerContext* context,
197  ::grpc::CompletionQueue* call_cq,
198  ::grpc::ServerCompletionQueue* notification_cq, void* tag,
199  bool delete_on_finalize);
200  virtual ~BaseAsyncRequest();
201 
202  bool FinalizeResult(void** tag, bool* status) override;
203 
204  private:
205  void ContinueFinalizeResultAfterInterception();
206 
207  protected:
213  void* const tag_;
219  };
220 
223  public:
225  ::grpc_impl::ServerContext* context,
227  ::grpc::CompletionQueue* call_cq,
228  ::grpc::ServerCompletionQueue* notification_cq,
229  void* tag, const char* name,
231 
232  virtual bool FinalizeResult(void** tag, bool* status) override {
233  /* If we are done intercepting, then there is nothing more for us to do */
234  if (done_intercepting_) {
235  return BaseAsyncRequest::FinalizeResult(tag, status);
236  }
239  context_->set_server_rpc_info(name_, type_,
240  *server_->interceptor_creators()));
241  return BaseAsyncRequest::FinalizeResult(tag, status);
242  }
243 
244  protected:
245  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
246  ::grpc::ServerCompletionQueue* notification_cq);
247  const char* name_;
249  };
250 
252  public:
255  ::grpc_impl::ServerContext* context,
257  ::grpc::CompletionQueue* call_cq,
258  ::grpc::ServerCompletionQueue* notification_cq,
259  void* tag)
261  server, context, stream, call_cq, notification_cq, tag,
262  registered_method->name(), registered_method->method_type()) {
263  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
264  }
265 
266  // uses RegisteredAsyncRequest::FinalizeResult
267  };
268 
269  template <class Message>
271  public:
274  ::grpc_impl::ServerContext* context,
276  ::grpc::CompletionQueue* call_cq,
277  ::grpc::ServerCompletionQueue* notification_cq,
278  void* tag, Message* request)
280  server, context, stream, call_cq, notification_cq, tag,
281  registered_method->name(), registered_method->method_type()),
282  registered_method_(registered_method),
283  request_(request) {
284  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
285  notification_cq);
286  }
287 
289  payload_.Release(); // We do not own the payload_
290  }
291 
292  bool FinalizeResult(void** tag, bool* status) override {
293  /* If we are done intercepting, then there is nothing more for us to do */
294  if (done_intercepting_) {
295  return RegisteredAsyncRequest::FinalizeResult(tag, status);
296  }
297  if (*status) {
299  payload_.bbuf_ptr(), request_)
300  .ok()) {
301  // If deserialization fails, we cancel the call and instantiate
302  // a new instance of ourselves to request another call. We then
303  // return false, which prevents the call from being returned to
304  // the application.
306  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
308  new PayloadAsyncRequest(registered_method_, server_, context_,
310  request_);
311  delete this;
312  return false;
313  }
314  }
315  /* Set interception point for recv message */
318  interceptor_methods_.SetRecvMessage(request_, nullptr);
319  return RegisteredAsyncRequest::FinalizeResult(tag, status);
320  }
321 
322  private:
323  internal::RpcServiceMethod* const registered_method_;
324  Message* const request_;
325  ByteBuffer payload_;
326  };
327 
329  public:
332  ::grpc::CompletionQueue* call_cq,
333  ::grpc::ServerCompletionQueue* notification_cq,
334  void* tag, bool delete_on_finalize);
335 
336  bool FinalizeResult(void** tag, bool* status) override;
337 
338  private:
339  grpc_call_details call_details_;
340  };
341 
342  template <class Message>
344  ::grpc_impl::ServerContext* context,
346  ::grpc::CompletionQueue* call_cq,
347  ::grpc::ServerCompletionQueue* notification_cq,
348  void* tag, Message* message) {
349  GPR_CODEGEN_ASSERT(method);
350  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
351  notification_cq, tag, message);
352  }
353 
355  ::grpc_impl::ServerContext* context,
357  ::grpc::CompletionQueue* call_cq,
358  ::grpc::ServerCompletionQueue* notification_cq,
359  void* tag) {
360  GPR_CODEGEN_ASSERT(method);
361  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
362  notification_cq, tag);
363  }
364 
367  ::grpc::CompletionQueue* call_cq,
368  ::grpc::ServerCompletionQueue* notification_cq,
369  void* tag) {
370  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
371  tag, true);
372  }
373 
374  private:
375  // EXPERIMENTAL
376  // Getter method for the vector of interceptor factory objects.
377  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
378  // and adding a new pure method to an interface would be a breaking change
379  // (even though this is private and non-API)
380  virtual std::vector<
381  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
382  interceptor_creators() {
383  return nullptr;
384  }
385 
386  // EXPERIMENTAL
387  // A method to get the callbackable completion queue associated with this
388  // server. If the return value is nullptr, this server doesn't support
389  // callback operations.
390  // TODO(vjpai): Consider a better default like using a global CQ
391  // Returns nullptr (rather than being pure) since this is a post-1.0 method
392  // and adding a new pure method to an interface would be a breaking change
393  // (even though this is private and non-API)
394  virtual ::grpc::CompletionQueue* CallbackCQ() { return nullptr; }
395 };
396 
397 } // namespace grpc
398 
399 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
grpc::ServerInterface::BaseAsyncRequest
Definition: server_interface.h:192
grpc::ServerInterface::RegisteredAsyncRequest
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:222
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:41
grpc::ServerInterface::BaseAsyncRequest::done_intercepting_
bool done_intercepting_
Definition: server_interface.h:218
GRPC_STATUS_INTERNAL
@ GRPC_STATUS_INTERNAL
Internal errors.
Definition: status.h:127
rpc_service_method.h
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
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::RequestAsyncCall
void RequestAsyncCall(internal::RpcServiceMethod *method, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:343
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:552
grpc::ServerInterface::BaseAsyncRequest::call_cq_
::grpc::CompletionQueue *const call_cq_
Definition: server_interface.h:211
grpc::ServerInterface::RegisteredAsyncRequest::name_
const char * name_
Definition: server_interface.h:247
grpc::ServerCredentials
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:65
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:270
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:60
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::tag_
void *const tag_
Definition: server_interface.h:213
grpc::CoreCodegenInterface::gpr_inf_future
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
grpc_types.h
grpc_impl::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context_impl.h:520
grpc::ServerInterface::BaseAsyncRequest::call_wrapper_
internal::Call call_wrapper_
Definition: server_interface.h:216
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::RegisteredAsyncRequest::RegisteredAsyncRequest
RegisteredAsyncRequest(ServerInterface *server, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag, const char *name, internal::RpcMethod::RpcType type)
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:67
grpc::ServerInterface::PayloadAsyncRequest::PayloadAsyncRequest
PayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:272
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:214
grpc::ServerInterface::RegisterAsyncGenericService
virtual void RegisterAsyncGenericService(AsyncGenericService *service)=0
Register a generic service.
grpc::ServerInterface::server
virtual grpc_server * server()=0
grpc::ServerInterface::max_receive_message_size
virtual int max_receive_message_size() const =0
grpc::ServerInterface::BaseAsyncRequest::BaseAsyncRequest
BaseAsyncRequest(ServerInterface *server, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag, bool delete_on_finalize)
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:251
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:215
grpc::experimental::CallbackGenericService
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:124
grpc::AsyncGenericService
Definition: async_generic_service.h:77
grpc::ServerInterface::BaseAsyncRequest::server_
ServerInterface *const server_
Definition: server_interface.h:208
grpc::internal::RpcServiceMethod::server_tag
void * server_tag() const
Definition: rpc_service_method.h:106
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc::GenericServerContext
Definition: async_generic_service.h:41
grpc::ServerInterface::PerformOpsOnCall
virtual void PerformOpsOnCall(internal::CallOpSetInterface *ops, internal::Call *call)=0
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::PayloadAsyncRequest::~PayloadAsyncRequest
~PayloadAsyncRequest()
Definition: server_interface.h:288
grpc::ServerInterface::RegisteredAsyncRequest::type_
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:248
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)
server_context_impl.h
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:365
grpc::ServerInterface::BaseAsyncRequest::~BaseAsyncRequest
virtual ~BaseAsyncRequest()
grpc::ServerInterface::AddListeningPort
virtual int AddListeningPort(const std::string &addr, ServerCredentials *creds)=0
Tries to bind server to the given addr.
grpc::ServerInterface::RegisteredAsyncRequest::IssueRequest
void IssueRequest(void *registered_method, grpc_byte_buffer **payload, ::grpc::ServerCompletionQueue *notification_cq)
port_platform.h
grpc::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:421
call.h
call_hook.h
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:99
grpc::ServerInterface::GenericAsyncRequest
Definition: server_interface.h:328
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:163
grpc::ServerInterface::BaseAsyncRequest::stream_
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:210
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue.h:93
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:87
grpc::ByteBuffer::Release
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:146
grpc::ServerInterface
Definition: server_interface.h:65
grpc::ServerInterface::~ServerInterface
virtual ~ServerInterface()
Definition: server_interface.h:67
gpr_timespec
Analogous to struct timespec.
Definition: gpr_types.h:47
grpc::protobuf::Message
::google::protobuf::Message Message
Definition: config_protobuf.h:75
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:217
grpc::ServerInterface::RegisteredAsyncRequest::FinalizeResult
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:232
grpc::ServerInterface::NoPayloadAsyncRequest::NoPayloadAsyncRequest
NoPayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:253
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:292
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:212
grpc::ServerInterface::RequestAsyncCall
void RequestAsyncCall(internal::RpcServiceMethod *method, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc::CompletionQueue *call_cq, ::grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:354
grpc::ServerInterface::BaseAsyncRequest::context_
::grpc_impl::ServerContext *const context_
Definition: server_interface.h:209