GRPC C++  1.18.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 
51  public:
52  virtual ~ServerInterface() {}
53 
86  template <class T>
87  void Shutdown(const T& deadline) {
88  ShutdownInternal(TimePoint<T>(deadline).raw_time());
89  }
90 
96  void Shutdown() {
97  ShutdownInternal(
98  g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_MONOTONIC));
99  }
100 
105  virtual void Wait() = 0;
106 
107  protected:
108  friend class ::grpc::Service;
109 
112  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
113 
116  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
117 
129  virtual int AddListeningPort(const grpc::string& addr,
130  ServerCredentials* creds) = 0;
131 
138  virtual void Start(ServerCompletionQueue** cqs, size_t num_cqs) = 0;
139 
140  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
141 
142  virtual int max_receive_message_size() const = 0;
143 
144  virtual grpc_server* server() = 0;
145 
146  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
147  internal::Call* call) = 0;
148 
150  public:
153  CompletionQueue* call_cq,
154  ServerCompletionQueue* notification_cq, void* tag,
155  bool delete_on_finalize);
156  virtual ~BaseAsyncRequest();
157 
158  bool FinalizeResult(void** tag, bool* status) override;
159 
160  private:
161  void ContinueFinalizeResultAfterInterception();
162 
163  protected:
169  void* const tag_;
175  };
176 
179  public:
182  CompletionQueue* call_cq,
183  ServerCompletionQueue* notification_cq, void* tag,
184  const char* name, internal::RpcMethod::RpcType type);
185 
186  virtual bool FinalizeResult(void** tag, bool* status) override {
187  /* If we are done intercepting, then there is nothing more for us to do */
188  if (done_intercepting_) {
189  return BaseAsyncRequest::FinalizeResult(tag, status);
190  }
191  call_wrapper_ = internal::Call(
192  call_, server_, call_cq_, server_->max_receive_message_size(),
193  context_->set_server_rpc_info(name_, type_,
194  *server_->interceptor_creators()));
195  return BaseAsyncRequest::FinalizeResult(tag, status);
196  }
197 
198  protected:
199  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
200  ServerCompletionQueue* notification_cq);
201  const char* name_;
203  };
204 
206  public:
208  ServerInterface* server, ServerContext* context,
210  CompletionQueue* call_cq,
211  ServerCompletionQueue* notification_cq, void* tag)
213  server, context, stream, call_cq, notification_cq, tag,
214  registered_method->name(), registered_method->method_type()) {
215  IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
216  }
217 
218  // uses RegisteredAsyncRequest::FinalizeResult
219  };
220 
221  template <class Message>
223  public:
225  ServerInterface* server, ServerContext* context,
227  CompletionQueue* call_cq,
228  ServerCompletionQueue* notification_cq, void* tag,
229  Message* request)
231  server, context, stream, call_cq, notification_cq, tag,
232  registered_method->name(), registered_method->method_type()),
233  registered_method_(registered_method),
234  server_(server),
235  context_(context),
236  stream_(stream),
237  call_cq_(call_cq),
238  notification_cq_(notification_cq),
239  tag_(tag),
240  request_(request) {
241  IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
242  notification_cq);
243  }
244 
246  payload_.Release(); // We do not own the payload_
247  }
248 
249  bool FinalizeResult(void** tag, bool* status) override {
250  /* If we are done intercepting, then there is nothing more for us to do */
251  if (done_intercepting_) {
252  return RegisteredAsyncRequest::FinalizeResult(tag, status);
253  }
254  if (*status) {
255  if (!payload_.Valid() || !SerializationTraits<Message>::Deserialize(
256  payload_.bbuf_ptr(), request_)
257  .ok()) {
258  // If deserialization fails, we cancel the call and instantiate
259  // a new instance of ourselves to request another call. We then
260  // return false, which prevents the call from being returned to
261  // the application.
262  g_core_codegen_interface->grpc_call_cancel_with_status(
263  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
264  g_core_codegen_interface->grpc_call_unref(call_);
265  new PayloadAsyncRequest(registered_method_, server_, context_,
266  stream_, call_cq_, notification_cq_, tag_,
267  request_);
268  delete this;
269  return false;
270  }
271  }
272  /* Set interception point for recv message */
273  interceptor_methods_.AddInterceptionHookPoint(
275  interceptor_methods_.SetRecvMessage(request_);
276  return RegisteredAsyncRequest::FinalizeResult(tag, status);
277  }
278 
279  private:
280  internal::RpcServiceMethod* const registered_method_;
281  ServerInterface* const server_;
282  ServerContext* const context_;
284  CompletionQueue* const call_cq_;
285 
286  ServerCompletionQueue* const notification_cq_;
287  void* const tag_;
288  Message* const request_;
289  ByteBuffer payload_;
290  };
291 
293  public:
296  CompletionQueue* call_cq,
297  ServerCompletionQueue* notification_cq, void* tag,
298  bool delete_on_finalize);
299 
300  bool FinalizeResult(void** tag, bool* status) override;
301 
302  private:
303  grpc_call_details call_details_;
304  };
305 
306  template <class Message>
308  ServerContext* context,
310  CompletionQueue* call_cq,
311  ServerCompletionQueue* notification_cq, void* tag,
312  Message* message) {
313  GPR_CODEGEN_ASSERT(method);
314  new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
315  notification_cq, tag, message);
316  }
317 
319  ServerContext* context,
321  CompletionQueue* call_cq,
322  ServerCompletionQueue* notification_cq, void* tag) {
323  GPR_CODEGEN_ASSERT(method);
324  new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
325  notification_cq, tag);
326  }
327 
330  CompletionQueue* call_cq,
331  ServerCompletionQueue* notification_cq,
332  void* tag) {
333  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
334  tag, true);
335  }
336 
337  private:
338  // EXPERIMENTAL
339  // Getter method for the vector of interceptor factory objects.
340  // Returns a nullptr (rather than being pure) since this is a post-1.0 method
341  // and adding a new pure method to an interface would be a breaking change
342  // (even though this is private and non-API)
343  virtual std::vector<
344  std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
345  interceptor_creators() {
346  return nullptr;
347  }
348 
349  // EXPERIMENTAL
350  // A method to get the callbackable completion queue associated with this
351  // server. If the return value is nullptr, this server doesn't support
352  // callback operations.
353  // TODO(vjpai): Consider a better default like using a global CQ
354  // Returns nullptr (rather than being pure) since this is a post-1.0 method
355  // and adding a new pure method to an interface would be a breaking change
356  // (even though this is private and non-API)
357  virtual CompletionQueue* CallbackCQ() { return nullptr; }
358 };
359 
360 } // namespace grpc
361 
362 #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:52
Definition: server_interface.h:205
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:328
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:141
void Shutdown()
Shutdown the server without a deadline and forced cancellation.
Definition: server_interface.h:96
std::string string
Definition: config.h:35
const internal::RpcMethod::RpcType type_
Definition: server_interface.h:202
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
void * server_tag() const
Definition: rpc_service_method.h:100
Definition: server_interface.h:222
ServerInterface *const server_
Definition: server_interface.h:164
ServerCompletionQueue *const notification_cq_
Definition: server_interface.h:168
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:178
internal::InterceptorBatchMethodsImpl interceptor_methods_
Definition: server_interface.h:173
bool done_intercepting_
Definition: server_interface.h:174
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:307
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:491
Definition: grpc_types.h:40
Definition: async_generic_service.h:65
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
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
internal::Call call_wrapper_
Definition: server_interface.h:172
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:224
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:35
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:166
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:170
Server side rpc method class.
Definition: rpc_service_method.h:81
CoreCodegenInterface * g_core_codegen_interface
Definition: call_op_set.h:50
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:318
Definition: interceptor_common.h:36
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:109
Definition: server_interface.h:50
grpc_call * call_
Definition: server_interface.h:171
CompletionQueue *const call_cq_
Definition: server_interface.h:167
const char * name_
Definition: server_interface.h:201
Definition: async_generic_service.h:35
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:95
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:249
void Shutdown(const T &deadline)
Shutdown does the following things:
Definition: server_interface.h:87
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:245
ServerContext *const context_
Definition: server_interface.h:165
Internal errors.
Definition: status.h:127
Analogous to struct timespec.
Definition: gpr_types.h:47
Definition: server_interface.h:292
void *const tag_
Definition: server_interface.h:169
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:373
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:207
A sequence of bytes.
Definition: byte_buffer.h:64
Definition: server_interface.h:149
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:186
Straightforward wrapping of the C call object.
Definition: call.h:36
::google::protobuf::Message Message
Definition: config_protobuf.h:78