Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
| 40 | #include <grpc++/completion_queue.h> |
| 41 | #include <grpc++/config.h> |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 42 | #include <grpc++/impl/call.h> |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 43 | #include <grpc++/impl/grpc_library.h> |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 44 | #include <grpc++/impl/sync.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <grpc++/status.h> |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 46 | #include <grpc/compression.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
| 48 | struct grpc_server; |
| 49 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | namespace grpc { |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 51 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 52 | class AsynchronousService; |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 53 | class GenericServerContext; |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 54 | class AsyncGenericService; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | class RpcService; |
| 56 | class RpcServiceMethod; |
Craig Tiller | 81fafa8 | 2015-06-04 08:51:17 -0700 | [diff] [blame] | 57 | class ServerAsyncStreamingInterface; |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 58 | class ServerCredentials; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | class ThreadPoolInterface; |
| 60 | |
| 61 | // Currently it only supports handling rpcs in a single thread. |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 62 | class Server GRPC_FINAL : public GrpcLibrary, private CallHook { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | public: |
| 64 | ~Server(); |
| 65 | |
| 66 | // Shutdown the server, block until all rpc processing finishes. |
| 67 | void Shutdown(); |
| 68 | |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 69 | // Block waiting for all work to complete (the server must either |
| 70 | // be shutting down or some other thread must call Shutdown for this |
| 71 | // function to ever return) |
| 72 | void Wait(); |
| 73 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | private: |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 75 | friend class AsyncGenericService; |
Craig Tiller | 81fafa8 | 2015-06-04 08:51:17 -0700 | [diff] [blame] | 76 | friend class AsynchronousService; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | friend class ServerBuilder; |
| 78 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 79 | class SyncRequest; |
| 80 | class AsyncRequest; |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 81 | class ShutdownRequest; |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 82 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | // ServerBuilder use only |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 84 | Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned, |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 85 | int max_message_size, grpc_compression_options compression_options); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | // Register a service. This call does not take ownership of the service. |
| 87 | // The service must exist for the lifetime of the Server instance. |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 88 | bool RegisterService(const grpc::string *host, RpcService* service); |
| 89 | bool RegisterAsyncService(const grpc::string *host, AsynchronousService* service); |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 90 | void RegisterAsyncGenericService(AsyncGenericService* service); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | // Add a listening port. Can be called multiple times. |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 92 | int AddListeningPort(const grpc::string& addr, ServerCredentials* creds); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | // Start the server. |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 94 | bool Start(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 95 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 96 | void HandleQueueClosed(); |
| 97 | void RunRpc(); |
| 98 | void ScheduleCallback(); |
| 99 | |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 100 | void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) GRPC_OVERRIDE; |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 101 | |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 102 | class BaseAsyncRequest : public CompletionQueueTag { |
| 103 | public: |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 104 | BaseAsyncRequest(Server* server, ServerContext* context, |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 105 | ServerAsyncStreamingInterface* stream, |
| 106 | CompletionQueue* call_cq, void* tag); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 107 | virtual ~BaseAsyncRequest(); |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 108 | |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 109 | bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE; |
| 110 | |
| 111 | protected: |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 112 | Server* const server_; |
| 113 | ServerContext* const context_; |
| 114 | ServerAsyncStreamingInterface* const stream_; |
| 115 | CompletionQueue* const call_cq_; |
Craig Tiller | 5095581 | 2015-06-05 08:03:17 -0700 | [diff] [blame] | 116 | void* const tag_; |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 117 | grpc_call* call_; |
| 118 | grpc_metadata_array initial_metadata_array_; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | class RegisteredAsyncRequest : public BaseAsyncRequest { |
| 122 | public: |
| 123 | RegisteredAsyncRequest(Server* server, ServerContext* context, |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 124 | ServerAsyncStreamingInterface* stream, |
| 125 | CompletionQueue* call_cq, void* tag); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 126 | |
| 127 | // uses BaseAsyncRequest::FinalizeResult |
| 128 | |
| 129 | protected: |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 130 | void IssueRequest(void* registered_method, grpc_byte_buffer** payload, |
| 131 | ServerCompletionQueue* notification_cq); |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 134 | class NoPayloadAsyncRequest GRPC_FINAL : public RegisteredAsyncRequest { |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 135 | public: |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 136 | NoPayloadAsyncRequest(void* registered_method, Server* server, |
| 137 | ServerContext* context, |
| 138 | ServerAsyncStreamingInterface* stream, |
| 139 | CompletionQueue* call_cq, |
| 140 | ServerCompletionQueue* notification_cq, void* tag) |
| 141 | : RegisteredAsyncRequest(server, context, stream, call_cq, tag) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 142 | IssueRequest(registered_method, nullptr, notification_cq); |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 143 | } |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 144 | |
| 145 | // uses RegisteredAsyncRequest::FinalizeResult |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | template <class Message> |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 149 | class PayloadAsyncRequest GRPC_FINAL : public RegisteredAsyncRequest { |
| 150 | public: |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 151 | PayloadAsyncRequest(void* registered_method, Server* server, |
| 152 | ServerContext* context, |
| 153 | ServerAsyncStreamingInterface* stream, |
| 154 | CompletionQueue* call_cq, |
| 155 | ServerCompletionQueue* notification_cq, void* tag, |
| 156 | Message* request) |
| 157 | : RegisteredAsyncRequest(server, context, stream, call_cq, tag), |
| 158 | request_(request) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 159 | IssueRequest(registered_method, &payload_, notification_cq); |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 160 | } |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 161 | |
| 162 | bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE { |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 163 | bool serialization_status = |
| 164 | *status && payload_ && |
| 165 | SerializationTraits<Message>::Deserialize(payload_, request_, |
| 166 | server_->max_message_size_) |
Craig Tiller | 2622ddb | 2015-06-16 17:30:26 -0700 | [diff] [blame] | 167 | .ok(); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 168 | bool ret = RegisteredAsyncRequest::FinalizeResult(tag, status); |
| 169 | *status = serialization_status && *status; |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | grpc_byte_buffer* payload_; |
| 175 | Message* const request_; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 178 | class GenericAsyncRequest GRPC_FINAL : public BaseAsyncRequest { |
| 179 | public: |
| 180 | GenericAsyncRequest(Server* server, GenericServerContext* context, |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 181 | ServerAsyncStreamingInterface* stream, |
| 182 | CompletionQueue* call_cq, |
| 183 | ServerCompletionQueue* notification_cq, void* tag); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 184 | |
| 185 | bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE; |
| 186 | |
| 187 | private: |
| 188 | grpc_call_details call_details_; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
Craig Tiller | 81fafa8 | 2015-06-04 08:51:17 -0700 | [diff] [blame] | 191 | template <class Message> |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 192 | void RequestAsyncCall(void* registered_method, ServerContext* context, |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 193 | ServerAsyncStreamingInterface* stream, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 194 | CompletionQueue* call_cq, |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 195 | ServerCompletionQueue* notification_cq, void* tag, |
| 196 | Message* message) { |
| 197 | new PayloadAsyncRequest<Message>(registered_method, this, context, stream, |
| 198 | call_cq, notification_cq, tag, message); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 199 | } |
Craig Tiller | 81fafa8 | 2015-06-04 08:51:17 -0700 | [diff] [blame] | 200 | |
| 201 | void RequestAsyncCall(void* registered_method, ServerContext* context, |
| 202 | ServerAsyncStreamingInterface* stream, |
| 203 | CompletionQueue* call_cq, |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 204 | ServerCompletionQueue* notification_cq, void* tag) { |
| 205 | new NoPayloadAsyncRequest(registered_method, this, context, stream, call_cq, |
| 206 | notification_cq, tag); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 207 | } |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 208 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 209 | void RequestAsyncGenericCall(GenericServerContext* context, |
| 210 | ServerAsyncStreamingInterface* stream, |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 211 | CompletionQueue* call_cq, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 212 | ServerCompletionQueue* notification_cq, |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 213 | void* tag) { |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 214 | new GenericAsyncRequest(this, context, stream, call_cq, notification_cq, |
| 215 | tag); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 216 | } |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 217 | |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 218 | const int max_message_size_; |
| 219 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 220 | // Completion queue. |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 221 | CompletionQueue cq_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 222 | |
| 223 | // Sever status |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 224 | grpc::mutex mu_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 225 | bool started_; |
| 226 | bool shutdown_; |
| 227 | // The number of threads which are running callbacks. |
| 228 | int num_running_cb_; |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 229 | grpc::condition_variable callback_cv_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 230 | |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 231 | std::list<SyncRequest>* sync_methods_; |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 232 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 233 | // Pointer to the c grpc server. |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 234 | grpc_server* const server_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 235 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 236 | ThreadPoolInterface* thread_pool_; |
| 237 | // Whether the thread pool is created and owned by the server. |
| 238 | bool thread_pool_owned_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | } // namespace grpc |
| 242 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 243 | #endif // GRPCXX_SERVER_H |