GRPC C++  1.30.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_impl {
35 
36 class Channel;
37 class CompletionQueue;
39 class ServerCredentials;
40 } // namespace grpc_impl
41 namespace grpc {
42 
43 class AsyncGenericService;
44 class GenericServerContext;
45 class Service;
46 
47 extern CoreCodegenInterface* g_core_codegen_interface;
48 
52 namespace internal {
53 class ServerAsyncStreamingInterface;
54 } // namespace internal
55 
56 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
57 namespace experimental {
58 #endif
59 class CallbackGenericService;
60 #ifndef GRPC_CALLBACK_API_NONEXPERIMENTAL
61 } // namespace experimental
62 #endif
63 
64 namespace experimental {
65 class ServerInterceptorFactoryInterface;
66 } // namespace experimental
67 
69  public:
70  virtual ~ServerInterface() {}
71 
104  template <class T>
105  void Shutdown(const T& deadline) {
106  ShutdownInternal(TimePoint<T>(deadline).raw_time());
107  }
108 
114  void Shutdown() {
117  }
118 
123  virtual void Wait() = 0;
124 
125  protected:
126  friend class ::grpc::Service;
127 
130  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
131 
134  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
135 
136 #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL
137 
141  virtual void RegisterCallbackGenericService(CallbackGenericService*
142  /*service*/) {}
143 #else
144  class experimental_registration_interface {
148  public:
152  experimental::CallbackGenericService* /*service*/) {}
153  };
154 
160  return nullptr;
161  }
162 #endif
163 
175  virtual int AddListeningPort(const grpc::string& addr,
176  grpc_impl::ServerCredentials* creds) = 0;
177 
184  virtual void Start(::grpc_impl::ServerCompletionQueue** cqs,
185  size_t num_cqs) = 0;
186 
187  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
188 
189  virtual int max_receive_message_size() const = 0;
190 
191  virtual grpc_server* server() = 0;
192 
194  internal::Call* call) = 0;
195 
197  public:
199  ::grpc_impl::ServerContext* context,
201  ::grpc_impl::CompletionQueue* call_cq,
202  ::grpc_impl::ServerCompletionQueue* notification_cq,
203  void* tag, bool delete_on_finalize);
204  virtual ~BaseAsyncRequest();
205 
206  bool FinalizeResult(void** tag, bool* status) override;
207 
208  private:
209  void ContinueFinalizeResultAfterInterception();
210 
211  protected:
217  void* const tag_;
223  };
224 
227  public:
229  ::grpc_impl::ServerContext* context,
231  ::grpc_impl::CompletionQueue* call_cq,
232  ::grpc_impl::ServerCompletionQueue* notification_cq,
233  void* tag, const char* name,
235 
236  virtual bool FinalizeResult(void** tag, bool* status) override {
237  /* If we are done intercepting, then there is nothing more for us to do */
238  if (done_intercepting_) {
239  return BaseAsyncRequest::FinalizeResult(tag, status);
240  }
243  context_->set_server_rpc_info(name_, type_,
244  *server_->interceptor_creators()));
245  return BaseAsyncRequest::FinalizeResult(tag, status);
246  }
247 
248  protected:
249  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
250  ::grpc_impl::ServerCompletionQueue* notification_cq);
251  const char* name_;
253  };
254 
256  public:
259  ::grpc_impl::ServerContext* context,
261  ::grpc_impl::CompletionQueue* call_cq,
262  ::grpc_impl::ServerCompletionQueue* notification_cq,
263  void* tag)
265  server, context, stream, call_cq, notification_cq, tag,
266  registered_method->name(), registered_method->method_type()) {
267  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
268  }
269 
270  // uses RegisteredAsyncRequest::FinalizeResult
271  };
272 
273  template <class Message>
275  public:
278  ::grpc_impl::ServerContext* context,
280  ::grpc_impl::CompletionQueue* call_cq,
281  ::grpc_impl::ServerCompletionQueue* notification_cq,
282  void* tag, Message* request)
284  server, context, stream, call_cq, notification_cq, tag,
285  registered_method->name(), registered_method->method_type()),
286  registered_method_(registered_method),
287  request_(request) {
288  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
289  notification_cq);
290  }
291 
293  payload_.Release(); // We do not own the payload_
294  }
295 
296  bool FinalizeResult(void** tag, bool* status) override {
297  /* If we are done intercepting, then there is nothing more for us to do */
298  if (done_intercepting_) {
299  return RegisteredAsyncRequest::FinalizeResult(tag, status);
300  }
301  if (*status) {
303  payload_.bbuf_ptr(), request_)
304  .ok()) {
305  // If deserialization fails, we cancel the call and instantiate
306  // a new instance of ourselves to request another call. We then
307  // return false, which prevents the call from being returned to
308  // the application.
310  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
312  new PayloadAsyncRequest(registered_method_, server_, context_,
314  request_);
315  delete this;
316  return false;
317  }
318  }
319  /* Set interception point for recv message */
322  interceptor_methods_.SetRecvMessage(request_, nullptr);
323  return RegisteredAsyncRequest::FinalizeResult(tag, status);
324  }
325 
326  private:
327  internal::RpcServiceMethod* const registered_method_;
328  Message* const request_;
329  ByteBuffer payload_;
330  };
331 
333  public:
336  ::grpc_impl::CompletionQueue* call_cq,
337  ::grpc_impl::ServerCompletionQueue* notification_cq,
338  void* tag, bool delete_on_finalize);
339 
340  bool FinalizeResult(void** tag, bool* status) override;
341 
342  private:
343  grpc_call_details call_details_;
344  };
345 
346  template <class Message>
348  ::grpc_impl::ServerContext* context,
350  ::grpc_impl::CompletionQueue* call_cq,
351  ::grpc_impl::ServerCompletionQueue* notification_cq,
352  void* tag, Message* message) {
353  GPR_CODEGEN_ASSERT(method);
354  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
355  notification_cq, tag, message);
356  }
357 
359  ::grpc_impl::ServerContext* context,
361  ::grpc_impl::CompletionQueue* call_cq,
362  ::grpc_impl::ServerCompletionQueue* notification_cq,
363  void* tag) {
364  GPR_CODEGEN_ASSERT(method);
365  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
366  notification_cq, tag);
367  }
368 
370  GenericServerContext* context,
372  ::grpc_impl::CompletionQueue* call_cq,
373  ::grpc_impl::ServerCompletionQueue* notification_cq, void* tag) {
374  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
375  tag, true);
376  }
377 
378  private:
379  // EXPERIMENTAL
380  // Getter method for the vector of interceptor factory objects.
381  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
382  // and adding a new pure method to an interface would be a breaking change
383  // (even though this is private and non-API)
384  virtual std::vector<
385  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
386  interceptor_creators() {
387  return nullptr;
388  }
389 
390  // EXPERIMENTAL
391  // A method to get the callbackable completion queue associated with this
392  // server. If the return value is nullptr, this server doesn't support
393  // callback operations.
394  // TODO(vjpai): Consider a better default like using a global CQ
395  // Returns nullptr (rather than being pure) since this is a post-1.0 method
396  // and adding a new pure method to an interface would be a breaking change
397  // (even though this is private and non-API)
398  virtual ::grpc_impl::CompletionQueue* CallbackCQ() { return nullptr; }
399 };
400 
401 } // namespace grpc
402 
403 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
grpc::ServerInterface::BaseAsyncRequest
Definition: server_interface.h:196
grpc::ServerInterface::RegisteredAsyncRequest
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:226
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:41
grpc::ServerInterface::BaseAsyncRequest::done_intercepting_
bool done_intercepting_
Definition: server_interface.h:222
GRPC_STATUS_INTERNAL
Internal errors.
Definition: status.h:127
grpc::ServerInterface::RegisteredAsyncRequest::RegisteredAsyncRequest
RegisteredAsyncRequest(ServerInterface *server, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag, const char *name, internal::RpcMethod::RpcType type)
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:159
interceptor_common.h
grpc::ServerCredentials
::grpc_impl::ServerCredentials ServerCredentials
Definition: server_credentials.h:30
grpc::ServerInterface::Shutdown
void Shutdown()
Shutdown the server without a deadline and forced cancellation.
Definition: server_interface.h:114
grpc::experimental::InterceptionHookPoints::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::CompletionQueue
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
grpc::ServerInterface::RegisteredAsyncRequest::name_
const char * name_
Definition: server_interface.h:251
grpc::ServerInterface::PayloadAsyncRequest::PayloadAsyncRequest
PayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:276
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:38
grpc::ServerInterface::BaseAsyncRequest::notification_cq_
::grpc_impl::ServerCompletionQueue *const notification_cq_
Definition: server_interface.h:216
grpc::ServerInterface::PayloadAsyncRequest
Definition: server_interface.h:274
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::BaseAsyncRequest::call_cq_
::grpc_impl::CompletionQueue *const call_cq_
Definition: server_interface.h:215
grpc::ServerInterface::BaseAsyncRequest::BaseAsyncRequest
BaseAsyncRequest(ServerInterface *server, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag, bool delete_on_finalize)
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_impl::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue_impl.h:424
grpc::ServerInterface::BaseAsyncRequest::tag_
void *const tag_
Definition: server_interface.h:217
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:510
grpc::ServerInterface::BaseAsyncRequest::call_wrapper_
internal::Call call_wrapper_
Definition: server_interface.h:220
grpc::ServerInterface::experimental_registration_interface::~experimental_registration_interface
virtual ~experimental_registration_interface()
Definition: server_interface.h:149
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::RegisterService
virtual bool RegisterService(const grpc::string *host, Service *service)=0
Register a service.
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::Start
virtual void Start(::grpc_impl::ServerCompletionQueue **cqs, size_t num_cqs)=0
Start the server.
grpc::ServerInterface::AddListeningPort
virtual int AddListeningPort(const grpc::string &addr, grpc_impl::ServerCredentials *creds)=0
Tries to bind server to the given addr.
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::experimental_registration_interface
NOTE: class experimental_registration_interface is not part of the public API of this class TODO(vjpa...
Definition: server_interface.h:147
completion_queue_tag.h
grpc::ServerInterface::BaseAsyncRequest::delete_on_finalize_
const bool delete_on_finalize_
Definition: server_interface.h:218
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
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:255
grpc::ServerInterface::Shutdown
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:105
grpc_impl::ServerCredentials
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials_impl.h:40
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:219
grpc::experimental::CallbackGenericService
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:125
grpc::AsyncGenericService
Definition: async_generic_service.h:77
grpc::ServerInterface::BaseAsyncRequest::server_
ServerInterface *const server_
Definition: server_interface.h:212
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::RequestAsyncGenericCall
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:369
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:169
grpc::CoreCodegenInterface::grpc_call_unref
virtual void grpc_call_unref(grpc_call *call)=0
grpc::ServerInterface::PayloadAsyncRequest::~PayloadAsyncRequest
~PayloadAsyncRequest()
Definition: server_interface.h:292
grpc::ServerInterface::RegisteredAsyncRequest::type_
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:252
server_context_impl.h
grpc::ServerInterface::GenericAsyncRequest::GenericAsyncRequest
GenericAsyncRequest(ServerInterface *server, GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::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:151
grpc::ServerInterface::BaseAsyncRequest::~BaseAsyncRequest
virtual ~BaseAsyncRequest()
grpc::ServerInterface::NoPayloadAsyncRequest::NoPayloadAsyncRequest
NoPayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:257
grpc_impl::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue_impl.h:103
port_platform.h
grpc::ServerInterface::RequestAsyncCall
void RequestAsyncCall(internal::RpcServiceMethod *method, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:358
call.h
grpc::string
std::string string
Definition: config.h:35
call_hook.h
grpc_impl
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
grpc::ServerInterface::GenericAsyncRequest
Definition: server_interface.h:332
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:214
grpc::ServerInterface::RequestAsyncCall
void RequestAsyncCall(internal::RpcServiceMethod *method, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:347
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.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:68
grpc::ServerInterface::~ServerInterface
virtual ~ServerInterface()
Definition: server_interface.h:70
grpc::ServerCompletionQueue
::grpc_impl::ServerCompletionQueue ServerCompletionQueue
Definition: completion_queue.h:27
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::RegisteredAsyncRequest::IssueRequest
void IssueRequest(void *registered_method, grpc_byte_buffer **payload, ::grpc_impl::ServerCompletionQueue *notification_cq)
grpc::ServerInterface::BaseAsyncRequest::interceptor_methods_
internal::InterceptorBatchMethodsImpl interceptor_methods_
Definition: server_interface.h:221
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:236
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:296
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::Channel
::grpc_impl::Channel Channel
Definition: channel.h:26
grpc::ServerInterface::BaseAsyncRequest::context_
::grpc_impl::ServerContext *const context_
Definition: server_interface.h:213