GRPC C++  1.66.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 "absl/log/absl_check.h"
23 
24 #include <grpc/grpc.h>
25 #include <grpc/impl/grpc_types.h>
26 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29 #include <grpcpp/impl/call.h>
30 #include <grpcpp/impl/call_hook.h>
34 #include <grpcpp/server_context.h>
36 
37 namespace grpc {
38 
39 class AsyncGenericService;
40 class Channel;
41 class CompletionQueue;
42 class GenericServerContext;
43 class ServerCompletionQueue;
44 class ServerCredentials;
45 class Service;
46 
50 namespace internal {
51 class ServerAsyncStreamingInterface;
52 } // namespace internal
53 
54 class CallbackGenericService;
55 
56 namespace experimental {
57 class ServerInterceptorFactoryInterface;
58 class ServerMetricRecorder;
59 } // namespace experimental
60 
62  public:
63  ~ServerInterface() override {}
64 
97  template <class T>
98  void Shutdown(const T& deadline) {
99  ShutdownInternal(TimePoint<T>(deadline).raw_time());
100  }
101 
108 
113  virtual void Wait() = 0;
114 
115  protected:
116  friend class grpc::Service;
117 
120  virtual bool RegisterService(const std::string* host, Service* service) = 0;
121 
124  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
125 
129 
131  /*service*/) {}
132 
144  virtual int AddListeningPort(const std::string& addr,
145  ServerCredentials* creds) = 0;
146 
153  virtual void Start(grpc::ServerCompletionQueue** cqs, size_t num_cqs) = 0;
154 
155  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
156 
157  virtual int max_receive_message_size() const = 0;
158 
159  virtual grpc_server* server() = 0;
160 
162  internal::Call* call) override = 0;
163 
165  public:
168  grpc::CompletionQueue* call_cq,
169  grpc::ServerCompletionQueue* notification_cq, void* tag,
170  bool delete_on_finalize);
171  ~BaseAsyncRequest() override;
172 
173  bool FinalizeResult(void** tag, bool* status) override;
174 
175  private:
176  void ContinueFinalizeResultAfterInterception();
177 
178  protected:
184  void* const tag_;
192  };
193 
196  public:
198  grpc::ServerContext* context,
200  grpc::CompletionQueue* call_cq,
201  grpc::ServerCompletionQueue* notification_cq,
202  void* tag, const char* name,
204 
205  bool FinalizeResult(void** tag, bool* status) override {
206  // If we are done intercepting, then there is nothing more for us to do
207  if (done_intercepting_) {
208  return BaseAsyncRequest::FinalizeResult(tag, status);
209  }
212  context_->set_server_rpc_info(name_, type_,
213  *server_->interceptor_creators()));
214  return BaseAsyncRequest::FinalizeResult(tag, status);
215  }
216 
217  protected:
218  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
219  grpc::ServerCompletionQueue* notification_cq);
220  const char* name_;
222  };
223 
225  public:
229  grpc::CompletionQueue* call_cq,
230  grpc::ServerCompletionQueue* notification_cq,
231  void* tag)
233  server, context, stream, call_cq, notification_cq, tag,
234  registered_method->name(), registered_method->method_type()) {
235  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
236  }
237 
238  // uses RegisteredAsyncRequest::FinalizeResult
239  };
240 
241  template <class Message>
243  public:
247  grpc::CompletionQueue* call_cq,
248  grpc::ServerCompletionQueue* notification_cq, void* tag,
249  Message* request)
251  server, context, stream, call_cq, notification_cq, tag,
252  registered_method->name(), registered_method->method_type()),
253  registered_method_(registered_method),
254  request_(request) {
255  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
256  notification_cq);
257  }
258 
259  ~PayloadAsyncRequest() override {
260  payload_.Release(); // We do not own the payload_
261  }
262 
263  bool FinalizeResult(void** tag, bool* status) override {
264  // If we are done intercepting, then there is nothing more for us to do
265  if (done_intercepting_) {
266  return RegisteredAsyncRequest::FinalizeResult(tag, status);
267  }
268  if (*status) {
270  payload_.bbuf_ptr(), request_)
271  .ok()) {
272  // If deserialization fails, we cancel the call and instantiate
273  // a new instance of ourselves to request another call. We then
274  // return false, which prevents the call from being returned to
275  // the application.
277  "Unable to parse request", nullptr);
279  new PayloadAsyncRequest(registered_method_, server_, context_,
281  request_);
282  delete this;
283  return false;
284  }
285  }
286  // Set interception point for recv message
289  interceptor_methods_.SetRecvMessage(request_, nullptr);
290  return RegisteredAsyncRequest::FinalizeResult(tag, status);
291  }
292 
293  private:
294  internal::RpcServiceMethod* const registered_method_;
295  Message* const request_;
296  ByteBuffer payload_;
297  };
298 
300  public:
303  grpc::CompletionQueue* call_cq,
304  grpc::ServerCompletionQueue* notification_cq, void* tag,
305  bool delete_on_finalize, bool issue_request = true);
306 
307  bool FinalizeResult(void** tag, bool* status) override;
308 
309  protected:
310  void IssueRequest();
311 
312  private:
313  grpc_call_details call_details_;
314  };
315 
316  template <class Message>
318  grpc::ServerContext* context,
320  grpc::CompletionQueue* call_cq,
321  grpc::ServerCompletionQueue* notification_cq, void* tag,
322  Message* message) {
323  ABSL_CHECK(method);
324  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
325  notification_cq, tag, message);
326  }
327 
329  grpc::ServerContext* context,
331  grpc::CompletionQueue* call_cq,
332  grpc::ServerCompletionQueue* notification_cq,
333  void* tag) {
334  ABSL_CHECK(method);
335  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
336  notification_cq, tag);
337  }
338 
341  grpc::CompletionQueue* call_cq,
342  grpc::ServerCompletionQueue* notification_cq,
343  void* tag) {
344  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
345  tag, true);
346  }
347 
348  private:
349  // EXPERIMENTAL
350  // Getter method for the vector of interceptor factory objects.
351  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
352  // and adding a new pure method to an interface would be a breaking change
353  // (even though this is private and non-API)
354  virtual std::vector<
355  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
356  interceptor_creators() {
357  return nullptr;
358  }
359 
360  // Whether per-call load reporting is enabled.
361  virtual bool call_metric_recording_enabled() const = 0;
362 
363  // Interface to read or update server-wide metrics. Returns null when not set.
364  virtual experimental::ServerMetricRecorder* server_metric_recorder()
365  const = 0;
366 
367  // A method to get the callbackable completion queue associated with this
368  // server. If the return value is nullptr, this server doesn't support
369  // callback operations.
370  // TODO(vjpai): Consider a better default like using a global CQ
371  // Returns nullptr (rather than being pure) since this is a post-1.0 method
372  // and adding a new pure method to an interface would be a breaking change
373  // (even though this is private and non-API)
374  virtual grpc::CompletionQueue* CallbackCQ() { return nullptr; }
375 };
376 
377 } // namespace grpc
378 
379 #endif // GRPCPP_SERVER_INTERFACE_H
grpc::ServerInterface::BaseAsyncRequest
Definition: server_interface.h:164
grpc::ServerInterface::RegisteredAsyncRequest
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:195
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:39
grpc::ServerInterface::BaseAsyncRequest::done_intercepting_
bool done_intercepting_
Definition: server_interface.h:189
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:578
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:183
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Monotonic clock.
Definition: time.h:34
grpc::ServerInterface::~ServerInterface
~ServerInterface() override
Definition: server_interface.h:63
grpc::ServerInterface::Shutdown
void Shutdown()
Shutdown the server without a deadline and forced cancellation.
Definition: server_interface.h:107
grpc::ServerInterface::BaseAsyncRequest::call_cq_
grpc::CompletionQueue *const call_cq_
Definition: server_interface.h:182
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:258
grpc::ServerInterface::RegisteredAsyncRequest::name_
const char * name_
Definition: server_interface.h:220
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:259
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:36
grpc::ServerInterface::PayloadAsyncRequest
Definition: server_interface.h:242
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:328
grpc::Service
Descriptor of an RPC service and its various RPC methods.
Definition: service_type.h:58
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:130
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:226
grpc::ServerInterface::BaseAsyncRequest::tag_
void *const tag_
Definition: server_interface.h:184
grpc_types.h
grpc::ServerInterface::BaseAsyncRequest::context_
grpc::ServerContext *const context_
Definition: server_interface.h:180
grpc::ServerInterface::BaseAsyncRequest::call_wrapper_
internal::Call call_wrapper_
Definition: server_interface.h:187
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...
log.h
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:244
grpc.h
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:69
grpc_byte_buffer
Definition: grpc_types.h:42
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:60
grpc::ServerInterface::BaseAsyncRequest::delete_on_finalize_
const bool delete_on_finalize_
Definition: server_interface.h:185
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:64
grpc::ServerInterface::NoPayloadAsyncRequest
Definition: server_interface.h:224
grpc::ServerInterface::Shutdown
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:98
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:186
grpc::AsyncGenericService
Definition: async_generic_service.h:66
grpc::ServerInterface::BaseAsyncRequest::server_
ServerInterface *const server_
Definition: server_interface.h:179
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:170
grpc::ServerInterface::RegisteredAsyncRequest::type_
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:221
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:317
grpc::ServerInterface::RequestAsyncGenericCall
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, grpc::CompletionQueue *call_cq, grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:339
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:205
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:427
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:190
grpc::ServerInterface::BaseAsyncRequest::server_metric_recorder_
experimental::ServerMetricRecorder * server_metric_recorder_
Definition: server_interface.h:191
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:105
grpc::ServerInterface::GenericAsyncRequest
Definition: server_interface.h:299
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:159
grpc::ServerInterface::BaseAsyncRequest::stream_
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:181
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:144
grpc::ServerInterface
Definition: server_interface.h:61
gpr_timespec
Analogous to struct timespec.
Definition: time.h:48
grpc::protobuf::Message
::google::protobuf::Message Message
Definition: config_protobuf.h:88
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:188
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:263
grpc::internal::InterceptorBatchMethodsImpl
Definition: interceptor_common.h:38
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:80
time.h
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