Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 | #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> |
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> |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 41 | #include <grpc++/impl/rpc_service_method.h> |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 42 | #include <grpc++/impl/service_type.h> |
Craig Tiller | b470169 | 2015-02-09 16:12:00 -0800 | [diff] [blame] | 43 | #include <grpc++/server_context.h> |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 44 | #include <grpc++/server_credentials.h> |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 45 | #include <grpc++/thread_pool_interface.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 47 | #include "src/cpp/proto/proto_utils.h" |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 48 | #include "src/cpp/util/time.h" |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 49 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | namespace grpc { |
| 51 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 52 | Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned, |
| 53 | ServerCredentials* creds) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | : started_(false), |
| 55 | shutdown_(false), |
| 56 | num_running_cb_(0), |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 57 | thread_pool_(thread_pool), |
| 58 | thread_pool_owned_(thread_pool_owned), |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 59 | secure_(creds != nullptr) { |
| 60 | if (creds) { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 61 | server_ = |
| 62 | grpc_secure_server_create(creds->GetRawCreds(), cq_.cq(), nullptr); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 63 | } else { |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 64 | server_ = grpc_server_create(cq_.cq(), nullptr); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 65 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | Server::Server() { |
| 69 | // Should not be called. |
| 70 | GPR_ASSERT(false); |
| 71 | } |
| 72 | |
| 73 | Server::~Server() { |
| 74 | std::unique_lock<std::mutex> lock(mu_); |
| 75 | if (started_ && !shutdown_) { |
| 76 | lock.unlock(); |
| 77 | Shutdown(); |
Craig Tiller | 061754a | 2015-02-09 14:56:49 -0800 | [diff] [blame] | 78 | } else { |
| 79 | lock.unlock(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | } |
| 81 | grpc_server_destroy(server_); |
| 82 | if (thread_pool_owned_) { |
| 83 | delete thread_pool_; |
| 84 | } |
| 85 | } |
| 86 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 87 | bool Server::RegisterService(RpcService* service) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | for (int i = 0; i < service->GetMethodCount(); ++i) { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 89 | RpcServiceMethod* method = service->GetMethod(i); |
| 90 | void* tag = |
| 91 | grpc_server_register_method(server_, method->name(), nullptr, cq_.cq()); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 92 | if (!tag) { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 93 | gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", |
| 94 | method->name()); |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 95 | return false; |
| 96 | } |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 97 | sync_methods_.emplace_back(method, tag); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 98 | } |
Craig Tiller | 7c72adc | 2015-02-09 14:07:26 -0800 | [diff] [blame] | 99 | return true; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 102 | bool Server::RegisterAsyncService(AsynchronousService* service) { |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 103 | GPR_ASSERT(service->dispatch_impl_ == nullptr && |
| 104 | "Can only register an asynchronous service against one server."); |
| 105 | service->dispatch_impl_ = this; |
| 106 | service->request_args_ = new void* [service->method_count_]; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 107 | for (size_t i = 0; i < service->method_count_; ++i) { |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 108 | void* tag = |
| 109 | grpc_server_register_method(server_, service->method_names_[i], nullptr, |
| 110 | service->completion_queue()->cq()); |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 111 | if (!tag) { |
| 112 | gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", |
| 113 | service->method_names_[i]); |
| 114 | return false; |
| 115 | } |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 116 | service->request_args_[i] = tag; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | int Server::AddPort(const grpc::string& addr) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | GPR_ASSERT(!started_); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 123 | if (secure_) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 124 | return grpc_server_add_secure_http2_port(server_, addr.c_str()); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 125 | } else { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 126 | return grpc_server_add_http2_port(server_, addr.c_str()); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 127 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 130 | class Server::SyncRequest final : public CompletionQueueTag { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 131 | public: |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 132 | SyncRequest(RpcServiceMethod* method, void* tag) |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 133 | : method_(method), |
| 134 | tag_(tag), |
| 135 | has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC || |
| 136 | method->method_type() == |
| 137 | RpcMethod::SERVER_STREAMING), |
| 138 | has_response_payload_(method->method_type() == RpcMethod::NORMAL_RPC || |
| 139 | method->method_type() == |
| 140 | RpcMethod::CLIENT_STREAMING) { |
| 141 | grpc_metadata_array_init(&request_metadata_); |
| 142 | } |
| 143 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 144 | static SyncRequest* Wait(CompletionQueue* cq, bool* ok) { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 145 | void* tag = nullptr; |
Craig Tiller | 504bd33 | 2015-02-11 20:34:33 -0800 | [diff] [blame] | 146 | *ok = false; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 147 | if (!cq->Next(&tag, ok)) { |
| 148 | return nullptr; |
| 149 | } |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 150 | auto* mrd = static_cast<SyncRequest*>(tag); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 151 | GPR_ASSERT(mrd->in_flight_); |
| 152 | return mrd; |
| 153 | } |
| 154 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 155 | void Request(grpc_server* server) { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 156 | GPR_ASSERT(!in_flight_); |
| 157 | in_flight_ = true; |
| 158 | cq_ = grpc_completion_queue_create(); |
| 159 | GPR_ASSERT(GRPC_CALL_OK == |
| 160 | grpc_server_request_registered_call( |
| 161 | server, tag_, &call_, &deadline_, &request_metadata_, |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 162 | has_request_payload_ ? &request_payload_ : nullptr, cq_, |
| 163 | this)); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 166 | void FinalizeResult(void** tag, bool* status) override { |
| 167 | if (!*status) { |
| 168 | grpc_completion_queue_destroy(cq_); |
| 169 | } |
| 170 | } |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 171 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 172 | class CallData final { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 173 | public: |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 174 | explicit CallData(Server* server, SyncRequest* mrd) |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 175 | : cq_(mrd->cq_), |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 176 | call_(mrd->call_, server, &cq_), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 177 | ctx_(mrd->deadline_, mrd->request_metadata_.metadata, |
| 178 | mrd->request_metadata_.count), |
| 179 | has_request_payload_(mrd->has_request_payload_), |
| 180 | has_response_payload_(mrd->has_response_payload_), |
| 181 | request_payload_(mrd->request_payload_), |
| 182 | method_(mrd->method_) { |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 183 | ctx_.call_ = mrd->call_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 184 | GPR_ASSERT(mrd->in_flight_); |
| 185 | mrd->in_flight_ = false; |
| 186 | mrd->request_metadata_.count = 0; |
| 187 | } |
| 188 | |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 189 | ~CallData() { |
| 190 | if (has_request_payload_ && request_payload_) { |
| 191 | grpc_byte_buffer_destroy(request_payload_); |
| 192 | } |
| 193 | } |
| 194 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 195 | void Run() { |
| 196 | std::unique_ptr<google::protobuf::Message> req; |
| 197 | std::unique_ptr<google::protobuf::Message> res; |
| 198 | if (has_request_payload_) { |
| 199 | req.reset(method_->AllocateRequestProto()); |
| 200 | if (!DeserializeProto(request_payload_, req.get())) { |
| 201 | abort(); // for now |
| 202 | } |
| 203 | } |
| 204 | if (has_response_payload_) { |
Craig Tiller | 7c2f3f7 | 2015-02-11 13:21:54 -0800 | [diff] [blame] | 205 | res.reset(method_->AllocateResponseProto()); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 206 | } |
| 207 | auto status = method_->handler()->RunHandler( |
| 208 | MethodHandler::HandlerParameter(&call_, &ctx_, req.get(), res.get())); |
| 209 | CallOpBuffer buf; |
Craig Tiller | bc8e3db | 2015-02-12 09:56:02 -0800 | [diff] [blame] | 210 | if (!ctx_.sent_initial_metadata_) { |
| 211 | buf.AddSendInitialMetadata(&ctx_.initial_metadata_); |
| 212 | } |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 213 | if (has_response_payload_) { |
| 214 | buf.AddSendMessage(*res); |
| 215 | } |
Craig Tiller | 9dcb0f8 | 2015-02-11 15:36:31 -0800 | [diff] [blame] | 216 | buf.AddServerSendStatus(&ctx_.trailing_metadata_, status); |
Craig Tiller | 504bd33 | 2015-02-11 20:34:33 -0800 | [diff] [blame] | 217 | bool cancelled; |
| 218 | buf.AddServerRecvClose(&cancelled); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 219 | call_.PerformOps(&buf); |
| 220 | GPR_ASSERT(cq_.Pluck(&buf)); |
| 221 | } |
| 222 | |
| 223 | private: |
| 224 | CompletionQueue cq_; |
| 225 | Call call_; |
| 226 | ServerContext ctx_; |
| 227 | const bool has_request_payload_; |
| 228 | const bool has_response_payload_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 229 | grpc_byte_buffer* request_payload_; |
| 230 | RpcServiceMethod* const method_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | private: |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 234 | RpcServiceMethod* const method_; |
| 235 | void* const tag_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 236 | bool in_flight_ = false; |
| 237 | const bool has_request_payload_; |
| 238 | const bool has_response_payload_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 239 | grpc_call* call_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 240 | gpr_timespec deadline_; |
| 241 | grpc_metadata_array request_metadata_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 242 | grpc_byte_buffer* request_payload_; |
| 243 | grpc_completion_queue* cq_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 244 | }; |
| 245 | |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 246 | bool Server::Start() { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 247 | GPR_ASSERT(!started_); |
| 248 | started_ = true; |
| 249 | grpc_server_start(server_); |
| 250 | |
| 251 | // Start processing rpcs. |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 252 | if (!sync_methods_.empty()) { |
| 253 | for (auto& m : sync_methods_) { |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 254 | m.Request(server_); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Craig Tiller | 7c72adc | 2015-02-09 14:07:26 -0800 | [diff] [blame] | 257 | ScheduleCallback(); |
| 258 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 259 | |
| 260 | return true; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 263 | void Server::Shutdown() { |
| 264 | { |
| 265 | std::unique_lock<std::mutex> lock(mu_); |
| 266 | if (started_ && !shutdown_) { |
| 267 | shutdown_ = true; |
| 268 | grpc_server_shutdown(server_); |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 269 | cq_.Shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 270 | |
| 271 | // Wait for running callbacks to finish. |
| 272 | while (num_running_cb_ != 0) { |
| 273 | callback_cv_.wait(lock); |
| 274 | } |
| 275 | } |
| 276 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 279 | void Server::PerformOpsOnCall(CallOpBuffer* buf, Call* call) { |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 280 | static const size_t MAX_OPS = 8; |
| 281 | size_t nops = MAX_OPS; |
| 282 | grpc_op ops[MAX_OPS]; |
| 283 | buf->FillOps(ops, &nops); |
| 284 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 285 | grpc_call_start_batch(call->call(), ops, nops, buf)); |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 288 | class Server::AsyncRequest final : public CompletionQueueTag { |
| 289 | public: |
| 290 | AsyncRequest(Server* server, void* registered_method, ServerContext* ctx, |
| 291 | ::google::protobuf::Message* request, |
| 292 | ServerAsyncStreamingInterface* stream, CompletionQueue* cq, |
| 293 | void* tag) |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 294 | : tag_(tag), |
| 295 | request_(request), |
| 296 | stream_(stream), |
| 297 | cq_(cq), |
| 298 | ctx_(ctx), |
| 299 | server_(server) { |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 300 | memset(&array_, 0, sizeof(array_)); |
| 301 | grpc_server_request_registered_call( |
| 302 | server->server_, registered_method, &call_, &deadline_, &array_, |
| 303 | request ? &payload_ : nullptr, cq->cq(), this); |
| 304 | } |
| 305 | |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 306 | ~AsyncRequest() { |
| 307 | if (payload_) { |
| 308 | grpc_byte_buffer_destroy(payload_); |
| 309 | } |
| 310 | grpc_metadata_array_destroy(&array_); |
| 311 | } |
| 312 | |
| 313 | void FinalizeResult(void** tag, bool* status) override { |
| 314 | *tag = tag_; |
| 315 | if (*status && request_) { |
| 316 | if (payload_) { |
| 317 | *status = DeserializeProto(payload_, request_); |
| 318 | } else { |
| 319 | *status = false; |
| 320 | } |
| 321 | } |
| 322 | if (*status) { |
| 323 | ctx_->deadline_ = Timespec2Timepoint(deadline_); |
| 324 | for (size_t i = 0; i < array_.count; i++) { |
| 325 | ctx_->client_metadata_.insert(std::make_pair( |
| 326 | grpc::string(array_.metadata[i].key), |
| 327 | grpc::string( |
| 328 | array_.metadata[i].value, |
| 329 | array_.metadata[i].value + array_.metadata[i].value_length))); |
| 330 | } |
| 331 | } |
| 332 | ctx_->call_ = call_; |
| 333 | Call call(call_, server_, cq_); |
| 334 | stream_->BindCall(&call); |
| 335 | delete this; |
| 336 | } |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 337 | |
| 338 | private: |
| 339 | void* const tag_; |
| 340 | ::google::protobuf::Message* const request_; |
| 341 | ServerAsyncStreamingInterface* const stream_; |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 342 | CompletionQueue* const cq_; |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 343 | ServerContext* const ctx_; |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 344 | Server* const server_; |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 345 | grpc_call* call_ = nullptr; |
| 346 | gpr_timespec deadline_; |
| 347 | grpc_metadata_array array_; |
| 348 | grpc_byte_buffer* payload_ = nullptr; |
| 349 | }; |
| 350 | |
| 351 | void Server::RequestAsyncCall(void* registered_method, ServerContext* context, |
| 352 | ::google::protobuf::Message* request, |
| 353 | ServerAsyncStreamingInterface* stream, |
| 354 | CompletionQueue* cq, void* tag) { |
| 355 | new AsyncRequest(this, registered_method, context, request, stream, cq, tag); |
| 356 | } |
| 357 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 358 | void Server::ScheduleCallback() { |
| 359 | { |
| 360 | std::unique_lock<std::mutex> lock(mu_); |
| 361 | num_running_cb_++; |
| 362 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 363 | thread_pool_->ScheduleCallback(std::bind(&Server::RunRpc, this)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void Server::RunRpc() { |
| 367 | // Wait for one more incoming rpc. |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 368 | bool ok; |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 369 | auto* mrd = SyncRequest::Wait(&cq_, &ok); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 370 | if (mrd) { |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 371 | ScheduleCallback(); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 372 | if (ok) { |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 373 | SyncRequest::CallData cd(this, mrd); |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 374 | mrd->Request(server_); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 375 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 376 | cd.Run(); |
| 377 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 378 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 379 | |
| 380 | { |
| 381 | std::unique_lock<std::mutex> lock(mu_); |
| 382 | num_running_cb_--; |
| 383 | if (shutdown_) { |
| 384 | callback_cv_.notify_all(); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | } // namespace grpc |