blob: 81c3907f86dd4d654012f7cb2a4c4f13115d90b4 [file] [log] [blame]
Muxi Yan0e00c432018-01-26 15:39:32 -08001/*
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>
29#include <grpcpp/completion_queue.h>
30#include <grpcpp/impl/call.h>
31#include <grpcpp/impl/codegen/grpc_library.h>
32#include <grpcpp/impl/codegen/server_interface.h>
33#include <grpcpp/impl/rpc_service_method.h>
34#include <grpcpp/security/server_credentials.h>
35#include <grpcpp/support/channel_arguments.h>
36#include <grpcpp/support/config.h>
37#include <grpcpp/support/status.h>
38
39struct grpc_server;
40
41namespace grpc {
42
43class AsyncGenericService;
44class HealthCheckServiceInterface;
45class ServerContext;
46class ServerInitializer;
47
48/// Represents a gRPC server.
49///
50/// Use a \a grpc::ServerBuilder to create, configure, and start
51/// \a Server instances.
makdharma80650002018-02-23 14:51:46 -080052class Server : public ServerInterface, private GrpcLibraryCodegen {
Muxi Yan0e00c432018-01-26 15:39:32 -080053 public:
54 ~Server();
55
56 /// Block until the server shuts down.
57 ///
58 /// \warning The server must be either shutting down or some other thread must
59 /// call \a Shutdown for this function to ever return.
60 void Wait() override;
61
62 /// Global callbacks are a set of hooks that are called when server
63 /// events occur. \a SetGlobalCallbacks method is used to register
64 /// the hooks with gRPC. Note that
65 /// the \a GlobalCallbacks instance will be shared among all
66 /// \a Server instances in an application and can be set exactly
67 /// once per application.
68 class GlobalCallbacks {
69 public:
70 virtual ~GlobalCallbacks() {}
71 /// Called before server is created.
72 virtual void UpdateArguments(ChannelArguments* args) {}
73 /// Called before application callback for each synchronous server request
74 virtual void PreSynchronousRequest(ServerContext* context) = 0;
75 /// Called after application callback for each synchronous server request
76 virtual void PostSynchronousRequest(ServerContext* context) = 0;
77 /// Called before server is started.
78 virtual void PreServerStart(Server* server) {}
79 /// Called after a server port is added.
80 virtual void AddPort(Server* server, const grpc::string& addr,
81 ServerCredentials* creds, int port) {}
82 };
83 /// Set the global callback object. Can only be called once per application.
84 /// Does not take ownership of callbacks, and expects the pointed to object
85 /// to be alive until all server objects in the process have been destroyed.
86 /// The same \a GlobalCallbacks object will be used throughout the
87 /// application and is shared among all \a Server objects.
88 static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
89
makdharma757af972018-03-09 10:46:35 -080090 /// Returns a \em raw pointer to the underlying \a grpc_server instance.
91 /// EXPERIMENTAL: for internal/test use only
Muxi Yan0e00c432018-01-26 15:39:32 -080092 grpc_server* c_server();
93
94 /// Returns the health check service.
95 HealthCheckServiceInterface* GetHealthCheckService() const {
96 return health_check_service_.get();
97 }
98
99 /// Establish a channel for in-process communication
100 std::shared_ptr<Channel> InProcessChannel(const ChannelArguments& args);
101
makdharma57237442018-02-26 16:03:57 -0800102 protected:
makdharma80650002018-02-23 14:51:46 -0800103 /// Register a service. This call does not take ownership of the service.
104 /// The service must exist for the lifetime of the Server instance.
105 bool RegisterService(const grpc::string* host, Service* service) override;
Muxi Yan0e00c432018-01-26 15:39:32 -0800106
makdharma80650002018-02-23 14:51:46 -0800107 /// Try binding the server to the given \a addr endpoint
108 /// (port, and optionally including IP address to bind to).
109 ///
110 /// It can be invoked multiple times. Should be used before
111 /// starting the server.
112 ///
113 /// \param addr The address to try to bind to the server (eg, localhost:1234,
114 /// 192.168.1.1:31416, [::1]:27182, etc.).
115 /// \param creds The credentials associated with the server.
116 ///
117 /// \return bound port number on success, 0 on failure.
118 ///
119 /// \warning It is an error to call this method on an already started server.
120 int AddListeningPort(const grpc::string& addr,
121 ServerCredentials* creds) override;
Muxi Yan0e00c432018-01-26 15:39:32 -0800122
123 /// Server constructors. To be used by \a ServerBuilder only.
124 ///
125 /// \param max_message_size Maximum message length that the channel can
126 /// receive.
127 ///
128 /// \param args The channel args
129 ///
130 /// \param sync_server_cqs The completion queues to use if the server is a
131 /// synchronous server (or a hybrid server). The server polls for new RPCs on
132 /// these queues
133 ///
134 /// \param min_pollers The minimum number of polling threads per server
135 /// completion queue (in param sync_server_cqs) to use for listening to
136 /// incoming requests (used only in case of sync server)
137 ///
138 /// \param max_pollers The maximum number of polling threads per server
139 /// completion queue (in param sync_server_cqs) to use for listening to
140 /// incoming requests (used only in case of sync server)
141 ///
142 /// \param sync_cq_timeout_msec The timeout to use when calling AsyncNext() on
143 /// server completion queues passed via sync_server_cqs param.
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
Muxi Yan0e00c432018-01-26 15:39:32 -0800149 /// Start the server.
150 ///
151 /// \param cqs Completion queues for handling asynchronous services. The
152 /// caller is required to keep all completion queues live until the server is
153 /// destroyed.
154 /// \param num_cqs How many completion queues does \a cqs hold.
155 void Start(ServerCompletionQueue** cqs, size_t num_cqs) override;
156
makdharma838af3e2018-03-09 09:39:42 -0800157 grpc_server* server() override { return server_; };
makdharma80650002018-02-23 14:51:46 -0800158
159 private:
160 friend class AsyncGenericService;
161 friend class ServerBuilder;
162 friend class ServerInitializer;
163
164 class SyncRequest;
Vijay Paia4da2d22018-03-21 21:07:22 -0700165 class UnimplementedAsyncRequest;
166 class UnimplementedAsyncResponse;
makdharma80650002018-02-23 14:51:46 -0800167
168 /// SyncRequestThreadManager is an implementation of ThreadManager. This class
169 /// is responsible for polling for incoming RPCs and calling the RPC handlers.
170 /// This is only used in case of a Sync server (i.e a server exposing a sync
171 /// interface)
172 class SyncRequestThreadManager;
173
makdharma80650002018-02-23 14:51:46 -0800174 /// Register a generic service. This call does not take ownership of the
175 /// service. The service must exist for the lifetime of the Server instance.
176 void RegisterAsyncGenericService(AsyncGenericService* service) override;
177
Muxi Yan0e00c432018-01-26 15:39:32 -0800178 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
Muxi Yan0e00c432018-01-26 15:39:32 -0800187 ServerInitializer* initializer();
188
189 const int max_receive_message_size_;
190
191 /// The following completion queues are ONLY used in case of Sync API
192 /// i.e. if the server has any services with sync methods. The server uses
193 /// these completion queues to poll for new RPCs
194 std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
195 sync_server_cqs_;
196
197 /// List of \a ThreadManager instances (one for each cq in
198 /// the \a sync_server_cqs)
199 std::vector<std::unique_ptr<SyncRequestThreadManager>> sync_req_mgrs_;
200
makdharma80650002018-02-23 14:51:46 -0800201 // Server status
Muxi Yan0e00c432018-01-26 15:39:32 -0800202 std::mutex mu_;
makdharma838af3e2018-03-09 09:39:42 -0800203 bool started_;
Muxi Yan0e00c432018-01-26 15:39:32 -0800204 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
makdharma838af3e2018-03-09 09:39:42 -0800214 // Pointer to the wrapped grpc_server.
215 grpc_server* server_;
216
Muxi Yan0e00c432018-01-26 15:39:32 -0800217 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