GRPC C++  1.4.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
server.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_SERVER_H
35 #define GRPCXX_SERVER_H
36 
37 #include <condition_variable>
38 #include <list>
39 #include <memory>
40 #include <mutex>
41 #include <vector>
42 
44 #include <grpc++/impl/call.h>
50 #include <grpc++/support/config.h>
51 #include <grpc++/support/status.h>
52 #include <grpc/compression.h>
53 
54 struct grpc_server;
55 
56 namespace grpc {
57 
58 class AsyncGenericService;
59 class HealthCheckServiceInterface;
60 class ServerContext;
61 class ServerInitializer;
62 
67 class Server final : public ServerInterface, private GrpcLibraryCodegen {
68  public:
69  ~Server();
70 
75  void Wait() override;
76 
84  public:
85  virtual ~GlobalCallbacks() {}
87  virtual void UpdateArguments(ChannelArguments* args) {}
89  virtual void PreSynchronousRequest(ServerContext* context) = 0;
91  virtual void PostSynchronousRequest(ServerContext* context) = 0;
93  virtual void PreServerStart(Server* server) {}
95  virtual void AddPort(Server* server, const grpc::string& addr,
96  ServerCredentials* creds, int port) {}
97  };
103  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
104 
105  // Returns a \em raw pointer to the underlying \a grpc_server instance.
107 
110  return health_check_service_.get();
111  }
112 
113  private:
114  friend class AsyncGenericService;
115  friend class ServerBuilder;
116  friend class ServerInitializer;
117 
118  class SyncRequest;
119  class AsyncRequest;
120  class ShutdownRequest;
121 
126  class SyncRequestThreadManager;
127 
128  class UnimplementedAsyncRequestContext;
129  class UnimplementedAsyncRequest;
130  class UnimplementedAsyncResponse;
131 
153  Server(int max_message_size, ChannelArguments* args,
154  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
155  sync_server_cqs,
156  int min_pollers, int max_pollers, int sync_cq_timeout_msec);
157 
160  bool RegisterService(const grpc::string* host, Service* service) override;
161 
164  void RegisterAsyncGenericService(AsyncGenericService* service) override;
165 
179  int AddListeningPort(const grpc::string& addr,
180  ServerCredentials* creds) override;
181 
188  void Start(ServerCompletionQueue** cqs, size_t num_cqs) override;
189 
190  void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) override;
191 
192  void ShutdownInternal(gpr_timespec deadline) override;
193 
194  int max_receive_message_size() const override {
195  return max_receive_message_size_;
196  };
197 
198  grpc_server* server() override { return server_; };
199 
200  ServerInitializer* initializer();
201 
202  const int max_receive_message_size_;
203 
207  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
208  sync_server_cqs_;
209 
212  std::vector<std::unique_ptr<SyncRequestThreadManager>> sync_req_mgrs_;
213 
214  // Sever status
215  std::mutex mu_;
216  bool started_;
217  bool shutdown_;
218  bool shutdown_notified_; // Was notify called on the shutdown_cv_
219 
220  std::condition_variable shutdown_cv_;
221 
222  std::shared_ptr<GlobalCallbacks> global_callbacks_;
223 
224  std::vector<grpc::string> services_;
225  bool has_generic_service_;
226 
227  // Pointer to the wrapped grpc_server.
228  grpc_server* server_;
229 
230  std::unique_ptr<ServerInitializer> server_initializer_;
231 
232  std::unique_ptr<HealthCheckServiceInterface> health_check_service_;
233  bool health_check_service_disabled_;
234 };
235 
236 } // namespace grpc
237 
238 #endif // GRPCXX_SERVER_H
std::string string
Definition: config.h:50
friend class ServerInitializer
Definition: server.h:116
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:567
void Wait() override
Block until the server shuts down.
virtual void AddPort(Server *server, const grpc::string &addr, ServerCredentials *creds, int port)
Called after a server port is added.
Definition: server.h:95
Options for channel creation.
Definition: channel_arguments.h:54
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:71
Definition: server_initializer.h:47
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:81
virtual ~GlobalCallbacks()
Definition: server.h:85
Definition: async_generic_service.h:60
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
Set the global callback object.
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:52
The gRPC server uses this interface to expose the health checking service without depending on protob...
Definition: health_check_service_interface.h:46
virtual void PreSynchronousRequest(ServerContext *context)=0
Called before application callback for each synchronous server request.
virtual void PreServerStart(Server *server)
Called before server is started.
Definition: server.h:93
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:50
Represents a gRPC server.
Definition: server.h:67
Straightforward wrapping of the C call object.
Definition: call.h:638
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:109
Models a gRPC server.
Definition: server_interface.h:60
Global callbacks are a set of hooks that are called when server events occur.
Definition: server.h:83
Analogous to struct timespec.
Definition: gpr_types.h:62
grpc_server * c_server()
virtual void UpdateArguments(ChannelArguments *args)
Called before server is created.
Definition: server.h:87
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:291
virtual void PostSynchronousRequest(ServerContext *context)=0
Called after application callback for each synchronous server request.
HealthCheckServiceInterface * GetHealthCheckService() const
Returns the health check service.
Definition: server.h:109
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:69