GRPC C++  1.22.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 
30 
31 namespace grpc_impl {
32 
33 class Channel;
34 class CompletionQueue;
36 class ServerCredentials;
37 class ServerContext;
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 } // namespace experimental
57 
59  public:
60  virtual ~ServerInterface() {}
61 
94  template <class T>
95  void Shutdown(const T& deadline) {
96  ShutdownInternal(TimePoint<T>(deadline).raw_time());
97  }
98 
104  void Shutdown() {
105  ShutdownInternal(
106  g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_MONOTONIC));
107  }
108 
113  virtual void Wait() = 0;
114 
115  protected:
116  friend class ::grpc::Service;
117 
120  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
121 
124  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
125 
130  public:
135  };
136 
142  return nullptr;
143  }
144 
156  virtual int AddListeningPort(const grpc::string& addr,
157  grpc_impl::ServerCredentials* creds) = 0;
158 
165  virtual void Start(::grpc_impl::ServerCompletionQueue** cqs,
166  size_t num_cqs) = 0;
167 
168  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
169 
170  virtual int max_receive_message_size() const = 0;
171 
172  virtual grpc_server* server() = 0;
173 
174  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
175  internal::Call* call) = 0;
176 
178  public:
180  ::grpc_impl::ServerContext* context,
182  ::grpc_impl::CompletionQueue* call_cq,
183  ::grpc_impl::ServerCompletionQueue* notification_cq,
184  void* tag, bool delete_on_finalize);
185  virtual ~BaseAsyncRequest();
186 
187  bool FinalizeResult(void** tag, bool* status) override;
188 
189  private:
190  void ContinueFinalizeResultAfterInterception();
191 
192  protected:
198  void* const tag_;
204  };
205 
208  public:
210  ::grpc_impl::ServerContext* context,
212  ::grpc_impl::CompletionQueue* call_cq,
213  ::grpc_impl::ServerCompletionQueue* notification_cq,
214  void* tag, const char* name,
216 
217  virtual bool FinalizeResult(void** tag, bool* status) override {
218  /* If we are done intercepting, then there is nothing more for us to do */
219  if (done_intercepting_) {
220  return BaseAsyncRequest::FinalizeResult(tag, status);
221  }
222  call_wrapper_ = ::grpc::internal::Call(
223  call_, server_, call_cq_, server_->max_receive_message_size(),
224  context_->set_server_rpc_info(name_, type_,
225  *server_->interceptor_creators()));
226  return BaseAsyncRequest::FinalizeResult(tag, status);
227  }
228 
229  protected:
230  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
231  ::grpc_impl::ServerCompletionQueue* notification_cq);
232  const char* name_;
234  };
235 
237  public:
239  ServerInterface* server,
240  ::grpc_impl::ServerContext* context,
242  ::grpc_impl::CompletionQueue* call_cq,
243  ::grpc_impl::ServerCompletionQueue* notification_cq,
244  void* tag)
246  server, context, stream, call_cq, notification_cq, tag,
247  registered_method->name(), registered_method->method_type()) {
248  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
249  }
250 
251  // uses RegisteredAsyncRequest::FinalizeResult
252  };
253 
254  template <class Message>
256  public:
258  ServerInterface* server,
259  ::grpc_impl::ServerContext* context,
261  ::grpc_impl::CompletionQueue* call_cq,
262  ::grpc_impl::ServerCompletionQueue* notification_cq,
263  void* tag, Message* request)
265  server, context, stream, call_cq, notification_cq, tag,
266  registered_method->name(), registered_method->method_type()),
267  registered_method_(registered_method),
268  server_(server),
269  context_(context),
270  stream_(stream),
271  call_cq_(call_cq),
272  notification_cq_(notification_cq),
273  tag_(tag),
274  request_(request) {
275  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
276  notification_cq);
277  }
278 
280  payload_.Release(); // We do not own the payload_
281  }
282 
283  bool FinalizeResult(void** tag, bool* status) override {
284  /* If we are done intercepting, then there is nothing more for us to do */
285  if (done_intercepting_) {
286  return RegisteredAsyncRequest::FinalizeResult(tag, status);
287  }
288  if (*status) {
289  if (!payload_.Valid() || !SerializationTraits<Message>::Deserialize(
290  payload_.bbuf_ptr(), request_)
291  .ok()) {
292  // If deserialization fails, we cancel the call and instantiate
293  // a new instance of ourselves to request another call. We then
294  // return false, which prevents the call from being returned to
295  // the application.
296  g_core_codegen_interface->grpc_call_cancel_with_status(
297  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
298  g_core_codegen_interface->grpc_call_unref(call_);
299  new PayloadAsyncRequest(registered_method_, server_, context_,
300  stream_, call_cq_, notification_cq_, tag_,
301  request_);
302  delete this;
303  return false;
304  }
305  }
306  /* Set interception point for recv message */
307  interceptor_methods_.AddInterceptionHookPoint(
308  experimental::InterceptionHookPoints::POST_RECV_MESSAGE);
309  interceptor_methods_.SetRecvMessage(request_, nullptr);
310  return RegisteredAsyncRequest::FinalizeResult(tag, status);
311  }
312 
313  private:
314  internal::RpcServiceMethod* const registered_method_;
315  ServerInterface* const server_;
316  ::grpc_impl::ServerContext* const context_;
318  ::grpc_impl::CompletionQueue* const call_cq_;
319 
320  ::grpc_impl::ServerCompletionQueue* const notification_cq_;
321  void* const tag_;
322  Message* const request_;
323  ByteBuffer payload_;
324  };
325 
327  public:
330  ::grpc_impl::CompletionQueue* call_cq,
331  ::grpc_impl::ServerCompletionQueue* notification_cq,
332  void* tag, bool delete_on_finalize);
333 
334  bool FinalizeResult(void** tag, bool* status) override;
335 
336  private:
337  grpc_call_details call_details_;
338  };
339 
340  template <class Message>
342  ::grpc_impl::ServerContext* context,
344  ::grpc_impl::CompletionQueue* call_cq,
345  ::grpc_impl::ServerCompletionQueue* notification_cq,
346  void* tag, Message* message) {
347  GPR_CODEGEN_ASSERT(method);
348  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
349  notification_cq, tag, message);
350  }
351 
353  ::grpc_impl::ServerContext* context,
355  ::grpc_impl::CompletionQueue* call_cq,
356  ::grpc_impl::ServerCompletionQueue* notification_cq,
357  void* tag) {
358  GPR_CODEGEN_ASSERT(method);
359  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
360  notification_cq, tag);
361  }
362 
364  GenericServerContext* context,
366  ::grpc_impl::CompletionQueue* call_cq,
367  ::grpc_impl::ServerCompletionQueue* notification_cq, void* tag) {
368  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
369  tag, true);
370  }
371 
372  private:
373  // EXPERIMENTAL
374  // Getter method for the vector of interceptor factory objects.
375  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
376  // and adding a new pure method to an interface would be a breaking change
377  // (even though this is private and non-API)
378  virtual std::vector<
379  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
380  interceptor_creators() {
381  return nullptr;
382  }
383 
384  // EXPERIMENTAL
385  // A method to get the callbackable completion queue associated with this
386  // server. If the return value is nullptr, this server doesn't support
387  // callback operations.
388  // TODO(vjpai): Consider a better default like using a global CQ
389  // Returns nullptr (rather than being pure) since this is a post-1.0 method
390  // and adding a new pure method to an interface would be a breaking change
391  // (even though this is private and non-API)
392  virtual ::grpc_impl::CompletionQueue* CallbackCQ() { return nullptr; }
393 };
394 
395 } // namespace grpc
396 
397 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
::grpc_impl::ServerCompletionQueue *const notification_cq_
Definition: server_interface.h:197
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
virtual ~ServerInterface()
Definition: server_interface.h:60
Definition: server_interface.h:236
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:257
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:145
void Shutdown()
Shutdown the server without a deadline and forced cancellation.
Definition: server_interface.h:104
std::string string
Definition: config.h:35
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:233
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:129
::grpc_impl::ServerCredentials ServerCredentials
Definition: server_credentials.h:30
void * server_tag() const
Definition: rpc_service_method.h:106
Definition: server_interface.h:255
ServerInterface *const server_
Definition: server_interface.h:193
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:207
internal::InterceptorBatchMethodsImpl interceptor_methods_
Definition: server_interface.h:202
bool done_intercepting_
Definition: server_interface.h:203
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:510
Definition: grpc_types.h:40
Definition: async_generic_service.h:72
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::ServerContext ServerContext
Definition: server_context.h:25
::grpc_impl::CompletionQueue *const call_cq_
Definition: server_interface.h:196
A ServerContext allows the person implementing a service handler to:
Definition: server_context_impl.h:114
virtual ~experimental_registration_interface()
Definition: server_interface.h:131
virtual void RegisterCallbackGenericService(experimental::CallbackGenericService *service)
May not be abstract since this is a post-1.0 API addition.
Definition: server_interface.h:133
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:201
::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:194
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:195
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:199
Server side rpc method class.
Definition: rpc_service_method.h:87
CoreCodegenInterface * g_core_codegen_interface
Definition: call_op_set.h:51
Definition: interceptor_common.h:36
Definition: server_interface.h:58
grpc_call * call_
Definition: server_interface.h:200
const char * name_
Definition: server_interface.h:232
Definition: async_generic_service.h:36
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:125
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:283
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:341
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials_impl.h:39
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:363
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:95
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:279
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:102
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:141
Analogous to struct timespec.
Definition: gpr_types.h:47
Definition: server_interface.h:326
void *const tag_
Definition: server_interface.h:198
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue_impl.h:391
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:238
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:65
Definition: server_interface.h:177
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:217
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:352
Straightforward wrapping of the C call object.
Definition: call.h:38
::google::protobuf::Message Message
Definition: config_protobuf.h:80