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 | |
| 34 | #include <grpc++/server.h> |
| 35 | #include <utility> |
| 36 | |
| 37 | #include <grpc/grpc.h> |
Yang Gao | 2a3c96a | 2015-03-11 23:32:40 -0700 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc++/completion_queue.h> |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 41 | #include <grpc++/async_generic_service.h> |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 42 | #include <grpc++/impl/rpc_service_method.h> |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 43 | #include <grpc++/impl/service_type.h> |
Craig Tiller | b470169 | 2015-02-09 16:12:00 -0800 | [diff] [blame] | 44 | #include <grpc++/server_context.h> |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 45 | #include <grpc++/server_credentials.h> |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 46 | #include <grpc++/thread_pool_interface.h> |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 47 | #include <grpc++/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
Vijay Pai | 9ffbd0c | 2015-04-15 01:02:50 -0700 | [diff] [blame] | 49 | #include "src/core/profiling/timers.h" |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 50 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | namespace grpc { |
| 52 | |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 53 | class Server::ShutdownRequest GRPC_FINAL : public CompletionQueueTag { |
| 54 | public: |
| 55 | bool FinalizeResult(void** tag, bool* status) { |
| 56 | delete this; |
| 57 | return false; |
| 58 | } |
| 59 | }; |
| 60 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 61 | class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 62 | public: |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 63 | SyncRequest(RpcServiceMethod* method, void* tag) |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 64 | : method_(method), |
| 65 | tag_(tag), |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 66 | in_flight_(false), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 67 | has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC || |
| 68 | method->method_type() == |
| 69 | RpcMethod::SERVER_STREAMING), |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 70 | cq_(nullptr) { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 71 | grpc_metadata_array_init(&request_metadata_); |
| 72 | } |
| 73 | |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 74 | ~SyncRequest() { grpc_metadata_array_destroy(&request_metadata_); } |
Yang Gao | 7b49a77 | 2015-05-28 14:07:54 -0700 | [diff] [blame] | 75 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 76 | static SyncRequest* Wait(CompletionQueue* cq, bool* ok) { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 77 | void* tag = nullptr; |
Craig Tiller | 504bd33 | 2015-02-11 20:34:33 -0800 | [diff] [blame] | 78 | *ok = false; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 79 | if (!cq->Next(&tag, ok)) { |
| 80 | return nullptr; |
| 81 | } |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 82 | auto* mrd = static_cast<SyncRequest*>(tag); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 83 | GPR_ASSERT(mrd->in_flight_); |
| 84 | return mrd; |
| 85 | } |
| 86 | |
Craig Tiller | 3de4b47 | 2015-06-22 10:51:35 -0700 | [diff] [blame] | 87 | void SetupRequest() { cq_ = grpc_completion_queue_create(); } |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 88 | |
| 89 | void TeardownRequest() { |
| 90 | grpc_completion_queue_destroy(cq_); |
| 91 | cq_ = nullptr; |
| 92 | } |
| 93 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 94 | void Request(grpc_server* server, grpc_completion_queue* notify_cq) { |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 95 | GPR_ASSERT(cq_ && !in_flight_); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 96 | in_flight_ = true; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 97 | GPR_ASSERT(GRPC_CALL_OK == |
| 98 | grpc_server_request_registered_call( |
| 99 | server, tag_, &call_, &deadline_, &request_metadata_, |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 100 | has_request_payload_ ? &request_payload_ : nullptr, cq_, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 101 | notify_cq, this)); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 104 | bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 105 | if (!*status) { |
| 106 | grpc_completion_queue_destroy(cq_); |
| 107 | } |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 108 | return true; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 109 | } |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 110 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 111 | class CallData GRPC_FINAL { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 112 | public: |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 113 | explicit CallData(Server* server, SyncRequest* mrd) |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 114 | : cq_(mrd->cq_), |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 115 | call_(mrd->call_, server, &cq_, server->max_message_size_), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 116 | ctx_(mrd->deadline_, mrd->request_metadata_.metadata, |
| 117 | mrd->request_metadata_.count), |
| 118 | has_request_payload_(mrd->has_request_payload_), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 119 | request_payload_(mrd->request_payload_), |
| 120 | method_(mrd->method_) { |
yang-g | 85c04f9 | 2015-07-07 17:47:31 -0700 | [diff] [blame] | 121 | ctx_.set_call(mrd->call_); |
Yang Gao | 1205f6f | 2015-03-22 15:18:14 -0700 | [diff] [blame] | 122 | ctx_.cq_ = &cq_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 123 | GPR_ASSERT(mrd->in_flight_); |
| 124 | mrd->in_flight_ = false; |
| 125 | mrd->request_metadata_.count = 0; |
| 126 | } |
| 127 | |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 128 | ~CallData() { |
| 129 | if (has_request_payload_ && request_payload_) { |
| 130 | grpc_byte_buffer_destroy(request_payload_); |
| 131 | } |
| 132 | } |
| 133 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 134 | void Run() { |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 135 | ctx_.BeginCompletionOp(&call_); |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 136 | method_->handler()->RunHandler(MethodHandler::HandlerParameter( |
| 137 | &call_, &ctx_, request_payload_, call_.max_message_size())); |
Craig Tiller | 928ec8e | 2015-06-05 08:45:45 -0700 | [diff] [blame] | 138 | request_payload_ = nullptr; |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 139 | void* ignored_tag; |
| 140 | bool ignored_ok; |
| 141 | cq_.Shutdown(); |
| 142 | GPR_ASSERT(cq_.Next(&ignored_tag, &ignored_ok) == false); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private: |
| 146 | CompletionQueue cq_; |
| 147 | Call call_; |
| 148 | ServerContext ctx_; |
| 149 | const bool has_request_payload_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 150 | grpc_byte_buffer* request_payload_; |
| 151 | RpcServiceMethod* const method_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | private: |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 155 | RpcServiceMethod* const method_; |
| 156 | void* const tag_; |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 157 | bool in_flight_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 158 | const bool has_request_payload_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 159 | grpc_call* call_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 160 | gpr_timespec deadline_; |
| 161 | grpc_metadata_array request_metadata_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 162 | grpc_byte_buffer* request_payload_; |
| 163 | grpc_completion_queue* cq_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 166 | static grpc_server* CreateServer( |
| 167 | int max_message_size, const grpc_compression_options& compression_options) { |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 168 | if (max_message_size > 0) { |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 169 | grpc_arg args[2]; |
| 170 | args[0].type = GRPC_ARG_INTEGER; |
| 171 | args[0].key = const_cast<char*>(GRPC_ARG_MAX_MESSAGE_LENGTH); |
| 172 | args[0].value.integer = max_message_size; |
| 173 | |
| 174 | args[1].type = GRPC_ARG_INTEGER; |
| 175 | args[1].key = const_cast<char*>(GRPC_COMPRESSION_ALGORITHM_STATE_ARG); |
| 176 | args[1].value.integer = compression_options.enabled_algorithms_bitset; |
| 177 | |
| 178 | grpc_channel_args channel_args = {2, args}; |
| 179 | return grpc_server_create(&channel_args); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 180 | } else { |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 181 | return grpc_server_create(nullptr); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned, |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 186 | int max_message_size, |
| 187 | grpc_compression_options compression_options) |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 188 | : max_message_size_(max_message_size), |
| 189 | started_(false), |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 190 | shutdown_(false), |
| 191 | num_running_cb_(0), |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 192 | sync_methods_(new std::list<SyncRequest>), |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame^] | 193 | server_(CreateServer(max_message_size, compression_options)), |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 194 | thread_pool_(thread_pool), |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 195 | thread_pool_owned_(thread_pool_owned) { |
| 196 | grpc_server_register_completion_queue(server_, cq_.cq()); |
| 197 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 198 | |
| 199 | Server::~Server() { |
Ruyi Wang | b486ba6 | 2015-03-14 22:19:44 +0800 | [diff] [blame] | 200 | { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 201 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Ruyi Wang | b486ba6 | 2015-03-14 22:19:44 +0800 | [diff] [blame] | 202 | if (started_ && !shutdown_) { |
| 203 | lock.unlock(); |
| 204 | Shutdown(); |
| 205 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 206 | } |
Craig Tiller | 29f79dc | 2015-05-27 15:59:23 -0700 | [diff] [blame] | 207 | void* got_tag; |
| 208 | bool ok; |
| 209 | GPR_ASSERT(!cq_.Next(&got_tag, &ok)); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 210 | grpc_server_destroy(server_); |
| 211 | if (thread_pool_owned_) { |
| 212 | delete thread_pool_; |
| 213 | } |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 214 | delete sync_methods_; |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 217 | bool Server::RegisterService(const grpc::string *host, RpcService* service) { |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 218 | for (int i = 0; i < service->GetMethodCount(); ++i) { |
| 219 | RpcServiceMethod* method = service->GetMethod(i); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 220 | void* tag = grpc_server_register_method( |
| 221 | server_, method->name(), host ? host->c_str() : nullptr); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 222 | if (!tag) { |
| 223 | gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", |
| 224 | method->name()); |
| 225 | return false; |
| 226 | } |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 227 | SyncRequest request(method, tag); |
| 228 | sync_methods_->emplace_back(request); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 233 | bool Server::RegisterAsyncService(const grpc::string *host, AsynchronousService* service) { |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 234 | GPR_ASSERT(service->server_ == nullptr && |
Yang Gao | 1ad253d | 2015-03-16 13:11:29 -0700 | [diff] [blame] | 235 | "Can only register an asynchronous service against one server."); |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 236 | service->server_ = this; |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 237 | service->request_args_ = new void*[service->method_count_]; |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 238 | for (size_t i = 0; i < service->method_count_; ++i) { |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 239 | void* tag = grpc_server_register_method(server_, service->method_names_[i], |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 240 | host ? host->c_str() : nullptr); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 241 | if (!tag) { |
| 242 | gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", |
| 243 | service->method_names_[i]); |
| 244 | return false; |
| 245 | } |
| 246 | service->request_args_[i] = tag; |
| 247 | } |
| 248 | return true; |
| 249 | } |
| 250 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 251 | void Server::RegisterAsyncGenericService(AsyncGenericService* service) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 252 | GPR_ASSERT(service->server_ == nullptr && |
Yang Gao | 1ad253d | 2015-03-16 13:11:29 -0700 | [diff] [blame] | 253 | "Can only register an async generic service against one server."); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 254 | service->server_ = this; |
| 255 | } |
| 256 | |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 257 | int Server::AddListeningPort(const grpc::string& addr, |
| 258 | ServerCredentials* creds) { |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 259 | GPR_ASSERT(!started_); |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 260 | return creds->AddPortToServer(addr, server_); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 263 | bool Server::Start() { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 264 | GPR_ASSERT(!started_); |
| 265 | started_ = true; |
| 266 | grpc_server_start(server_); |
| 267 | |
| 268 | // Start processing rpcs. |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 269 | if (!sync_methods_->empty()) { |
| 270 | for (auto m = sync_methods_->begin(); m != sync_methods_->end(); m++) { |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 271 | m->SetupRequest(); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 272 | m->Request(server_, cq_.cq()); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Craig Tiller | 7c72adc | 2015-02-09 14:07:26 -0800 | [diff] [blame] | 275 | ScheduleCallback(); |
| 276 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 277 | |
| 278 | return true; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 281 | void Server::Shutdown() { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 282 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 283 | if (started_ && !shutdown_) { |
| 284 | shutdown_ = true; |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 285 | grpc_server_shutdown_and_notify(server_, cq_.cq(), new ShutdownRequest()); |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 286 | cq_.Shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 287 | |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 288 | // Wait for running callbacks to finish. |
| 289 | while (num_running_cb_ != 0) { |
| 290 | callback_cv_.wait(lock); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 295 | void Server::Wait() { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 296 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 297 | while (num_running_cb_ != 0) { |
| 298 | callback_cv_.wait(lock); |
| 299 | } |
| 300 | } |
| 301 | |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 302 | void Server::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) { |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 303 | static const size_t MAX_OPS = 8; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 304 | size_t nops = 0; |
| 305 | grpc_op cops[MAX_OPS]; |
| 306 | ops->FillOps(cops, &nops); |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 307 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 308 | grpc_call_start_batch(call->call(), cops, nops, ops)); |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 311 | Server::BaseAsyncRequest::BaseAsyncRequest( |
| 312 | Server* server, ServerContext* context, |
| 313 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag) |
| 314 | : server_(server), |
| 315 | context_(context), |
| 316 | stream_(stream), |
| 317 | call_cq_(call_cq), |
Craig Tiller | 5095581 | 2015-06-05 08:03:17 -0700 | [diff] [blame] | 318 | tag_(tag), |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 319 | call_(nullptr) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 320 | memset(&initial_metadata_array_, 0, sizeof(initial_metadata_array_)); |
| 321 | } |
| 322 | |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 323 | Server::BaseAsyncRequest::~BaseAsyncRequest() {} |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 324 | |
| 325 | bool Server::BaseAsyncRequest::FinalizeResult(void** tag, bool* status) { |
| 326 | if (*status) { |
| 327 | for (size_t i = 0; i < initial_metadata_array_.count; i++) { |
| 328 | context_->client_metadata_.insert(std::make_pair( |
| 329 | grpc::string(initial_metadata_array_.metadata[i].key), |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 330 | grpc::string(initial_metadata_array_.metadata[i].value, |
| 331 | initial_metadata_array_.metadata[i].value + |
| 332 | initial_metadata_array_.metadata[i].value_length))); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
Craig Tiller | 2f4a49c | 2015-06-05 09:36:52 -0700 | [diff] [blame] | 335 | grpc_metadata_array_destroy(&initial_metadata_array_); |
yang-g | 85c04f9 | 2015-07-07 17:47:31 -0700 | [diff] [blame] | 336 | context_->set_call(call_); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 337 | context_->cq_ = call_cq_; |
| 338 | Call call(call_, server_, call_cq_, server_->max_message_size_); |
| 339 | if (*status && call_) { |
| 340 | context_->BeginCompletionOp(&call); |
| 341 | } |
| 342 | // just the pointers inside call are copied here |
| 343 | stream_->BindCall(&call); |
Craig Tiller | 5095581 | 2015-06-05 08:03:17 -0700 | [diff] [blame] | 344 | *tag = tag_; |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 345 | delete this; |
| 346 | return true; |
| 347 | } |
| 348 | |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 349 | Server::RegisteredAsyncRequest::RegisteredAsyncRequest( |
| 350 | Server* server, ServerContext* context, |
| 351 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag) |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 352 | : BaseAsyncRequest(server, context, stream, call_cq, tag) {} |
| 353 | |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 354 | void Server::RegisteredAsyncRequest::IssueRequest( |
| 355 | void* registered_method, grpc_byte_buffer** payload, |
| 356 | ServerCompletionQueue* notification_cq) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 357 | grpc_server_request_registered_call( |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 358 | server_->server_, registered_method, &call_, &context_->deadline_, |
| 359 | &initial_metadata_array_, payload, call_cq_->cq(), notification_cq->cq(), |
| 360 | this); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 363 | Server::GenericAsyncRequest::GenericAsyncRequest( |
| 364 | Server* server, GenericServerContext* context, |
| 365 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, |
| 366 | ServerCompletionQueue* notification_cq, void* tag) |
| 367 | : BaseAsyncRequest(server, context, stream, call_cq, tag) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 368 | grpc_call_details_init(&call_details_); |
| 369 | GPR_ASSERT(notification_cq); |
| 370 | GPR_ASSERT(call_cq); |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 371 | grpc_server_request_call(server->server_, &call_, &call_details_, |
| 372 | &initial_metadata_array_, call_cq->cq(), |
| 373 | notification_cq->cq(), this); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | bool Server::GenericAsyncRequest::FinalizeResult(void** tag, bool* status) { |
| 377 | // TODO(yangg) remove the copy here. |
yang-g | 3deb006 | 2015-06-23 14:06:08 -0700 | [diff] [blame] | 378 | if (*status) { |
yang-g | a58cab3 | 2015-06-23 14:37:15 -0700 | [diff] [blame] | 379 | static_cast<GenericServerContext*>(context_)->method_ = |
| 380 | call_details_.method; |
yang-g | 3deb006 | 2015-06-23 14:06:08 -0700 | [diff] [blame] | 381 | static_cast<GenericServerContext*>(context_)->host_ = call_details_.host; |
yang-g | 3deb006 | 2015-06-23 14:06:08 -0700 | [diff] [blame] | 382 | } |
yang-g | a58cab3 | 2015-06-23 14:37:15 -0700 | [diff] [blame] | 383 | gpr_free(call_details_.method); |
| 384 | gpr_free(call_details_.host); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 385 | return BaseAsyncRequest::FinalizeResult(tag, status); |
| 386 | } |
| 387 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 388 | void Server::ScheduleCallback() { |
| 389 | { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 390 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 391 | num_running_cb_++; |
| 392 | } |
vjpai | 72a4417 | 2015-07-16 21:44:44 -0700 | [diff] [blame] | 393 | thread_pool_->Add(std::bind(&Server::RunRpc, this)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void Server::RunRpc() { |
| 397 | // Wait for one more incoming rpc. |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 398 | bool ok; |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 399 | auto* mrd = SyncRequest::Wait(&cq_, &ok); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 400 | if (mrd) { |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 401 | ScheduleCallback(); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 402 | if (ok) { |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 403 | SyncRequest::CallData cd(this, mrd); |
Yang Gao | b8a5f86 | 2015-05-07 16:26:33 -0700 | [diff] [blame] | 404 | { |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 405 | mrd->SetupRequest(); |
Yang Gao | b8a5f86 | 2015-05-07 16:26:33 -0700 | [diff] [blame] | 406 | grpc::unique_lock<grpc::mutex> lock(mu_); |
| 407 | if (!shutdown_) { |
Craig Tiller | a33acb7 | 2015-05-08 08:02:55 -0700 | [diff] [blame] | 408 | mrd->Request(server_, cq_.cq()); |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 409 | } else { |
| 410 | // destroy the structure that was created |
| 411 | mrd->TeardownRequest(); |
Yang Gao | b8a5f86 | 2015-05-07 16:26:33 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 414 | cd.Run(); |
| 415 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 416 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 417 | |
| 418 | { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 419 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 420 | num_running_cb_--; |
| 421 | if (shutdown_) { |
| 422 | callback_cv_.notify_all(); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | } // namespace grpc |