GRPC C++  1.6.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 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 GRPCXX_IMPL_CODEGEN_SERVER_INTERFACE_H
20 #define GRPCXX_IMPL_CODEGEN_SERVER_INTERFACE_H
21 
27 
28 namespace grpc {
29 
30 class AsyncGenericService;
31 class Channel;
32 class GenericServerContext;
33 class RpcService;
34 class ServerAsyncStreamingInterface;
35 class ServerCompletionQueue;
36 class ServerContext;
37 class ServerCredentials;
38 class Service;
39 class ThreadPoolInterface;
40 
41 extern CoreCodegenInterface* g_core_codegen_interface;
42 
46 class ServerInterface : public CallHook {
47  public:
48  virtual ~ServerInterface() {}
49 
59  template <class T>
60  void Shutdown(const T& deadline) {
61  ShutdownInternal(TimePoint<T>(deadline).raw_time());
62  }
63 
69  void Shutdown() {
72  }
73 
78  virtual void Wait() = 0;
79 
80  protected:
81  friend class Service;
82 
85  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
86 
89  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
90 
102  virtual int AddListeningPort(const grpc::string& addr,
103  ServerCredentials* creds) = 0;
104 
111  virtual void Start(ServerCompletionQueue** cqs, size_t num_cqs) = 0;
112 
113  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
114 
115  virtual int max_receive_message_size() const = 0;
116 
117  virtual grpc_server* server() = 0;
118 
119  virtual void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) = 0;
120 
122  public:
125  CompletionQueue* call_cq, void* tag,
126  bool delete_on_finalize);
127  virtual ~BaseAsyncRequest();
128 
129  bool FinalizeResult(void** tag, bool* status) override;
130 
131  protected:
136  void* const tag_;
139  };
140 
142  public:
145  CompletionQueue* call_cq, void* tag);
146 
147  // uses BaseAsyncRequest::FinalizeResult
148 
149  protected:
150  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
151  ServerCompletionQueue* notification_cq);
152  };
153 
155  public:
156  NoPayloadAsyncRequest(void* registered_method, ServerInterface* server,
157  ServerContext* context,
159  CompletionQueue* call_cq,
160  ServerCompletionQueue* notification_cq, void* tag)
161  : RegisteredAsyncRequest(server, context, stream, call_cq, tag) {
162  IssueRequest(registered_method, nullptr, notification_cq);
163  }
164 
165  // uses RegisteredAsyncRequest::FinalizeResult
166  };
167 
168  template <class Message>
170  public:
171  PayloadAsyncRequest(void* registered_method, ServerInterface* server,
172  ServerContext* context,
174  CompletionQueue* call_cq,
175  ServerCompletionQueue* notification_cq, void* tag,
176  Message* request)
177  : RegisteredAsyncRequest(server, context, stream, call_cq, tag),
178  registered_method_(registered_method),
179  server_(server),
180  context_(context),
181  stream_(stream),
182  call_cq_(call_cq),
183  notification_cq_(notification_cq),
184  tag_(tag),
185  request_(request) {
186  IssueRequest(registered_method, &payload_, notification_cq);
187  }
188 
189  bool FinalizeResult(void** tag, bool* status) override {
190  if (*status) {
191  if (payload_ == nullptr ||
192  !SerializationTraits<Message>::Deserialize(payload_, request_)
193  .ok()) {
194  // If deserialization fails, we cancel the call and instantiate
195  // a new instance of ourselves to request another call. We then
196  // return false, which prevents the call from being returned to
197  // the application.
199  call_, GRPC_STATUS_INTERNAL, "Unable to parse request", nullptr);
201  new PayloadAsyncRequest(registered_method_, server_, context_,
202  stream_, call_cq_, notification_cq_, tag_,
203  request_);
204  delete this;
205  return false;
206  }
207  }
208  return RegisteredAsyncRequest::FinalizeResult(tag, status);
209  }
210 
211  private:
212  void* const registered_method_;
213  ServerInterface* const server_;
214  ServerContext* const context_;
215  ServerAsyncStreamingInterface* const stream_;
216  CompletionQueue* const call_cq_;
217  ServerCompletionQueue* const notification_cq_;
218  void* const tag_;
219  Message* const request_;
220  grpc_byte_buffer* payload_;
221  };
222 
224  public:
227  CompletionQueue* call_cq,
228  ServerCompletionQueue* notification_cq, void* tag,
229  bool delete_on_finalize);
230 
231  bool FinalizeResult(void** tag, bool* status) override;
232 
233  private:
234  grpc_call_details call_details_;
235  };
236 
237  template <class Message>
240  CompletionQueue* call_cq,
241  ServerCompletionQueue* notification_cq, void* tag,
242  Message* message) {
243  GPR_CODEGEN_ASSERT(method);
244  new PayloadAsyncRequest<Message>(method->server_tag(), this, context,
245  stream, call_cq, notification_cq, tag,
246  message);
247  }
248 
251  CompletionQueue* call_cq,
252  ServerCompletionQueue* notification_cq, void* tag) {
253  GPR_CODEGEN_ASSERT(method);
254  new NoPayloadAsyncRequest(method->server_tag(), this, context, stream,
255  call_cq, notification_cq, tag);
256  }
257 
260  CompletionQueue* call_cq,
261  ServerCompletionQueue* notification_cq,
262  void* tag) {
263  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
264  tag, true);
265  }
266 };
267 
268 } // namespace grpc
269 
270 #endif // GRPCXX_IMPL_CODEGEN_SERVER_INTERFACE_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:71
PayloadAsyncRequest(void *registered_method, ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:171
virtual ~ServerInterface()
Definition: server_interface.h:48
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:25
Definition: server_interface.h:154
void Shutdown()
Shutdown the server, waiting for all rpc processing to finish.
Definition: server_interface.h:69
std::string string
Definition: config.h:35
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
virtual void Start(ServerCompletionQueue **cqs, size_t num_cqs)=0
Start the server.
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:575
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:126
Definition: server_interface.h:169
ServerInterface *const server_
Definition: server_interface.h:132
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:56
virtual void ShutdownInternal(gpr_timespec deadline)=0
Definition: server_interface.h:141
virtual void grpc_call_unref(grpc_call *call)=0
Definition: service_type.h:38
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:66
RegisteredAsyncRequest(ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, void *tag)
Definition: grpc_types.h:432
Definition: grpc_types.h:41
Definition: async_generic_service.h:45
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:38
Server side rpc method class.
Definition: rpc_service_method.h:56
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:249
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:49
bool FinalizeResult(void **tag, bool *status) override
Called prior to returning from Next(), return value is the status of the operation (return status is ...
virtual grpc_server * server()=0
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:35
virtual void PerformOpsOnCall(CallOpSetInterface *ops, Call *call)=0
Straightforward wrapping of the C call object.
Definition: call.h:647
const bool delete_on_finalize_
Definition: server_interface.h:137
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:49
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:95
Models a gRPC server.
Definition: server_interface.h:46
grpc_call * call_
Definition: server_interface.h:138
CompletionQueue *const call_cq_
Definition: server_interface.h:135
virtual bool RegisterService(const grpc::string *host, Service *service)=0
Register a service.
ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:134
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:86
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:189
void Shutdown(const T &deadline)
Shutdown the server, blocking until all rpc processing finishes.
Definition: server_interface.h:60
BaseAsyncRequest(ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, void *tag, bool delete_on_finalize)
ServerContext *const context_
Definition: server_interface.h:133
bool FinalizeResult(void **tag, bool *status) override
Called prior to returning from Next(), return value is the status of the operation (return status is ...
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:64
Internal errors.
Definition: status.h:127
Analogous to struct timespec.
Definition: gpr_types.h:47
Definition: server_interface.h:223
void RequestAsyncGenericCall(GenericServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:258
void *const tag_
Definition: server_interface.h:136
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:276
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:156
This is an interface that Channel and Server implement to allow them to hook performing ops...
Definition: call_hook.h:29
Definition: server_interface.h:121
virtual grpc_call_error grpc_call_cancel_with_status(grpc_call *call, grpc_status_code status, const char *description, void *reserved)=0
void RequestAsyncCall(RpcServiceMethod *method, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:238
::google::protobuf::Message Message
Definition: config_protobuf.h:70