GRPC C++  1.23.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 } // namespace grpc_impl
38 namespace grpc {
39 
40 class AsyncGenericService;
41 class GenericServerContext;
42 class Service;
43 
44 extern CoreCodegenInterface* g_core_codegen_interface;
45 
49 namespace internal {
50 class ServerAsyncStreamingInterface;
51 } // namespace internal
52 
53 namespace experimental {
54 class CallbackGenericService;
55 } // namespace experimental
56 
58  public:
59  virtual ~ServerInterface() {}
60 
93  template <class T>
94  void Shutdown(const T& deadline) {
95  ShutdownInternal(TimePoint<T>(deadline).raw_time());
96  }
97 
103  void Shutdown() {
104  ShutdownInternal(
105  g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_MONOTONIC));
106  }
107 
112  virtual void Wait() = 0;
113 
114  protected:
115  friend class ::grpc::Service;
116 
119  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
120 
123  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
124 
129  public:
134  };
135 
141  return nullptr;
142  }
143 
155  virtual int AddListeningPort(const grpc::string& addr,
156  grpc_impl::ServerCredentials* creds) = 0;
157 
164  virtual void Start(::grpc_impl::ServerCompletionQueue** cqs,
165  size_t num_cqs) = 0;
166 
167  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
168 
169  virtual int max_receive_message_size() const = 0;
170 
171  virtual grpc_server* server() = 0;
172 
173  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
174  internal::Call* call) = 0;
175 
177  public:
179  ::grpc_impl::ServerContext* context,
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:
209  ::grpc_impl::ServerContext* context,
211  ::grpc_impl::CompletionQueue* call_cq,
212  ::grpc_impl::ServerCompletionQueue* notification_cq,
213  void* tag, const char* name,
215 
216  virtual bool FinalizeResult(void** tag, bool* status) override {
217  /* If we are done intercepting, then there is nothing more for us to do */
218  if (done_intercepting_) {
219  return BaseAsyncRequest::FinalizeResult(tag, status);
220  }
221  call_wrapper_ = ::grpc::internal::Call(
222  call_, server_, call_cq_, server_->max_receive_message_size(),
223  context_->set_server_rpc_info(name_, type_,
224  *server_->interceptor_creators()));
225  return BaseAsyncRequest::FinalizeResult(tag, status);
226  }
227 
228  protected:
229  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
230  ::grpc_impl::ServerCompletionQueue* notification_cq);
231  const char* name_;
233  };
234 
236  public:
238  ServerInterface* server,
239  ::grpc_impl::ServerContext* context,
241  ::grpc_impl::CompletionQueue* call_cq,
242  ::grpc_impl::ServerCompletionQueue* notification_cq,
243  void* tag)
245  server, context, stream, call_cq, notification_cq, tag,
246  registered_method->name(), registered_method->method_type()) {
247  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
248  }
249 
250  // uses RegisteredAsyncRequest::FinalizeResult
251  };
252 
253  template <class Message>
255  public:
257  ServerInterface* server,
258  ::grpc_impl::ServerContext* context,
260  ::grpc_impl::CompletionQueue* call_cq,
261  ::grpc_impl::ServerCompletionQueue* notification_cq,
262  void* tag, Message* request)
264  server, context, stream, call_cq, notification_cq, tag,
265  registered_method->name(), registered_method->method_type()),
266  registered_method_(registered_method),
267  server_(server),
268  context_(context),
269  stream_(stream),
270  call_cq_(call_cq),
271  notification_cq_(notification_cq),
272  tag_(tag),
273  request_(request) {
274  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
275  notification_cq);
276  }
277 
279  payload_.Release(); // We do not own the payload_
280  }
281 
282  bool FinalizeResult(void** tag, bool* status) override {
283  /* If we are done intercepting, then there is nothing more for us to do */
284  if (done_intercepting_) {
285  return RegisteredAsyncRequest::FinalizeResult(tag, status);
286  }
287  if (*status) {
288  if (!payload_.Valid() || !SerializationTraits<Message>::Deserialize(
289  payload_.bbuf_ptr(), request_)
290  .ok()) {
291  // If deserialization fails, we cancel the call and instantiate
292  // a new instance of ourselves to request another call. We then
293  // return false, which prevents the call from being returned to
294  // the application.
295  g_core_codegen_interface->grpc_call_cancel_with_status(
296  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
297  g_core_codegen_interface->grpc_call_unref(call_);
298  new PayloadAsyncRequest(registered_method_, server_, context_,
299  stream_, call_cq_, notification_cq_, tag_,
300  request_);
301  delete this;
302  return false;
303  }
304  }
305  /* Set interception point for recv message */
306  interceptor_methods_.AddInterceptionHookPoint(
307  experimental::InterceptionHookPoints::POST_RECV_MESSAGE);
308  interceptor_methods_.SetRecvMessage(request_, nullptr);
309  return RegisteredAsyncRequest::FinalizeResult(tag, status);
310  }
311 
312  private:
313  internal::RpcServiceMethod* const registered_method_;
314  ServerInterface* const server_;
315  ::grpc_impl::ServerContext* const context_;
317  ::grpc_impl::CompletionQueue* const call_cq_;
318 
319  ::grpc_impl::ServerCompletionQueue* const notification_cq_;
320  void* const tag_;
321  Message* const request_;
322  ByteBuffer payload_;
323  };
324 
326  public:
329  ::grpc_impl::CompletionQueue* call_cq,
330  ::grpc_impl::ServerCompletionQueue* notification_cq,
331  void* tag, bool delete_on_finalize);
332 
333  bool FinalizeResult(void** tag, bool* status) override;
334 
335  private:
336  grpc_call_details call_details_;
337  };
338 
339  template <class Message>
341  ::grpc_impl::ServerContext* context,
343  ::grpc_impl::CompletionQueue* call_cq,
344  ::grpc_impl::ServerCompletionQueue* notification_cq,
345  void* tag, Message* message) {
346  GPR_CODEGEN_ASSERT(method);
347  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
348  notification_cq, tag, message);
349  }
350 
352  ::grpc_impl::ServerContext* context,
354  ::grpc_impl::CompletionQueue* call_cq,
355  ::grpc_impl::ServerCompletionQueue* notification_cq,
356  void* tag) {
357  GPR_CODEGEN_ASSERT(method);
358  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
359  notification_cq, tag);
360  }
361 
363  GenericServerContext* context,
365  ::grpc_impl::CompletionQueue* call_cq,
366  ::grpc_impl::ServerCompletionQueue* notification_cq, void* tag) {
367  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
368  tag, true);
369  }
370 
371  private:
372  // EXPERIMENTAL
373  // Getter method for the vector of interceptor factory objects.
374  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
375  // and adding a new pure method to an interface would be a breaking change
376  // (even though this is private and non-API)
377  virtual std::vector<
378  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
379  interceptor_creators() {
380  return nullptr;
381  }
382 
383  // EXPERIMENTAL
384  // A method to get the callbackable completion queue associated with this
385  // server. If the return value is nullptr, this server doesn't support
386  // callback operations.
387  // TODO(vjpai): Consider a better default like using a global CQ
388  // Returns nullptr (rather than being pure) since this is a post-1.0 method
389  // and adding a new pure method to an interface would be a breaking change
390  // (even though this is private and non-API)
391  virtual ::grpc_impl::CompletionQueue* CallbackCQ() { return nullptr; }
392 };
393 
394 } // namespace grpc
395 
396 #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:59
Definition: server_interface.h:235
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:256
#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:103
std::string string
Definition: config.h:35
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:232
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:128
::grpc_impl::ServerCredentials ServerCredentials
Definition: server_credentials.h:30
void * server_tag() const
Definition: rpc_service_method.h:106
Definition: server_interface.h:254
ServerInterface *const server_
Definition: server_interface.h:192
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:206
internal::InterceptorBatchMethodsImpl interceptor_methods_
Definition: server_interface.h:201
bool done_intercepting_
Definition: server_interface.h:202
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:519
Definition: grpc_types.h:40
Definition: async_generic_service.h:74
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
A ServerContext allows the person implementing a service handler to:
Definition: server_context_impl.h:118
virtual ~experimental_registration_interface()
Definition: server_interface.h:130
virtual void RegisterCallbackGenericService(experimental::CallbackGenericService *service)
May not be abstract since this is a post-1.0 API addition.
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:200
::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:193
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:87
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.h:91
Definition: interceptor_common.h:36
Definition: server_interface.h:57
grpc_call * call_
Definition: server_interface.h:199
const char * name_
Definition: server_interface.h:231
Definition: async_generic_service.h:38
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:129
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:282
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:340
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:362
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:94
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:278
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:101
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:140
Analogous to struct timespec.
Definition: gpr_types.h:47
Definition: server_interface.h:325
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:390
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:237
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:72
Definition: server_interface.h:176
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:216
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:351
Straightforward wrapping of the C call object.
Definition: call.h:38
::google::protobuf::Message Message
Definition: config_protobuf.h:80