blob: 6bd3ecda32a23c7cfa622ab4b67392b40e300d50 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08003 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08007 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08009 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080015 *
16 */
17
18#include <grpc++/server.h>
yang-g9e2f90c2015-08-21 15:35:03 -070019
yang-g66e6c232017-08-30 15:51:53 -070020#include <cstdlib>
Yuchen Zenga42ec212016-04-29 13:03:06 -070021#include <sstream>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080022#include <utility>
23
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080024#include <grpc++/completion_queue.h>
yang-g9e2f90c2015-08-21 15:35:03 -070025#include <grpc++/generic/async_generic_service.h>
yang-g50993b72016-12-29 10:00:27 -080026#include <grpc++/impl/codegen/async_unary_call.h>
David Garcia Quintase1300de2016-01-27 18:41:26 -080027#include <grpc++/impl/codegen/completion_queue_tag.h>
28#include <grpc++/impl/grpc_library.h>
Craig Tiller15f383c2016-01-07 12:45:32 -080029#include <grpc++/impl/method_handler_impl.h>
yangg1b151092015-01-09 15:31:05 -080030#include <grpc++/impl/rpc_service_method.h>
Yuchen Zenga42ec212016-04-29 13:03:06 -070031#include <grpc++/impl/server_initializer.h>
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080032#include <grpc++/impl/service_type.h>
Julien Boeuf5be92a32015-08-28 16:28:18 -070033#include <grpc++/security/server_credentials.h>
David Garcia Quintase1300de2016-01-27 18:41:26 -080034#include <grpc++/server_context.h>
yang-g9e2f90c2015-08-21 15:35:03 -070035#include <grpc++/support/time.h>
David Garcia Quintase1300de2016-01-27 18:41:26 -080036#include <grpc/grpc.h>
37#include <grpc/support/alloc.h>
38#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
Vijay Pai3d7d5f42017-05-04 10:02:24 -070040#include "src/core/ext/transport/inproc/inproc_transport.h"
Craig Tiller9533d042016-03-25 17:11:06 -070041#include "src/core/lib/profiling/timers.h"
yang-g66e6c232017-08-30 15:51:53 -070042#include "src/core/lib/surface/call.h"
Vijay Pai3d7d5f42017-05-04 10:02:24 -070043#include "src/cpp/client/create_channel_internal.h"
yang-gc3c475f2016-12-27 10:37:26 -080044#include "src/cpp/server/health/default_health_check_service.h"
Sree Kuchibhotlaf72ec6b2016-10-24 11:09:43 -070045#include "src/cpp/thread_manager/thread_manager.h"
Craig Tillerc4165772015-02-11 10:51:04 -080046
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047namespace grpc {
48
Vijay Paic0b2acb2016-11-01 16:31:56 -070049class DefaultGlobalCallbacks final : public Server::GlobalCallbacks {
Craig Tiller7221d992015-11-24 06:59:33 -080050 public:
Vijay Paic0b2acb2016-11-01 16:31:56 -070051 ~DefaultGlobalCallbacks() override {}
52 void PreSynchronousRequest(ServerContext* context) override {}
53 void PostSynchronousRequest(ServerContext* context) override {}
Craig Tiller7221d992015-11-24 06:59:33 -080054};
55
Craig Tiller64616492016-01-26 14:16:20 -080056static std::shared_ptr<Server::GlobalCallbacks> g_callbacks = nullptr;
Craig Tiller8352b9e2015-12-02 11:43:40 -080057static gpr_once g_once_init_callbacks = GPR_ONCE_INIT;
58
Craig Tiller8352b9e2015-12-02 11:43:40 -080059static void InitGlobalCallbacks() {
Vijay Paib645a2d2016-06-09 18:39:06 -070060 if (!g_callbacks) {
Craig Tiller64616492016-01-26 14:16:20 -080061 g_callbacks.reset(new DefaultGlobalCallbacks());
Craig Tiller8352b9e2015-12-02 11:43:40 -080062 }
63}
Craig Tiller7221d992015-11-24 06:59:33 -080064
Craig Tiller8f7bff72015-08-17 13:23:14 -070065class Server::UnimplementedAsyncRequestContext {
66 protected:
67 UnimplementedAsyncRequestContext() : generic_stream_(&server_context_) {}
68
69 GenericServerContext server_context_;
70 GenericServerAsyncReaderWriter generic_stream_;
71};
72
Vijay Paic0b2acb2016-11-01 16:31:56 -070073class Server::UnimplementedAsyncRequest final
Craig Tiller8f7bff72015-08-17 13:23:14 -070074 : public UnimplementedAsyncRequestContext,
75 public GenericAsyncRequest {
76 public:
77 UnimplementedAsyncRequest(Server* server, ServerCompletionQueue* cq)
78 : GenericAsyncRequest(server, &server_context_, &generic_stream_, cq, cq,
79 NULL, false),
80 server_(server),
81 cq_(cq) {}
82
Vijay Paic0b2acb2016-11-01 16:31:56 -070083 bool FinalizeResult(void** tag, bool* status) override;
Craig Tiller8f7bff72015-08-17 13:23:14 -070084
85 ServerContext* context() { return &server_context_; }
86 GenericServerAsyncReaderWriter* stream() { return &generic_stream_; }
87
88 private:
89 Server* const server_;
90 ServerCompletionQueue* const cq_;
91};
92
Vijay Pai6abd2002017-07-25 00:43:30 -070093typedef SneakyCallOpSet<CallOpSendInitialMetadata, CallOpServerSendStatus>
Craig Tiller8f7bff72015-08-17 13:23:14 -070094 UnimplementedAsyncResponseOp;
Vijay Paic0b2acb2016-11-01 16:31:56 -070095class Server::UnimplementedAsyncResponse final
Craig Tiller8f7bff72015-08-17 13:23:14 -070096 : public UnimplementedAsyncResponseOp {
97 public:
98 UnimplementedAsyncResponse(UnimplementedAsyncRequest* request);
99 ~UnimplementedAsyncResponse() { delete request_; }
100
Vijay Paic0b2acb2016-11-01 16:31:56 -0700101 bool FinalizeResult(void** tag, bool* status) override {
Craig Tiller8f7bff72015-08-17 13:23:14 -0700102 bool r = UnimplementedAsyncResponseOp::FinalizeResult(tag, status);
103 delete this;
104 return r;
105 }
106
107 private:
108 UnimplementedAsyncRequest* const request_;
109};
110
Vijay Pai6abd2002017-07-25 00:43:30 -0700111class ShutdownTag : public CompletionQueueTag {
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700112 public:
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700113 bool FinalizeResult(void** tag, bool* status) { return false; }
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700114};
115
Vijay Pai6abd2002017-07-25 00:43:30 -0700116class DummyTag : public CompletionQueueTag {
Sree Kuchibhotla982a6f22017-03-03 02:19:31 -0800117 public:
118 bool FinalizeResult(void** tag, bool* status) {
119 *status = true;
120 return true;
121 }
122};
123
Vijay Pai6abd2002017-07-25 00:43:30 -0700124class Server::SyncRequest final : public CompletionQueueTag {
Craig Tillerc4165772015-02-11 10:51:04 -0800125 public:
Vijay Pai6abd2002017-07-25 00:43:30 -0700126 SyncRequest(RpcServiceMethod* method, void* tag)
Craig Tillerc4165772015-02-11 10:51:04 -0800127 : method_(method),
128 tag_(tag),
Craig Tillercf133f42015-02-26 14:05:56 -0800129 in_flight_(false),
Vijay Pai6abd2002017-07-25 00:43:30 -0700130 has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC ||
131 method->method_type() ==
132 RpcMethod::SERVER_STREAMING),
yang-g9b7757d2015-08-13 11:15:53 -0700133 call_details_(nullptr),
Vijay Paiced73bd2015-06-17 10:23:28 -0700134 cq_(nullptr) {
Craig Tillerc4165772015-02-11 10:51:04 -0800135 grpc_metadata_array_init(&request_metadata_);
136 }
137
yang-g9b7757d2015-08-13 11:15:53 -0700138 ~SyncRequest() {
139 if (call_details_) {
140 delete call_details_;
141 }
142 grpc_metadata_array_destroy(&request_metadata_);
143 }
Yang Gao7b49a772015-05-28 14:07:54 -0700144
Sree Kuchibhotlaf2c32152017-03-22 03:01:24 -0700145 void SetupRequest() { cq_ = grpc_completion_queue_create_for_pluck(nullptr); }
Vijay Paiced73bd2015-06-17 10:23:28 -0700146
147 void TeardownRequest() {
148 grpc_completion_queue_destroy(cq_);
149 cq_ = nullptr;
150 }
151
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700152 void Request(grpc_server* server, grpc_completion_queue* notify_cq) {
Vijay Paiced73bd2015-06-17 10:23:28 -0700153 GPR_ASSERT(cq_ && !in_flight_);
Craig Tillerc4165772015-02-11 10:51:04 -0800154 in_flight_ = true;
yang-g9b7757d2015-08-13 11:15:53 -0700155 if (tag_) {
yang-g097cbfc2017-07-28 12:51:50 -0700156 if (GRPC_CALL_OK !=
157 grpc_server_request_registered_call(
yang-g0eaf7de2017-07-05 16:50:51 -0700158 server, tag_, &call_, &deadline_, &request_metadata_,
159 has_request_payload_ ? &request_payload_ : nullptr, cq_,
yang-g097cbfc2017-07-28 12:51:50 -0700160 notify_cq, this)) {
yang-g0eaf7de2017-07-05 16:50:51 -0700161 TeardownRequest();
162 return;
163 }
yang-g9b7757d2015-08-13 11:15:53 -0700164 } else {
165 if (!call_details_) {
166 call_details_ = new grpc_call_details;
167 grpc_call_details_init(call_details_);
168 }
yang-g0eaf7de2017-07-05 16:50:51 -0700169 if (grpc_server_request_call(server, &call_, call_details_,
170 &request_metadata_, cq_, notify_cq,
171 this) != GRPC_CALL_OK) {
172 TeardownRequest();
173 return;
174 }
yang-g9b7757d2015-08-13 11:15:53 -0700175 }
Craig Tillerc4165772015-02-11 10:51:04 -0800176 }
177
Vijay Paic0b2acb2016-11-01 16:31:56 -0700178 bool FinalizeResult(void** tag, bool* status) override {
Craig Tillerec3257c2015-02-12 15:59:43 -0800179 if (!*status) {
180 grpc_completion_queue_destroy(cq_);
181 }
yang-g9b7757d2015-08-13 11:15:53 -0700182 if (call_details_) {
183 deadline_ = call_details_->deadline;
184 grpc_call_details_destroy(call_details_);
185 grpc_call_details_init(call_details_);
186 }
Craig Tiller645466e2015-02-18 09:18:33 -0800187 return true;
Craig Tillerec3257c2015-02-12 15:59:43 -0800188 }
Craig Tillerc4165772015-02-11 10:51:04 -0800189
Vijay Paic0b2acb2016-11-01 16:31:56 -0700190 class CallData final {
Craig Tillerc4165772015-02-11 10:51:04 -0800191 public:
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800192 explicit CallData(Server* server, SyncRequest* mrd)
Craig Tillerc4165772015-02-11 10:51:04 -0800193 : cq_(mrd->cq_),
yang-gf07ed452017-02-16 23:01:28 -0800194 call_(mrd->call_, server, &cq_, server->max_receive_message_size()),
yang-gc42d8442017-02-15 00:05:00 -0800195 ctx_(mrd->deadline_, &mrd->request_metadata_),
Craig Tillerc4165772015-02-11 10:51:04 -0800196 has_request_payload_(mrd->has_request_payload_),
Craig Tillerc4165772015-02-11 10:51:04 -0800197 request_payload_(mrd->request_payload_),
198 method_(mrd->method_) {
yang-g85c04f92015-07-07 17:47:31 -0700199 ctx_.set_call(mrd->call_);
Yang Gao1205f6f2015-03-22 15:18:14 -0700200 ctx_.cq_ = &cq_;
Craig Tillerc4165772015-02-11 10:51:04 -0800201 GPR_ASSERT(mrd->in_flight_);
202 mrd->in_flight_ = false;
203 mrd->request_metadata_.count = 0;
204 }
205
Craig Tillerec3257c2015-02-12 15:59:43 -0800206 ~CallData() {
207 if (has_request_payload_ && request_payload_) {
208 grpc_byte_buffer_destroy(request_payload_);
209 }
210 }
211
Craig Tiller64616492016-01-26 14:16:20 -0800212 void Run(std::shared_ptr<GlobalCallbacks> global_callbacks) {
Craig Tiller492968f2015-02-18 13:14:03 -0800213 ctx_.BeginCompletionOp(&call_);
Craig Tiller64616492016-01-26 14:16:20 -0800214 global_callbacks->PreSynchronousRequest(&ctx_);
Vijay Pai6abd2002017-07-25 00:43:30 -0700215 method_->handler()->RunHandler(
216 MethodHandler::HandlerParameter(&call_, &ctx_, request_payload_));
Craig Tiller64616492016-01-26 14:16:20 -0800217 global_callbacks->PostSynchronousRequest(&ctx_);
Craig Tiller928ec8e2015-06-05 08:45:45 -0700218 request_payload_ = nullptr;
Sree Kuchibhotlafa71f6f2017-04-01 17:32:34 -0700219
Craig Tiller492968f2015-02-18 13:14:03 -0800220 cq_.Shutdown();
Sree Kuchibhotlafa71f6f2017-04-01 17:32:34 -0700221
Vijay Pai6abd2002017-07-25 00:43:30 -0700222 CompletionQueueTag* op_tag = ctx_.GetCompletionOpTag();
Sree Kuchibhotlafa71f6f2017-04-01 17:32:34 -0700223 cq_.TryPluck(op_tag, gpr_inf_future(GPR_CLOCK_REALTIME));
224
Sree Kuchibhotla44268002017-04-01 17:33:50 -0700225 /* Ensure the cq_ is shutdown */
Sree Kuchibhotlafa71f6f2017-04-01 17:32:34 -0700226 DummyTag ignored_tag;
Sree Kuchibhotla982a6f22017-03-03 02:19:31 -0800227 GPR_ASSERT(cq_.Pluck(&ignored_tag) == false);
Craig Tillerc4165772015-02-11 10:51:04 -0800228 }
229
230 private:
231 CompletionQueue cq_;
Vijay Pai6abd2002017-07-25 00:43:30 -0700232 Call call_;
Craig Tillerc4165772015-02-11 10:51:04 -0800233 ServerContext ctx_;
234 const bool has_request_payload_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800235 grpc_byte_buffer* request_payload_;
Vijay Pai6abd2002017-07-25 00:43:30 -0700236 RpcServiceMethod* const method_;
Craig Tillerc4165772015-02-11 10:51:04 -0800237 };
238
239 private:
Vijay Pai6abd2002017-07-25 00:43:30 -0700240 RpcServiceMethod* const method_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800241 void* const tag_;
Craig Tillercf133f42015-02-26 14:05:56 -0800242 bool in_flight_;
Craig Tillerc4165772015-02-11 10:51:04 -0800243 const bool has_request_payload_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800244 grpc_call* call_;
yang-g9b7757d2015-08-13 11:15:53 -0700245 grpc_call_details* call_details_;
Craig Tillerc4165772015-02-11 10:51:04 -0800246 gpr_timespec deadline_;
247 grpc_metadata_array request_metadata_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800248 grpc_byte_buffer* request_payload_;
249 grpc_completion_queue* cq_;
Craig Tillerc4165772015-02-11 10:51:04 -0800250};
251
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700252// Implementation of ThreadManager. Each instance of SyncRequestThreadManager
253// manages a pool of threads that poll for incoming Sync RPCs and call the
254// appropriate RPC handlers
255class Server::SyncRequestThreadManager : public ThreadManager {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700256 public:
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700257 SyncRequestThreadManager(Server* server, CompletionQueue* server_cq,
258 std::shared_ptr<GlobalCallbacks> global_callbacks,
259 int min_pollers, int max_pollers,
260 int cq_timeout_msec)
261 : ThreadManager(min_pollers, max_pollers),
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700262 server_(server),
263 server_cq_(server_cq),
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700264 cq_timeout_msec_(cq_timeout_msec),
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700265 global_callbacks_(global_callbacks) {}
266
Vijay Paic0b2acb2016-11-01 16:31:56 -0700267 WorkStatus PollForWork(void** tag, bool* ok) override {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700268 *tag = nullptr;
269 gpr_timespec deadline =
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700270 gpr_time_from_millis(cq_timeout_msec_, GPR_TIMESPAN);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700271
272 switch (server_cq_->AsyncNext(tag, ok, deadline)) {
273 case CompletionQueue::TIMEOUT:
274 return TIMEOUT;
275 case CompletionQueue::SHUTDOWN:
276 return SHUTDOWN;
277 case CompletionQueue::GOT_EVENT:
278 return WORK_FOUND;
279 }
280
281 GPR_UNREACHABLE_CODE(return TIMEOUT);
282 }
283
Vijay Paic0b2acb2016-11-01 16:31:56 -0700284 void DoWork(void* tag, bool ok) override {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700285 SyncRequest* sync_req = static_cast<SyncRequest*>(tag);
Sree Kuchibhotla4306eee2016-09-21 09:56:29 -0700286
287 if (!sync_req) {
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700288 // No tag. Nothing to work on. This is an unlikley scenario and possibly a
289 // bug in RPC Manager implementation.
290 gpr_log(GPR_ERROR, "Sync server. DoWork() was called with NULL tag");
Sree Kuchibhotla4306eee2016-09-21 09:56:29 -0700291 return;
292 }
293
294 if (ok) {
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700295 // Calldata takes ownership of the completion queue inside sync_req
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700296 SyncRequest::CallData cd(server_, sync_req);
yang-g0eaf7de2017-07-05 16:50:51 -0700297 // Prepare for the next request
298 if (!IsShutdown()) {
299 sync_req->SetupRequest(); // Create new completion queue for sync_req
300 sync_req->Request(server_->c_server(), server_cq_->cq());
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700301 }
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700302
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700303 GPR_TIMER_SCOPE("cd.Run()", 0);
304 cd.Run(global_callbacks_);
305 }
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700306 // TODO (sreek) If ok is false here (which it isn't in case of
307 // grpc_request_registered_call), we should still re-queue the request
308 // object
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700309 }
310
Vijay Pai6abd2002017-07-25 00:43:30 -0700311 void AddSyncMethod(RpcServiceMethod* method, void* tag) {
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700312 sync_requests_.emplace_back(new SyncRequest(method, tag));
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700313 }
314
315 void AddUnknownSyncMethod() {
Sree Kuchibhotlada069a52016-10-19 11:22:29 -0700316 if (!sync_requests_.empty()) {
Vijay Pai6abd2002017-07-25 00:43:30 -0700317 unknown_method_.reset(new RpcServiceMethod(
318 "unknown", RpcMethod::BIDI_STREAMING, new UnknownMethodHandler));
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700319 sync_requests_.emplace_back(
320 new SyncRequest(unknown_method_.get(), nullptr));
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700321 }
322 }
323
Craig Tiller991c1012017-04-18 19:43:14 +0000324 void Shutdown() override {
Craig Tillera3e87892017-04-18 13:08:08 -0700325 ThreadManager::Shutdown();
yang-g0eaf7de2017-07-05 16:50:51 -0700326 server_cq_->Shutdown();
Craig Tillera3e87892017-04-18 13:08:08 -0700327 }
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700328
Craig Tillera3e87892017-04-18 13:08:08 -0700329 void Wait() override {
330 ThreadManager::Wait();
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700331 // Drain any pending items from the queue
332 void* tag;
333 bool ok;
334 while (server_cq_->Next(&tag, &ok)) {
Craig Tillera3e87892017-04-18 13:08:08 -0700335 // Do nothing
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700336 }
337 }
338
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700339 void Start() {
Sree Kuchibhotlada069a52016-10-19 11:22:29 -0700340 if (!sync_requests_.empty()) {
341 for (auto m = sync_requests_.begin(); m != sync_requests_.end(); m++) {
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700342 (*m)->SetupRequest();
343 (*m)->Request(server_->c_server(), server_cq_->cq());
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700344 }
345
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700346 Initialize(); // ThreadManager's Initialize()
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700347 }
348 }
349
350 private:
351 Server* server_;
352 CompletionQueue* server_cq_;
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700353 int cq_timeout_msec_;
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700354 std::vector<std::unique_ptr<SyncRequest>> sync_requests_;
Vijay Pai6abd2002017-07-25 00:43:30 -0700355 std::unique_ptr<RpcServiceMethod> unknown_method_;
356 std::unique_ptr<RpcServiceMethod> health_check_;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700357 std::shared_ptr<Server::GlobalCallbacks> global_callbacks_;
358};
359
David Garcia Quintasd79ef3a2016-01-28 00:21:27 -0800360static internal::GrpcLibraryInitializer g_gli_initializer;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700361Server::Server(
Sree Kuchibhotlae4eb51f2016-10-18 11:51:28 -0700362 int max_receive_message_size, ChannelArguments* args,
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700363 std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
364 sync_server_cqs,
Sree Kuchibhotlae4eb51f2016-10-18 11:51:28 -0700365 int min_pollers, int max_pollers, int sync_cq_timeout_msec)
Mark D. Roth69803622016-09-06 08:15:36 -0700366 : max_receive_message_size_(max_receive_message_size),
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700367 sync_server_cqs_(sync_server_cqs),
Yang Gao3921c562015-04-30 16:07:06 -0700368 started_(false),
vjpaicad5f0a2015-02-18 22:02:52 -0800369 shutdown_(false),
yang-g6ec11f22016-07-14 14:53:35 -0700370 shutdown_notified_(false),
yang-g9b7757d2015-08-13 11:15:53 -0700371 has_generic_service_(false),
yang-geb62c942016-02-17 15:37:25 -0800372 server_(nullptr),
yang-gad327642016-12-12 14:32:09 -0800373 server_initializer_(new ServerInitializer(this)),
374 health_check_service_disabled_(false) {
David Garcia Quintasd79ef3a2016-01-28 00:21:27 -0800375 g_gli_initializer.summon();
Craig Tiller8352b9e2015-12-02 11:43:40 -0800376 gpr_once_init(&g_once_init_callbacks, InitGlobalCallbacks);
Craig Tiller64616492016-01-26 14:16:20 -0800377 global_callbacks_ = g_callbacks;
yang-geb62c942016-02-17 15:37:25 -0800378 global_callbacks_->UpdateArguments(args);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700379
380 for (auto it = sync_server_cqs_->begin(); it != sync_server_cqs_->end();
381 it++) {
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700382 sync_req_mgrs_.emplace_back(new SyncRequestThreadManager(
383 this, (*it).get(), global_callbacks_, min_pollers, max_pollers,
384 sync_cq_timeout_msec));
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700385 }
386
yang-geb62c942016-02-17 15:37:25 -0800387 grpc_channel_args channel_args;
388 args->SetChannelArgs(&channel_args);
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700389
yang-g8d668d82016-12-01 15:09:28 -0800390 for (size_t i = 0; i < channel_args.num_args; i++) {
yang-gb90631d2016-12-29 14:08:13 -0800391 if (0 ==
392 strcmp(channel_args.args[i].key, kHealthCheckServiceInterfaceArg)) {
yang-g6d0fbfa2016-12-06 16:49:04 -0800393 if (channel_args.args[i].value.pointer.p == nullptr) {
yang-gad327642016-12-12 14:32:09 -0800394 health_check_service_disabled_ = true;
yang-g8d668d82016-12-01 15:09:28 -0800395 } else {
yang-g6d0fbfa2016-12-06 16:49:04 -0800396 health_check_service_.reset(static_cast<HealthCheckServiceInterface*>(
397 channel_args.args[i].value.pointer.p));
yang-g8d668d82016-12-01 15:09:28 -0800398 }
399 break;
400 }
401 }
402
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700403 server_ = grpc_server_create(&channel_args, nullptr);
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700404}
vjpaicad5f0a2015-02-18 22:02:52 -0800405
406Server::~Server() {
Ruyi Wangb486ba62015-03-14 22:19:44 +0800407 {
Vijay Pai320ed132016-11-01 17:16:55 -0700408 std::unique_lock<std::mutex> lock(mu_);
Ruyi Wangb486ba62015-03-14 22:19:44 +0800409 if (started_ && !shutdown_) {
410 lock.unlock();
411 Shutdown();
yang-gb9711732016-01-15 16:53:08 -0800412 } else if (!started_) {
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700413 // Shutdown the completion queues
414 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
Craig Tiller991c1012017-04-18 19:43:14 +0000415 (*it)->Shutdown();
Sree Kuchibhotla4306eee2016-09-21 09:56:29 -0700416 }
Ruyi Wangb486ba62015-03-14 22:19:44 +0800417 }
vjpaicad5f0a2015-02-18 22:02:52 -0800418 }
Sree Kuchibhotlabb5519f2016-07-19 11:00:39 -0700419
vjpaicad5f0a2015-02-18 22:02:52 -0800420 grpc_server_destroy(server_);
vjpaicad5f0a2015-02-18 22:02:52 -0800421}
422
Craig Tiller7221d992015-11-24 06:59:33 -0800423void Server::SetGlobalCallbacks(GlobalCallbacks* callbacks) {
Vijay Paib645a2d2016-06-09 18:39:06 -0700424 GPR_ASSERT(!g_callbacks);
425 GPR_ASSERT(callbacks);
Craig Tiller64616492016-01-26 14:16:20 -0800426 g_callbacks.reset(callbacks);
Craig Tiller7221d992015-11-24 06:59:33 -0800427}
428
Adam Michalik4ad746e2016-06-07 15:02:59 -0700429grpc_server* Server::c_server() { return server_; }
Adam Michalikb97e2d12016-06-02 12:12:55 -0700430
Vijay Pai3d7d5f42017-05-04 10:02:24 -0700431std::shared_ptr<Channel> Server::InProcessChannel(
432 const ChannelArguments& args) {
433 grpc_channel_args channel_args = args.c_channel_args();
434 return CreateChannelInternal(
435 "inproc", grpc_inproc_channel_create(server_, &channel_args, nullptr));
436}
437
Craig Tiller06cb1a92016-04-04 08:10:47 -0700438static grpc_server_register_method_payload_handling PayloadHandlingForMethod(
Vijay Pai6abd2002017-07-25 00:43:30 -0700439 RpcServiceMethod* method) {
Craig Tiller06cb1a92016-04-04 08:10:47 -0700440 switch (method->method_type()) {
Vijay Pai6abd2002017-07-25 00:43:30 -0700441 case RpcMethod::NORMAL_RPC:
442 case RpcMethod::SERVER_STREAMING:
Craig Tiller06cb1a92016-04-04 08:10:47 -0700443 return GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER;
Vijay Pai6abd2002017-07-25 00:43:30 -0700444 case RpcMethod::CLIENT_STREAMING:
445 case RpcMethod::BIDI_STREAMING:
Craig Tiller06cb1a92016-04-04 08:10:47 -0700446 return GRPC_SRM_PAYLOAD_NONE;
447 }
448 GPR_UNREACHABLE_CODE(return GRPC_SRM_PAYLOAD_NONE;);
449}
450
Craig Tiller15f383c2016-01-07 12:45:32 -0800451bool Server::RegisterService(const grpc::string* host, Service* service) {
452 bool has_async_methods = service->has_async_methods();
453 if (has_async_methods) {
454 GPR_ASSERT(service->server_ == nullptr &&
455 "Can only register an asynchronous service against one server.");
456 service->server_ = this;
457 }
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700458
Yuchen Zenga42ec212016-04-29 13:03:06 -0700459 const char* method_name = nullptr;
Craig Tiller15f383c2016-01-07 12:45:32 -0800460 for (auto it = service->methods_.begin(); it != service->methods_.end();
461 ++it) {
yang-g269b8be2016-01-15 10:46:26 -0800462 if (it->get() == nullptr) { // Handled by generic service if any.
463 continue;
464 }
Sree Kuchibhotlada069a52016-10-19 11:22:29 -0700465
Vijay Pai6abd2002017-07-25 00:43:30 -0700466 RpcServiceMethod* method = it->get();
Craig Tiller06cb1a92016-04-04 08:10:47 -0700467 void* tag = grpc_server_register_method(
468 server_, method->name(), host ? host->c_str() : nullptr,
469 PayloadHandlingForMethod(method), 0);
Craig Tiller15f383c2016-01-07 12:45:32 -0800470 if (tag == nullptr) {
vjpaicad5f0a2015-02-18 22:02:52 -0800471 gpr_log(GPR_DEBUG, "Attempt to register %s multiple times",
472 method->name());
473 return false;
474 }
Sree Kuchibhotlada069a52016-10-19 11:22:29 -0700475
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700476 if (method->handler() == nullptr) { // Async method
Craig Tiller15f383c2016-01-07 12:45:32 -0800477 method->set_server_tag(tag);
478 } else {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700479 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
480 (*it)->AddSyncMethod(method, tag);
481 }
vjpaicad5f0a2015-02-18 22:02:52 -0800482 }
Sree Kuchibhotlada069a52016-10-19 11:22:29 -0700483
Yuchen Zenga42ec212016-04-29 13:03:06 -0700484 method_name = method->name();
485 }
486
487 // Parse service name.
488 if (method_name != nullptr) {
489 std::stringstream ss(method_name);
490 grpc::string service_name;
491 if (std::getline(ss, service_name, '/') &&
492 std::getline(ss, service_name, '/')) {
493 services_.push_back(service_name);
494 }
vjpaicad5f0a2015-02-18 22:02:52 -0800495 }
496 return true;
497}
498
Yang Gao49996492015-03-12 16:40:19 -0700499void Server::RegisterAsyncGenericService(AsyncGenericService* service) {
Yang Gao1c402332015-03-05 16:39:25 -0800500 GPR_ASSERT(service->server_ == nullptr &&
Yang Gao1ad253d2015-03-16 13:11:29 -0700501 "Can only register an async generic service against one server.");
Yang Gao1c402332015-03-05 16:39:25 -0800502 service->server_ = this;
yang-g9b7757d2015-08-13 11:15:53 -0700503 has_generic_service_ = true;
Yang Gao1c402332015-03-05 16:39:25 -0800504}
505
Nicolas Noblecfd60732015-03-18 16:27:43 -0700506int Server::AddListeningPort(const grpc::string& addr,
507 ServerCredentials* creds) {
vjpaicad5f0a2015-02-18 22:02:52 -0800508 GPR_ASSERT(!started_);
yang-gd510fcf2017-03-31 11:00:02 -0700509 int port = creds->AddPortToServer(addr, server_);
yang-geafeea42017-04-14 12:34:10 -0700510 global_callbacks_->AddPort(this, addr, creds, port);
yang-gd510fcf2017-03-31 11:00:02 -0700511 return port;
vjpaicad5f0a2015-02-18 22:02:52 -0800512}
513
Craig Tiller9d9313c2017-04-11 15:05:46 -0700514void Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800515 GPR_ASSERT(!started_);
yang-gf2fe4f72017-02-07 12:47:22 -0800516 global_callbacks_->PreServerStart(this);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800517 started_ = true;
yang-gad327642016-12-12 14:32:09 -0800518
519 // Only create default health check service when user did not provide an
520 // explicit one.
521 if (health_check_service_ == nullptr && !health_check_service_disabled_ &&
522 DefaultHealthCheckServiceEnabled()) {
yang-g076bac02017-02-07 13:50:36 -0800523 if (sync_server_cqs_->empty()) {
yang-gf070d412017-05-08 15:02:28 -0700524 gpr_log(GPR_INFO,
yang-g076bac02017-02-07 13:50:36 -0800525 "Default health check service disabled at async-only server.");
526 } else {
527 auto* default_hc_service = new DefaultHealthCheckService;
528 health_check_service_.reset(default_hc_service);
529 RegisterService(nullptr, default_hc_service->GetHealthCheckService());
530 }
yang-gad327642016-12-12 14:32:09 -0800531 }
532
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800533 grpc_server_start(server_);
534
yang-g9b7757d2015-08-13 11:15:53 -0700535 if (!has_generic_service_) {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700536 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
537 (*it)->AddUnknownSyncMethod();
Craig Tiller86d31772015-08-19 12:52:50 -0700538 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700539
Craig Tiller8f7bff72015-08-17 13:23:14 -0700540 for (size_t i = 0; i < num_cqs; i++) {
Craig Tillere0049582016-05-20 10:31:09 -0700541 if (cqs[i]->IsFrequentlyPolled()) {
542 new UnimplementedAsyncRequest(this, cqs[i]);
543 }
Craig Tillercbd04852015-02-10 17:39:54 -0800544 }
yang-g9b7757d2015-08-13 11:15:53 -0700545 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700546
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700547 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
548 (*it)->Start();
549 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800550}
551
Craig Tillere50e5cb2015-08-18 12:44:57 -0700552void Server::ShutdownInternal(gpr_timespec deadline) {
Vijay Pai320ed132016-11-01 17:16:55 -0700553 std::unique_lock<std::mutex> lock(mu_);
Craig Tiller12352b22017-01-10 15:16:14 -0800554 if (!shutdown_) {
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800555 shutdown_ = true;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700556
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700557 /// The completion queue to use for server shutdown completion notification
558 CompletionQueue shutdown_cq;
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700559 ShutdownTag shutdown_tag; // Dummy shutdown tag
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700560 grpc_server_shutdown_and_notify(server_, shutdown_cq.cq(), &shutdown_tag);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700561
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700562 shutdown_cq.Shutdown();
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700563
564 void* tag;
565 bool ok;
566 CompletionQueue::NextStatus status =
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700567 shutdown_cq.AsyncNext(&tag, &ok, deadline);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700568
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700569 // If this timed out, it means we are done with the grace period for a clean
570 // shutdown. We should force a shutdown now by cancelling all inflight calls
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700571 if (status == CompletionQueue::NextStatus::TIMEOUT) {
572 grpc_server_cancel_all_calls(server_);
573 }
574 // Else in case of SHUTDOWN or GOT_EVENT, it means that the server has
575 // successfully shutdown
576
Craig Tiller9c5318a2016-12-05 15:07:04 -0800577 // Shutdown all ThreadManagers. This will try to gracefully stop all the
578 // threads in the ThreadManagers (once they process any inflight requests)
579 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
580 (*it)->Shutdown(); // ThreadManager's Shutdown()
581 }
582
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700583 // Wait for threads in all ThreadManagers to terminate
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700584 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
585 (*it)->Wait();
586 }
587
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700588 // Drain the shutdown queue (if the previous call to AsyncNext() timed out
589 // and we didn't remove the tag from the queue yet)
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700590 while (shutdown_cq.Next(&tag, &ok)) {
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700591 // Nothing to be done here. Just ignore ok and tag values
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700592 }
593
yang-g6ec11f22016-07-14 14:53:35 -0700594 shutdown_notified_ = true;
yang-ge89dc6c2016-07-11 15:48:01 -0700595 shutdown_cv_.notify_all();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800596 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800597}
598
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800599void Server::Wait() {
Vijay Pai320ed132016-11-01 17:16:55 -0700600 std::unique_lock<std::mutex> lock(mu_);
yang-g6ec11f22016-07-14 14:53:35 -0700601 while (started_ && !shutdown_notified_) {
602 shutdown_cv_.wait(lock);
603 }
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800604}
605
Vijay Pai6abd2002017-07-25 00:43:30 -0700606void Server::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) {
Craig Tillerbb5227f2015-02-11 13:34:48 -0800607 static const size_t MAX_OPS = 8;
Craig Tiller50a7a682015-06-04 12:53:40 -0700608 size_t nops = 0;
609 grpc_op cops[MAX_OPS];
Craig Tiller66051c62017-03-31 09:16:35 -0700610 ops->FillOps(call->call(), cops, &nops);
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200611 auto result = grpc_call_start_batch(call->call(), cops, nops, ops, nullptr);
yang-g66e6c232017-08-30 15:51:53 -0700612 if (result != GRPC_CALL_OK) {
613 gpr_log(GPR_ERROR, "Fatal: grpc_call_start_batch returned %d", result);
yang-gd3cbd722017-09-05 13:42:35 -0700614 grpc_call_log_batch(__FILE__, __LINE__, GPR_LOG_SEVERITY_ERROR,
615 call->call(), cops, nops, ops);
yang-g66e6c232017-08-30 15:51:53 -0700616 abort();
617 }
Craig Tillerbb5227f2015-02-11 13:34:48 -0800618}
619
David Garcia Quintas44f32492016-01-14 18:00:04 -0800620ServerInterface::BaseAsyncRequest::BaseAsyncRequest(
621 ServerInterface* server, ServerContext* context,
Vijay Pai6abd2002017-07-25 00:43:30 -0700622 ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag,
623 bool delete_on_finalize)
Craig Tillerce40de52015-06-05 07:14:58 -0700624 : server_(server),
625 context_(context),
626 stream_(stream),
627 call_cq_(call_cq),
Craig Tiller50955812015-06-05 08:03:17 -0700628 tag_(tag),
Craig Tiller8f7bff72015-08-17 13:23:14 -0700629 delete_on_finalize_(delete_on_finalize),
Craig Tillerce40de52015-06-05 07:14:58 -0700630 call_(nullptr) {
Vijay Paibf24dd92016-12-05 13:59:09 -0800631 call_cq_->RegisterAvalanching(); // This op will trigger more ops
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700632}
633
Vijay Paicbe15992016-12-05 13:56:29 -0800634ServerInterface::BaseAsyncRequest::~BaseAsyncRequest() {
635 call_cq_->CompleteAvalanching();
636}
637
David Garcia Quintasf3ddb7c2016-01-20 16:02:22 -0800638bool ServerInterface::BaseAsyncRequest::FinalizeResult(void** tag,
639 bool* status) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700640 if (*status) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800641 context_->client_metadata_.FillMap();
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700642 }
yang-g85c04f92015-07-07 17:47:31 -0700643 context_->set_call(call_);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700644 context_->cq_ = call_cq_;
Vijay Pai6abd2002017-07-25 00:43:30 -0700645 Call call(call_, server_, call_cq_, server_->max_receive_message_size());
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700646 if (*status && call_) {
647 context_->BeginCompletionOp(&call);
648 }
649 // just the pointers inside call are copied here
650 stream_->BindCall(&call);
Craig Tiller50955812015-06-05 08:03:17 -0700651 *tag = tag_;
Craig Tiller8f7bff72015-08-17 13:23:14 -0700652 if (delete_on_finalize_) {
653 delete this;
654 }
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700655 return true;
656}
657
David Garcia Quintas44f32492016-01-14 18:00:04 -0800658ServerInterface::RegisteredAsyncRequest::RegisteredAsyncRequest(
659 ServerInterface* server, ServerContext* context,
Vijay Pai6abd2002017-07-25 00:43:30 -0700660 ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag)
yang-g076bac02017-02-07 13:50:36 -0800661 : BaseAsyncRequest(server, context, stream, call_cq, tag, true) {}
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700662
David Garcia Quintas44f32492016-01-14 18:00:04 -0800663void ServerInterface::RegisteredAsyncRequest::IssueRequest(
Craig Tillerce40de52015-06-05 07:14:58 -0700664 void* registered_method, grpc_byte_buffer** payload,
665 ServerCompletionQueue* notification_cq) {
yang-g0eaf7de2017-07-05 16:50:51 -0700666 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_registered_call(
667 server_->server(), registered_method, &call_,
668 &context_->deadline_,
669 context_->client_metadata_.arr(), payload,
670 call_cq_->cq(), notification_cq->cq(), this));
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700671}
672
David Garcia Quintas44f32492016-01-14 18:00:04 -0800673ServerInterface::GenericAsyncRequest::GenericAsyncRequest(
674 ServerInterface* server, GenericServerContext* context,
Vijay Pai6abd2002017-07-25 00:43:30 -0700675 ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq,
Craig Tiller8f7bff72015-08-17 13:23:14 -0700676 ServerCompletionQueue* notification_cq, void* tag, bool delete_on_finalize)
677 : BaseAsyncRequest(server, context, stream, call_cq, tag,
678 delete_on_finalize) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700679 grpc_call_details_init(&call_details_);
680 GPR_ASSERT(notification_cq);
681 GPR_ASSERT(call_cq);
yang-g0eaf7de2017-07-05 16:50:51 -0700682 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(
683 server->server(), &call_, &call_details_,
684 context->client_metadata_.arr(), call_cq->cq(),
685 notification_cq->cq(), this));
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700686}
687
David Garcia Quintasf3ddb7c2016-01-20 16:02:22 -0800688bool ServerInterface::GenericAsyncRequest::FinalizeResult(void** tag,
689 bool* status) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700690 // TODO(yangg) remove the copy here.
yang-g3deb0062015-06-23 14:06:08 -0700691 if (*status) {
yang-ga58cab32015-06-23 14:37:15 -0700692 static_cast<GenericServerContext*>(context_)->method_ =
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800693 StringFromCopiedSlice(call_details_.method);
694 static_cast<GenericServerContext*>(context_)->host_ =
695 StringFromCopiedSlice(call_details_.host);
yang-ga044f6d2017-05-11 15:37:51 -0700696 context_->deadline_ = call_details_.deadline;
yang-g3deb0062015-06-23 14:06:08 -0700697 }
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800698 grpc_slice_unref(call_details_.method);
699 grpc_slice_unref(call_details_.host);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700700 return BaseAsyncRequest::FinalizeResult(tag, status);
701}
702
Craig Tiller8f7bff72015-08-17 13:23:14 -0700703bool Server::UnimplementedAsyncRequest::FinalizeResult(void** tag,
704 bool* status) {
705 if (GenericAsyncRequest::FinalizeResult(tag, status) && *status) {
706 new UnimplementedAsyncRequest(server_, cq_);
707 new UnimplementedAsyncResponse(this);
708 } else {
709 delete this;
710 }
711 return false;
712}
713
714Server::UnimplementedAsyncResponse::UnimplementedAsyncResponse(
715 UnimplementedAsyncRequest* request)
716 : request_(request) {
717 Status status(StatusCode::UNIMPLEMENTED, "");
Vijay Pai6abd2002017-07-25 00:43:30 -0700718 UnknownMethodHandler::FillOps(request_->context(), this);
Craig Tiller8f7bff72015-08-17 13:23:14 -0700719 request_->stream()->call_.PerformOps(this);
720}
721
Yuchen Zenga42ec212016-04-29 13:03:06 -0700722ServerInitializer* Server::initializer() { return server_initializer_.get(); }
723
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800724} // namespace grpc