GRPC C++  1.0.0
server_interface.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_IMPL_CODEGEN_SERVER_INTERFACE_H
35 #define GRPCXX_IMPL_CODEGEN_SERVER_INTERFACE_H
36 
42 
43 namespace grpc {
44 
45 class AsyncGenericService;
46 class GenericServerContext;
47 class RpcService;
48 class ServerAsyncStreamingInterface;
49 class ServerCompletionQueue;
50 class ServerContext;
51 class ServerCredentials;
52 class Service;
53 class ThreadPoolInterface;
54 
58 class ServerInterface : public CallHook {
59  public:
60  virtual ~ServerInterface() {}
61 
71  template <class T>
72  void Shutdown(const T& deadline) {
73  ShutdownInternal(TimePoint<T>(deadline).raw_time());
74  }
75 
82 
87  virtual void Wait() = 0;
88 
89  protected:
90  friend class Service;
91 
94  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
95 
98  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
99 
111  virtual int AddListeningPort(const grpc::string& addr,
112  ServerCredentials* creds) = 0;
113 
122  virtual bool Start(ServerCompletionQueue** cqs, size_t num_cqs) = 0;
123 
125  virtual void RunRpc() = 0;
126 
128  virtual void ScheduleCallback() = 0;
129 
130  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
131 
132  virtual int max_message_size() const = 0;
133 
134  virtual grpc_server* server() = 0;
135 
136  virtual void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) = 0;
137 
139  public:
142  CompletionQueue* call_cq, void* tag,
143  bool delete_on_finalize);
144  virtual ~BaseAsyncRequest() {}
145 
146  bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE;
147 
148  protected:
153  void* const tag_;
157  };
158 
160  public:
163  CompletionQueue* call_cq, void* tag);
164 
165  // uses BaseAsyncRequest::FinalizeResult
166 
167  protected:
168  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
169  ServerCompletionQueue* notification_cq);
170  };
171 
173  public:
174  NoPayloadAsyncRequest(void* registered_method, ServerInterface* server,
175  ServerContext* context,
177  CompletionQueue* call_cq,
178  ServerCompletionQueue* notification_cq, void* tag)
179  : RegisteredAsyncRequest(server, context, stream, call_cq, tag) {
180  IssueRequest(registered_method, nullptr, notification_cq);
181  }
182 
183  // uses RegisteredAsyncRequest::FinalizeResult
184  };
185 
186  template <class Message>
188  public:
189  PayloadAsyncRequest(void* registered_method, ServerInterface* server,
190  ServerContext* context,
192  CompletionQueue* call_cq,
193  ServerCompletionQueue* notification_cq, void* tag,
194  Message* request)
195  : RegisteredAsyncRequest(server, context, stream, call_cq, tag),
196  request_(request) {
197  IssueRequest(registered_method, &payload_, notification_cq);
198  }
199 
200  bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
201  bool serialization_status =
202  *status && payload_ &&
205  .ok();
206  bool ret = RegisteredAsyncRequest::FinalizeResult(tag, status);
207  *status = serialization_status && *status;
208  return ret;
209  }
210 
211  private:
212  grpc_byte_buffer* payload_;
213  Message* const request_;
214  };
215 
217  public:
220  CompletionQueue* call_cq,
221  ServerCompletionQueue* notification_cq, void* tag,
222  bool delete_on_finalize);
223 
224  bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE;
225 
226  private:
227  grpc_call_details call_details_;
228  };
229 
230  template <class Message>
233  CompletionQueue* call_cq,
234  ServerCompletionQueue* notification_cq, void* tag,
235  Message* message) {
236  GPR_CODEGEN_ASSERT(method);
237  new PayloadAsyncRequest<Message>(method->server_tag(), this, context,
238  stream, call_cq, notification_cq, tag,
239  message);
240  }
241 
244  CompletionQueue* call_cq,
245  ServerCompletionQueue* notification_cq, void* tag) {
246  GPR_CODEGEN_ASSERT(method);
247  new NoPayloadAsyncRequest(method->server_tag(), this, context, stream,
248  call_cq, notification_cq, tag);
249  }
250 
253  CompletionQueue* call_cq,
254  ServerCompletionQueue* notification_cq,
255  void* tag) {
256  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
257  tag, true);
258  }
259 };
260 
261 } // namespace grpc
262 
263 #endif // GRPCXX_IMPL_CODEGEN_SERVER_INTERFACE_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:62
PayloadAsyncRequest(void *registered_method, ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:189
virtual ~ServerInterface()
Definition: server_interface.h:60
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:40
Definition: server_interface.h:172
void Shutdown()
Shutdown the server, waiting for all rpc processing to finish.
Definition: server_interface.h:81
bool FinalizeResult(void **tag, bool *status) GRPC_OVERRIDE
std::string string
Definition: config.h:118
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:563
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:97
Definition: server_interface.h:187
ServerInterface *const server_
Definition: server_interface.h:149
Definition: service_type.h:64
virtual void ShutdownInternal(gpr_timespec deadline)=0
Definition: server_interface.h:159
Definition: service_type.h:53
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:57
Definition: grpc_types.h:275
Definition: byte_buffer.h:49
Definition: grpc_types.h:269
Definition: async_generic_service.h:60
Definition: time.h:54
virtual void RunRpc()=0
Process one or more incoming calls.
virtual ~BaseAsyncRequest()
Definition: server_interface.h:144
Definition: rpc_service_method.h:73
void RequestAsyncCall(RpcServiceMethod *method, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:242
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:64
virtual grpc_server * server()=0
Definition: server_credentials.h:50
virtual void PerformOpsOnCall(CallOpSetInterface *ops, Call *call)=0
Definition: call.h:645
Definition: alarm.h:48
const bool delete_on_finalize_
Definition: server_interface.h:154
grpc_metadata_array initial_metadata_array_
Definition: server_interface.h:156
bool FinalizeResult(void **tag, bool *status) GRPC_OVERRIDE
Definition: server_interface.h:200
Definition: server_context.h:91
Models a gRPC server.
Definition: server_interface.h:58
grpc_call * call_
Definition: server_interface.h:155
CompletionQueue *const call_cq_
Definition: server_interface.h:152
virtual bool RegisterService(const grpc::string *host, Service *service)=0
Register a service.
ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:151
Definition: async_generic_service.h:47
A thin wrapper around grpc_completion_queue (see / src/core/surface/completion_queue.h).
Definition: completion_queue.h:97
#define GRPC_FINAL
Definition: config.h:72
virtual int max_message_size() const =0
void Shutdown(const T &deadline)
Shutdown the server, blocking until all rpc processing finishes.
Definition: server_interface.h:72
Definition: time.h:51
BaseAsyncRequest(ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, void *tag, bool delete_on_finalize)
virtual void ScheduleCallback()=0
Schedule RunRpc to run in the threadpool.
ServerContext *const context_
Definition: server_interface.h:150
virtual void RegisterAsyncGenericService(AsyncGenericService *service)=0
Register a generic service.
virtual int AddListeningPort(const grpc::string &addr, ServerCredentials *creds)=0
Tries to bind server to the given addr.
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
void * server_tag() const
Definition: rpc_service_method.h:81
Definition: time.h:63
Definition: server_interface.h:216
void RequestAsyncGenericCall(GenericServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:251
void *const tag_
Definition: server_interface.h:153
virtual void Wait()=0
Block waiting for all work to complete.
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:235
NoPayloadAsyncRequest(void *registered_method, ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:174
Channel and Server implement this to allow them to hook performing ops.
Definition: call_hook.h:43
#define GRPC_OVERRIDE
Definition: config.h:78
Definition: server_interface.h:138
void RequestAsyncCall(RpcServiceMethod *method, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:231
virtual bool Start(ServerCompletionQueue **cqs, size_t num_cqs)=0
Start the server.