GRPC C++  1.80.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_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>
30 #include <grpcpp/server_context.h>
32 
33 #include "absl/log/absl_check.h"
34 
35 namespace grpc {
36 
37 class AsyncGenericService;
38 class Channel;
39 class CompletionQueue;
40 class GenericServerContext;
41 class ServerCompletionQueue;
42 class ServerCredentials;
43 class Service;
44 
48 namespace internal {
49 class ServerAsyncStreamingInterface;
50 } // namespace internal
51 
52 class CallbackGenericService;
53 
54 namespace experimental {
55 class ServerInterceptorFactoryInterface;
56 class ServerMetricRecorder;
57 } // namespace experimental
58 
60  public:
61  virtual ~ServerInterface() {}
62 
95  template <class T>
96  void Shutdown(const T& deadline) {
97  ShutdownInternal(TimePoint<T>(deadline).raw_time());
98  }
99 
106 
111  virtual void Wait() = 0;
112 
114  memory_allocator() = 0;
115 
116  protected:
117  friend class grpc::Service;
118 
121  virtual bool RegisterService(const std::string* host, Service* service) = 0;
122 
125  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
126 
130 
132  /*service*/) {}
133 
145  virtual int AddListeningPort(const std::string& addr,
146  ServerCredentials* creds) = 0;
147 
154  virtual void Start(grpc::ServerCompletionQueue** cqs, size_t num_cqs) = 0;
155 
156  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
157 
158  virtual int max_receive_message_size() const = 0;
159 
160  virtual grpc_server* server() = 0;
161 
163  public:
166  grpc::CompletionQueue* call_cq,
167  grpc::ServerCompletionQueue* notification_cq, void* tag,
168  bool delete_on_finalize);
169  ~BaseAsyncRequest() override;
170 
171  bool FinalizeResult(void** tag, bool* status) override;
172 
173  private:
174  void ContinueFinalizeResultAfterInterception();
175 
176  protected:
182  void* const tag_;
190  };
191 
194  public:
196  grpc::ServerContext* context,
198  grpc::CompletionQueue* call_cq,
199  grpc::ServerCompletionQueue* notification_cq,
200  void* tag, const char* name,
202 
203  bool FinalizeResult(void** tag, bool* status) override {
204  // If we are done intercepting, then there is nothing more for us to do
205  if (done_intercepting_) {
206  return BaseAsyncRequest::FinalizeResult(tag, status);
207  }
210  context_->set_server_rpc_info(name_, type_,
211  *server_->interceptor_creators()));
212  return BaseAsyncRequest::FinalizeResult(tag, status);
213  }
214 
215  protected:
216  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
217  grpc::ServerCompletionQueue* notification_cq);
218  const char* name_;
220  };
221 
223  public:
227  grpc::CompletionQueue* call_cq,
228  grpc::ServerCompletionQueue* notification_cq,
229  void* tag)
231  server, context, stream, call_cq, notification_cq, tag,
232  registered_method->name(), registered_method->method_type()) {
233  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
234  }
235 
236  // uses RegisteredAsyncRequest::FinalizeResult
237  };
238 
239  template <class Message>
241  public:
245  grpc::CompletionQueue* call_cq,
246  grpc::ServerCompletionQueue* notification_cq, void* tag,
247  Message* request)
249  server, context, stream, call_cq, notification_cq, tag,
250  registered_method->name(), registered_method->method_type()),
251  registered_method_(registered_method),
252  request_(request) {
253  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
254  notification_cq);
255  }
256 
257  ~PayloadAsyncRequest() override {
258  payload_.Release(); // We do not own the payload_
259  }
260 
261  bool FinalizeResult(void** tag, bool* status) override {
262  // If we are done intercepting, then there is nothing more for us to do
263  if (done_intercepting_) {
264  return RegisteredAsyncRequest::FinalizeResult(tag, status);
265  }
266  if (*status) {
267  if (!payload_.Valid() ||
268  !grpc::Deserialize(payload_.bbuf_ptr(), request_).ok()) {
269  // If deserialization fails, we cancel the call and instantiate
270  // a new instance of ourselves to request another call. We then
271  // return false, which prevents the call from being returned to
272  // the application.
274  "Unable to parse request", nullptr);
276  new PayloadAsyncRequest(registered_method_, server_, context_,
278  request_);
279  delete this;
280  return false;
281  }
282  }
283  // Set interception point for recv message
286  interceptor_methods_.SetRecvMessage(request_, nullptr);
287  return RegisteredAsyncRequest::FinalizeResult(tag, status);
288  }
289 
290  private:
291  internal::RpcServiceMethod* const registered_method_;
292  Message* const request_;
293  ByteBuffer payload_;
294  };
295 
297  public:
300  grpc::CompletionQueue* call_cq,
301  grpc::ServerCompletionQueue* notification_cq, void* tag,
302  bool delete_on_finalize, bool issue_request = true);
303 
304  bool FinalizeResult(void** tag, bool* status) override;
305 
306  protected:
307  void IssueRequest();
308 
309  private:
310  grpc_call_details call_details_;
311  };
312 
313  template <class Message>
315  grpc::ServerContext* context,
317  grpc::CompletionQueue* call_cq,
318  grpc::ServerCompletionQueue* notification_cq, void* tag,
319  Message* message) {
320  ABSL_CHECK(method);
321  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
322  notification_cq, tag, message);
323  }
324 
326  grpc::ServerContext* context,
328  grpc::CompletionQueue* call_cq,
329  grpc::ServerCompletionQueue* notification_cq,
330  void* tag) {
331  ABSL_CHECK(method);
332  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
333  notification_cq, tag);
334  }
335 
338  grpc::CompletionQueue* call_cq,
339  grpc::ServerCompletionQueue* notification_cq,
340  void* tag) {
341  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
342  tag, true);
343  }
344 
345  private:
346  // EXPERIMENTAL
347  // Getter method for the vector of interceptor factory objects.
348  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
349  // and adding a new pure method to an interface would be a breaking change
350  // (even though this is private and non-API)
351  virtual std::vector<
352  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
353  interceptor_creators() {
354  return nullptr;
355  }
356 
357  // Whether per-call load reporting is enabled.
358  virtual bool call_metric_recording_enabled() const = 0;
359 
360  // Interface to read or update server-wide metrics. Returns null when not set.
361  virtual experimental::ServerMetricRecorder* server_metric_recorder()
362  const = 0;
363 
364  // A method to get the callbackable completion queue associated with this
365  // server. If the return value is nullptr, this server doesn't support
366  // callback operations.
367  // TODO(vjpai): Consider a better default like using a global CQ
368  // Returns nullptr (rather than being pure) since this is a post-1.0 method
369  // and adding a new pure method to an interface would be a breaking change
370  // (even though this is private and non-API)
371  virtual grpc::CompletionQueue* CallbackCQ() { return nullptr; }
372 };
373 
374 } // namespace grpc
375 
376 #endif // GRPCPP_SERVER_INTERFACE_H
grpc::ServerInterface::BaseAsyncRequest
Definition: server_interface.h:162
grpc::ServerInterface::RegisteredAsyncRequest
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:193
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:38
grpc::ServerInterface::BaseAsyncRequest::done_intercepting_
bool done_intercepting_
Definition: server_interface.h:187
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:181
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::Shutdown
void Shutdown()
Shutdown the server without a deadline and forced cancellation.
Definition: server_interface.h:105
grpc::ServerInterface::BaseAsyncRequest::call_cq_
grpc::CompletionQueue *const call_cq_
Definition: server_interface.h:180
grpc::experimental::InterceptionHookPoints::POST_RECV_MESSAGE
@ POST_RECV_MESSAGE
grpc_call_details
Definition: grpc_types.h:257
grpc::ServerInterface::RegisteredAsyncRequest::name_
const char * name_
Definition: server_interface.h:218
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:257
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:34
grpc::ServerInterface::PayloadAsyncRequest
Definition: server_interface.h:240
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:325
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:131
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:224
grpc::ServerInterface::BaseAsyncRequest::tag_
void *const tag_
Definition: server_interface.h:182
grpc_types.h
grpc::ServerInterface::BaseAsyncRequest::context_
grpc::ServerContext *const context_
Definition: server_interface.h:178
grpc::ServerInterface::BaseAsyncRequest::call_wrapper_
internal::Call call_wrapper_
Definition: server_interface.h:185
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:242
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::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:183
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_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:222
grpc::ServerInterface::Shutdown
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:96
grpc::ServerInterface::BaseAsyncRequest::call_
grpc_call * call_
Definition: server_interface.h:184
grpc::AsyncGenericService
Definition: async_generic_service.h:66
grpc::ServerInterface::BaseAsyncRequest::server_
ServerInterface *const server_
Definition: server_interface.h:177
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
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:219
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:314
grpc::ServerInterface::RequestAsyncGenericCall
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, grpc::CompletionQueue *call_cq, grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:336
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:203
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:188
grpc::ServerInterface::BaseAsyncRequest::server_metric_recorder_
experimental::ServerMetricRecorder * server_metric_recorder_
Definition: server_interface.h:189
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:296
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:179
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:59
grpc::ServerInterface::~ServerInterface
virtual ~ServerInterface()
Definition: server_interface.h:61
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:186
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:261
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