blob: f930dbb2b8921eb90a0078e18553c4e5426f4347 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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>
yangg9e21f722014-12-08 15:49:52 -080038#include <grpc/grpc_security.h>
Yang Gao2a3c96a2015-03-11 23:32:40 -070039#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include <grpc++/completion_queue.h>
Yang Gao49996492015-03-12 16:40:19 -070042#include <grpc++/async_generic_service.h>
yangg1b151092015-01-09 15:31:05 -080043#include <grpc++/impl/rpc_service_method.h>
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080044#include <grpc++/impl/service_type.h>
Craig Tillerb4701692015-02-09 16:12:00 -080045#include <grpc++/server_context.h>
yangg9e21f722014-12-08 15:49:52 -080046#include <grpc++/server_credentials.h>
Craig Tiller0db1bef2015-02-09 13:47:39 -080047#include <grpc++/thread_pool_interface.h>
Nicolas Noble89219162015-04-07 18:01:18 -070048#include <grpc++/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049
Vijay Pai9ffbd0c2015-04-15 01:02:50 -070050#include "src/core/profiling/timers.h"
Craig Tillerc4165772015-02-11 10:51:04 -080051
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052namespace grpc {
53
Craig Tillerbce999f2015-05-27 09:55:51 -070054class 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 Tillercf133f42015-02-26 14:05:56 -080062class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag {
Craig Tillerc4165772015-02-11 10:51:04 -080063 public:
Craig Tiller1c9a2a92015-02-12 14:10:25 -080064 SyncRequest(RpcServiceMethod* method, void* tag)
Craig Tillerc4165772015-02-11 10:51:04 -080065 : method_(method),
66 tag_(tag),
Craig Tillercf133f42015-02-26 14:05:56 -080067 in_flight_(false),
Craig Tillerc4165772015-02-11 10:51:04 -080068 has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC ||
69 method->method_type() ==
70 RpcMethod::SERVER_STREAMING),
Vijay Paiced73bd2015-06-17 10:23:28 -070071 cq_(nullptr) {
Craig Tillerc4165772015-02-11 10:51:04 -080072 grpc_metadata_array_init(&request_metadata_);
73 }
74
Craig Tillerce40de52015-06-05 07:14:58 -070075 ~SyncRequest() { grpc_metadata_array_destroy(&request_metadata_); }
Yang Gao7b49a772015-05-28 14:07:54 -070076
Craig Tiller1c9a2a92015-02-12 14:10:25 -080077 static SyncRequest* Wait(CompletionQueue* cq, bool* ok) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080078 void* tag = nullptr;
Craig Tiller504bd332015-02-11 20:34:33 -080079 *ok = false;
Craig Tillerc4165772015-02-11 10:51:04 -080080 if (!cq->Next(&tag, ok)) {
81 return nullptr;
82 }
Craig Tiller1c9a2a92015-02-12 14:10:25 -080083 auto* mrd = static_cast<SyncRequest*>(tag);
Craig Tillerc4165772015-02-11 10:51:04 -080084 GPR_ASSERT(mrd->in_flight_);
85 return mrd;
86 }
87
Craig Tiller3de4b472015-06-22 10:51:35 -070088 void SetupRequest() { cq_ = grpc_completion_queue_create(); }
Vijay Paiced73bd2015-06-17 10:23:28 -070089
90 void TeardownRequest() {
91 grpc_completion_queue_destroy(cq_);
92 cq_ = nullptr;
93 }
94
Craig Tillerf9e6adf2015-05-06 11:45:59 -070095 void Request(grpc_server* server, grpc_completion_queue* notify_cq) {
Vijay Paiced73bd2015-06-17 10:23:28 -070096 GPR_ASSERT(cq_ && !in_flight_);
Craig Tillerc4165772015-02-11 10:51:04 -080097 in_flight_ = true;
Craig Tillerc4165772015-02-11 10:51:04 -080098 GPR_ASSERT(GRPC_CALL_OK ==
99 grpc_server_request_registered_call(
100 server, tag_, &call_, &deadline_, &request_metadata_,
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800101 has_request_payload_ ? &request_payload_ : nullptr, cq_,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700102 notify_cq, this));
Craig Tillerc4165772015-02-11 10:51:04 -0800103 }
104
Craig Tillercf133f42015-02-26 14:05:56 -0800105 bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
Craig Tillerec3257c2015-02-12 15:59:43 -0800106 if (!*status) {
107 grpc_completion_queue_destroy(cq_);
108 }
Craig Tiller645466e2015-02-18 09:18:33 -0800109 return true;
Craig Tillerec3257c2015-02-12 15:59:43 -0800110 }
Craig Tillerc4165772015-02-11 10:51:04 -0800111
Craig Tillercf133f42015-02-26 14:05:56 -0800112 class CallData GRPC_FINAL {
Craig Tillerc4165772015-02-11 10:51:04 -0800113 public:
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800114 explicit CallData(Server* server, SyncRequest* mrd)
Craig Tillerc4165772015-02-11 10:51:04 -0800115 : cq_(mrd->cq_),
Yang Gao3921c562015-04-30 16:07:06 -0700116 call_(mrd->call_, server, &cq_, server->max_message_size_),
Craig Tillerc4165772015-02-11 10:51:04 -0800117 ctx_(mrd->deadline_, mrd->request_metadata_.metadata,
118 mrd->request_metadata_.count),
119 has_request_payload_(mrd->has_request_payload_),
Craig Tillerc4165772015-02-11 10:51:04 -0800120 request_payload_(mrd->request_payload_),
121 method_(mrd->method_) {
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800122 ctx_.call_ = mrd->call_;
Yang Gao1205f6f2015-03-22 15:18:14 -0700123 ctx_.cq_ = &cq_;
Craig Tillerc4165772015-02-11 10:51:04 -0800124 GPR_ASSERT(mrd->in_flight_);
125 mrd->in_flight_ = false;
126 mrd->request_metadata_.count = 0;
127 }
128
Craig Tillerec3257c2015-02-12 15:59:43 -0800129 ~CallData() {
130 if (has_request_payload_ && request_payload_) {
131 grpc_byte_buffer_destroy(request_payload_);
132 }
133 }
134
Craig Tillerc4165772015-02-11 10:51:04 -0800135 void Run() {
Craig Tiller492968f2015-02-18 13:14:03 -0800136 ctx_.BeginCompletionOp(&call_);
Craig Tillerce40de52015-06-05 07:14:58 -0700137 method_->handler()->RunHandler(MethodHandler::HandlerParameter(
138 &call_, &ctx_, request_payload_, call_.max_message_size()));
Craig Tiller928ec8e2015-06-05 08:45:45 -0700139 request_payload_ = nullptr;
Craig Tiller492968f2015-02-18 13:14:03 -0800140 void* ignored_tag;
141 bool ignored_ok;
142 cq_.Shutdown();
143 GPR_ASSERT(cq_.Next(&ignored_tag, &ignored_ok) == false);
Craig Tillerc4165772015-02-11 10:51:04 -0800144 }
145
146 private:
147 CompletionQueue cq_;
148 Call call_;
149 ServerContext ctx_;
150 const bool has_request_payload_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800151 grpc_byte_buffer* request_payload_;
152 RpcServiceMethod* const method_;
Craig Tillerc4165772015-02-11 10:51:04 -0800153 };
154
155 private:
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800156 RpcServiceMethod* const method_;
157 void* const tag_;
Craig Tillercf133f42015-02-26 14:05:56 -0800158 bool in_flight_;
Craig Tillerc4165772015-02-11 10:51:04 -0800159 const bool has_request_payload_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800160 grpc_call* call_;
Craig Tillerc4165772015-02-11 10:51:04 -0800161 gpr_timespec deadline_;
162 grpc_metadata_array request_metadata_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800163 grpc_byte_buffer* request_payload_;
164 grpc_completion_queue* cq_;
Craig Tillerc4165772015-02-11 10:51:04 -0800165};
166
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700167static grpc_server* CreateServer(int max_message_size) {
Yang Gao3921c562015-04-30 16:07:06 -0700168 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 Tillerf9e6adf2015-05-06 11:45:59 -0700174 return grpc_server_create(&args);
Yang Gao3921c562015-04-30 16:07:06 -0700175 } else {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700176 return grpc_server_create(nullptr);
Yang Gao3921c562015-04-30 16:07:06 -0700177 }
178}
179
180Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned,
181 int max_message_size)
182 : max_message_size_(max_message_size),
183 started_(false),
vjpaicad5f0a2015-02-18 22:02:52 -0800184 shutdown_(false),
185 num_running_cb_(0),
Nicolas Noble30862032015-04-24 18:17:45 -0700186 sync_methods_(new std::list<SyncRequest>),
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700187 server_(CreateServer(max_message_size)),
vjpaicad5f0a2015-02-18 22:02:52 -0800188 thread_pool_(thread_pool),
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700189 thread_pool_owned_(thread_pool_owned) {
190 grpc_server_register_completion_queue(server_, cq_.cq());
191}
vjpaicad5f0a2015-02-18 22:02:52 -0800192
193Server::~Server() {
Ruyi Wangb486ba62015-03-14 22:19:44 +0800194 {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200195 grpc::unique_lock<grpc::mutex> lock(mu_);
Ruyi Wangb486ba62015-03-14 22:19:44 +0800196 if (started_ && !shutdown_) {
197 lock.unlock();
198 Shutdown();
199 }
vjpaicad5f0a2015-02-18 22:02:52 -0800200 }
Craig Tiller29f79dc2015-05-27 15:59:23 -0700201 void* got_tag;
202 bool ok;
203 GPR_ASSERT(!cq_.Next(&got_tag, &ok));
vjpaicad5f0a2015-02-18 22:02:52 -0800204 grpc_server_destroy(server_);
205 if (thread_pool_owned_) {
206 delete thread_pool_;
207 }
Nicolas Noble30862032015-04-24 18:17:45 -0700208 delete sync_methods_;
vjpaicad5f0a2015-02-18 22:02:52 -0800209}
210
211bool Server::RegisterService(RpcService* service) {
212 for (int i = 0; i < service->GetMethodCount(); ++i) {
213 RpcServiceMethod* method = service->GetMethod(i);
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700214 void* tag = grpc_server_register_method(server_, method->name(), nullptr);
vjpaicad5f0a2015-02-18 22:02:52 -0800215 if (!tag) {
216 gpr_log(GPR_DEBUG, "Attempt to register %s multiple times",
217 method->name());
218 return false;
219 }
Nicolas Noble30862032015-04-24 18:17:45 -0700220 SyncRequest request(method, tag);
221 sync_methods_->emplace_back(request);
vjpaicad5f0a2015-02-18 22:02:52 -0800222 }
223 return true;
224}
225
226bool Server::RegisterAsyncService(AsynchronousService* service) {
Craig Tiller50a7a682015-06-04 12:53:40 -0700227 GPR_ASSERT(service->server_ == nullptr &&
Yang Gao1ad253d2015-03-16 13:11:29 -0700228 "Can only register an asynchronous service against one server.");
Craig Tiller50a7a682015-06-04 12:53:40 -0700229 service->server_ = this;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700230 service->request_args_ = new void*[service->method_count_];
vjpaicad5f0a2015-02-18 22:02:52 -0800231 for (size_t i = 0; i < service->method_count_; ++i) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700232 void* tag = grpc_server_register_method(server_, service->method_names_[i],
233 nullptr);
vjpaicad5f0a2015-02-18 22:02:52 -0800234 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 Gao49996492015-03-12 16:40:19 -0700244void Server::RegisterAsyncGenericService(AsyncGenericService* service) {
Yang Gao1c402332015-03-05 16:39:25 -0800245 GPR_ASSERT(service->server_ == nullptr &&
Yang Gao1ad253d2015-03-16 13:11:29 -0700246 "Can only register an async generic service against one server.");
Yang Gao1c402332015-03-05 16:39:25 -0800247 service->server_ = this;
248}
249
Nicolas Noblecfd60732015-03-18 16:27:43 -0700250int Server::AddListeningPort(const grpc::string& addr,
251 ServerCredentials* creds) {
vjpaicad5f0a2015-02-18 22:02:52 -0800252 GPR_ASSERT(!started_);
Craig Tiller42bc87c2015-02-23 08:50:19 -0800253 return creds->AddPortToServer(addr, server_);
vjpaicad5f0a2015-02-18 22:02:52 -0800254}
255
Craig Tiller0db1bef2015-02-09 13:47:39 -0800256bool Server::Start() {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800257 GPR_ASSERT(!started_);
258 started_ = true;
259 grpc_server_start(server_);
260
261 // Start processing rpcs.
Nicolas Noble30862032015-04-24 18:17:45 -0700262 if (!sync_methods_->empty()) {
263 for (auto m = sync_methods_->begin(); m != sync_methods_->end(); m++) {
Vijay Paiced73bd2015-06-17 10:23:28 -0700264 m->SetupRequest();
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700265 m->Request(server_, cq_.cq());
Craig Tillercbd04852015-02-10 17:39:54 -0800266 }
267
Craig Tiller7c72adc2015-02-09 14:07:26 -0800268 ScheduleCallback();
269 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800270
271 return true;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800272}
273
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800274void Server::Shutdown() {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200275 grpc::unique_lock<grpc::mutex> lock(mu_);
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800276 if (started_ && !shutdown_) {
277 shutdown_ = true;
Craig Tillerbce999f2015-05-27 09:55:51 -0700278 grpc_server_shutdown_and_notify(server_, cq_.cq(), new ShutdownRequest());
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800279 cq_.Shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800280
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800281 // Wait for running callbacks to finish.
282 while (num_running_cb_ != 0) {
283 callback_cv_.wait(lock);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800284 }
285 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800286}
287
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800288void Server::Wait() {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200289 grpc::unique_lock<grpc::mutex> lock(mu_);
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800290 while (num_running_cb_ != 0) {
291 callback_cv_.wait(lock);
292 }
293}
294
Craig Tiller50a7a682015-06-04 12:53:40 -0700295void Server::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) {
Craig Tillerbb5227f2015-02-11 13:34:48 -0800296 static const size_t MAX_OPS = 8;
Craig Tiller50a7a682015-06-04 12:53:40 -0700297 size_t nops = 0;
298 grpc_op cops[MAX_OPS];
299 ops->FillOps(cops, &nops);
Craig Tillerbb5227f2015-02-11 13:34:48 -0800300 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tiller50a7a682015-06-04 12:53:40 -0700301 grpc_call_start_batch(call->call(), cops, nops, ops));
Craig Tillerbb5227f2015-02-11 13:34:48 -0800302}
303
Craig Tillerce40de52015-06-05 07:14:58 -0700304Server::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 Tiller50955812015-06-05 08:03:17 -0700311 tag_(tag),
Craig Tillerce40de52015-06-05 07:14:58 -0700312 call_(nullptr) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700313 memset(&initial_metadata_array_, 0, sizeof(initial_metadata_array_));
314}
315
Craig Tillerce40de52015-06-05 07:14:58 -0700316Server::BaseAsyncRequest::~BaseAsyncRequest() {}
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700317
318bool 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 Tillerce40de52015-06-05 07:14:58 -0700323 grpc::string(initial_metadata_array_.metadata[i].value,
324 initial_metadata_array_.metadata[i].value +
325 initial_metadata_array_.metadata[i].value_length)));
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700326 }
327 }
Craig Tiller2f4a49c2015-06-05 09:36:52 -0700328 grpc_metadata_array_destroy(&initial_metadata_array_);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700329 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 Tiller50955812015-06-05 08:03:17 -0700337 *tag = tag_;
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700338 delete this;
339 return true;
340}
341
Craig Tillerce40de52015-06-05 07:14:58 -0700342Server::RegisteredAsyncRequest::RegisteredAsyncRequest(
343 Server* server, ServerContext* context,
344 ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag)
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700345 : BaseAsyncRequest(server, context, stream, call_cq, tag) {}
346
Craig Tillerce40de52015-06-05 07:14:58 -0700347void Server::RegisteredAsyncRequest::IssueRequest(
348 void* registered_method, grpc_byte_buffer** payload,
349 ServerCompletionQueue* notification_cq) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700350 grpc_server_request_registered_call(
Craig Tillerce40de52015-06-05 07:14:58 -0700351 server_->server_, registered_method, &call_, &context_->deadline_,
352 &initial_metadata_array_, payload, call_cq_->cq(), notification_cq->cq(),
353 this);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700354}
355
Craig Tillerce40de52015-06-05 07:14:58 -0700356Server::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 Tiller7bc97bc2015-06-04 17:22:54 -0700361 grpc_call_details_init(&call_details_);
362 GPR_ASSERT(notification_cq);
363 GPR_ASSERT(call_cq);
Craig Tillerce40de52015-06-05 07:14:58 -0700364 grpc_server_request_call(server->server_, &call_, &call_details_,
365 &initial_metadata_array_, call_cq->cq(),
366 notification_cq->cq(), this);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700367}
368
369bool Server::GenericAsyncRequest::FinalizeResult(void** tag, bool* status) {
370 // TODO(yangg) remove the copy here.
yang-g3deb0062015-06-23 14:06:08 -0700371 if (*status) {
yang-ga58cab32015-06-23 14:37:15 -0700372 static_cast<GenericServerContext*>(context_)->method_ =
373 call_details_.method;
yang-g3deb0062015-06-23 14:06:08 -0700374 static_cast<GenericServerContext*>(context_)->host_ = call_details_.host;
yang-g3deb0062015-06-23 14:06:08 -0700375 }
yang-ga58cab32015-06-23 14:37:15 -0700376 gpr_free(call_details_.method);
377 gpr_free(call_details_.host);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700378 return BaseAsyncRequest::FinalizeResult(tag, status);
379}
380
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800381void Server::ScheduleCallback() {
382 {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200383 grpc::unique_lock<grpc::mutex> lock(mu_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800384 num_running_cb_++;
385 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800386 thread_pool_->ScheduleCallback(std::bind(&Server::RunRpc, this));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800387}
388
389void Server::RunRpc() {
390 // Wait for one more incoming rpc.
Craig Tillerc4165772015-02-11 10:51:04 -0800391 bool ok;
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800392 auto* mrd = SyncRequest::Wait(&cq_, &ok);
Craig Tillercbd04852015-02-10 17:39:54 -0800393 if (mrd) {
Craig Tillerbd217572015-02-11 18:10:56 -0800394 ScheduleCallback();
Craig Tillerc4165772015-02-11 10:51:04 -0800395 if (ok) {
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800396 SyncRequest::CallData cd(this, mrd);
Yang Gaob8a5f862015-05-07 16:26:33 -0700397 {
Vijay Paiced73bd2015-06-17 10:23:28 -0700398 mrd->SetupRequest();
Yang Gaob8a5f862015-05-07 16:26:33 -0700399 grpc::unique_lock<grpc::mutex> lock(mu_);
400 if (!shutdown_) {
Craig Tillera33acb72015-05-08 08:02:55 -0700401 mrd->Request(server_, cq_.cq());
Vijay Paiced73bd2015-06-17 10:23:28 -0700402 } else {
403 // destroy the structure that was created
404 mrd->TeardownRequest();
Yang Gaob8a5f862015-05-07 16:26:33 -0700405 }
406 }
Craig Tillerc4165772015-02-11 10:51:04 -0800407 cd.Run();
408 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800409 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800410
411 {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200412 grpc::unique_lock<grpc::mutex> lock(mu_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800413 num_running_cb_--;
414 if (shutdown_) {
415 callback_cv_.notify_all();
416 }
417 }
418}
419
420} // namespace grpc