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> |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 44 | #include <grpc++/impl/service_type.h> |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 45 | #include <grpc++/impl/sync.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include <grpc++/status.h> |
| 47 | |
| 48 | struct grpc_server; |
| 49 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | namespace grpc { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 51 | class AsynchronousService; |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 52 | class GenericServerContext; |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 53 | class AsyncGenericService; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | class RpcService; |
| 55 | class RpcServiceMethod; |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 56 | class ServerCredentials; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | class ThreadPoolInterface; |
| 58 | |
| 59 | // Currently it only supports handling rpcs in a single thread. |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 60 | class Server GRPC_FINAL : public GrpcLibrary, |
| 61 | private CallHook, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 62 | private AsynchronousService::DispatchImpl { |
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; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 76 | friend class ServerBuilder; |
| 77 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 78 | class SyncRequest; |
| 79 | class AsyncRequest; |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 80 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | // ServerBuilder use only |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 82 | Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned, |
| 83 | int max_message_size); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | // Register a service. This call does not take ownership of the service. |
| 85 | // The service must exist for the lifetime of the Server instance. |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 86 | bool RegisterService(RpcService* service); |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 87 | bool RegisterAsyncService(AsynchronousService* service); |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 88 | void RegisterAsyncGenericService(AsyncGenericService* service); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 89 | // Add a listening port. Can be called multiple times. |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 90 | int AddListeningPort(const grpc::string& addr, ServerCredentials* creds); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | // Start the server. |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 92 | bool Start(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 94 | void HandleQueueClosed(); |
| 95 | void RunRpc(); |
| 96 | void ScheduleCallback(); |
| 97 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 98 | void PerformOpsOnCall(CallOpBuffer* ops, Call* call) GRPC_OVERRIDE; |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 99 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 100 | // DispatchImpl |
| 101 | void RequestAsyncCall(void* registered_method, ServerContext* context, |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 102 | grpc::protobuf::Message* request, |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 103 | ServerAsyncStreamingInterface* stream, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 104 | CompletionQueue* call_cq, |
| 105 | ServerCompletionQueue* notification_cq, |
| 106 | void* tag) GRPC_OVERRIDE; |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 107 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 108 | void RequestAsyncGenericCall(GenericServerContext* context, |
| 109 | ServerAsyncStreamingInterface* stream, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 110 | CompletionQueue* cq, |
| 111 | ServerCompletionQueue* notification_cq, |
| 112 | void* tag); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 113 | |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 114 | const int max_message_size_; |
| 115 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 116 | // Completion queue. |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 117 | CompletionQueue cq_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | |
| 119 | // Sever status |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 120 | grpc::mutex mu_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | bool started_; |
| 122 | bool shutdown_; |
| 123 | // The number of threads which are running callbacks. |
| 124 | int num_running_cb_; |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 125 | grpc::condition_variable callback_cv_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 127 | std::list<SyncRequest>* sync_methods_; |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 128 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | // Pointer to the c grpc server. |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 130 | grpc_server* const server_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 131 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 132 | ThreadPoolInterface* thread_pool_; |
| 133 | // Whether the thread pool is created and owned by the server. |
| 134 | bool thread_pool_owned_; |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 135 | private: |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 136 | Server() : max_message_size_(-1), server_(NULL) { abort(); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | } // namespace grpc |
| 140 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 141 | #endif // GRPCXX_SERVER_H |