GRPC C++  1.78.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_SERVER_INTERFACE_H
20 #define GRPCPP_SERVER_INTERFACE_H
21 
22 #include <grpc/grpc.h>
23 #include <grpc/impl/grpc_types.h>
25 #include <grpc/support/time.h>
26 #include <grpcpp/impl/call.h>
27 #include <grpcpp/impl/call_hook.h>
31 #include <grpcpp/server_context.h>
33 
34 #include "absl/log/absl_check.h"
35 
36 namespace grpc {
37 
38 class AsyncGenericService;
39 class Channel;
40 class CompletionQueue;
41 class GenericServerContext;
42 class ServerCompletionQueue;
43 class ServerCredentials;
44 class Service;
45 
49 namespace internal {
50 class ServerAsyncStreamingInterface;
51 } // namespace internal
52 
53 class CallbackGenericService;
54 
55 namespace experimental {
56 class ServerInterceptorFactoryInterface;
57 class ServerMetricRecorder;
58 } // namespace experimental
59 
61  public:
62  ~ServerInterface() override {}
63 
96  template <class T>
97  void Shutdown(const T& deadline) {
98  ShutdownInternal(TimePoint<T>(deadline).raw_time());
99  }
100 
107 
112  virtual void Wait() = 0;
113 
115  memory_allocator() = 0;
116 
117  protected:
118  friend class grpc::Service;
119 
122  virtual bool RegisterService(const std::string* host, Service* service) = 0;
123 
126  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
127 
131 
133  /*service*/) {}
134 
146  virtual int AddListeningPort(const std::string& addr,
147  ServerCredentials* creds) = 0;
148 
155  virtual void Start(grpc::ServerCompletionQueue** cqs, size_t num_cqs) = 0;
156 
157  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
158 
159  virtual int max_receive_message_size() const = 0;
160 
161  virtual grpc_server* server() = 0;
162 
164  internal::Call* call) override = 0;
165 
167  public:
170  grpc::CompletionQueue* call_cq,
171  grpc::ServerCompletionQueue* notification_cq, void* tag,
172  bool delete_on_finalize);
173  ~BaseAsyncRequest() override;
174 
175  bool FinalizeResult(void** tag, bool* status) override;
176 
177  private:
178  void ContinueFinalizeResultAfterInterception();
179 
180  protected:
186  void* const tag_;
194  };
195 
198  public:
200  grpc::ServerContext* context,
202  grpc::CompletionQueue* call_cq,
203  grpc::ServerCompletionQueue* notification_cq,
204  void* tag, const char* name,
206 
207  bool FinalizeResult(void** tag, bool* status) override {
208  // If we are done intercepting, then there is nothing more for us to do
209  if (done_intercepting_) {
210  return BaseAsyncRequest::FinalizeResult(tag, status);
211  }
214  context_->set_server_rpc_info(name_, type_,
215  *server_->interceptor_creators()));
216  return BaseAsyncRequest::FinalizeResult(tag, status);
217  }
218 
219  protected:
220  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
221  grpc::ServerCompletionQueue* notification_cq);
222  const char* name_;
224  };
225 
227  public:
231  grpc::CompletionQueue* call_cq,
232  grpc::ServerCompletionQueue* notification_cq,
233  void* tag)
235  server, context, stream, call_cq, notification_cq, tag,
236  registered_method->name(), registered_method->method_type()) {
237  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
238  }
239 
240  // uses RegisteredAsyncRequest::FinalizeResult
241  };
242 
243  template <class Message>
245  public:
249  grpc::CompletionQueue* call_cq,
250  grpc::ServerCompletionQueue* notification_cq, void* tag,
251  Message* request)
253  server, context, stream, call_cq, notification_cq, tag,
254  registered_method->name(), registered_method->method_type()),
255  registered_method_(registered_method),
256  request_(request) {
257  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
258  notification_cq);
259  }
260 
261  ~PayloadAsyncRequest() override {
262  payload_.Release(); // We do not own the payload_
263  }
264 
265  bool FinalizeResult(void** tag, bool* status) override {
266  // If we are done intercepting, then there is nothing more for us to do
267  if (done_intercepting_) {
268  return RegisteredAsyncRequest::FinalizeResult(tag, status);
269  }
270  if (*status) {
271  if (!payload_.Valid() ||
272  !grpc::Deserialize(payload_.bbuf_ptr(), request_).ok()) {
273  // If deserialization fails, we cancel the call and instantiate
274  // a new instance of ourselves to request another call. We then
275  // return false, which prevents the call from being returned to
276  // the application.
278  "Unable to parse request", nullptr);
280  new PayloadAsyncRequest(registered_method_, server_, context_,
282  request_);
283  delete this;
284  return false;
285  }
286  }
287  // Set interception point for recv message
290  interceptor_methods_.SetRecvMessage(request_, nullptr);
291  return RegisteredAsyncRequest::FinalizeResult(tag, status);
292  }
293 
294  private:
295  internal::RpcServiceMethod* const registered_method_;
296  Message* const request_;
297  ByteBuffer payload_;
298  };
299 
301  public:
304  grpc::CompletionQueue* call_cq,
305  grpc::ServerCompletionQueue* notification_cq, void* tag,
306  bool delete_on_finalize, bool issue_request = true);
307 
308  bool FinalizeResult(void** tag, bool* status) override;
309 
310  protected:
311  void IssueRequest();
312 
313  private:
314  grpc_call_details call_details_;
315  };
316 
317  template <class Message>
319  grpc::ServerContext* context,
321  grpc::CompletionQueue* call_cq,
322  grpc::ServerCompletionQueue* notification_cq, void* tag,
323  Message* message) {
324  ABSL_CHECK(method);
325  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
326  notification_cq, tag, message);
327  }
328 
330  grpc::ServerContext* context,
332  grpc::CompletionQueue* call_cq,
333  grpc::ServerCompletionQueue* notification_cq,
334  void* tag) {
335  ABSL_CHECK(method);
336  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
337  notification_cq, tag);
338  }
339 
342  grpc::CompletionQueue* call_cq,
343  grpc::ServerCompletionQueue* notification_cq,
344  void* tag) {
345  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
346  tag, true);
347  }
348 
349  private:
350  // EXPERIMENTAL
351  // Getter method for the vector of interceptor factory objects.
352  // Returns a 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 std::vector<
356  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
357  interceptor_creators() {
358  return nullptr;
359  }
360 
361  // Whether per-call load reporting is enabled.
362  virtual bool call_metric_recording_enabled() const = 0;
363 
364  // Interface to read or update server-wide metrics. Returns null when not set.
365  virtual experimental::ServerMetricRecorder* server_metric_recorder()
366  const = 0;
367 
368  // A method to get the callbackable completion queue associated with this
369  // server. If the return value is nullptr, this server doesn't support
370  // callback operations.
371  // TODO(vjpai): Consider a better default like using a global CQ
372  // Returns nullptr (rather than being pure) since this is a post-1.0 method
373  // and adding a new pure method to an interface would be a breaking change
374  // (even though this is private and non-API)
375  virtual grpc::CompletionQueue* CallbackCQ() { return nullptr; }
376 };
377 
378 } // namespace grpc
379 
380 #endif // GRPCPP_SERVER_INTERFACE_H
grpc::ServerInterface::BaseAsyncRequest
Definition: server_interface.h:166
grpc::ServerInterface::RegisteredAsyncRequest
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:197
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:38
grpc::ServerInterface::BaseAsyncRequest::done_intercepting_
bool done_intercepting_
Definition: server_interface.h:191
grpc::experimental::ServerMetricRecorder
Records server wide metrics to be reported to the client.
Definition: server_metric_recorder.h:42
grpc::ServerContext
A ServerContext or CallbackServerContext allows the code implementing a service handler to:
Definition: server_context.h:586
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::ServerInterface::BaseAsyncRequest::notification_cq_
grpc::ServerCompletionQueue *const notification_cq_
Definition: server_interface.h:185
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Monotonic clock.
Definition: time.h:33
grpc::Deserialize
auto Deserialize(BufferPtr buffer, Message *msg)
Definition: serialization_traits.h:120
grpc::ServerInterface::~ServerInterface
~ServerInterface() override
Definition: server_interface.h:62
grpc::ServerInterface::Shutdown
void Shutdown()
Shutdown the server without a deadline and forced cancellation.
Definition: server_interface.h:106
grpc::ServerInterface::BaseAsyncRequest::call_cq_
grpc::CompletionQueue *const call_cq_
Definition: server_interface.h:184
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:257
grpc::ServerInterface::RegisteredAsyncRequest::name_
const char * name_
Definition: server_interface.h:222
grpc::ServerCredentials
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:69
grpc::ServerInterface::PayloadAsyncRequest::~PayloadAsyncRequest
~PayloadAsyncRequest() override
Definition: server_interface.h:261
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:36
grpc::ServerInterface::PayloadAsyncRequest
Definition: server_interface.h:244
grpc::ServerInterface::ShutdownInternal
virtual void ShutdownInternal(gpr_timespec deadline)=0
rpc_service_method.h
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:329
grpc::Service
Descriptor of an RPC service and its various RPC methods.
Definition: service_type.h:57
completion_queue_tag.h
grpc::ServerInterface::RegisterService
virtual bool RegisterService(const std::string *host, Service *service)=0
Register a service.
grpc::ServerInterface::RegisterCallbackGenericService
virtual void RegisterCallbackGenericService(CallbackGenericService *)
Register a callback generic service.
Definition: server_interface.h:132
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
The far future.
grpc::ServerInterface::BaseAsyncRequest::~BaseAsyncRequest
~BaseAsyncRequest() override
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:228
grpc::ServerInterface::BaseAsyncRequest::tag_
void *const tag_
Definition: server_interface.h:186
grpc_types.h
grpc::ServerInterface::BaseAsyncRequest::context_
grpc::ServerContext *const context_
Definition: server_interface.h:182
grpc::ServerInterface::BaseAsyncRequest::call_wrapper_
internal::Call call_wrapper_
Definition: server_interface.h:189
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_STATUS_INTERNAL
@ GRPC_STATUS_INTERNAL
Internal errors.
Definition: status.h:129
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_call_unref
GRPCAPI void grpc_call_unref(grpc_call *call)
Unref a call.
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:246
grpc.h
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:68
grpc_byte_buffer
Definition: grpc_types.h:41
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:36
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:59
grpc::ServerInterface::BaseAsyncRequest::delete_on_finalize_
const bool delete_on_finalize_
Definition: server_interface.h:187
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::RegisteredAsyncRequest::IssueRequest
void IssueRequest(void *registered_method, grpc_byte_buffer **payload, grpc::ServerCompletionQueue *notification_cq)
grpc::ServerInterface::PerformOpsOnCall
void PerformOpsOnCall(internal::CallOpSetInterface *ops, internal::Call *call) override=0
grpc_server
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:63
grpc::ServerInterface::NoPayloadAsyncRequest
Definition: server_interface.h:226
grpc::ServerInterface::Shutdown
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:97
grpc::ServerInterface::BaseAsyncRequest::call_
grpc_call * call_
Definition: server_interface.h:188
grpc::AsyncGenericService
Definition: async_generic_service.h:66
grpc::ServerInterface::BaseAsyncRequest::server_
ServerInterface *const server_
Definition: server_interface.h:181
grpc::internal::RpcServiceMethod::server_tag
void * server_tag() const
Definition: rpc_service_method.h:105
grpc::internal::RpcMethod::RpcType
RpcType
Definition: rpc_method.h:31
grpc::GenericServerContext
Definition: async_generic_service.h:37
call_hook.h
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::ServerInterface::RegisteredAsyncRequest::type_
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:223
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::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:318
grpc::ServerInterface::RequestAsyncGenericCall
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, grpc::CompletionQueue *call_cq, grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:340
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:207
byte_buffer.h
interceptor_common.h
grpc::ServerCompletionQueue
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:426
server_context.h
call.h
grpc::ServerInterface::GenericAsyncRequest::IssueRequest
void IssueRequest()
grpc::ServerInterface::BaseAsyncRequest::call_metric_recording_enabled_
bool call_metric_recording_enabled_
Definition: server_interface.h:192
grpc::ServerInterface::BaseAsyncRequest::server_metric_recorder_
experimental::ServerMetricRecorder * server_metric_recorder_
Definition: server_interface.h:193
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:104
grpc::ServerInterface::GenericAsyncRequest
Definition: server_interface.h:300
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:33
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:158
grpc::ServerInterface::BaseAsyncRequest::stream_
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:183
grpc::internal::RpcServiceMethod
Server side rpc method class.
Definition: rpc_service_method.h:86
grpc::ByteBuffer::Release
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:143
grpc::ServerInterface
Definition: server_interface.h:60
gpr_timespec
Analogous to struct timespec.
Definition: time.h:47
grpc::protobuf::Message
::google::protobuf::Message Message
Definition: config_protobuf.h:89
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:190
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::ServerInterface::Start
virtual void Start(grpc::ServerCompletionQueue **cqs, size_t num_cqs)=0
Start the server.
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:265
grpc::internal::InterceptorBatchMethodsImpl
Definition: interceptor_common.h:37
grpc_call_cancel_with_status
GRPCAPI grpc_call_error grpc_call_cancel_with_status(grpc_call *call, grpc_status_code status, const char *description, void *reserved)
Cancel an RPC.
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, bool issue_request=true)
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:79
time.h
grpc::ServerInterface::memory_allocator
virtual grpc_event_engine::experimental::MemoryAllocator * memory_allocator()=0
grpc::CallbackGenericService
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: callback_generic_service.h:51
port_platform.h