GRPC C++  1.62.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 
23 
24 #include <grpc/grpc.h>
25 #include <grpc/impl/grpc_types.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/time.h>
28 #include <grpcpp/impl/call.h>
29 #include <grpcpp/impl/call_hook.h>
33 #include <grpcpp/server_context.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 
114  protected:
115  friend class grpc::Service;
116 
119  virtual bool RegisterService(const std::string* host, Service* service) = 0;
120 
123  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
124 
128 
130  /*service*/) {}
131 
143  virtual int AddListeningPort(const std::string& addr,
144  ServerCredentials* creds) = 0;
145 
152  virtual void Start(grpc::ServerCompletionQueue** cqs, size_t num_cqs) = 0;
153 
154  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
155 
156  virtual int max_receive_message_size() const = 0;
157 
158  virtual grpc_server* server() = 0;
159 
161  internal::Call* call) override = 0;
162 
164  public:
167  grpc::CompletionQueue* call_cq,
168  grpc::ServerCompletionQueue* notification_cq, void* tag,
169  bool delete_on_finalize);
170  ~BaseAsyncRequest() override;
171 
172  bool FinalizeResult(void** tag, bool* status) override;
173 
174  private:
175  void ContinueFinalizeResultAfterInterception();
176 
177  protected:
183  void* const tag_;
191  };
192 
195  public:
197  grpc::ServerContext* context,
199  grpc::CompletionQueue* call_cq,
200  grpc::ServerCompletionQueue* notification_cq,
201  void* tag, const char* name,
203 
204  bool FinalizeResult(void** tag, bool* status) override {
205  // If we are done intercepting, then there is nothing more for us to do
206  if (done_intercepting_) {
207  return BaseAsyncRequest::FinalizeResult(tag, status);
208  }
211  context_->set_server_rpc_info(name_, type_,
212  *server_->interceptor_creators()));
213  return BaseAsyncRequest::FinalizeResult(tag, status);
214  }
215 
216  protected:
217  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
218  grpc::ServerCompletionQueue* notification_cq);
219  const char* name_;
221  };
222 
224  public:
228  grpc::CompletionQueue* call_cq,
229  grpc::ServerCompletionQueue* notification_cq,
230  void* tag)
232  server, context, stream, call_cq, notification_cq, tag,
233  registered_method->name(), registered_method->method_type()) {
234  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
235  }
236 
237  // uses RegisteredAsyncRequest::FinalizeResult
238  };
239 
240  template <class Message>
242  public:
246  grpc::CompletionQueue* call_cq,
247  grpc::ServerCompletionQueue* notification_cq, void* tag,
248  Message* request)
250  server, context, stream, call_cq, notification_cq, tag,
251  registered_method->name(), registered_method->method_type()),
252  registered_method_(registered_method),
253  request_(request) {
254  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
255  notification_cq);
256  }
257 
258  ~PayloadAsyncRequest() override {
259  payload_.Release(); // We do not own the payload_
260  }
261 
262  bool FinalizeResult(void** tag, bool* status) override {
263  // If we are done intercepting, then there is nothing more for us to do
264  if (done_intercepting_) {
265  return RegisteredAsyncRequest::FinalizeResult(tag, status);
266  }
267  if (*status) {
269  payload_.bbuf_ptr(), request_)
270  .ok()) {
271  // If deserialization fails, we cancel the call and instantiate
272  // a new instance of ourselves to request another call. We then
273  // return false, which prevents the call from being returned to
274  // the application.
276  "Unable to parse request", nullptr);
278  new PayloadAsyncRequest(registered_method_, server_, context_,
280  request_);
281  delete this;
282  return false;
283  }
284  }
285  // Set interception point for recv message
288  interceptor_methods_.SetRecvMessage(request_, nullptr);
289  return RegisteredAsyncRequest::FinalizeResult(tag, status);
290  }
291 
292  private:
293  internal::RpcServiceMethod* const registered_method_;
294  Message* const request_;
295  ByteBuffer payload_;
296  };
297 
299  public:
302  grpc::CompletionQueue* call_cq,
303  grpc::ServerCompletionQueue* notification_cq, void* tag,
304  bool delete_on_finalize, bool issue_request = true);
305 
306  bool FinalizeResult(void** tag, bool* status) override;
307 
308  protected:
309  void IssueRequest();
310 
311  private:
312  grpc_call_details call_details_;
313  };
314 
315  template <class Message>
317  grpc::ServerContext* context,
319  grpc::CompletionQueue* call_cq,
320  grpc::ServerCompletionQueue* notification_cq, void* tag,
321  Message* message) {
322  GPR_ASSERT(method);
323  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
324  notification_cq, tag, message);
325  }
326 
328  grpc::ServerContext* context,
330  grpc::CompletionQueue* call_cq,
331  grpc::ServerCompletionQueue* notification_cq,
332  void* tag) {
333  GPR_ASSERT(method);
334  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
335  notification_cq, tag);
336  }
337 
340  grpc::CompletionQueue* call_cq,
341  grpc::ServerCompletionQueue* notification_cq,
342  void* tag) {
343  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
344  tag, true);
345  }
346 
347  private:
348  // EXPERIMENTAL
349  // Getter method for the vector of interceptor factory objects.
350  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
351  // and adding a new pure method to an interface would be a breaking change
352  // (even though this is private and non-API)
353  virtual std::vector<
354  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
355  interceptor_creators() {
356  return nullptr;
357  }
358 
359  // Whether per-call load reporting is enabled.
360  virtual bool call_metric_recording_enabled() const = 0;
361 
362  // Interface to read or update server-wide metrics. Returns null when not set.
363  virtual experimental::ServerMetricRecorder* server_metric_recorder()
364  const = 0;
365 
366  // A method to get the callbackable completion queue associated with this
367  // server. If the return value is nullptr, this server doesn't support
368  // callback operations.
369  // TODO(vjpai): Consider a better default like using a global CQ
370  // Returns nullptr (rather than being pure) since this is a post-1.0 method
371  // and adding a new pure method to an interface would be a breaking change
372  // (even though this is private and non-API)
373  virtual grpc::CompletionQueue* CallbackCQ() { return nullptr; }
374 };
375 
376 } // namespace grpc
377 
378 #endif // GRPCPP_SERVER_INTERFACE_H
grpc::ServerInterface::BaseAsyncRequest
Definition: server_interface.h:163
grpc::ServerInterface::RegisteredAsyncRequest
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:194
grpc::internal::ServerAsyncStreamingInterface
Definition: service_type.h:37
grpc::ServerInterface::BaseAsyncRequest::done_intercepting_
bool done_intercepting_
Definition: server_interface.h:188
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:572
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:182
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Monotonic clock.
Definition: time.h:34
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:181
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:259
grpc::ServerInterface::RegisteredAsyncRequest::name_
const char * name_
Definition: server_interface.h:219
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:258
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:36
GPR_ASSERT
#define GPR_ASSERT(x)
abort() the process if x is zero, having written a line to the log.
Definition: log.h:95
grpc::ServerInterface::PayloadAsyncRequest
Definition: server_interface.h:241
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:327
grpc::Service
Descriptor of an RPC service and its various RPC methods.
Definition: service_type.h:56
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:129
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:225
grpc::ServerInterface::BaseAsyncRequest::tag_
void *const tag_
Definition: server_interface.h:183
grpc_types.h
grpc::ServerInterface::BaseAsyncRequest::context_
grpc::ServerContext *const context_
Definition: server_interface.h:179
grpc::ServerInterface::BaseAsyncRequest::call_wrapper_
internal::Call call_wrapper_
Definition: server_interface.h:186
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:243
grpc.h
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
grpc_byte_buffer
Definition: grpc_types.h:43
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:184
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:65
grpc::ServerInterface::NoPayloadAsyncRequest
Definition: server_interface.h:223
grpc::ServerInterface::Shutdown
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:97
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:185
grpc::AsyncGenericService
Definition: async_generic_service.h:68
grpc::ServerInterface::BaseAsyncRequest::server_
ServerInterface *const server_
Definition: server_interface.h:178
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
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:168
grpc::ServerInterface::RegisteredAsyncRequest::type_
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:220
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:316
grpc::ServerInterface::RequestAsyncGenericCall
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, grpc::CompletionQueue *call_cq, grpc::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:338
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:204
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:425
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:189
grpc::ServerInterface::BaseAsyncRequest::server_metric_recorder_
experimental::ServerMetricRecorder * server_metric_recorder_
Definition: server_interface.h:190
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:103
grpc::ServerInterface::GenericAsyncRequest
Definition: server_interface.h:298
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:180
grpc::internal::RpcServiceMethod
Server side rpc method class.
Definition: rpc_service_method.h:84
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:60
gpr_timespec
Analogous to struct timespec.
Definition: time.h:48
grpc::protobuf::Message
::google::protobuf::Message Message
Definition: config_protobuf.h:82
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:187
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:262
grpc::internal::InterceptorBatchMethodsImpl
Definition: interceptor_common.h:36
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:78
time.h
grpc::CallbackGenericService
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:102
port_platform.h