GRPC C++  1.3.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
55 extern CoreCodegenInterface* g_core_codegen_interface;
56 
60 class ServerInterface : public CallHook {
61  public:
62  virtual ~ServerInterface() {}
63 
73  template <class T>
74  void Shutdown(const T& deadline) {
75  ShutdownInternal(TimePoint<T>(deadline).raw_time());
76  }
77 
83  void Shutdown() {
86  }
87 
92  virtual void Wait() = 0;
93 
94  protected:
95  friend class Service;
96 
99  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
100 
103  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
104 
116  virtual int AddListeningPort(const grpc::string& addr,
117  ServerCredentials* creds) = 0;
118 
127  virtual bool Start(ServerCompletionQueue** cqs, size_t num_cqs) = 0;
128 
129  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
130 
131  virtual int max_receive_message_size() const = 0;
132 
133  virtual grpc_server* server() = 0;
134 
135  virtual void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) = 0;
136 
138  public:
141  CompletionQueue* call_cq, void* tag,
142  bool delete_on_finalize);
143  virtual ~BaseAsyncRequest();
144 
145  bool FinalizeResult(void** tag, bool* status) override;
146 
147  protected:
152  void* const tag_;
155  };
156 
158  public:
161  CompletionQueue* call_cq, void* tag);
162 
163  // uses BaseAsyncRequest::FinalizeResult
164 
165  protected:
166  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
167  ServerCompletionQueue* notification_cq);
168  };
169 
171  public:
172  NoPayloadAsyncRequest(void* registered_method, ServerInterface* server,
173  ServerContext* context,
175  CompletionQueue* call_cq,
176  ServerCompletionQueue* notification_cq, void* tag)
177  : RegisteredAsyncRequest(server, context, stream, call_cq, tag) {
178  IssueRequest(registered_method, nullptr, notification_cq);
179  }
180 
181  // uses RegisteredAsyncRequest::FinalizeResult
182  };
183 
184  template <class Message>
186  public:
187  PayloadAsyncRequest(void* registered_method, ServerInterface* server,
188  ServerContext* context,
190  CompletionQueue* call_cq,
191  ServerCompletionQueue* notification_cq, void* tag,
192  Message* request)
193  : RegisteredAsyncRequest(server, context, stream, call_cq, tag),
194  request_(request) {
195  IssueRequest(registered_method, &payload_, notification_cq);
196  }
197 
198  bool FinalizeResult(void** tag, bool* status) override {
199  bool serialization_status =
200  *status && payload_ &&
201  SerializationTraits<Message>::Deserialize(payload_, request_).ok();
202  bool ret = RegisteredAsyncRequest::FinalizeResult(tag, status);
203  *status = serialization_status && *status;
204  return ret;
205  }
206 
207  private:
208  grpc_byte_buffer* payload_;
209  Message* const request_;
210  };
211 
213  public:
216  CompletionQueue* call_cq,
217  ServerCompletionQueue* notification_cq, void* tag,
218  bool delete_on_finalize);
219 
220  bool FinalizeResult(void** tag, bool* status) override;
221 
222  private:
223  grpc_call_details call_details_;
224  };
225 
226  template <class Message>
229  CompletionQueue* call_cq,
230  ServerCompletionQueue* notification_cq, void* tag,
231  Message* message) {
232  GPR_CODEGEN_ASSERT(method);
233  new PayloadAsyncRequest<Message>(method->server_tag(), this, context,
234  stream, call_cq, notification_cq, tag,
235  message);
236  }
237 
240  CompletionQueue* call_cq,
241  ServerCompletionQueue* notification_cq, void* tag) {
242  GPR_CODEGEN_ASSERT(method);
243  new NoPayloadAsyncRequest(method->server_tag(), this, context, stream,
244  call_cq, notification_cq, tag);
245  }
246 
249  CompletionQueue* call_cq,
250  ServerCompletionQueue* notification_cq,
251  void* tag) {
252  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
253  tag, true);
254  }
255 };
256 
257 } // namespace grpc
258 
259 #endif // GRPCXX_IMPL_CODEGEN_SERVER_INTERFACE_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:85
PayloadAsyncRequest(void *registered_method, ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:187
virtual ~ServerInterface()
Definition: server_interface.h:62
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:40
Definition: server_interface.h:170
void Shutdown()
Shutdown the server, waiting for all rpc processing to finish.
Definition: server_interface.h:83
std::string string
Definition: config.h:50
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:598
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:122
Definition: server_interface.h:185
ServerInterface *const server_
Definition: server_interface.h:148
Definition: service_type.h:64
virtual void ShutdownInternal(gpr_timespec deadline)=0
Definition: server_interface.h:157
Definition: service_type.h:53
Definition: gpr_types.h:49
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:80
RegisteredAsyncRequest(ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, void *tag)
Definition: grpc_types.h:410
Definition: grpc_types.h:55
Definition: async_generic_service.h:60
Definition: time.h:54
Definition: rpc_service_method.h:71
virtual int max_receive_message_size() const =0
void RequestAsyncCall(RpcServiceMethod *method, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:238
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:64
bool FinalizeResult(void **tag, bool *status) override
virtual grpc_server * server()=0
Definition: server_credentials.h:50
virtual void PerformOpsOnCall(CallOpSetInterface *ops, Call *call)=0
Definition: call.h:675
const bool delete_on_finalize_
Definition: server_interface.h:153
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:63
Definition: server_context.h:94
Models a gRPC server.
Definition: server_interface.h:60
grpc_call * call_
Definition: server_interface.h:154
CompletionQueue *const call_cq_
Definition: server_interface.h:151
virtual bool RegisterService(const grpc::string *host, Service *service)=0
Register a service.
ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:150
Definition: async_generic_service.h:47
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:101
bool FinalizeResult(void **tag, bool *status) override
Definition: server_interface.h:198
void Shutdown(const T &deadline)
Shutdown the server, blocking until all rpc processing finishes.
Definition: server_interface.h:74
BaseAsyncRequest(ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, void *tag, bool delete_on_finalize)
ServerContext *const context_
Definition: server_interface.h:149
bool FinalizeResult(void **tag, bool *status) override
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.
void * server_tag() const
Definition: rpc_service_method.h:79
Definition: gpr_types.h:63
Definition: server_interface.h:212
void RequestAsyncGenericCall(GenericServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:247
void *const tag_
Definition: server_interface.h:152
void IssueRequest(void *registered_method, grpc_byte_buffer **payload, ServerCompletionQueue *notification_cq)
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:258
GenericAsyncRequest(ServerInterface *server, GenericServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, bool delete_on_finalize)
NoPayloadAsyncRequest(void *registered_method, ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:172
Channel and Server implement this to allow them to hook performing ops.
Definition: call_hook.h:43
Definition: server_interface.h:137
void RequestAsyncCall(RpcServiceMethod *method, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:227
virtual bool Start(ServerCompletionQueue **cqs, size_t num_cqs)=0
Start the server.
::google::protobuf::Message Message
Definition: config_protobuf.h:85