blob: 6e576ab8b39306e78d32b8035b57af6f169a1feb [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>
Yang Gao2a3c96a2015-03-11 23:32:40 -070038#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc++/completion_queue.h>
Yang Gao49996492015-03-12 16:40:19 -070041#include <grpc++/async_generic_service.h>
yangg1b151092015-01-09 15:31:05 -080042#include <grpc++/impl/rpc_service_method.h>
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080043#include <grpc++/impl/service_type.h>
Craig Tillerb4701692015-02-09 16:12:00 -080044#include <grpc++/server_context.h>
yangg9e21f722014-12-08 15:49:52 -080045#include <grpc++/server_credentials.h>
Craig Tiller0db1bef2015-02-09 13:47:39 -080046#include <grpc++/thread_pool_interface.h>
Nicolas Noble89219162015-04-07 18:01:18 -070047#include <grpc++/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
Vijay Pai9ffbd0c2015-04-15 01:02:50 -070049#include "src/core/profiling/timers.h"
Craig Tillerc4165772015-02-11 10:51:04 -080050
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051namespace grpc {
52
Craig Tillerbce999f2015-05-27 09:55:51 -070053class 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 Tillercf133f42015-02-26 14:05:56 -080061class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag {
Craig Tillerc4165772015-02-11 10:51:04 -080062 public:
Craig Tiller1c9a2a92015-02-12 14:10:25 -080063 SyncRequest(RpcServiceMethod* method, void* tag)
Craig Tillerc4165772015-02-11 10:51:04 -080064 : method_(method),
65 tag_(tag),
Craig Tillercf133f42015-02-26 14:05:56 -080066 in_flight_(false),
Craig Tillerc4165772015-02-11 10:51:04 -080067 has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC ||
68 method->method_type() ==
69 RpcMethod::SERVER_STREAMING),
Vijay Paiced73bd2015-06-17 10:23:28 -070070 cq_(nullptr) {
Craig Tillerc4165772015-02-11 10:51:04 -080071 grpc_metadata_array_init(&request_metadata_);
72 }
73
Craig Tillerce40de52015-06-05 07:14:58 -070074 ~SyncRequest() { grpc_metadata_array_destroy(&request_metadata_); }
Yang Gao7b49a772015-05-28 14:07:54 -070075
Craig Tiller1c9a2a92015-02-12 14:10:25 -080076 static SyncRequest* Wait(CompletionQueue* cq, bool* ok) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080077 void* tag = nullptr;
Craig Tiller504bd332015-02-11 20:34:33 -080078 *ok = false;
Craig Tillerc4165772015-02-11 10:51:04 -080079 if (!cq->Next(&tag, ok)) {
80 return nullptr;
81 }
Craig Tiller1c9a2a92015-02-12 14:10:25 -080082 auto* mrd = static_cast<SyncRequest*>(tag);
Craig Tillerc4165772015-02-11 10:51:04 -080083 GPR_ASSERT(mrd->in_flight_);
84 return mrd;
85 }
86
Craig Tiller3de4b472015-06-22 10:51:35 -070087 void SetupRequest() { cq_ = grpc_completion_queue_create(); }
Vijay Paiced73bd2015-06-17 10:23:28 -070088
89 void TeardownRequest() {
90 grpc_completion_queue_destroy(cq_);
91 cq_ = nullptr;
92 }
93
Craig Tillerf9e6adf2015-05-06 11:45:59 -070094 void Request(grpc_server* server, grpc_completion_queue* notify_cq) {
Vijay Paiced73bd2015-06-17 10:23:28 -070095 GPR_ASSERT(cq_ && !in_flight_);
Craig Tillerc4165772015-02-11 10:51:04 -080096 in_flight_ = true;
Craig Tillerc4165772015-02-11 10:51:04 -080097 GPR_ASSERT(GRPC_CALL_OK ==
98 grpc_server_request_registered_call(
99 server, tag_, &call_, &deadline_, &request_metadata_,
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800100 has_request_payload_ ? &request_payload_ : nullptr, cq_,
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700101 notify_cq, this));
Craig Tillerc4165772015-02-11 10:51:04 -0800102 }
103
Craig Tillercf133f42015-02-26 14:05:56 -0800104 bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
Craig Tillerec3257c2015-02-12 15:59:43 -0800105 if (!*status) {
106 grpc_completion_queue_destroy(cq_);
107 }
Craig Tiller645466e2015-02-18 09:18:33 -0800108 return true;
Craig Tillerec3257c2015-02-12 15:59:43 -0800109 }
Craig Tillerc4165772015-02-11 10:51:04 -0800110
Craig Tillercf133f42015-02-26 14:05:56 -0800111 class CallData GRPC_FINAL {
Craig Tillerc4165772015-02-11 10:51:04 -0800112 public:
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800113 explicit CallData(Server* server, SyncRequest* mrd)
Craig Tillerc4165772015-02-11 10:51:04 -0800114 : cq_(mrd->cq_),
Yang Gao3921c562015-04-30 16:07:06 -0700115 call_(mrd->call_, server, &cq_, server->max_message_size_),
Craig Tillerc4165772015-02-11 10:51:04 -0800116 ctx_(mrd->deadline_, mrd->request_metadata_.metadata,
117 mrd->request_metadata_.count),
118 has_request_payload_(mrd->has_request_payload_),
Craig Tillerc4165772015-02-11 10:51:04 -0800119 request_payload_(mrd->request_payload_),
120 method_(mrd->method_) {
yang-g85c04f92015-07-07 17:47:31 -0700121 ctx_.set_call(mrd->call_);
Yang Gao1205f6f2015-03-22 15:18:14 -0700122 ctx_.cq_ = &cq_;
Craig Tillerc4165772015-02-11 10:51:04 -0800123 GPR_ASSERT(mrd->in_flight_);
124 mrd->in_flight_ = false;
125 mrd->request_metadata_.count = 0;
126 }
127
Craig Tillerec3257c2015-02-12 15:59:43 -0800128 ~CallData() {
129 if (has_request_payload_ && request_payload_) {
130 grpc_byte_buffer_destroy(request_payload_);
131 }
132 }
133
Craig Tillerc4165772015-02-11 10:51:04 -0800134 void Run() {
Craig Tiller492968f2015-02-18 13:14:03 -0800135 ctx_.BeginCompletionOp(&call_);
Craig Tillerce40de52015-06-05 07:14:58 -0700136 method_->handler()->RunHandler(MethodHandler::HandlerParameter(
137 &call_, &ctx_, request_payload_, call_.max_message_size()));
Craig Tiller928ec8e2015-06-05 08:45:45 -0700138 request_payload_ = nullptr;
Craig Tiller492968f2015-02-18 13:14:03 -0800139 void* ignored_tag;
140 bool ignored_ok;
141 cq_.Shutdown();
142 GPR_ASSERT(cq_.Next(&ignored_tag, &ignored_ok) == false);
Craig Tillerc4165772015-02-11 10:51:04 -0800143 }
144
145 private:
146 CompletionQueue cq_;
147 Call call_;
148 ServerContext ctx_;
149 const bool has_request_payload_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800150 grpc_byte_buffer* request_payload_;
151 RpcServiceMethod* const method_;
Craig Tillerc4165772015-02-11 10:51:04 -0800152 };
153
154 private:
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800155 RpcServiceMethod* const method_;
156 void* const tag_;
Craig Tillercf133f42015-02-26 14:05:56 -0800157 bool in_flight_;
Craig Tillerc4165772015-02-11 10:51:04 -0800158 const bool has_request_payload_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800159 grpc_call* call_;
Craig Tillerc4165772015-02-11 10:51:04 -0800160 gpr_timespec deadline_;
161 grpc_metadata_array request_metadata_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800162 grpc_byte_buffer* request_payload_;
163 grpc_completion_queue* cq_;
Craig Tillerc4165772015-02-11 10:51:04 -0800164};
165
David Garcia Quintasbeac88c2015-08-10 13:39:52 -0700166static grpc_server* CreateServer(
167 int max_message_size, const grpc_compression_options& compression_options) {
Yang Gao3921c562015-04-30 16:07:06 -0700168 if (max_message_size > 0) {
David Garcia Quintasbeac88c2015-08-10 13:39:52 -0700169 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 Gao3921c562015-04-30 16:07:06 -0700180 } else {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700181 return grpc_server_create(nullptr);
Yang Gao3921c562015-04-30 16:07:06 -0700182 }
183}
184
185Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned,
David Garcia Quintasbeac88c2015-08-10 13:39:52 -0700186 int max_message_size,
187 grpc_compression_options compression_options)
Yang Gao3921c562015-04-30 16:07:06 -0700188 : max_message_size_(max_message_size),
189 started_(false),
vjpaicad5f0a2015-02-18 22:02:52 -0800190 shutdown_(false),
191 num_running_cb_(0),
Nicolas Noble30862032015-04-24 18:17:45 -0700192 sync_methods_(new std::list<SyncRequest>),
David Garcia Quintasbeac88c2015-08-10 13:39:52 -0700193 server_(CreateServer(max_message_size, compression_options)),
vjpaicad5f0a2015-02-18 22:02:52 -0800194 thread_pool_(thread_pool),
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700195 thread_pool_owned_(thread_pool_owned) {
196 grpc_server_register_completion_queue(server_, cq_.cq());
197}
vjpaicad5f0a2015-02-18 22:02:52 -0800198
199Server::~Server() {
Ruyi Wangb486ba62015-03-14 22:19:44 +0800200 {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200201 grpc::unique_lock<grpc::mutex> lock(mu_);
Ruyi Wangb486ba62015-03-14 22:19:44 +0800202 if (started_ && !shutdown_) {
203 lock.unlock();
204 Shutdown();
205 }
vjpaicad5f0a2015-02-18 22:02:52 -0800206 }
Craig Tiller29f79dc2015-05-27 15:59:23 -0700207 void* got_tag;
208 bool ok;
209 GPR_ASSERT(!cq_.Next(&got_tag, &ok));
vjpaicad5f0a2015-02-18 22:02:52 -0800210 grpc_server_destroy(server_);
211 if (thread_pool_owned_) {
212 delete thread_pool_;
213 }
Nicolas Noble30862032015-04-24 18:17:45 -0700214 delete sync_methods_;
vjpaicad5f0a2015-02-18 22:02:52 -0800215}
216
Craig Tiller822d2c72015-07-07 16:08:00 -0700217bool Server::RegisterService(const grpc::string *host, RpcService* service) {
vjpaicad5f0a2015-02-18 22:02:52 -0800218 for (int i = 0; i < service->GetMethodCount(); ++i) {
219 RpcServiceMethod* method = service->GetMethod(i);
Craig Tiller822d2c72015-07-07 16:08:00 -0700220 void* tag = grpc_server_register_method(
221 server_, method->name(), host ? host->c_str() : nullptr);
vjpaicad5f0a2015-02-18 22:02:52 -0800222 if (!tag) {
223 gpr_log(GPR_DEBUG, "Attempt to register %s multiple times",
224 method->name());
225 return false;
226 }
Nicolas Noble30862032015-04-24 18:17:45 -0700227 SyncRequest request(method, tag);
228 sync_methods_->emplace_back(request);
vjpaicad5f0a2015-02-18 22:02:52 -0800229 }
230 return true;
231}
232
Craig Tiller822d2c72015-07-07 16:08:00 -0700233bool Server::RegisterAsyncService(const grpc::string *host, AsynchronousService* service) {
Craig Tiller50a7a682015-06-04 12:53:40 -0700234 GPR_ASSERT(service->server_ == nullptr &&
Yang Gao1ad253d2015-03-16 13:11:29 -0700235 "Can only register an asynchronous service against one server.");
Craig Tiller50a7a682015-06-04 12:53:40 -0700236 service->server_ = this;
Yang Gaoc71a9d22015-05-04 00:22:12 -0700237 service->request_args_ = new void*[service->method_count_];
vjpaicad5f0a2015-02-18 22:02:52 -0800238 for (size_t i = 0; i < service->method_count_; ++i) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700239 void* tag = grpc_server_register_method(server_, service->method_names_[i],
Craig Tiller822d2c72015-07-07 16:08:00 -0700240 host ? host->c_str() : nullptr);
vjpaicad5f0a2015-02-18 22:02:52 -0800241 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 Gao49996492015-03-12 16:40:19 -0700251void Server::RegisterAsyncGenericService(AsyncGenericService* service) {
Yang Gao1c402332015-03-05 16:39:25 -0800252 GPR_ASSERT(service->server_ == nullptr &&
Yang Gao1ad253d2015-03-16 13:11:29 -0700253 "Can only register an async generic service against one server.");
Yang Gao1c402332015-03-05 16:39:25 -0800254 service->server_ = this;
255}
256
Nicolas Noblecfd60732015-03-18 16:27:43 -0700257int Server::AddListeningPort(const grpc::string& addr,
258 ServerCredentials* creds) {
vjpaicad5f0a2015-02-18 22:02:52 -0800259 GPR_ASSERT(!started_);
Craig Tiller42bc87c2015-02-23 08:50:19 -0800260 return creds->AddPortToServer(addr, server_);
vjpaicad5f0a2015-02-18 22:02:52 -0800261}
262
Craig Tiller0db1bef2015-02-09 13:47:39 -0800263bool Server::Start() {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800264 GPR_ASSERT(!started_);
265 started_ = true;
266 grpc_server_start(server_);
267
268 // Start processing rpcs.
Nicolas Noble30862032015-04-24 18:17:45 -0700269 if (!sync_methods_->empty()) {
270 for (auto m = sync_methods_->begin(); m != sync_methods_->end(); m++) {
Vijay Paiced73bd2015-06-17 10:23:28 -0700271 m->SetupRequest();
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700272 m->Request(server_, cq_.cq());
Craig Tillercbd04852015-02-10 17:39:54 -0800273 }
274
Craig Tiller7c72adc2015-02-09 14:07:26 -0800275 ScheduleCallback();
276 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800277
278 return true;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800279}
280
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800281void Server::Shutdown() {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200282 grpc::unique_lock<grpc::mutex> lock(mu_);
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800283 if (started_ && !shutdown_) {
284 shutdown_ = true;
Craig Tillerbce999f2015-05-27 09:55:51 -0700285 grpc_server_shutdown_and_notify(server_, cq_.cq(), new ShutdownRequest());
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800286 cq_.Shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800287
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800288 // Wait for running callbacks to finish.
289 while (num_running_cb_ != 0) {
290 callback_cv_.wait(lock);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800291 }
292 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800293}
294
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800295void Server::Wait() {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200296 grpc::unique_lock<grpc::mutex> lock(mu_);
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800297 while (num_running_cb_ != 0) {
298 callback_cv_.wait(lock);
299 }
300}
301
Craig Tiller50a7a682015-06-04 12:53:40 -0700302void Server::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) {
Craig Tillerbb5227f2015-02-11 13:34:48 -0800303 static const size_t MAX_OPS = 8;
Craig Tiller50a7a682015-06-04 12:53:40 -0700304 size_t nops = 0;
305 grpc_op cops[MAX_OPS];
306 ops->FillOps(cops, &nops);
Craig Tillerbb5227f2015-02-11 13:34:48 -0800307 GPR_ASSERT(GRPC_CALL_OK ==
Craig Tiller50a7a682015-06-04 12:53:40 -0700308 grpc_call_start_batch(call->call(), cops, nops, ops));
Craig Tillerbb5227f2015-02-11 13:34:48 -0800309}
310
Craig Tillerce40de52015-06-05 07:14:58 -0700311Server::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 Tiller50955812015-06-05 08:03:17 -0700318 tag_(tag),
Craig Tillerce40de52015-06-05 07:14:58 -0700319 call_(nullptr) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700320 memset(&initial_metadata_array_, 0, sizeof(initial_metadata_array_));
321}
322
Craig Tillerce40de52015-06-05 07:14:58 -0700323Server::BaseAsyncRequest::~BaseAsyncRequest() {}
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700324
325bool 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 Tillerce40de52015-06-05 07:14:58 -0700330 grpc::string(initial_metadata_array_.metadata[i].value,
331 initial_metadata_array_.metadata[i].value +
332 initial_metadata_array_.metadata[i].value_length)));
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700333 }
334 }
Craig Tiller2f4a49c2015-06-05 09:36:52 -0700335 grpc_metadata_array_destroy(&initial_metadata_array_);
yang-g85c04f92015-07-07 17:47:31 -0700336 context_->set_call(call_);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700337 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 Tiller50955812015-06-05 08:03:17 -0700344 *tag = tag_;
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700345 delete this;
346 return true;
347}
348
Craig Tillerce40de52015-06-05 07:14:58 -0700349Server::RegisteredAsyncRequest::RegisteredAsyncRequest(
350 Server* server, ServerContext* context,
351 ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag)
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700352 : BaseAsyncRequest(server, context, stream, call_cq, tag) {}
353
Craig Tillerce40de52015-06-05 07:14:58 -0700354void Server::RegisteredAsyncRequest::IssueRequest(
355 void* registered_method, grpc_byte_buffer** payload,
356 ServerCompletionQueue* notification_cq) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700357 grpc_server_request_registered_call(
Craig Tillerce40de52015-06-05 07:14:58 -0700358 server_->server_, registered_method, &call_, &context_->deadline_,
359 &initial_metadata_array_, payload, call_cq_->cq(), notification_cq->cq(),
360 this);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700361}
362
Craig Tillerce40de52015-06-05 07:14:58 -0700363Server::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 Tiller7bc97bc2015-06-04 17:22:54 -0700368 grpc_call_details_init(&call_details_);
369 GPR_ASSERT(notification_cq);
370 GPR_ASSERT(call_cq);
Craig Tillerce40de52015-06-05 07:14:58 -0700371 grpc_server_request_call(server->server_, &call_, &call_details_,
372 &initial_metadata_array_, call_cq->cq(),
373 notification_cq->cq(), this);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700374}
375
376bool Server::GenericAsyncRequest::FinalizeResult(void** tag, bool* status) {
377 // TODO(yangg) remove the copy here.
yang-g3deb0062015-06-23 14:06:08 -0700378 if (*status) {
yang-ga58cab32015-06-23 14:37:15 -0700379 static_cast<GenericServerContext*>(context_)->method_ =
380 call_details_.method;
yang-g3deb0062015-06-23 14:06:08 -0700381 static_cast<GenericServerContext*>(context_)->host_ = call_details_.host;
yang-g3deb0062015-06-23 14:06:08 -0700382 }
yang-ga58cab32015-06-23 14:37:15 -0700383 gpr_free(call_details_.method);
384 gpr_free(call_details_.host);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700385 return BaseAsyncRequest::FinalizeResult(tag, status);
386}
387
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800388void Server::ScheduleCallback() {
389 {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200390 grpc::unique_lock<grpc::mutex> lock(mu_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800391 num_running_cb_++;
392 }
vjpai72a44172015-07-16 21:44:44 -0700393 thread_pool_->Add(std::bind(&Server::RunRpc, this));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800394}
395
396void Server::RunRpc() {
397 // Wait for one more incoming rpc.
Craig Tillerc4165772015-02-11 10:51:04 -0800398 bool ok;
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800399 auto* mrd = SyncRequest::Wait(&cq_, &ok);
Craig Tillercbd04852015-02-10 17:39:54 -0800400 if (mrd) {
Craig Tillerbd217572015-02-11 18:10:56 -0800401 ScheduleCallback();
Craig Tillerc4165772015-02-11 10:51:04 -0800402 if (ok) {
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800403 SyncRequest::CallData cd(this, mrd);
Yang Gaob8a5f862015-05-07 16:26:33 -0700404 {
Vijay Paiced73bd2015-06-17 10:23:28 -0700405 mrd->SetupRequest();
Yang Gaob8a5f862015-05-07 16:26:33 -0700406 grpc::unique_lock<grpc::mutex> lock(mu_);
407 if (!shutdown_) {
Craig Tillera33acb72015-05-08 08:02:55 -0700408 mrd->Request(server_, cq_.cq());
Vijay Paiced73bd2015-06-17 10:23:28 -0700409 } else {
410 // destroy the structure that was created
411 mrd->TeardownRequest();
Yang Gaob8a5f862015-05-07 16:26:33 -0700412 }
413 }
Craig Tillerc4165772015-02-11 10:51:04 -0800414 cd.Run();
415 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800416 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800417
418 {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200419 grpc::unique_lock<grpc::mutex> lock(mu_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800420 num_running_cb_--;
421 if (shutdown_) {
422 callback_cv_.notify_all();
423 }
424 }
425}
426
427} // namespace grpc