GRPC C++  1.22.0-dev
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 } // namespace grpc_impl
38 namespace grpc {
39 
40 class AsyncGenericService;
41 class GenericServerContext;
42 class ServerContext;
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:
181  ::grpc_impl::CompletionQueue* call_cq,
182  ::grpc_impl::ServerCompletionQueue* notification_cq,
183  void* tag, bool delete_on_finalize);
184  virtual ~BaseAsyncRequest();
185 
186  bool FinalizeResult(void** tag, bool* status) override;
187 
188  private:
189  void ContinueFinalizeResultAfterInterception();
190 
191  protected:
197  void* const tag_;
203  };
204 
207  public:
210  ::grpc_impl::CompletionQueue* call_cq,
211  ::grpc_impl::ServerCompletionQueue* notification_cq,
212  void* tag, const char* name,
214 
215  virtual bool FinalizeResult(void** tag, bool* status) override {
216  /* If we are done intercepting, then there is nothing more for us to do */
217  if (done_intercepting_) {
218  return BaseAsyncRequest::FinalizeResult(tag, status);
219  }
220  call_wrapper_ = ::grpc::internal::Call(
221  call_, server_, call_cq_, server_->max_receive_message_size(),
222  context_->set_server_rpc_info(name_, type_,
223  *server_->interceptor_creators()));
224  return BaseAsyncRequest::FinalizeResult(tag, status);
225  }
226 
227  protected:
228  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
229  ::grpc_impl::ServerCompletionQueue* notification_cq);
230  const char* name_;
232  };
233 
235  public:
237  ServerInterface* server, ServerContext* context,
239  ::grpc_impl::CompletionQueue* call_cq,
240  ::grpc_impl::ServerCompletionQueue* notification_cq,
241  void* tag)
243  server, context, stream, call_cq, notification_cq, tag,
244  registered_method->name(), registered_method->method_type()) {
245  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
246  }
247 
248  // uses RegisteredAsyncRequest::FinalizeResult
249  };
250 
251  template <class Message>
253  public:
255  ServerInterface* server, ServerContext* context,
257  ::grpc_impl::CompletionQueue* call_cq,
258  ::grpc_impl::ServerCompletionQueue* notification_cq,
259  void* tag, Message* request)
261  server, context, stream, call_cq, notification_cq, tag,
262  registered_method->name(), registered_method->method_type()),
263  registered_method_(registered_method),
264  server_(server),
265  context_(context),
266  stream_(stream),
267  call_cq_(call_cq),
268  notification_cq_(notification_cq),
269  tag_(tag),
270  request_(request) {
271  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
272  notification_cq);
273  }
274 
276  payload_.Release(); // We do not own the payload_
277  }
278 
279  bool FinalizeResult(void** tag, bool* status) override {
280  /* If we are done intercepting, then there is nothing more for us to do */
281  if (done_intercepting_) {
282  return RegisteredAsyncRequest::FinalizeResult(tag, status);
283  }
284  if (*status) {
285  if (!payload_.Valid() || !SerializationTraits<Message>::Deserialize(
286  payload_.bbuf_ptr(), request_)
287  .ok()) {
288  // If deserialization fails, we cancel the call and instantiate
289  // a new instance of ourselves to request another call. We then
290  // return false, which prevents the call from being returned to
291  // the application.
292  g_core_codegen_interface->grpc_call_cancel_with_status(
293  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
294  g_core_codegen_interface->grpc_call_unref(call_);
295  new PayloadAsyncRequest(registered_method_, server_, context_,
296  stream_, call_cq_, notification_cq_, tag_,
297  request_);
298  delete this;
299  return false;
300  }
301  }
302  /* Set interception point for recv message */
303  interceptor_methods_.AddInterceptionHookPoint(
304  experimental::InterceptionHookPoints::POST_RECV_MESSAGE);
305  interceptor_methods_.SetRecvMessage(request_, nullptr);
306  return RegisteredAsyncRequest::FinalizeResult(tag, status);
307  }
308 
309  private:
310  internal::RpcServiceMethod* const registered_method_;
311  ServerInterface* const server_;
312  ServerContext* const context_;
314  ::grpc_impl::CompletionQueue* const call_cq_;
315 
316  ::grpc_impl::ServerCompletionQueue* const notification_cq_;
317  void* const tag_;
318  Message* const request_;
319  ByteBuffer payload_;
320  };
321 
323  public:
326  ::grpc_impl::CompletionQueue* call_cq,
327  ::grpc_impl::ServerCompletionQueue* notification_cq,
328  void* tag, bool delete_on_finalize);
329 
330  bool FinalizeResult(void** tag, bool* status) override;
331 
332  private:
333  grpc_call_details call_details_;
334  };
335 
336  template <class Message>
338  ServerContext* context,
340  ::grpc_impl::CompletionQueue* call_cq,
341  ::grpc_impl::ServerCompletionQueue* notification_cq,
342  void* tag, Message* message) {
343  GPR_CODEGEN_ASSERT(method);
344  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
345  notification_cq, tag, message);
346  }
347 
349  ServerContext* context,
351  ::grpc_impl::CompletionQueue* call_cq,
352  ::grpc_impl::ServerCompletionQueue* notification_cq,
353  void* tag) {
354  GPR_CODEGEN_ASSERT(method);
355  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
356  notification_cq, tag);
357  }
358 
360  GenericServerContext* context,
362  ::grpc_impl::CompletionQueue* call_cq,
363  ::grpc_impl::ServerCompletionQueue* notification_cq, void* tag) {
364  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
365  tag, true);
366  }
367 
368  private:
369  // EXPERIMENTAL
370  // Getter method for the vector of interceptor factory objects.
371  // Returns a 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 std::vector<
375  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
376  interceptor_creators() {
377  return nullptr;
378  }
379 
380  // EXPERIMENTAL
381  // A method to get the callbackable completion queue associated with this
382  // server. If the return value is nullptr, this server doesn't support
383  // callback operations.
384  // TODO(vjpai): Consider a better default like using a global CQ
385  // Returns nullptr (rather than being pure) since this is a post-1.0 method
386  // and adding a new pure method to an interface would be a breaking change
387  // (even though this is private and non-API)
388  virtual ::grpc_impl::CompletionQueue* CallbackCQ() { return nullptr; }
389 };
390 
391 } // namespace grpc
392 
393 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
::grpc_impl::ServerCompletionQueue *const notification_cq_
Definition: server_interface.h:196
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:234
#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:231
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:104
Definition: server_interface.h:252
ServerInterface *const server_
Definition: server_interface.h:192
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:60
NoPayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:236
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:206
internal::InterceptorBatchMethodsImpl interceptor_methods_
Definition: server_interface.h:201
bool done_intercepting_
Definition: server_interface.h:202
PayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:254
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
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:348
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:195
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:200
::grpc_impl::Channel Channel
Definition: channel.h:26
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, ::grpc_impl::CompletionQueue *call_cq, ::grpc_impl::ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:337
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:194
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:198
Server side rpc method class.
Definition: rpc_service_method.h:85
CoreCodegenInterface * g_core_codegen_interface
Definition: call_op_set.h:51
Definition: interceptor_common.h:36
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:114
Definition: server_interface.h:58
grpc_call * call_
Definition: server_interface.h:199
const char * name_
Definition: server_interface.h:230
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:279
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:359
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:275
ServerContext *const context_
Definition: server_interface.h:193
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:103
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:322
void *const tag_
Definition: server_interface.h:197
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue_impl.h:392
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:215
Straightforward wrapping of the C call object.
Definition: call.h:38
::google::protobuf::Message Message
Definition: config_protobuf.h:80