Muxi Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 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> |
| 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 | |
| 39 | struct grpc_server; |
| 40 | |
| 41 | namespace grpc { |
| 42 | |
| 43 | class AsyncGenericService; |
| 44 | class HealthCheckServiceInterface; |
| 45 | class ServerContext; |
| 46 | class ServerInitializer; |
| 47 | |
| 48 | /// Represents a gRPC server. |
| 49 | /// |
| 50 | /// Use a \a grpc::ServerBuilder to create, configure, and start |
| 51 | /// \a Server instances. |
makdharma | 8065000 | 2018-02-23 14:51:46 -0800 | [diff] [blame] | 52 | class Server : public ServerInterface, private GrpcLibraryCodegen { |
Muxi Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 53 | 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 | |
makdharma | 757af97 | 2018-03-09 10:46:35 -0800 | [diff] [blame] | 90 | /// Returns a \em raw pointer to the underlying \a grpc_server instance. |
| 91 | /// EXPERIMENTAL: for internal/test use only |
Muxi Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 92 | 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 | |
makdharma | 5723744 | 2018-02-26 16:03:57 -0800 | [diff] [blame] | 102 | protected: |
makdharma | 8065000 | 2018-02-23 14:51:46 -0800 | [diff] [blame] | 103 | /// 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 Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 106 | |
makdharma | 8065000 | 2018-02-23 14:51:46 -0800 | [diff] [blame] | 107 | /// 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 Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 122 | |
| 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 Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 149 | /// 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 | |
makdharma | 838af3e | 2018-03-09 09:39:42 -0800 | [diff] [blame] | 157 | grpc_server* server() override { return server_; }; |
makdharma | 8065000 | 2018-02-23 14:51:46 -0800 | [diff] [blame] | 158 | |
| 159 | private: |
| 160 | friend class AsyncGenericService; |
| 161 | friend class ServerBuilder; |
| 162 | friend class ServerInitializer; |
| 163 | |
| 164 | class SyncRequest; |
| 165 | class AsyncRequest; |
| 166 | class ShutdownRequest; |
| 167 | |
| 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 | |
| 174 | class UnimplementedAsyncRequestContext; |
| 175 | class UnimplementedAsyncRequest; |
| 176 | class UnimplementedAsyncResponse; |
| 177 | |
| 178 | /// Register a generic service. This call does not take ownership of the |
| 179 | /// service. The service must exist for the lifetime of the Server instance. |
| 180 | void RegisterAsyncGenericService(AsyncGenericService* service) override; |
| 181 | |
Muxi Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 182 | void PerformOpsOnCall(internal::CallOpSetInterface* ops, |
| 183 | internal::Call* call) override; |
| 184 | |
| 185 | void ShutdownInternal(gpr_timespec deadline) override; |
| 186 | |
| 187 | int max_receive_message_size() const override { |
| 188 | return max_receive_message_size_; |
| 189 | }; |
| 190 | |
Muxi Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 191 | ServerInitializer* initializer(); |
| 192 | |
| 193 | const int max_receive_message_size_; |
| 194 | |
| 195 | /// The following completion queues are ONLY used in case of Sync API |
| 196 | /// i.e. if the server has any services with sync methods. The server uses |
| 197 | /// these completion queues to poll for new RPCs |
| 198 | std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>> |
| 199 | sync_server_cqs_; |
| 200 | |
| 201 | /// List of \a ThreadManager instances (one for each cq in |
| 202 | /// the \a sync_server_cqs) |
| 203 | std::vector<std::unique_ptr<SyncRequestThreadManager>> sync_req_mgrs_; |
| 204 | |
makdharma | 8065000 | 2018-02-23 14:51:46 -0800 | [diff] [blame] | 205 | // Server status |
Muxi Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 206 | std::mutex mu_; |
makdharma | 838af3e | 2018-03-09 09:39:42 -0800 | [diff] [blame] | 207 | bool started_; |
Muxi Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 208 | bool shutdown_; |
| 209 | bool shutdown_notified_; // Was notify called on the shutdown_cv_ |
| 210 | |
| 211 | std::condition_variable shutdown_cv_; |
| 212 | |
| 213 | std::shared_ptr<GlobalCallbacks> global_callbacks_; |
| 214 | |
| 215 | std::vector<grpc::string> services_; |
| 216 | bool has_generic_service_; |
| 217 | |
makdharma | 838af3e | 2018-03-09 09:39:42 -0800 | [diff] [blame] | 218 | // Pointer to the wrapped grpc_server. |
| 219 | grpc_server* server_; |
| 220 | |
Muxi Yan | 0e00c43 | 2018-01-26 15:39:32 -0800 | [diff] [blame] | 221 | std::unique_ptr<ServerInitializer> server_initializer_; |
| 222 | |
| 223 | std::unique_ptr<HealthCheckServiceInterface> health_check_service_; |
| 224 | bool health_check_service_disabled_; |
| 225 | }; |
| 226 | |
| 227 | } // namespace grpc |
| 228 | |
| 229 | #endif // GRPCPP_SERVER_H |