GRPC C++  1.13.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 
28 
29 namespace grpc {
30 
31 class AsyncGenericService;
32 class Channel;
33 class GenericServerContext;
34 class ServerCompletionQueue;
35 class ServerContext;
36 class ServerCredentials;
37 class Service;
38 
39 extern CoreCodegenInterface* g_core_codegen_interface;
40 
44 namespace internal {
45 class ServerAsyncStreamingInterface;
46 } // namespace internal
47 
49  public:
50  virtual ~ServerInterface() {}
51 
61  template <class T>
62  void Shutdown(const T& deadline) {
63  ShutdownInternal(TimePoint<T>(deadline).raw_time());
64  }
65 
71  void Shutdown() {
72  ShutdownInternal(
73  g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_MONOTONIC));
74  }
75 
80  virtual void Wait() = 0;
81 
82  protected:
83  friend class ::grpc::Service;
84 
87  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
88 
91  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
92 
104  virtual int AddListeningPort(const grpc::string& addr,
105  ServerCredentials* creds) = 0;
106 
113  virtual void Start(ServerCompletionQueue** cqs, size_t num_cqs) = 0;
114 
115  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
116 
117  virtual int max_receive_message_size() const = 0;
118 
119  virtual grpc_server* server() = 0;
120 
121  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
122  internal::Call* call) = 0;
123 
125  public:
128  CompletionQueue* call_cq, void* tag,
129  bool delete_on_finalize);
130  virtual ~BaseAsyncRequest();
131 
132  bool FinalizeResult(void** tag, bool* status) override;
133 
134  protected:
139  void* const tag_;
142  };
143 
145  public:
148  CompletionQueue* call_cq, void* tag);
149 
150  // uses BaseAsyncRequest::FinalizeResult
151 
152  protected:
153  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
154  ServerCompletionQueue* notification_cq);
155  };
156 
158  public:
159  NoPayloadAsyncRequest(void* registered_method, ServerInterface* server,
160  ServerContext* context,
162  CompletionQueue* call_cq,
163  ServerCompletionQueue* notification_cq, void* tag)
164  : RegisteredAsyncRequest(server, context, stream, call_cq, tag) {
165  IssueRequest(registered_method, nullptr, notification_cq);
166  }
167 
168  // uses RegisteredAsyncRequest::FinalizeResult
169  };
170 
171  template <class Message>
173  public:
174  PayloadAsyncRequest(void* registered_method, ServerInterface* server,
175  ServerContext* context,
177  CompletionQueue* call_cq,
178  ServerCompletionQueue* notification_cq, void* tag,
179  Message* request)
180  : RegisteredAsyncRequest(server, context, stream, call_cq, tag),
181  registered_method_(registered_method),
182  server_(server),
183  context_(context),
184  stream_(stream),
185  call_cq_(call_cq),
186  notification_cq_(notification_cq),
187  tag_(tag),
188  request_(request) {
189  IssueRequest(registered_method, payload_.bbuf_ptr(), notification_cq);
190  }
191 
193  payload_.Release(); // We do not own the payload_
194  }
195 
196  bool FinalizeResult(void** tag, bool* status) override {
197  if (*status) {
198  if (!payload_.Valid() || !SerializationTraits<Message>::Deserialize(
199  payload_.bbuf_ptr(), request_)
200  .ok()) {
201  // If deserialization fails, we cancel the call and instantiate
202  // a new instance of ourselves to request another call. We then
203  // return false, which prevents the call from being returned to
204  // the application.
205  g_core_codegen_interface->grpc_call_cancel_with_status(
206  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
207  g_core_codegen_interface->grpc_call_unref(call_);
208  new PayloadAsyncRequest(registered_method_, server_, context_,
209  stream_, call_cq_, notification_cq_, tag_,
210  request_);
211  delete this;
212  return false;
213  }
214  }
215  return RegisteredAsyncRequest::FinalizeResult(tag, status);
216  }
217 
218  private:
219  void* const registered_method_;
220  ServerInterface* const server_;
221  ServerContext* const context_;
223  CompletionQueue* const call_cq_;
224  ServerCompletionQueue* const notification_cq_;
225  void* const tag_;
226  Message* const request_;
227  ByteBuffer payload_;
228  };
229 
231  public:
234  CompletionQueue* call_cq,
235  ServerCompletionQueue* notification_cq, void* tag,
236  bool delete_on_finalize);
237 
238  bool FinalizeResult(void** tag, bool* status) override;
239 
240  private:
241  grpc_call_details call_details_;
242  };
243 
244  template <class Message>
246  ServerContext* context,
248  CompletionQueue* call_cq,
249  ServerCompletionQueue* notification_cq, void* tag,
250  Message* message) {
251  GPR_CODEGEN_ASSERT(method);
252  new PayloadAsyncRequest<Message>(method->server_tag(), this, context,
253  stream, call_cq, notification_cq, tag,
254  message);
255  }
256 
258  ServerContext* context,
260  CompletionQueue* call_cq,
261  ServerCompletionQueue* notification_cq, void* tag) {
262  GPR_CODEGEN_ASSERT(method);
263  new NoPayloadAsyncRequest(method->server_tag(), this, context, stream,
264  call_cq, notification_cq, tag);
265  }
266 
269  CompletionQueue* call_cq,
270  ServerCompletionQueue* notification_cq,
271  void* tag) {
272  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
273  tag, true);
274  }
275 };
276 
277 } // namespace grpc
278 
279 #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:50
Definition: server_interface.h:157
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:267
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:138
void Shutdown()
Shutdown the server, waiting for all rpc processing to finish.
Definition: server_interface.h:71
std::string string
Definition: config.h:35
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:64
Definition: server_interface.h:172
ServerInterface *const server_
Definition: server_interface.h:135
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:58
Definition: server_interface.h:144
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:245
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:472
Definition: grpc_types.h:40
Definition: async_generic_service.h:62
If you are trying to use CompletionQueue::AsyncNext with a time class that isn&#39;t either gpr_timespec ...
Definition: time.h:40
PayloadAsyncRequest(void *registered_method, ServerInterface *server, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:174
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:35
internal::ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:137
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
const bool delete_on_finalize_
Definition: server_interface.h:140
Server side rpc method class.
Definition: rpc_service_method.h:56
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:46
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:257
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:96
Definition: server_interface.h:48
grpc_call * call_
Definition: server_interface.h:141
CompletionQueue *const call_cq_
Definition: server_interface.h:138
Definition: async_generic_service.h:32
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:94
bool FinalizeResult(void **tag, bool *status) override
Called prior to returning from Next(), return value is the status of the operation (return status is ...
Definition: server_interface.h:196
void Shutdown(const T &deadline)
Shutdown the server, blocking until all rpc processing finishes.
Definition: server_interface.h:62
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:603
~PayloadAsyncRequest()
Definition: server_interface.h:192
ServerContext *const context_
Definition: server_interface.h:136
Internal errors.
Definition: status.h:127
Analogous to struct timespec.
Definition: gpr_types.h:47
Definition: server_interface.h:230
void *const tag_
Definition: server_interface.h:139
NoPayloadAsyncRequest(void *registered_method, ServerInterface *server, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:159
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:364
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:53
Definition: server_interface.h:124
Straightforward wrapping of the C call object.
Definition: call.h:660
::google::protobuf::Message Message
Definition: config_protobuf.h:72