blob: 44c8a768d2c0d0ff353a7d1e96c969079c09dc8a [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>
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 Gao005eb882015-03-11 22:17:13 -070041#include <grpc++/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 Nobleb7ebd3b2014-11-26 16:33:03 -080047
Craig Tillerc4165772015-02-11 10:51:04 -080048#include "src/cpp/proto/proto_utils.h"
Craig Tiller3d6ceb62015-02-12 14:33:54 -080049#include "src/cpp/util/time.h"
Craig Tillerc4165772015-02-11 10:51:04 -080050
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051namespace grpc {
52
Craig Tillercf133f42015-02-26 14:05:56 -080053class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag {
Craig Tillerc4165772015-02-11 10:51:04 -080054 public:
Craig Tiller1c9a2a92015-02-12 14:10:25 -080055 SyncRequest(RpcServiceMethod* method, void* tag)
Craig Tillerc4165772015-02-11 10:51:04 -080056 : method_(method),
57 tag_(tag),
Craig Tillercf133f42015-02-26 14:05:56 -080058 in_flight_(false),
Craig Tillerc4165772015-02-11 10:51:04 -080059 has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC ||
60 method->method_type() ==
61 RpcMethod::SERVER_STREAMING),
62 has_response_payload_(method->method_type() == RpcMethod::NORMAL_RPC ||
63 method->method_type() ==
64 RpcMethod::CLIENT_STREAMING) {
65 grpc_metadata_array_init(&request_metadata_);
66 }
67
Craig Tiller1c9a2a92015-02-12 14:10:25 -080068 static SyncRequest* Wait(CompletionQueue* cq, bool* ok) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080069 void* tag = nullptr;
Craig Tiller504bd332015-02-11 20:34:33 -080070 *ok = false;
Craig Tillerc4165772015-02-11 10:51:04 -080071 if (!cq->Next(&tag, ok)) {
72 return nullptr;
73 }
Craig Tiller1c9a2a92015-02-12 14:10:25 -080074 auto* mrd = static_cast<SyncRequest*>(tag);
Craig Tillerc4165772015-02-11 10:51:04 -080075 GPR_ASSERT(mrd->in_flight_);
76 return mrd;
77 }
78
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080079 void Request(grpc_server* server) {
Craig Tillerc4165772015-02-11 10:51:04 -080080 GPR_ASSERT(!in_flight_);
81 in_flight_ = true;
82 cq_ = grpc_completion_queue_create();
83 GPR_ASSERT(GRPC_CALL_OK ==
84 grpc_server_request_registered_call(
85 server, tag_, &call_, &deadline_, &request_metadata_,
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080086 has_request_payload_ ? &request_payload_ : nullptr, cq_,
87 this));
Craig Tillerc4165772015-02-11 10:51:04 -080088 }
89
Craig Tillercf133f42015-02-26 14:05:56 -080090 bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
Craig Tillerec3257c2015-02-12 15:59:43 -080091 if (!*status) {
92 grpc_completion_queue_destroy(cq_);
93 }
Craig Tiller645466e2015-02-18 09:18:33 -080094 return true;
Craig Tillerec3257c2015-02-12 15:59:43 -080095 }
Craig Tillerc4165772015-02-11 10:51:04 -080096
Craig Tillercf133f42015-02-26 14:05:56 -080097 class CallData GRPC_FINAL {
Craig Tillerc4165772015-02-11 10:51:04 -080098 public:
Craig Tiller1c9a2a92015-02-12 14:10:25 -080099 explicit CallData(Server* server, SyncRequest* mrd)
Craig Tillerc4165772015-02-11 10:51:04 -0800100 : cq_(mrd->cq_),
Craig Tillerbb5227f2015-02-11 13:34:48 -0800101 call_(mrd->call_, server, &cq_),
Craig Tillerc4165772015-02-11 10:51:04 -0800102 ctx_(mrd->deadline_, mrd->request_metadata_.metadata,
103 mrd->request_metadata_.count),
104 has_request_payload_(mrd->has_request_payload_),
105 has_response_payload_(mrd->has_response_payload_),
106 request_payload_(mrd->request_payload_),
107 method_(mrd->method_) {
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800108 ctx_.call_ = mrd->call_;
Craig Tillerc4165772015-02-11 10:51:04 -0800109 GPR_ASSERT(mrd->in_flight_);
110 mrd->in_flight_ = false;
111 mrd->request_metadata_.count = 0;
112 }
113
Craig Tillerec3257c2015-02-12 15:59:43 -0800114 ~CallData() {
115 if (has_request_payload_ && request_payload_) {
116 grpc_byte_buffer_destroy(request_payload_);
117 }
118 }
119
Craig Tillerc4165772015-02-11 10:51:04 -0800120 void Run() {
Yang Gao7694c352015-03-03 09:48:06 -0800121 std::unique_ptr<grpc::protobuf::Message> req;
122 std::unique_ptr<grpc::protobuf::Message> res;
Craig Tillerc4165772015-02-11 10:51:04 -0800123 if (has_request_payload_) {
124 req.reset(method_->AllocateRequestProto());
125 if (!DeserializeProto(request_payload_, req.get())) {
126 abort(); // for now
127 }
128 }
129 if (has_response_payload_) {
Craig Tiller7c2f3f72015-02-11 13:21:54 -0800130 res.reset(method_->AllocateResponseProto());
Craig Tillerc4165772015-02-11 10:51:04 -0800131 }
Craig Tiller492968f2015-02-18 13:14:03 -0800132 ctx_.BeginCompletionOp(&call_);
Craig Tillerc4165772015-02-11 10:51:04 -0800133 auto status = method_->handler()->RunHandler(
134 MethodHandler::HandlerParameter(&call_, &ctx_, req.get(), res.get()));
135 CallOpBuffer buf;
Craig Tillerbc8e3db2015-02-12 09:56:02 -0800136 if (!ctx_.sent_initial_metadata_) {
137 buf.AddSendInitialMetadata(&ctx_.initial_metadata_);
138 }
Craig Tillerc4165772015-02-11 10:51:04 -0800139 if (has_response_payload_) {
140 buf.AddSendMessage(*res);
141 }
Craig Tiller9dcb0f82015-02-11 15:36:31 -0800142 buf.AddServerSendStatus(&ctx_.trailing_metadata_, status);
Craig Tillerc4165772015-02-11 10:51:04 -0800143 call_.PerformOps(&buf);
144 GPR_ASSERT(cq_.Pluck(&buf));
Craig Tiller492968f2015-02-18 13:14:03 -0800145 void* ignored_tag;
146 bool ignored_ok;
147 cq_.Shutdown();
148 GPR_ASSERT(cq_.Next(&ignored_tag, &ignored_ok) == false);
Craig Tillerc4165772015-02-11 10:51:04 -0800149 }
150
151 private:
152 CompletionQueue cq_;
153 Call call_;
154 ServerContext ctx_;
155 const bool has_request_payload_;
156 const bool has_response_payload_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800157 grpc_byte_buffer* request_payload_;
158 RpcServiceMethod* const method_;
Craig Tillerc4165772015-02-11 10:51:04 -0800159 };
160
161 private:
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800162 RpcServiceMethod* const method_;
163 void* const tag_;
Craig Tillercf133f42015-02-26 14:05:56 -0800164 bool in_flight_;
Craig Tillerc4165772015-02-11 10:51:04 -0800165 const bool has_request_payload_;
166 const bool has_response_payload_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800167 grpc_call* call_;
Craig Tillerc4165772015-02-11 10:51:04 -0800168 gpr_timespec deadline_;
169 grpc_metadata_array request_metadata_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800170 grpc_byte_buffer* request_payload_;
171 grpc_completion_queue* cq_;
Craig Tillerc4165772015-02-11 10:51:04 -0800172};
173
Craig Tiller42bc87c2015-02-23 08:50:19 -0800174Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned)
vjpaicad5f0a2015-02-18 22:02:52 -0800175 : started_(false),
176 shutdown_(false),
177 num_running_cb_(0),
Craig Tiller42bc87c2015-02-23 08:50:19 -0800178 server_(grpc_server_create(cq_.cq(), nullptr)),
vjpaicad5f0a2015-02-18 22:02:52 -0800179 thread_pool_(thread_pool),
Craig Tiller42bc87c2015-02-23 08:50:19 -0800180 thread_pool_owned_(thread_pool_owned) {}
vjpaicad5f0a2015-02-18 22:02:52 -0800181
182Server::~Server() {
183 std::unique_lock<std::mutex> lock(mu_);
184 if (started_ && !shutdown_) {
185 lock.unlock();
186 Shutdown();
187 } else {
188 lock.unlock();
189 }
190 grpc_server_destroy(server_);
191 if (thread_pool_owned_) {
192 delete thread_pool_;
193 }
194}
195
196bool Server::RegisterService(RpcService* service) {
197 for (int i = 0; i < service->GetMethodCount(); ++i) {
198 RpcServiceMethod* method = service->GetMethod(i);
199 void* tag =
200 grpc_server_register_method(server_, method->name(), nullptr, cq_.cq());
201 if (!tag) {
202 gpr_log(GPR_DEBUG, "Attempt to register %s multiple times",
203 method->name());
204 return false;
205 }
206 sync_methods_.emplace_back(method, tag);
207 }
208 return true;
209}
210
211bool Server::RegisterAsyncService(AsynchronousService* service) {
212 GPR_ASSERT(service->dispatch_impl_ == nullptr &&
213 "Can only register an asynchronous service against one server.");
214 service->dispatch_impl_ = this;
215 service->request_args_ = new void* [service->method_count_];
216 for (size_t i = 0; i < service->method_count_; ++i) {
217 void* tag =
218 grpc_server_register_method(server_, service->method_names_[i], nullptr,
219 service->completion_queue()->cq());
220 if (!tag) {
221 gpr_log(GPR_DEBUG, "Attempt to register %s multiple times",
222 service->method_names_[i]);
223 return false;
224 }
225 service->request_args_[i] = tag;
226 }
227 return true;
228}
229
Yang Gao005eb882015-03-11 22:17:13 -0700230void Server::RegisterGenericService(GenericService* service) {
Yang Gao1c402332015-03-05 16:39:25 -0800231 GPR_ASSERT(service->server_ == nullptr &&
Yang Gao005eb882015-03-11 22:17:13 -0700232 "Can only register an generic service against one server.");
Yang Gao1c402332015-03-05 16:39:25 -0800233 service->server_ = this;
234}
235
Craig Tiller42bc87c2015-02-23 08:50:19 -0800236int Server::AddPort(const grpc::string& addr, ServerCredentials* creds) {
vjpaicad5f0a2015-02-18 22:02:52 -0800237 GPR_ASSERT(!started_);
Craig Tiller42bc87c2015-02-23 08:50:19 -0800238 return creds->AddPortToServer(addr, server_);
vjpaicad5f0a2015-02-18 22:02:52 -0800239}
240
Craig Tiller0db1bef2015-02-09 13:47:39 -0800241bool Server::Start() {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242 GPR_ASSERT(!started_);
243 started_ = true;
244 grpc_server_start(server_);
245
246 // Start processing rpcs.
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800247 if (!sync_methods_.empty()) {
248 for (auto& m : sync_methods_) {
Craig Tiller3b29b562015-02-11 12:58:46 -0800249 m.Request(server_);
Craig Tillercbd04852015-02-10 17:39:54 -0800250 }
251
Craig Tiller7c72adc2015-02-09 14:07:26 -0800252 ScheduleCallback();
253 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800254
255 return true;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800256}
257
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800258void Server::Shutdown() {
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800259 std::unique_lock<std::mutex> lock(mu_);
260 if (started_ && !shutdown_) {
261 shutdown_ = true;
262 grpc_server_shutdown(server_);
263 cq_.Shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800264
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800265 // Wait for running callbacks to finish.
266 while (num_running_cb_ != 0) {
267 callback_cv_.wait(lock);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800268 }
269 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800270}
271
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800272void Server::Wait() {
273 std::unique_lock<std::mutex> lock(mu_);
274 while (num_running_cb_ != 0) {
275 callback_cv_.wait(lock);
276 }
277}
278
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800279void Server::PerformOpsOnCall(CallOpBuffer* buf, Call* call) {
Craig Tillerbb5227f2015-02-11 13:34:48 -0800280 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 Tiller8c8d0aa2015-02-12 11:38:36 -0800285 grpc_call_start_batch(call->call(), ops, nops, buf));
Craig Tillerbb5227f2015-02-11 13:34:48 -0800286}
287
Craig Tillercf133f42015-02-26 14:05:56 -0800288class Server::AsyncRequest GRPC_FINAL : public CompletionQueueTag {
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800289 public:
290 AsyncRequest(Server* server, void* registered_method, ServerContext* ctx,
Yang Gao7694c352015-03-03 09:48:06 -0800291 grpc::protobuf::Message* request,
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800292 ServerAsyncStreamingInterface* stream, CompletionQueue* cq,
293 void* tag)
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800294 : tag_(tag),
295 request_(request),
296 stream_(stream),
297 cq_(cq),
298 ctx_(ctx),
Yang Gao005eb882015-03-11 22:17:13 -0700299 generic_ctx_(nullptr),
Craig Tillercf133f42015-02-26 14:05:56 -0800300 server_(server),
301 call_(nullptr),
302 payload_(nullptr) {
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800303 memset(&array_, 0, sizeof(array_));
Yang Gao1c402332015-03-05 16:39:25 -0800304 grpc_call_details_init(&call_details_);
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800305 grpc_server_request_registered_call(
Yang Gao1c402332015-03-05 16:39:25 -0800306 server->server_, registered_method, &call_, &call_details_.deadline,
307 &array_, request ? &payload_ : nullptr, cq->cq(), this);
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800308 }
309
Yang Gao005eb882015-03-11 22:17:13 -0700310 AsyncRequest(Server* server, GenericServerContext* ctx,
Yang Gao1c402332015-03-05 16:39:25 -0800311 ServerAsyncStreamingInterface* stream, CompletionQueue* cq,
312 void* tag)
313 : tag_(tag),
314 request_(nullptr),
315 stream_(stream),
316 cq_(cq),
317 ctx_(nullptr),
Yang Gao005eb882015-03-11 22:17:13 -0700318 generic_ctx_(ctx),
Yang Gao1c402332015-03-05 16:39:25 -0800319 server_(server),
320 call_(nullptr),
321 payload_(nullptr) {
322 memset(&array_, 0, sizeof(array_));
323 grpc_call_details_init(&call_details_);
324 grpc_server_request_call(
325 server->server_, &call_, &call_details_, &array_, cq->cq(), this);
326 }
327
328
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800329 ~AsyncRequest() {
330 if (payload_) {
331 grpc_byte_buffer_destroy(payload_);
332 }
333 grpc_metadata_array_destroy(&array_);
334 }
335
Craig Tillercf133f42015-02-26 14:05:56 -0800336 bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800337 *tag = tag_;
Vijay Paidbb79632015-03-03 11:54:27 -0800338 bool orig_status = *status;
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800339 if (*status && request_) {
340 if (payload_) {
Craig Tiller645466e2015-02-18 09:18:33 -0800341 *status = DeserializeProto(payload_, request_);
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800342 } else {
343 *status = false;
344 }
345 }
Yang Gao005eb882015-03-11 22:17:13 -0700346 ServerContext* ctx = ctx_ ? ctx_ : generic_ctx_;
Yang Gao1c402332015-03-05 16:39:25 -0800347 GPR_ASSERT(ctx);
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800348 if (*status) {
Yang Gao1c402332015-03-05 16:39:25 -0800349 ctx->deadline_ = Timespec2Timepoint(call_details_.deadline);
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800350 for (size_t i = 0; i < array_.count; i++) {
Yang Gao1c402332015-03-05 16:39:25 -0800351 ctx->client_metadata_.insert(std::make_pair(
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800352 grpc::string(array_.metadata[i].key),
353 grpc::string(
354 array_.metadata[i].value,
355 array_.metadata[i].value + array_.metadata[i].value_length)));
356 }
Yang Gao005eb882015-03-11 22:17:13 -0700357 if (generic_ctx_) {
358 generic_ctx_->method_ = call_details_.method;
359 generic_ctx_->host_ = call_details_.host;
Yang Gao5f4539f2015-03-06 16:11:16 -0800360 }
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800361 }
Yang Gao1c402332015-03-05 16:39:25 -0800362 ctx->call_ = call_;
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800363 Call call(call_, server_, cq_);
Vijay Paidbb79632015-03-03 11:54:27 -0800364 if (orig_status && call_) {
Yang Gao1c402332015-03-05 16:39:25 -0800365 ctx->BeginCompletionOp(&call);
Vijay Pai0823cb72015-03-02 15:48:51 -0800366 }
Craig Tiller645466e2015-02-18 09:18:33 -0800367 // just the pointers inside call are copied here
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800368 stream_->BindCall(&call);
369 delete this;
Craig Tiller645466e2015-02-18 09:18:33 -0800370 return true;
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800371 }
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800372
373 private:
374 void* const tag_;
Yang Gao7694c352015-03-03 09:48:06 -0800375 grpc::protobuf::Message* const request_;
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800376 ServerAsyncStreamingInterface* const stream_;
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800377 CompletionQueue* const cq_;
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800378 ServerContext* const ctx_;
Yang Gao005eb882015-03-11 22:17:13 -0700379 GenericServerContext* const generic_ctx_;
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800380 Server* const server_;
Craig Tillercf133f42015-02-26 14:05:56 -0800381 grpc_call* call_;
Yang Gao1c402332015-03-05 16:39:25 -0800382 grpc_call_details call_details_;
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800383 grpc_metadata_array array_;
Craig Tillercf133f42015-02-26 14:05:56 -0800384 grpc_byte_buffer* payload_;
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800385};
386
387void Server::RequestAsyncCall(void* registered_method, ServerContext* context,
Yang Gao7694c352015-03-03 09:48:06 -0800388 grpc::protobuf::Message* request,
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800389 ServerAsyncStreamingInterface* stream,
390 CompletionQueue* cq, void* tag) {
391 new AsyncRequest(this, registered_method, context, request, stream, cq, tag);
392}
Yang Gao5f4539f2015-03-06 16:11:16 -0800393
Yang Gao005eb882015-03-11 22:17:13 -0700394void Server::RequestGenericCall(GenericServerContext* context,
395 ServerAsyncStreamingInterface* stream,
396 CompletionQueue* cq, void* tag) {
Yang Gao1c402332015-03-05 16:39:25 -0800397 new AsyncRequest(this, context, stream, cq, tag);
398}
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800399
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800400void Server::ScheduleCallback() {
401 {
402 std::unique_lock<std::mutex> lock(mu_);
403 num_running_cb_++;
404 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800405 thread_pool_->ScheduleCallback(std::bind(&Server::RunRpc, this));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800406}
407
408void Server::RunRpc() {
409 // Wait for one more incoming rpc.
Craig Tillerc4165772015-02-11 10:51:04 -0800410 bool ok;
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800411 auto* mrd = SyncRequest::Wait(&cq_, &ok);
Craig Tillercbd04852015-02-10 17:39:54 -0800412 if (mrd) {
Craig Tillerbd217572015-02-11 18:10:56 -0800413 ScheduleCallback();
Craig Tillerc4165772015-02-11 10:51:04 -0800414 if (ok) {
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800415 SyncRequest::CallData cd(this, mrd);
Craig Tiller3b29b562015-02-11 12:58:46 -0800416 mrd->Request(server_);
Craig Tillercbd04852015-02-10 17:39:54 -0800417
Craig Tillerc4165772015-02-11 10:51:04 -0800418 cd.Run();
419 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800420 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800421
422 {
423 std::unique_lock<std::mutex> lock(mu_);
424 num_running_cb_--;
425 if (shutdown_) {
426 callback_cv_.notify_all();
427 }
428 }
429}
430
431} // namespace grpc