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