GRPC C++  1.20.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 {
32 
33 class AsyncGenericService;
34 class Channel;
35 class GenericServerContext;
36 class ServerCompletionQueue;
37 class ServerContext;
38 class ServerCredentials;
39 class Service;
40 
41 extern CoreCodegenInterface* g_core_codegen_interface;
42 
46 namespace internal {
47 class ServerAsyncStreamingInterface;
48 } // namespace internal
49 
50 namespace experimental {
51 class CallbackGenericService;
52 } // namespace experimental
53 
55  public:
56  virtual ~ServerInterface() {}
57 
90  template <class T>
91  void Shutdown(const T& deadline) {
92  ShutdownInternal(TimePoint<T>(deadline).raw_time());
93  }
94 
100  void Shutdown() {
101  ShutdownInternal(
102  g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_MONOTONIC));
103  }
104 
109  virtual void Wait() = 0;
110 
111  protected:
112  friend class ::grpc::Service;
113 
116  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
117 
120  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
121 
126  public:
131  };
132 
138  return nullptr;
139  }
140 
152  virtual int AddListeningPort(const grpc::string& addr,
153  ServerCredentials* creds) = 0;
154 
161  virtual void Start(ServerCompletionQueue** cqs, size_t num_cqs) = 0;
162 
163  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
164 
165  virtual int max_receive_message_size() const = 0;
166 
167  virtual grpc_server* server() = 0;
168 
169  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
170  internal::Call* call) = 0;
171 
173  public:
176  CompletionQueue* call_cq,
177  ServerCompletionQueue* notification_cq, void* tag,
178  bool delete_on_finalize);
179  virtual ~BaseAsyncRequest();
180 
181  bool FinalizeResult(void** tag, bool* status) override;
182 
183  private:
184  void ContinueFinalizeResultAfterInterception();
185 
186  protected:
192  void* const tag_;
198  };
199 
202  public:
205  CompletionQueue* call_cq,
206  ServerCompletionQueue* notification_cq, void* tag,
207  const char* name, internal::RpcMethod::RpcType type);
208 
209  virtual bool FinalizeResult(void** tag, bool* status) override {
210  /* If we are done intercepting, then there is nothing more for us to do */
211  if (done_intercepting_) {
212  return BaseAsyncRequest::FinalizeResult(tag, status);
213  }
214  call_wrapper_ = internal::Call(
215  call_, server_, call_cq_, server_->max_receive_message_size(),
216  context_->set_server_rpc_info(name_, type_,
217  *server_->interceptor_creators()));
218  return BaseAsyncRequest::FinalizeResult(tag, status);
219  }
220 
221  protected:
222  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
223  ServerCompletionQueue* notification_cq);
224  const char* name_;
226  };
227 
229  public:
231  ServerInterface* server, ServerContext* context,
233  CompletionQueue* call_cq,
234  ServerCompletionQueue* notification_cq, void* tag)
236  server, context, stream, call_cq, notification_cq, tag,
237  registered_method->name(), registered_method->method_type()) {
238  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
239  }
240 
241  // uses RegisteredAsyncRequest::FinalizeResult
242  };
243 
244  template <class Message>
246  public:
248  ServerInterface* server, ServerContext* context,
250  CompletionQueue* call_cq,
251  ServerCompletionQueue* notification_cq, void* tag,
252  Message* request)
254  server, context, stream, call_cq, notification_cq, tag,
255  registered_method->name(), registered_method->method_type()),
256  registered_method_(registered_method),
257  server_(server),
258  context_(context),
259  stream_(stream),
260  call_cq_(call_cq),
261  notification_cq_(notification_cq),
262  tag_(tag),
263  request_(request) {
264  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
265  notification_cq);
266  }
267 
269  payload_.Release(); // We do not own the payload_
270  }
271 
272  bool FinalizeResult(void** tag, bool* status) override {
273  /* If we are done intercepting, then there is nothing more for us to do */
274  if (done_intercepting_) {
275  return RegisteredAsyncRequest::FinalizeResult(tag, status);
276  }
277  if (*status) {
278  if (!payload_.Valid() || !SerializationTraits<Message>::Deserialize(
279  payload_.bbuf_ptr(), request_)
280  .ok()) {
281  // If deserialization fails, we cancel the call and instantiate
282  // a new instance of ourselves to request another call. We then
283  // return false, which prevents the call from being returned to
284  // the application.
285  g_core_codegen_interface->grpc_call_cancel_with_status(
286  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
287  g_core_codegen_interface->grpc_call_unref(call_);
288  new PayloadAsyncRequest(registered_method_, server_, context_,
289  stream_, call_cq_, notification_cq_, tag_,
290  request_);
291  delete this;
292  return false;
293  }
294  }
295  /* Set interception point for recv message */
296  interceptor_methods_.AddInterceptionHookPoint(
298  interceptor_methods_.SetRecvMessage(request_, nullptr);
299  return RegisteredAsyncRequest::FinalizeResult(tag, status);
300  }
301 
302  private:
303  internal::RpcServiceMethod* const registered_method_;
304  ServerInterface* const server_;
305  ServerContext* const context_;
307  CompletionQueue* const call_cq_;
308 
309  ServerCompletionQueue* const notification_cq_;
310  void* const tag_;
311  Message* const request_;
312  ByteBuffer payload_;
313  };
314 
316  public:
319  CompletionQueue* call_cq,
320  ServerCompletionQueue* notification_cq, void* tag,
321  bool delete_on_finalize);
322 
323  bool FinalizeResult(void** tag, bool* status) override;
324 
325  private:
326  grpc_call_details call_details_;
327  };
328 
329  template <class Message>
331  ServerContext* context,
333  CompletionQueue* call_cq,
334  ServerCompletionQueue* notification_cq, void* tag,
335  Message* message) {
336  GPR_CODEGEN_ASSERT(method);
337  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
338  notification_cq, tag, message);
339  }
340 
342  ServerContext* context,
344  CompletionQueue* call_cq,
345  ServerCompletionQueue* notification_cq, void* tag) {
346  GPR_CODEGEN_ASSERT(method);
347  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
348  notification_cq, tag);
349  }
350 
353  CompletionQueue* call_cq,
354  ServerCompletionQueue* notification_cq,
355  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 CompletionQueue* CallbackCQ() { return nullptr; }
381 };
382 
383 } // namespace grpc
384 
385 #endif // GRPCPP_IMPL_CODEGEN_SERVER_INTERFACE_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
virtual ~ServerInterface()
Definition: server_interface.h:56
Definition: server_interface.h:228
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:351
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:144
void Shutdown()
Shutdown the server without a deadline and forced cancellation.
Definition: server_interface.h:100
std::string string
Definition: config.h:35
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:225
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:125
void * server_tag() const
Definition: rpc_service_method.h:100
Definition: server_interface.h:245
ServerInterface *const server_
Definition: server_interface.h:187
ServerCompletionQueue *const notification_cq_
Definition: server_interface.h:191
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:58
RegisteredAsyncRequest is not part of the C++ API.
Definition: server_interface.h:201
internal::InterceptorBatchMethodsImpl interceptor_methods_
Definition: server_interface.h:196
bool done_intercepting_
Definition: server_interface.h:197
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:330
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:507
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
virtual ~experimental_registration_interface()
Definition: server_interface.h:127
virtual void RegisterCallbackGenericService(experimental::CallbackGenericService *service)
May not be abstract since this is a post-1.0 API addition.
Definition: server_interface.h:129
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
internal::Call call_wrapper_
Definition: server_interface.h:195
PayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:247
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:35
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:189
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:193
Server side rpc method class.
Definition: rpc_service_method.h:81
CoreCodegenInterface * g_core_codegen_interface
Definition: call_op_set.h:51
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:341
Definition: interceptor_common.h:36
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:110
Definition: server_interface.h:54
grpc_call * call_
Definition: server_interface.h:194
CompletionQueue *const call_cq_
Definition: server_interface.h:190
const char * name_
Definition: server_interface.h:224
Definition: async_generic_service.h:36
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:97
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:272
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:91
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:268
ServerContext *const context_
Definition: server_interface.h:188
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:137
Analogous to struct timespec.
Definition: gpr_types.h:47
Definition: server_interface.h:315
void *const tag_
Definition: server_interface.h:192
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:384
This is an interface that Channel and Server implement to allow them to hook performing ops...
Definition: call_hook.h:30
NoPayloadAsyncRequest(internal::RpcServiceMethod *registered_method, ServerInterface *server, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:230
A sequence of bytes.
Definition: byte_buffer.h:64
Definition: server_interface.h:172
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:209
Straightforward wrapping of the C call object.
Definition: call.h:36
::google::protobuf::Message Message
Definition: config_protobuf.h:78