Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPCXX_SERVER_H |
| 35 | #define GRPCXX_SERVER_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 37 | #include <list> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <memory> |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 39 | #include <vector> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | |
| 41 | #include <grpc++/completion_queue.h> |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 42 | #include <grpc++/impl/call.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 43 | #include <grpc++/impl/codegen/grpc_library.h> |
| 44 | #include <grpc++/impl/codegen/server_interface.h> |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 45 | #include <grpc++/impl/rpc_service_method.h> |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 46 | #include <grpc++/impl/sync.h> |
Julien Boeuf | 0d47192 | 2015-08-30 22:18:50 -0700 | [diff] [blame] | 47 | #include <grpc++/security/server_credentials.h> |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 48 | #include <grpc++/support/channel_arguments.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 49 | #include <grpc++/support/config.h> |
| 50 | #include <grpc++/support/status.h> |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 51 | #include <grpc/compression.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | |
Sree Kuchibhotla | bb5519f | 2016-07-19 11:00:39 -0700 | [diff] [blame^] | 53 | #include "src/cpp/rpcmanager/grpc_rpc_manager.h" |
| 54 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | struct grpc_server; |
| 56 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | namespace grpc { |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 58 | |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 59 | class GenericServerContext; |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 60 | class AsyncGenericService; |
Craig Tiller | 81fafa8 | 2015-06-04 08:51:17 -0700 | [diff] [blame] | 61 | class ServerAsyncStreamingInterface; |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 62 | class ServerContext; |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 63 | class ServerInitializer; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | class ThreadPoolInterface; |
| 65 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 66 | /// Models a gRPC server. |
| 67 | /// |
| 68 | /// Servers are configured and started via \a grpc::ServerBuilder. |
Sree Kuchibhotla | bb5519f | 2016-07-19 11:00:39 -0700 | [diff] [blame^] | 69 | class Server GRPC_FINAL : public ServerInterface, |
| 70 | private GrpcLibraryCodegen, |
| 71 | public GrpcRpcManager { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | public: |
| 73 | ~Server(); |
| 74 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 75 | /// Block waiting for all work to complete. |
| 76 | /// |
| 77 | /// \warning The server must be either shutting down or some other thread must |
| 78 | /// call \a Shutdown for this function to ever return. |
David Garcia Quintas | 1f4e72c | 2016-01-19 14:45:32 -0800 | [diff] [blame] | 79 | void Wait() GRPC_OVERRIDE; |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 80 | |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 81 | /// Global Callbacks |
| 82 | /// |
| 83 | /// Can be set exactly once per application to install hooks whenever |
| 84 | /// a server event occurs |
| 85 | class GlobalCallbacks { |
| 86 | public: |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 87 | virtual ~GlobalCallbacks() {} |
yang-g | eb62c94 | 2016-02-17 15:37:25 -0800 | [diff] [blame] | 88 | /// Called before server is created. |
| 89 | virtual void UpdateArguments(ChannelArguments* args) {} |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 90 | /// Called before application callback for each synchronous server request |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 91 | virtual void PreSynchronousRequest(ServerContext* context) = 0; |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 92 | /// Called after application callback for each synchronous server request |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 93 | virtual void PostSynchronousRequest(ServerContext* context) = 0; |
| 94 | }; |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 95 | /// Set the global callback object. Can only be called once. Does not take |
| 96 | /// ownership of callbacks, and expects the pointed to object to be alive |
| 97 | /// until all server objects in the process have been destroyed. |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 98 | static void SetGlobalCallbacks(GlobalCallbacks* callbacks); |
| 99 | |
Adam Michalik | b97e2d1 | 2016-06-02 12:12:55 -0700 | [diff] [blame] | 100 | // Returns a \em raw pointer to the underlying grpc_server instance. |
| 101 | grpc_server* c_server(); |
| 102 | |
| 103 | // Returns a \em raw pointer to the underlying CompletionQueue. |
| 104 | CompletionQueue* completion_queue(); |
| 105 | |
Sree Kuchibhotla | bb5519f | 2016-07-19 11:00:39 -0700 | [diff] [blame^] | 106 | /// GRPC RPC Manager functions |
| 107 | void PollForWork(bool& is_work_found, void** tag) GRPC_OVERRIDE; |
| 108 | void DoWork(void* tag) GRPC_OVERRIDE; |
| 109 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | private: |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 111 | friend class AsyncGenericService; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 112 | friend class ServerBuilder; |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 113 | friend class ServerInitializer; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 115 | class SyncRequest; |
| 116 | class AsyncRequest; |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 117 | class ShutdownRequest; |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 118 | |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 119 | class UnimplementedAsyncRequestContext; |
| 120 | class UnimplementedAsyncRequest; |
| 121 | class UnimplementedAsyncResponse; |
| 122 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 123 | /// Server constructors. To be used by \a ServerBuilder only. |
| 124 | /// |
| 125 | /// \param thread_pool The threadpool instance to use for call processing. |
| 126 | /// \param thread_pool_owned Does the server own the \a thread_pool instance? |
| 127 | /// \param max_message_size Maximum message length that the channel can |
| 128 | /// receive. |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 129 | Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned, |
yang-g | eb62c94 | 2016-02-17 15:37:25 -0800 | [diff] [blame] | 130 | int max_message_size, ChannelArguments* args); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 131 | |
| 132 | /// Register a service. This call does not take ownership of the service. |
| 133 | /// The service must exist for the lifetime of the Server instance. |
David Garcia Quintas | f3ddb7c | 2016-01-20 16:02:22 -0800 | [diff] [blame] | 134 | bool RegisterService(const grpc::string* host, |
| 135 | Service* service) GRPC_OVERRIDE; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 136 | |
| 137 | /// Register a generic service. This call does not take ownership of the |
| 138 | /// service. The service must exist for the lifetime of the Server instance. |
David Garcia Quintas | 1f4e72c | 2016-01-19 14:45:32 -0800 | [diff] [blame] | 139 | void RegisterAsyncGenericService(AsyncGenericService* service) GRPC_OVERRIDE; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 140 | |
| 141 | /// Tries to bind \a server to the given \a addr. |
| 142 | /// |
| 143 | /// It can be invoked multiple times. |
| 144 | /// |
| 145 | /// \param addr The address to try to bind to the server (eg, localhost:1234, |
| 146 | /// 192.168.1.1:31416, [::1]:27182, etc.). |
| 147 | /// \params creds The credentials associated with the server. |
| 148 | /// |
| 149 | /// \return bound port number on sucess, 0 on failure. |
| 150 | /// |
| 151 | /// \warning It's an error to call this method on an already started server. |
David Garcia Quintas | f3ddb7c | 2016-01-20 16:02:22 -0800 | [diff] [blame] | 152 | int AddListeningPort(const grpc::string& addr, |
| 153 | ServerCredentials* creds) GRPC_OVERRIDE; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 154 | |
| 155 | /// Start the server. |
| 156 | /// |
| 157 | /// \param cqs Completion queues for handling asynchronous services. The |
| 158 | /// caller is required to keep all completion queues live until the server is |
| 159 | /// destroyed. |
| 160 | /// \param num_cqs How many completion queues does \a cqs hold. |
| 161 | /// |
| 162 | /// \return true on a successful shutdown. |
David Garcia Quintas | 1f4e72c | 2016-01-19 14:45:32 -0800 | [diff] [blame] | 163 | bool Start(ServerCompletionQueue** cqs, size_t num_cqs) GRPC_OVERRIDE; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 164 | |
| 165 | /// Process one or more incoming calls. |
David Garcia Quintas | 1f4e72c | 2016-01-19 14:45:32 -0800 | [diff] [blame] | 166 | void RunRpc() GRPC_OVERRIDE; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 167 | |
| 168 | /// Schedule \a RunRpc to run in the threadpool. |
David Garcia Quintas | 1f4e72c | 2016-01-19 14:45:32 -0800 | [diff] [blame] | 169 | void ScheduleCallback() GRPC_OVERRIDE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 171 | void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) GRPC_OVERRIDE; |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 172 | |
David Garcia Quintas | 1f4e72c | 2016-01-19 14:45:32 -0800 | [diff] [blame] | 173 | void ShutdownInternal(gpr_timespec deadline) GRPC_OVERRIDE; |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 174 | |
David Garcia Quintas | 1f4e72c | 2016-01-19 14:45:32 -0800 | [diff] [blame] | 175 | int max_message_size() const GRPC_OVERRIDE { return max_message_size_; }; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 176 | |
David Garcia Quintas | 1f4e72c | 2016-01-19 14:45:32 -0800 | [diff] [blame] | 177 | grpc_server* server() GRPC_OVERRIDE { return server_; }; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 178 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 179 | ServerInitializer* initializer(); |
| 180 | |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 181 | const int max_message_size_; |
| 182 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 183 | // Completion queue. |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 184 | CompletionQueue cq_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 185 | |
| 186 | // Sever status |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 187 | grpc::mutex mu_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 188 | bool started_; |
| 189 | bool shutdown_; |
yang-g | 6ec11f2 | 2016-07-14 14:53:35 -0700 | [diff] [blame] | 190 | bool shutdown_notified_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 191 | // The number of threads which are running callbacks. |
| 192 | int num_running_cb_; |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 193 | grpc::condition_variable callback_cv_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 194 | |
yang-g | e89dc6c | 2016-07-11 15:48:01 -0700 | [diff] [blame] | 195 | grpc::condition_variable shutdown_cv_; |
| 196 | |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 197 | std::shared_ptr<GlobalCallbacks> global_callbacks_; |
| 198 | |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 199 | std::list<SyncRequest>* sync_methods_; |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 200 | std::vector<grpc::string> services_; |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 201 | std::unique_ptr<RpcServiceMethod> unknown_method_; |
| 202 | bool has_generic_service_; |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 203 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 204 | // Pointer to the c grpc server. |
yang-g | eb62c94 | 2016-02-17 15:37:25 -0800 | [diff] [blame] | 205 | grpc_server* server_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 206 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 207 | ThreadPoolInterface* thread_pool_; |
| 208 | // Whether the thread pool is created and owned by the server. |
| 209 | bool thread_pool_owned_; |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 210 | |
| 211 | std::unique_ptr<ServerInitializer> server_initializer_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | } // namespace grpc |
| 215 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 216 | #endif // GRPCXX_SERVER_H |