GRPC C++  1.26.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_IMPL_CODEGEN_SERVER_INTERFACE_H
20 #define GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
21 
31 
32 namespace grpc_impl {
33 
34 class Channel;
35 class CompletionQueue;
37 class ServerCredentials;
38 } // namespace grpc_impl
39 namespace grpc {
40 
41 class AsyncGenericService;
42 class GenericServerContext;
43 class Service;
44 
45 extern CoreCodegenInterface* g_core_codegen_interface;
46 
50 namespace internal {
51 class ServerAsyncStreamingInterface;
52 } // namespace internal
53 
54 namespace experimental {
55 class CallbackGenericService;
56 class ServerInterceptorFactoryInterface;
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 
105  void Shutdown() {
106  ShutdownInternal(
107  g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_MONOTONIC));
108  }
109 
114  virtual void Wait() = 0;
115 
116  protected:
117  friend class ::grpc::Service;
118 
121  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
122 
125  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
126 
131  public:
135  experimental::CallbackGenericService* /*service*/) {}
136  };
137 
143  return nullptr;
144  }
145 
157  virtual int AddListeningPort(const grpc::string& addr,
158  grpc_impl::ServerCredentials* creds) = 0;
159 
166  virtual void Start(::grpc_impl::ServerCompletionQueue** cqs,
167  size_t num_cqs) = 0;
168 
169  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
170 
171  virtual int max_receive_message_size() const = 0;
172 
173  virtual grpc_server* server() = 0;
174 
175  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
176  internal::Call* call) = 0;
177 
179  public:
181  ::grpc_impl::ServerContext* context,
183  ::grpc_impl::CompletionQueue* call_cq,
184  ::grpc_impl::ServerCompletionQueue* notification_cq,
185  void* tag, bool delete_on_finalize);
186  virtual ~BaseAsyncRequest();
187 
188  bool FinalizeResult(void** tag, bool* status) override;
189 
190  private:
191  void ContinueFinalizeResultAfterInterception();
192 
193  protected:
199  void* const tag_;
205  };
206 
209  public:
211  ::grpc_impl::ServerContext* context,
213  ::grpc_impl::CompletionQueue* call_cq,
214  ::grpc_impl::ServerCompletionQueue* notification_cq,
215  void* tag, const char* name,
217 
218  virtual bool FinalizeResult(void** tag, bool* status) override {
219  /* If we are done intercepting, then there is nothing more for us to do */
220  if (done_intercepting_) {
221  return BaseAsyncRequest::FinalizeResult(tag, status);
222  }
223  call_wrapper_ = ::grpc::internal::Call(
224  call_, server_, call_cq_, server_->max_receive_message_size(),
225  context_->set_server_rpc_info(name_, type_,
226  *server_->interceptor_creators()));
227  return BaseAsyncRequest::FinalizeResult(tag, status);
228  }
229 
230  protected:
231  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
232  ::grpc_impl::ServerCompletionQueue* notification_cq);
233  const char* name_;
235  };
236 
238  public:
240  ServerInterface* server,
241  ::grpc_impl::ServerContext* context,
243  ::grpc_impl::CompletionQueue* call_cq,
244  ::grpc_impl::ServerCompletionQueue* notification_cq,
245  void* tag)
247  server, context, stream, call_cq, notification_cq, tag,
248  registered_method->name(), registered_method->method_type()) {
249  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
250  }
251 
252  // uses RegisteredAsyncRequest::FinalizeResult
253  };
254 
255  template <class Message>
257  public:
259  ServerInterface* server,
260  ::grpc_impl::ServerContext* context,
262  ::grpc_impl::CompletionQueue* call_cq,
263  ::grpc_impl::ServerCompletionQueue* notification_cq,
264  void* tag, Message* request)
266  server, context, stream, call_cq, notification_cq, tag,
267  registered_method->name(), registered_method->method_type()),
268  registered_method_(registered_method),
269  request_(request) {
270  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
271  notification_cq);
272  }
273 
275  payload_.Release(); // We do not own the payload_
276  }
277 
278  bool FinalizeResult(void** tag, bool* status) override {
279  /* If we are done intercepting, then there is nothing more for us to do */
280  if (done_intercepting_) {
281  return RegisteredAsyncRequest::FinalizeResult(tag, status);
282  }
283  if (*status) {
284  if (!payload_.Valid() || !SerializationTraits<Message>::Deserialize(
285  payload_.bbuf_ptr(), request_)
286  .ok()) {
287  // If deserialization fails, we cancel the call and instantiate
288  // a new instance of ourselves to request another call. We then
289  // return false, which prevents the call from being returned to
290  // the application.
291  g_core_codegen_interface->grpc_call_cancel_with_status(
292  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
293  g_core_codegen_interface->grpc_call_unref(call_);
294  new PayloadAsyncRequest(registered_method_, server_, context_,
295  stream_, call_cq_, notification_cq_, tag_,
296  request_);
297  delete this;
298  return false;
299  }
300  }
301  /* Set interception point for recv message */
302  interceptor_methods_.AddInterceptionHookPoint(
303  experimental::InterceptionHookPoints::POST_RECV_MESSAGE);
304  interceptor_methods_.SetRecvMessage(request_, nullptr);
305  return RegisteredAsyncRequest::FinalizeResult(tag, status);
306  }
307 
308  private:
309  internal::RpcServiceMethod* const registered_method_;
310  Message* const request_;
311  ByteBuffer payload_;
312  };
313 
315  public:
318  ::grpc_impl::CompletionQueue* call_cq,
319  ::grpc_impl::ServerCompletionQueue* notification_cq,
320  void* tag, bool delete_on_finalize);
321 
322  bool FinalizeResult(void** tag, bool* status) override;
323 
324  private:
325  grpc_call_details call_details_;
326  };
327 
328  template <class Message>
330  ::grpc_impl::ServerContext* context,
332  ::grpc_impl::CompletionQueue* call_cq,
333  ::grpc_impl::ServerCompletionQueue* notification_cq,
334  void* tag, Message* message) {
335  GPR_CODEGEN_ASSERT(method);
336  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
337  notification_cq, tag, message);
338  }
339 
341  ::grpc_impl::ServerContext* context,
343  ::grpc_impl::CompletionQueue* call_cq,
344  ::grpc_impl::ServerCompletionQueue* notification_cq,
345  void* tag) {
346  GPR_CODEGEN_ASSERT(method);
347  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
348  notification_cq, tag);
349  }
350 
352  GenericServerContext* context,
354  ::grpc_impl::CompletionQueue* call_cq,
355  ::grpc_impl::ServerCompletionQueue* notification_cq, void* tag) {
356  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
357  tag, true);
358  }
359 
360  private:
361  // EXPERIMENTAL
362  // Getter method for the vector of interceptor factory objects.
363  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
364  // and adding a new pure method to an interface would be a breaking change
365  // (even though this is private and non-API)
366  virtual std::vector<
367  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
368  interceptor_creators() {
369  return nullptr;
370  }
371 
372  // EXPERIMENTAL
373  // A method to get the callbackable completion queue associated with this
374  // server. If the return value is nullptr, this server doesn't support
375  // callback operations.
376  // TODO(vjpai): Consider a better default like using a global CQ
377  // Returns 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 ::grpc_impl::CompletionQueue* CallbackCQ() { return nullptr; }
381 };
382 
383 } // namespace grpc
384 
385 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
::grpc_impl::ServerCompletionQueue *const notification_cq_
Definition: server_interface.h:198
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
virtual ~ServerInterface()
Definition: server_interface.h:61
Definition: server_interface.h:237
PayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:258
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
void Shutdown()
Shutdown the server without a deadline and forced cancellation.
Definition: server_interface.h:105
std::string string
Definition: config.h:35
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:234
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
NOTE: class experimental_registration_interface is not part of the public API of this class TODO(vjpa...
Definition: server_interface.h:130
::grpc_impl::ServerCredentials ServerCredentials
Definition: server_credentials.h:30
void * server_tag() const
Definition: rpc_service_method.h:106
Definition: server_interface.h:256
ServerInterface *const server_
Definition: server_interface.h:194
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:60
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:208
internal::InterceptorBatchMethodsImpl interceptor_methods_
Definition: server_interface.h:203
bool done_intercepting_
Definition: server_interface.h:204
Monotonic clock.
Definition: gpr_types.h:33
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:65
Definition: grpc_types.h:535
Definition: grpc_types.h:40
Definition: async_generic_service.h:75
RpcType
Definition: rpc_method.h:31
If you are trying to use CompletionQueue::AsyncNext with a time class that isn&#39;t either gpr_timespec ...
Definition: time.h:40
::grpc_impl::CompletionQueue *const call_cq_
Definition: server_interface.h:197
A ServerContext or CallbackServerContext allows the code implementing a service handler to: ...
Definition: server_context_impl.h:488
virtual ~experimental_registration_interface()
Definition: server_interface.h:132
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
::grpc_impl::ServerCompletionQueue ServerCompletionQueue
Definition: completion_queue.h:27
internal::Call call_wrapper_
Definition: server_interface.h:202
::grpc_impl::Channel Channel
Definition: channel.h:26
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
::grpc_impl::ServerContext *const context_
Definition: server_interface.h:195
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:196
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
const bool delete_on_finalize_
Definition: server_interface.h:200
Server side rpc method class.
Definition: rpc_service_method.h:87
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.h:90
Definition: interceptor_common.h:36
Definition: server_interface.h:59
grpc_call * call_
Definition: server_interface.h:201
const char * name_
Definition: server_interface.h:233
Definition: async_generic_service.h:39
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:121
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:278
void RequestAsyncCall(internal::RpcServiceMethod *method, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:329
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials_impl.h:40
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:351
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:96
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
~PayloadAsyncRequest()
Definition: server_interface.h:274
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:100
Internal errors.
Definition: status.h:127
virtual experimental_registration_interface * experimental_registration()
NOTE: The function experimental_registration() is not stable public API.
Definition: server_interface.h:142
Analogous to struct timespec.
Definition: gpr_types.h:47
Definition: server_interface.h:314
void *const tag_
Definition: server_interface.h:199
virtual void RegisterCallbackGenericService(experimental::CallbackGenericService *)
May not be abstract since this is a post-1.0 API addition.
Definition: server_interface.h:134
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue_impl.h:389
NoPayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:239
This is an interface that Channel and Server implement to allow them to hook performing ops...
Definition: call_hook.h:30
A sequence of bytes.
Definition: byte_buffer.h:67
Definition: server_interface.h:178
virtual 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:218
void RequestAsyncCall(internal::RpcServiceMethod *method, ::grpc_impl::ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:340
Straightforward wrapping of the C call object.
Definition: call.h:38
::google::protobuf::Message Message
Definition: config_protobuf.h:75