GRPC C++  1.10.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 
27 
28 namespace grpc {
29 
30 class AsyncGenericService;
31 class Channel;
32 class GenericServerContext;
33 class ServerCompletionQueue;
34 class ServerContext;
35 class ServerCredentials;
36 class Service;
37 
38 extern CoreCodegenInterface* g_core_codegen_interface;
39 
43 namespace internal {
44 class ServerAsyncStreamingInterface;
45 } // namespace internal
46 
48  public:
49  virtual ~ServerInterface() {}
50 
60  template <class T>
61  void Shutdown(const T& deadline) {
62  ShutdownInternal(TimePoint<T>(deadline).raw_time());
63  }
64 
70  void Shutdown() {
71  ShutdownInternal(
72  g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_MONOTONIC));
73  }
74 
79  virtual void Wait() = 0;
80 
81  protected:
82  friend class ::grpc::Service;
83 
86  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
87 
90  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
91 
103  virtual int AddListeningPort(const grpc::string& addr,
104  ServerCredentials* creds) = 0;
105 
112  virtual void Start(ServerCompletionQueue** cqs, size_t num_cqs) = 0;
113 
114  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
115 
116  virtual int max_receive_message_size() const = 0;
117 
118  virtual grpc_server* server() = 0;
119 
120  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
121  internal::Call* call) = 0;
122 
124  public:
127  CompletionQueue* call_cq, void* tag,
128  bool delete_on_finalize);
129  virtual ~BaseAsyncRequest();
130 
131  bool FinalizeResult(void** tag, bool* status) override;
132 
133  protected:
138  void* const tag_;
141  };
142 
144  public:
147  CompletionQueue* call_cq, void* tag);
148 
149  // uses BaseAsyncRequest::FinalizeResult
150 
151  protected:
152  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
153  ServerCompletionQueue* notification_cq);
154  };
155 
157  public:
158  NoPayloadAsyncRequest(void* registered_method, ServerInterface* server,
159  ServerContext* context,
161  CompletionQueue* call_cq,
162  ServerCompletionQueue* notification_cq, void* tag)
163  : RegisteredAsyncRequest(server, context, stream, call_cq, tag) {
164  IssueRequest(registered_method, nullptr, notification_cq);
165  }
166 
167  // uses RegisteredAsyncRequest::FinalizeResult
168  };
169 
170  template <class Message>
172  public:
173  PayloadAsyncRequest(void* registered_method, ServerInterface* server,
174  ServerContext* context,
176  CompletionQueue* call_cq,
177  ServerCompletionQueue* notification_cq, void* tag,
178  Message* request)
179  : RegisteredAsyncRequest(server, context, stream, call_cq, tag),
180  registered_method_(registered_method),
181  server_(server),
182  context_(context),
183  stream_(stream),
184  call_cq_(call_cq),
185  notification_cq_(notification_cq),
186  tag_(tag),
187  request_(request) {
188  IssueRequest(registered_method, &payload_, notification_cq);
189  }
190 
191  bool FinalizeResult(void** tag, bool* status) override {
192  if (*status) {
193  if (payload_ == nullptr ||
194  !SerializationTraits<Message>::Deserialize(payload_, request_)
195  .ok()) {
196  // If deserialization fails, we cancel the call and instantiate
197  // a new instance of ourselves to request another call. We then
198  // return false, which prevents the call from being returned to
199  // the application.
200  g_core_codegen_interface->grpc_call_cancel_with_status(
201  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
202  g_core_codegen_interface->grpc_call_unref(call_);
203  new PayloadAsyncRequest(registered_method_, server_, context_,
204  stream_, call_cq_, notification_cq_, tag_,
205  request_);
206  delete this;
207  return false;
208  }
209  }
210  return RegisteredAsyncRequest::FinalizeResult(tag, status);
211  }
212 
213  private:
214  void* const registered_method_;
215  ServerInterface* const server_;
216  ServerContext* const context_;
218  CompletionQueue* const call_cq_;
219  ServerCompletionQueue* const notification_cq_;
220  void* const tag_;
221  Message* const request_;
222  grpc_byte_buffer* payload_;
223  };
224 
226  public:
229  CompletionQueue* call_cq,
230  ServerCompletionQueue* notification_cq, void* tag,
231  bool delete_on_finalize);
232 
233  bool FinalizeResult(void** tag, bool* status) override;
234 
235  private:
236  grpc_call_details call_details_;
237  };
238 
239  template <class Message>
241  ServerContext* context,
243  CompletionQueue* call_cq,
244  ServerCompletionQueue* notification_cq, void* tag,
245  Message* message) {
246  GPR_CODEGEN_ASSERT(method);
247  new PayloadAsyncRequest<Message>(method->server_tag(), this, context,
248  stream, call_cq, notification_cq, tag,
249  message);
250  }
251 
253  ServerContext* context,
255  CompletionQueue* call_cq,
256  ServerCompletionQueue* notification_cq, void* tag) {
257  GPR_CODEGEN_ASSERT(method);
258  new NoPayloadAsyncRequest(method->server_tag(), this, context, stream,
259  call_cq, notification_cq, tag);
260  }
261 
264  CompletionQueue* call_cq,
265  ServerCompletionQueue* notification_cq,
266  void* tag) {
267  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
268  tag, true);
269  }
270 };
271 
272 } // namespace grpc
273 
274 #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:49
Definition: server_interface.h:156
void RequestAsyncGenericCall(GenericServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:262
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:133
void Shutdown()
Shutdown the server, waiting for all rpc processing to finish.
Definition: server_interface.h:70
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:171
ServerInterface *const server_
Definition: server_interface.h:134
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:58
Definition: server_interface.h:143
void RequestAsyncCall(internal::RpcServiceMethod *method, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:240
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:450
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:173
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:136
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:139
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:252
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:96
Definition: server_interface.h:47
grpc_call * call_
Definition: server_interface.h:140
CompletionQueue *const call_cq_
Definition: server_interface.h:137
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:191
void Shutdown(const T &deadline)
Shutdown the server, blocking until all rpc processing finishes.
Definition: server_interface.h:61
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:603
ServerContext *const context_
Definition: server_interface.h:135
Internal errors.
Definition: status.h:127
Analogous to struct timespec.
Definition: gpr_types.h:47
Definition: server_interface.h:225
void *const tag_
Definition: server_interface.h:138
NoPayloadAsyncRequest(void *registered_method, ServerInterface *server, ServerContext *context, internal::ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:158
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
Definition: server_interface.h:123
Straightforward wrapping of the C call object.
Definition: call.h:675
::google::protobuf::Message Message
Definition: config_protobuf.h:72