GRPC C++  1.13.0-dev
server.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_SERVER_H
20 #define GRPCPP_SERVER_H
21 
22 #include <condition_variable>
23 #include <list>
24 #include <memory>
25 #include <mutex>
26 #include <vector>
27 
28 #include <grpc/compression.h>
30 #include <grpcpp/impl/call.h>
36 #include <grpcpp/support/config.h>
37 #include <grpcpp/support/status.h>
38 
39 struct grpc_server;
40 
41 namespace grpc {
42 
43 class AsyncGenericService;
44 class HealthCheckServiceInterface;
45 class ServerContext;
46 class ServerInitializer;
47 
52 class Server : public ServerInterface, private GrpcLibraryCodegen {
53  public:
54  ~Server();
55 
60  void Wait() override;
61 
69  public:
70  virtual ~GlobalCallbacks() {}
72  virtual void UpdateArguments(ChannelArguments* args) {}
74  virtual void PreSynchronousRequest(ServerContext* context) = 0;
76  virtual void PostSynchronousRequest(ServerContext* context) = 0;
78  virtual void PreServerStart(Server* server) {}
80  virtual void AddPort(Server* server, const grpc::string& addr,
81  ServerCredentials* creds, int port) {}
82  };
88  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
89 
93 
96  return health_check_service_.get();
97  }
98 
100  std::shared_ptr<Channel> InProcessChannel(const ChannelArguments& args);
101 
102  protected:
105  bool RegisterService(const grpc::string* host, Service* service) override;
106 
120  int AddListeningPort(const grpc::string& addr,
121  ServerCredentials* creds) override;
122 
144  Server(int max_message_size, ChannelArguments* args,
145  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
146  sync_server_cqs,
147  int min_pollers, int max_pollers, int sync_cq_timeout_msec);
148 
155  void Start(ServerCompletionQueue** cqs, size_t num_cqs) override;
156 
157  grpc_server* server() override { return server_; };
158 
159  private:
160  friend class AsyncGenericService;
161  friend class ServerBuilder;
162  friend class ServerInitializer;
163 
164  class SyncRequest;
165  class UnimplementedAsyncRequest;
166  class UnimplementedAsyncResponse;
167 
172  class SyncRequestThreadManager;
173 
176  void RegisterAsyncGenericService(AsyncGenericService* service) override;
177 
178  void PerformOpsOnCall(internal::CallOpSetInterface* ops,
179  internal::Call* call) override;
180 
181  void ShutdownInternal(gpr_timespec deadline) override;
182 
183  int max_receive_message_size() const override {
184  return max_receive_message_size_;
185  };
186 
187  ServerInitializer* initializer();
188 
189  const int max_receive_message_size_;
190 
194  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
195  sync_server_cqs_;
196 
199  std::vector<std::unique_ptr<SyncRequestThreadManager>> sync_req_mgrs_;
200 
201  // Server status
202  std::mutex mu_;
203  bool started_;
204  bool shutdown_;
205  bool shutdown_notified_; // Was notify called on the shutdown_cv_
206 
207  std::condition_variable shutdown_cv_;
208 
209  std::shared_ptr<GlobalCallbacks> global_callbacks_;
210 
211  std::vector<grpc::string> services_;
212  bool has_generic_service_;
213 
214  // Pointer to the wrapped grpc_server.
215  grpc_server* server_;
216 
217  std::unique_ptr<ServerInitializer> server_initializer_;
218 
219  std::unique_ptr<HealthCheckServiceInterface> health_check_service_;
220  bool health_check_service_disabled_;
221 };
222 
223 } // namespace grpc
224 
225 #endif // GRPCPP_SERVER_H
bool RegisterService(const grpc::string *host, Service *service) override
Register a service.
std::string string
Definition: config.h:35
void Start(ServerCompletionQueue **cqs, size_t num_cqs) override
Start the server.
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:80
Options for channel creation.
Definition: channel_arguments.h:39
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:58
Definition: server_initializer.h:32
struct grpc_server grpc_server
A server listens to some port and responds to request calls.
Definition: grpc_types.h:65
virtual ~GlobalCallbacks()
Definition: server.h:70
Definition: async_generic_service.h:62
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:38
Server(int max_message_size, ChannelArguments *args, std::shared_ptr< std::vector< std::unique_ptr< ServerCompletionQueue >>> sync_server_cqs, int min_pollers, int max_pollers, int sync_cq_timeout_msec)
Server constructors.
grpc_server * server() override
Definition: server.h:157
The gRPC server uses this interface to expose the health checking service without depending on protob...
Definition: health_check_service_interface.h:31
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:78
int AddListeningPort(const grpc::string &addr, ServerCredentials *creds) override
Try binding the server to the given addr endpoint (port, and optionally including IP address to bind ...
Wrapper around grpc_server_credentials, a way to authenticate a server.
Definition: server_credentials.h:35
Represents a gRPC server.
Definition: server.h:52
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
A ServerContext allows the person implementing a service handler to:
Definition: server_context.h:96
Definition: server_interface.h:48
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:603
Global callbacks are a set of hooks that are called when server events occur.
Definition: server.h:68
Analogous to struct timespec.
Definition: gpr_types.h:47
grpc_server * c_server()
Returns a raw pointer to the underlying grpc_server instance.
virtual void UpdateArguments(ChannelArguments *args)
Called before server is created.
Definition: server.h:72
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:364
virtual void PostSynchronousRequest(ServerContext *context)=0
Called after application callback for each synchronous server request.
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:52
std::shared_ptr< Channel > InProcessChannel(const ChannelArguments &args)
Establish a channel for in-process communication.
Straightforward wrapping of the C call object.
Definition: call.h:660
HealthCheckServiceInterface * GetHealthCheckService() const
Returns the health check service.
Definition: server.h:95