blob: 489937712ec16db3c3d234c573870f0fcd4e4065 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPCXX_SERVER_H
35#define GRPCXX_SERVER_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
Vijay Pai320ed132016-11-01 17:16:55 -070037#include <condition_variable>
Craig Tillercbd04852015-02-10 17:39:54 -080038#include <list>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <memory>
Vijay Pai320ed132016-11-01 17:16:55 -070040#include <mutex>
Yuchen Zenga42ec212016-04-29 13:03:06 -070041#include <vector>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042
43#include <grpc++/completion_queue.h>
Craig Tillerbb5227f2015-02-11 13:34:48 -080044#include <grpc++/impl/call.h>
David Garcia Quintase1300de2016-01-27 18:41:26 -080045#include <grpc++/impl/codegen/grpc_library.h>
46#include <grpc++/impl/codegen/server_interface.h>
Craig Tiller15f383c2016-01-07 12:45:32 -080047#include <grpc++/impl/rpc_service_method.h>
Julien Boeuf0d471922015-08-30 22:18:50 -070048#include <grpc++/security/server_credentials.h>
yang-ga23f17b2015-11-25 10:21:05 -080049#include <grpc++/support/channel_arguments.h>
yang-g9e2f90c2015-08-21 15:35:03 -070050#include <grpc++/support/config.h>
51#include <grpc++/support/status.h>
yang-ga23f17b2015-11-25 10:21:05 -080052#include <grpc/compression.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
54struct grpc_server;
55
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056namespace grpc {
Craig Tiller50a7a682015-06-04 12:53:40 -070057
Yang Gao49996492015-03-12 16:40:19 -070058class AsyncGenericService;
yang-gc9d29542016-12-06 14:20:17 -080059class HealthCheckServiceInterface;
Craig Tiller7221d992015-11-24 06:59:33 -080060class ServerContext;
Yuchen Zenga42ec212016-04-29 13:03:06 -070061class ServerInitializer;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062
Craig Tillerd6599a32015-09-03 09:37:02 -070063/// Models a gRPC server.
64///
65/// Servers are configured and started via \a grpc::ServerBuilder.
Vijay Paic0b2acb2016-11-01 16:31:56 -070066class Server final : public ServerInterface, private GrpcLibraryCodegen {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067 public:
68 ~Server();
69
Craig Tillerd6599a32015-09-03 09:37:02 -070070 /// Block waiting for all work to complete.
71 ///
72 /// \warning The server must be either shutting down or some other thread must
73 /// call \a Shutdown for this function to ever return.
Vijay Paic0b2acb2016-11-01 16:31:56 -070074 void Wait() override;
Craig Tiller6e57b9e2015-02-24 15:46:22 -080075
Craig Tiller7221d992015-11-24 06:59:33 -080076 /// Global Callbacks
77 ///
78 /// Can be set exactly once per application to install hooks whenever
79 /// a server event occurs
80 class GlobalCallbacks {
81 public:
Craig Tiller8352b9e2015-12-02 11:43:40 -080082 virtual ~GlobalCallbacks() {}
yang-geb62c942016-02-17 15:37:25 -080083 /// Called before server is created.
84 virtual void UpdateArguments(ChannelArguments* args) {}
Craig Tiller8352b9e2015-12-02 11:43:40 -080085 /// Called before application callback for each synchronous server request
Craig Tiller7221d992015-11-24 06:59:33 -080086 virtual void PreSynchronousRequest(ServerContext* context) = 0;
Craig Tiller8352b9e2015-12-02 11:43:40 -080087 /// Called after application callback for each synchronous server request
Craig Tiller7221d992015-11-24 06:59:33 -080088 virtual void PostSynchronousRequest(ServerContext* context) = 0;
yang-gf2fe4f72017-02-07 12:47:22 -080089 /// Called before server is started.
90 virtual void PreServerStart(Server* server) {}
yang-gd510fcf2017-03-31 11:00:02 -070091 /// Called after a server port is added.
92 virtual void AddPort(Server* server, int port) {}
Craig Tiller7221d992015-11-24 06:59:33 -080093 };
Craig Tiller8352b9e2015-12-02 11:43:40 -080094 /// Set the global callback object. Can only be called once. Does not take
95 /// ownership of callbacks, and expects the pointed to object to be alive
96 /// until all server objects in the process have been destroyed.
Craig Tiller7221d992015-11-24 06:59:33 -080097 static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
98
Adam Michalikb97e2d12016-06-02 12:12:55 -070099 // Returns a \em raw pointer to the underlying grpc_server instance.
100 grpc_server* c_server();
101
yang-g8d668d82016-12-01 15:09:28 -0800102 /// Returns the health check service.
103 HealthCheckServiceInterface* GetHealthCheckService() const {
yang-gc9d29542016-12-06 14:20:17 -0800104 return health_check_service_.get();
yang-g8d668d82016-12-01 15:09:28 -0800105 }
106
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800107 private:
Yang Gao49996492015-03-12 16:40:19 -0700108 friend class AsyncGenericService;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109 friend class ServerBuilder;
Yuchen Zenga42ec212016-04-29 13:03:06 -0700110 friend class ServerInitializer;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800112 class SyncRequest;
113 class AsyncRequest;
Craig Tillerbce999f2015-05-27 09:55:51 -0700114 class ShutdownRequest;
Craig Tillercbd04852015-02-10 17:39:54 -0800115
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700116 /// SyncRequestThreadManager is an implementation of ThreadManager. This class
117 /// is responsible for polling for incoming RPCs and calling the RPC handlers.
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700118 /// This is only used in case of a Sync server (i.e a server exposing a sync
119 /// interface)
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700120 class SyncRequestThreadManager;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700121
David Garcia Quintas44f32492016-01-14 18:00:04 -0800122 class UnimplementedAsyncRequestContext;
123 class UnimplementedAsyncRequest;
124 class UnimplementedAsyncResponse;
125
Craig Tillerd6599a32015-09-03 09:37:02 -0700126 /// Server constructors. To be used by \a ServerBuilder only.
127 ///
Craig Tillerd6599a32015-09-03 09:37:02 -0700128 /// \param max_message_size Maximum message length that the channel can
129 /// receive.
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700130 ///
131 /// \param args The channel args
132 ///
Sree Kuchibhotlae4eb51f2016-10-18 11:51:28 -0700133 /// \param sync_server_cqs The completion queues to use if the server is a
134 /// synchronous server (or a hybrid server). The server polls for new RPCs on
135 /// these queues
136 ///
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700137 /// \param min_pollers The minimum number of polling threads per server
138 /// completion queue (in param sync_server_cqs) to use for listening to
139 /// incoming requests (used only in case of sync server)
140 ///
141 /// \param max_pollers The maximum number of polling threads per server
142 /// completion queue (in param sync_server_cqs) to use for listening to
143 /// incoming requests (used only in case of sync server)
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700144 ///
145 /// \param sync_cq_timeout_msec The timeout to use when calling AsyncNext() on
146 /// server completion queues passed via sync_server_cqs param.
Sree Kuchibhotlae4eb51f2016-10-18 11:51:28 -0700147 Server(int max_message_size, ChannelArguments* args,
148 std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700149 sync_server_cqs,
Sree Kuchibhotlae4eb51f2016-10-18 11:51:28 -0700150 int min_pollers, int max_pollers, int sync_cq_timeout_msec);
Craig Tillerd6599a32015-09-03 09:37:02 -0700151
152 /// Register a service. This call does not take ownership of the service.
153 /// The service must exist for the lifetime of the Server instance.
Vijay Pai713c7b82016-11-01 16:33:18 -0700154 bool RegisterService(const grpc::string* host, Service* service) override;
Craig Tillerd6599a32015-09-03 09:37:02 -0700155
156 /// Register a generic service. This call does not take ownership of the
157 /// service. The service must exist for the lifetime of the Server instance.
Vijay Paic0b2acb2016-11-01 16:31:56 -0700158 void RegisterAsyncGenericService(AsyncGenericService* service) override;
Craig Tillerd6599a32015-09-03 09:37:02 -0700159
160 /// Tries to bind \a server to the given \a addr.
161 ///
162 /// It can be invoked multiple times.
163 ///
164 /// \param addr The address to try to bind to the server (eg, localhost:1234,
165 /// 192.168.1.1:31416, [::1]:27182, etc.).
166 /// \params creds The credentials associated with the server.
167 ///
168 /// \return bound port number on sucess, 0 on failure.
169 ///
170 /// \warning It's an error to call this method on an already started server.
David Garcia Quintasf3ddb7c2016-01-20 16:02:22 -0800171 int AddListeningPort(const grpc::string& addr,
Vijay Paic0b2acb2016-11-01 16:31:56 -0700172 ServerCredentials* creds) override;
Craig Tillerd6599a32015-09-03 09:37:02 -0700173
174 /// Start the server.
175 ///
176 /// \param cqs Completion queues for handling asynchronous services. The
177 /// caller is required to keep all completion queues live until the server is
178 /// destroyed.
179 /// \param num_cqs How many completion queues does \a cqs hold.
180 ///
181 /// \return true on a successful shutdown.
Vijay Paic0b2acb2016-11-01 16:31:56 -0700182 bool Start(ServerCompletionQueue** cqs, size_t num_cqs) override;
Craig Tillerd6599a32015-09-03 09:37:02 -0700183
Vijay Paic0b2acb2016-11-01 16:31:56 -0700184 void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) override;
Craig Tillerbb5227f2015-02-11 13:34:48 -0800185
Vijay Paic0b2acb2016-11-01 16:31:56 -0700186 void ShutdownInternal(gpr_timespec deadline) override;
Craig Tillere50e5cb2015-08-18 12:44:57 -0700187
Vijay Paic0b2acb2016-11-01 16:31:56 -0700188 int max_receive_message_size() const override {
Mark D. Roth69803622016-09-06 08:15:36 -0700189 return max_receive_message_size_;
190 };
Craig Tiller50a7a682015-06-04 12:53:40 -0700191
Vijay Paic0b2acb2016-11-01 16:31:56 -0700192 grpc_server* server() override { return server_; };
Yang Gao1c402332015-03-05 16:39:25 -0800193
Yuchen Zenga42ec212016-04-29 13:03:06 -0700194 ServerInitializer* initializer();
195
Mark D. Roth69803622016-09-06 08:15:36 -0700196 const int max_receive_message_size_;
Yang Gao3921c562015-04-30 16:07:06 -0700197
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700198 /// The following completion queues are ONLY used in case of Sync API i.e if
199 /// the server has any services with sync methods. The server uses these
200 /// completion queues to poll for new RPCs
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700201 std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
202 sync_server_cqs_;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700203
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700204 /// List of ThreadManager instances (one for each cq in the sync_server_cqs)
205 std::vector<std::unique_ptr<SyncRequestThreadManager>> sync_req_mgrs_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206
207 // Sever status
Vijay Pai320ed132016-11-01 17:16:55 -0700208 std::mutex mu_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 bool started_;
210 bool shutdown_;
Sree Kuchibhotla26ef5742016-10-24 12:39:04 -0700211 bool shutdown_notified_; // Was notify called on the shutdown_cv_
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700212
Vijay Pai320ed132016-11-01 17:16:55 -0700213 std::condition_variable shutdown_cv_;
yang-ge89dc6c2016-07-11 15:48:01 -0700214
Craig Tiller64616492016-01-26 14:16:20 -0800215 std::shared_ptr<GlobalCallbacks> global_callbacks_;
216
Yuchen Zenga42ec212016-04-29 13:03:06 -0700217 std::vector<grpc::string> services_;
yang-g9b7757d2015-08-13 11:15:53 -0700218 bool has_generic_service_;
Craig Tillercbd04852015-02-10 17:39:54 -0800219
Sree Kuchibhotla385c9b22016-10-18 16:26:38 -0700220 // Pointer to the wrapped grpc_server.
yang-geb62c942016-02-17 15:37:25 -0800221 grpc_server* server_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800222
Yuchen Zenga42ec212016-04-29 13:03:06 -0700223 std::unique_ptr<ServerInitializer> server_initializer_;
yang-g8d668d82016-12-01 15:09:28 -0800224
225 std::unique_ptr<HealthCheckServiceInterface> health_check_service_;
yang-gad327642016-12-12 14:32:09 -0800226 bool health_check_service_disabled_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800227};
228
229} // namespace grpc
230
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100231#endif // GRPCXX_SERVER_H