GRPC C++  1.36.1
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*/) {}
151  std::unique_ptr<ContextAllocator> context_allocator) {}
152  };
153 
159  return nullptr;
160  }
161 #endif
162 
174  virtual int AddListeningPort(const std::string& addr,
175  ServerCredentials* creds) = 0;
176 
183  virtual void Start(::grpc::ServerCompletionQueue** cqs, size_t num_cqs) = 0;
184 
185  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
186 
187  virtual int max_receive_message_size() const = 0;
188 
189  virtual grpc_server* server() = 0;
190 
192  internal::Call* call) override = 0;
193 
195  public:
198  ::grpc::CompletionQueue* call_cq,
199  ::grpc::ServerCompletionQueue* notification_cq, void* tag,
200  bool delete_on_finalize);
201  ~BaseAsyncRequest() override;
202 
203  bool FinalizeResult(void** tag, bool* status) override;
204 
205  private:
206  void ContinueFinalizeResultAfterInterception();
207 
208  protected:
214  void* const tag_;
220  };
221 
224  public:
226  ::grpc::ServerContext* context,
228  ::grpc::CompletionQueue* call_cq,
229  ::grpc::ServerCompletionQueue* notification_cq,
230  void* tag, const char* name,
232 
233  bool FinalizeResult(void** tag, bool* status) override {
234  /* If we are done intercepting, then there is nothing more for us to do */
235  if (done_intercepting_) {
236  return BaseAsyncRequest::FinalizeResult(tag, status);
237  }
240  context_->set_server_rpc_info(name_, type_,
241  *server_->interceptor_creators()));
242  return BaseAsyncRequest::FinalizeResult(tag, status);
243  }
244 
245  protected:
246  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
247  ::grpc::ServerCompletionQueue* notification_cq);
248  const char* name_;
250  };
251 
253  public:
256  ::grpc::ServerContext* context,
258  ::grpc::CompletionQueue* call_cq,
259  ::grpc::ServerCompletionQueue* notification_cq,
260  void* tag)
262  server, context, stream, call_cq, notification_cq, tag,
263  registered_method->name(), registered_method->method_type()) {
264  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
265  }
266 
267  // uses RegisteredAsyncRequest::FinalizeResult
268  };
269 
270  template <class Message>
272  public:
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 
288  ~PayloadAsyncRequest() override {
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::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::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:194
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:343
grpc::ServerInterface::RegisteredAsyncRequest
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:223
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:37
grpc::ServerInterface::BaseAsyncRequest::done_intercepting_
bool done_intercepting_
Definition: server_interface.h:219
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:546
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:158
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:212
grpc::ServerInterface::RegisteredAsyncRequest::name_
const char * name_
Definition: server_interface.h:248
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:288
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:271
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:214
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:217
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::experimental_registration_interface::RegisterContextAllocator
virtual void RegisterContextAllocator(std::unique_ptr< ContextAllocator > context_allocator)
Definition: server_interface.h:150
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:273
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:215
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:354
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:252
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:216
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:209
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:249
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:365
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:233
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:210
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:328
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:211
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:254
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:218
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:213