GRPC C++  1.3.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 
66 class Server final : public ServerInterface, private GrpcLibraryCodegen {
67  public:
68  ~Server();
69 
74  void Wait() override;
75 
81  public:
82  virtual ~GlobalCallbacks() {}
84  virtual void UpdateArguments(ChannelArguments* args) {}
86  virtual void PreSynchronousRequest(ServerContext* context) = 0;
88  virtual void PostSynchronousRequest(ServerContext* context) = 0;
90  virtual void PreServerStart(Server* server) {}
92  virtual void AddPort(Server* server, int port) {}
93  };
97  static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
98 
99  // Returns a \em raw pointer to the underlying grpc_server instance.
101 
104  return health_check_service_.get();
105  }
106 
107  private:
108  friend class AsyncGenericService;
109  friend class ServerBuilder;
110  friend class ServerInitializer;
111 
112  class SyncRequest;
113  class AsyncRequest;
114  class ShutdownRequest;
115 
120  class SyncRequestThreadManager;
121 
122  class UnimplementedAsyncRequestContext;
123  class UnimplementedAsyncRequest;
124  class UnimplementedAsyncResponse;
125 
147  Server(int max_message_size, ChannelArguments* args,
148  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
149  sync_server_cqs,
150  int min_pollers, int max_pollers, int sync_cq_timeout_msec);
151 
154  bool RegisterService(const grpc::string* host, Service* service) override;
155 
158  void RegisterAsyncGenericService(AsyncGenericService* service) override;
159 
171  int AddListeningPort(const grpc::string& addr,
172  ServerCredentials* creds) override;
173 
182  bool Start(ServerCompletionQueue** cqs, size_t num_cqs) override;
183 
184  void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) override;
185 
186  void ShutdownInternal(gpr_timespec deadline) override;
187 
188  int max_receive_message_size() const override {
189  return max_receive_message_size_;
190  };
191 
192  grpc_server* server() override { return server_; };
193 
194  ServerInitializer* initializer();
195 
196  const int max_receive_message_size_;
197 
201  std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
202  sync_server_cqs_;
203 
205  std::vector<std::unique_ptr<SyncRequestThreadManager>> sync_req_mgrs_;
206 
207  // Sever status
208  std::mutex mu_;
209  bool started_;
210  bool shutdown_;
211  bool shutdown_notified_; // Was notify called on the shutdown_cv_
212 
213  std::condition_variable shutdown_cv_;
214 
215  std::shared_ptr<GlobalCallbacks> global_callbacks_;
216 
217  std::vector<grpc::string> services_;
218  bool has_generic_service_;
219 
220  // Pointer to the wrapped grpc_server.
221  grpc_server* server_;
222 
223  std::unique_ptr<ServerInitializer> server_initializer_;
224 
225  std::unique_ptr<HealthCheckServiceInterface> health_check_service_;
226  bool health_check_service_disabled_;
227 };
228 
229 } // namespace grpc
230 
231 #endif // GRPCXX_SERVER_H
std::string string
Definition: config.h:50
friend class ServerInitializer
Definition: server.h:110
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:598
void Wait() override
Block waiting for all work to complete.
Options for channel creation.
Definition: channel_arguments.h:54
Definition: service_type.h:64
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:80
virtual ~GlobalCallbacks()
Definition: server.h:82
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
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:90
Definition: server_credentials.h:50
Models a gRPC server.
Definition: server.h:66
Definition: call.h:675
virtual void AddPort(Server *server, int port)
Called after a server port is added.
Definition: server.h:92
Definition: server_context.h:94
Models a gRPC server.
Definition: server_interface.h:60
Global Callbacks.
Definition: server.h:80
Definition: gpr_types.h:63
grpc_server * c_server()
virtual void UpdateArguments(ChannelArguments *args)
Called before server is created.
Definition: server.h:84
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:258
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:103
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:68