OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 34 #ifndef GRPCXX_SERVER_H |
| 35 #define GRPCXX_SERVER_H |
| 36 |
| 37 #include <list> |
| 38 #include <memory> |
| 39 |
| 40 #include <grpc++/completion_queue.h> |
| 41 #include <grpc++/impl/call.h> |
| 42 #include <grpc++/impl/codegen/grpc_library.h> |
| 43 #include <grpc++/impl/codegen/server_interface.h> |
| 44 #include <grpc++/impl/rpc_service_method.h> |
| 45 #include <grpc++/impl/sync.h> |
| 46 #include <grpc++/security/server_credentials.h> |
| 47 #include <grpc++/support/channel_arguments.h> |
| 48 #include <grpc++/support/config.h> |
| 49 #include <grpc++/support/status.h> |
| 50 #include <grpc/compression.h> |
| 51 |
| 52 struct grpc_server; |
| 53 |
| 54 namespace grpc { |
| 55 |
| 56 class GenericServerContext; |
| 57 class AsyncGenericService; |
| 58 class ServerAsyncStreamingInterface; |
| 59 class ServerContext; |
| 60 class ThreadPoolInterface; |
| 61 |
| 62 /// Models a gRPC server. |
| 63 /// |
| 64 /// Servers are configured and started via \a grpc::ServerBuilder. |
| 65 class Server GRPC_FINAL : public ServerInterface, private GrpcLibrary { |
| 66 public: |
| 67 ~Server(); |
| 68 |
| 69 /// Block waiting for all work to complete. |
| 70 /// |
| 71 /// \warning The server must be either shutting down or some other thread must |
| 72 /// call \a Shutdown for this function to ever return. |
| 73 void Wait() GRPC_OVERRIDE; |
| 74 |
| 75 /// Global Callbacks |
| 76 /// |
| 77 /// Can be set exactly once per application to install hooks whenever |
| 78 /// a server event occurs |
| 79 class GlobalCallbacks { |
| 80 public: |
| 81 virtual ~GlobalCallbacks() {} |
| 82 /// Called before server is created. |
| 83 virtual void UpdateArguments(ChannelArguments* args) {} |
| 84 /// Called before application callback for each synchronous server request |
| 85 virtual void PreSynchronousRequest(ServerContext* context) = 0; |
| 86 /// Called after application callback for each synchronous server request |
| 87 virtual void PostSynchronousRequest(ServerContext* context) = 0; |
| 88 }; |
| 89 /// Set the global callback object. Can only be called once. Does not take |
| 90 /// ownership of callbacks, and expects the pointed to object to be alive |
| 91 /// until all server objects in the process have been destroyed. |
| 92 static void SetGlobalCallbacks(GlobalCallbacks* callbacks); |
| 93 |
| 94 private: |
| 95 friend class AsyncGenericService; |
| 96 friend class ServerBuilder; |
| 97 |
| 98 class SyncRequest; |
| 99 class AsyncRequest; |
| 100 class ShutdownRequest; |
| 101 |
| 102 class UnimplementedAsyncRequestContext; |
| 103 class UnimplementedAsyncRequest; |
| 104 class UnimplementedAsyncResponse; |
| 105 |
| 106 /// Server constructors. To be used by \a ServerBuilder only. |
| 107 /// |
| 108 /// \param thread_pool The threadpool instance to use for call processing. |
| 109 /// \param thread_pool_owned Does the server own the \a thread_pool instance? |
| 110 /// \param max_message_size Maximum message length that the channel can |
| 111 /// receive. |
| 112 Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned, |
| 113 int max_message_size, ChannelArguments* args); |
| 114 |
| 115 /// Register a service. This call does not take ownership of the service. |
| 116 /// The service must exist for the lifetime of the Server instance. |
| 117 bool RegisterService(const grpc::string* host, |
| 118 Service* service) GRPC_OVERRIDE; |
| 119 |
| 120 /// Register a generic service. This call does not take ownership of the |
| 121 /// service. The service must exist for the lifetime of the Server instance. |
| 122 void RegisterAsyncGenericService(AsyncGenericService* service) GRPC_OVERRIDE; |
| 123 |
| 124 /// Tries to bind \a server to the given \a addr. |
| 125 /// |
| 126 /// It can be invoked multiple times. |
| 127 /// |
| 128 /// \param addr The address to try to bind to the server (eg, localhost:1234, |
| 129 /// 192.168.1.1:31416, [::1]:27182, etc.). |
| 130 /// \params creds The credentials associated with the server. |
| 131 /// |
| 132 /// \return bound port number on sucess, 0 on failure. |
| 133 /// |
| 134 /// \warning It's an error to call this method on an already started server. |
| 135 int AddListeningPort(const grpc::string& addr, |
| 136 ServerCredentials* creds) GRPC_OVERRIDE; |
| 137 |
| 138 /// Start the server. |
| 139 /// |
| 140 /// \param cqs Completion queues for handling asynchronous services. The |
| 141 /// caller is required to keep all completion queues live until the server is |
| 142 /// destroyed. |
| 143 /// \param num_cqs How many completion queues does \a cqs hold. |
| 144 /// |
| 145 /// \return true on a successful shutdown. |
| 146 bool Start(ServerCompletionQueue** cqs, size_t num_cqs) GRPC_OVERRIDE; |
| 147 |
| 148 /// Process one or more incoming calls. |
| 149 void RunRpc() GRPC_OVERRIDE; |
| 150 |
| 151 /// Schedule \a RunRpc to run in the threadpool. |
| 152 void ScheduleCallback() GRPC_OVERRIDE; |
| 153 |
| 154 void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) GRPC_OVERRIDE; |
| 155 |
| 156 void ShutdownInternal(gpr_timespec deadline) GRPC_OVERRIDE; |
| 157 |
| 158 int max_message_size() const GRPC_OVERRIDE { return max_message_size_; }; |
| 159 |
| 160 grpc_server* server() GRPC_OVERRIDE { return server_; }; |
| 161 |
| 162 const int max_message_size_; |
| 163 |
| 164 // Completion queue. |
| 165 CompletionQueue cq_; |
| 166 |
| 167 // Sever status |
| 168 grpc::mutex mu_; |
| 169 bool started_; |
| 170 bool shutdown_; |
| 171 // The number of threads which are running callbacks. |
| 172 int num_running_cb_; |
| 173 grpc::condition_variable callback_cv_; |
| 174 |
| 175 std::shared_ptr<GlobalCallbacks> global_callbacks_; |
| 176 |
| 177 std::list<SyncRequest>* sync_methods_; |
| 178 std::unique_ptr<RpcServiceMethod> unknown_method_; |
| 179 bool has_generic_service_; |
| 180 |
| 181 // Pointer to the c grpc server. |
| 182 grpc_server* server_; |
| 183 |
| 184 ThreadPoolInterface* thread_pool_; |
| 185 // Whether the thread pool is created and owned by the server. |
| 186 bool thread_pool_owned_; |
| 187 }; |
| 188 |
| 189 } // namespace grpc |
| 190 |
| 191 #endif // GRPCXX_SERVER_H |
OLD | NEW |