Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <grpc++/server.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 35 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 36 | #include <sstream> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <utility> |
| 38 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc++/completion_queue.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 40 | #include <grpc++/generic/async_generic_service.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 41 | #include <grpc++/impl/codegen/completion_queue_tag.h> |
| 42 | #include <grpc++/impl/grpc_library.h> |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 43 | #include <grpc++/impl/method_handler_impl.h> |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 44 | #include <grpc++/impl/rpc_service_method.h> |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 45 | #include <grpc++/impl/server_initializer.h> |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 46 | #include <grpc++/impl/service_type.h> |
Julien Boeuf | 5be92a3 | 2015-08-28 16:28:18 -0700 | [diff] [blame] | 47 | #include <grpc++/security/server_credentials.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 48 | #include <grpc++/server_context.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 49 | #include <grpc++/support/time.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 50 | #include <grpc/grpc.h> |
| 51 | #include <grpc/support/alloc.h> |
| 52 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 54 | #include "src/core/lib/profiling/timers.h" |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 55 | #include "src/cpp/server/thread_pool_interface.h" |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 56 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | namespace grpc { |
| 58 | |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 59 | class DefaultGlobalCallbacks GRPC_FINAL : public Server::GlobalCallbacks { |
| 60 | public: |
Bogdan Drutu | 618051c | 2016-01-14 08:54:43 -0800 | [diff] [blame] | 61 | ~DefaultGlobalCallbacks() GRPC_OVERRIDE {} |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 62 | void PreSynchronousRequest(ServerContext* context) GRPC_OVERRIDE {} |
| 63 | void PostSynchronousRequest(ServerContext* context) GRPC_OVERRIDE {} |
| 64 | }; |
| 65 | |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 66 | static std::shared_ptr<Server::GlobalCallbacks> g_callbacks = nullptr; |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 67 | static gpr_once g_once_init_callbacks = GPR_ONCE_INIT; |
| 68 | |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 69 | static void InitGlobalCallbacks() { |
Vijay Pai | b645a2d | 2016-06-09 18:39:06 -0700 | [diff] [blame^] | 70 | if (!g_callbacks) { |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 71 | g_callbacks.reset(new DefaultGlobalCallbacks()); |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 74 | |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 75 | class Server::UnimplementedAsyncRequestContext { |
| 76 | protected: |
| 77 | UnimplementedAsyncRequestContext() : generic_stream_(&server_context_) {} |
| 78 | |
| 79 | GenericServerContext server_context_; |
| 80 | GenericServerAsyncReaderWriter generic_stream_; |
| 81 | }; |
| 82 | |
| 83 | class Server::UnimplementedAsyncRequest GRPC_FINAL |
| 84 | : public UnimplementedAsyncRequestContext, |
| 85 | public GenericAsyncRequest { |
| 86 | public: |
| 87 | UnimplementedAsyncRequest(Server* server, ServerCompletionQueue* cq) |
| 88 | : GenericAsyncRequest(server, &server_context_, &generic_stream_, cq, cq, |
| 89 | NULL, false), |
| 90 | server_(server), |
| 91 | cq_(cq) {} |
| 92 | |
| 93 | bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE; |
| 94 | |
| 95 | ServerContext* context() { return &server_context_; } |
| 96 | GenericServerAsyncReaderWriter* stream() { return &generic_stream_; } |
| 97 | |
| 98 | private: |
| 99 | Server* const server_; |
| 100 | ServerCompletionQueue* const cq_; |
| 101 | }; |
| 102 | |
| 103 | typedef SneakyCallOpSet<CallOpSendInitialMetadata, CallOpServerSendStatus> |
| 104 | UnimplementedAsyncResponseOp; |
| 105 | class Server::UnimplementedAsyncResponse GRPC_FINAL |
| 106 | : public UnimplementedAsyncResponseOp { |
| 107 | public: |
| 108 | UnimplementedAsyncResponse(UnimplementedAsyncRequest* request); |
| 109 | ~UnimplementedAsyncResponse() { delete request_; } |
| 110 | |
| 111 | bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE { |
| 112 | bool r = UnimplementedAsyncResponseOp::FinalizeResult(tag, status); |
| 113 | delete this; |
| 114 | return r; |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | UnimplementedAsyncRequest* const request_; |
| 119 | }; |
| 120 | |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 121 | class Server::ShutdownRequest GRPC_FINAL : public CompletionQueueTag { |
| 122 | public: |
| 123 | bool FinalizeResult(void** tag, bool* status) { |
| 124 | delete this; |
| 125 | return false; |
| 126 | } |
| 127 | }; |
| 128 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 129 | class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 130 | public: |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 131 | SyncRequest(RpcServiceMethod* method, void* tag) |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 132 | : method_(method), |
| 133 | tag_(tag), |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 134 | in_flight_(false), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 135 | has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC || |
| 136 | method->method_type() == |
| 137 | RpcMethod::SERVER_STREAMING), |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 138 | call_details_(nullptr), |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 139 | cq_(nullptr) { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 140 | grpc_metadata_array_init(&request_metadata_); |
| 141 | } |
| 142 | |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 143 | ~SyncRequest() { |
| 144 | if (call_details_) { |
| 145 | delete call_details_; |
| 146 | } |
| 147 | grpc_metadata_array_destroy(&request_metadata_); |
| 148 | } |
Yang Gao | 7b49a77 | 2015-05-28 14:07:54 -0700 | [diff] [blame] | 149 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 150 | static SyncRequest* Wait(CompletionQueue* cq, bool* ok) { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 151 | void* tag = nullptr; |
Craig Tiller | 504bd33 | 2015-02-11 20:34:33 -0800 | [diff] [blame] | 152 | *ok = false; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 153 | if (!cq->Next(&tag, ok)) { |
| 154 | return nullptr; |
| 155 | } |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 156 | auto* mrd = static_cast<SyncRequest*>(tag); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 157 | GPR_ASSERT(mrd->in_flight_); |
| 158 | return mrd; |
| 159 | } |
| 160 | |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 161 | static bool AsyncWait(CompletionQueue* cq, SyncRequest** req, bool* ok, |
| 162 | gpr_timespec deadline) { |
| 163 | void* tag = nullptr; |
| 164 | *ok = false; |
| 165 | switch (cq->AsyncNext(&tag, ok, deadline)) { |
| 166 | case CompletionQueue::TIMEOUT: |
| 167 | *req = nullptr; |
| 168 | return true; |
| 169 | case CompletionQueue::SHUTDOWN: |
| 170 | *req = nullptr; |
| 171 | return false; |
| 172 | case CompletionQueue::GOT_EVENT: |
| 173 | *req = static_cast<SyncRequest*>(tag); |
| 174 | GPR_ASSERT((*req)->in_flight_); |
| 175 | return true; |
| 176 | } |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 177 | GPR_UNREACHABLE_CODE(return false); |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 180 | void SetupRequest() { cq_ = grpc_completion_queue_create(nullptr); } |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 181 | |
| 182 | void TeardownRequest() { |
| 183 | grpc_completion_queue_destroy(cq_); |
| 184 | cq_ = nullptr; |
| 185 | } |
| 186 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 187 | void Request(grpc_server* server, grpc_completion_queue* notify_cq) { |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 188 | GPR_ASSERT(cq_ && !in_flight_); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 189 | in_flight_ = true; |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 190 | if (tag_) { |
| 191 | GPR_ASSERT(GRPC_CALL_OK == |
| 192 | grpc_server_request_registered_call( |
| 193 | server, tag_, &call_, &deadline_, &request_metadata_, |
| 194 | has_request_payload_ ? &request_payload_ : nullptr, cq_, |
| 195 | notify_cq, this)); |
| 196 | } else { |
| 197 | if (!call_details_) { |
| 198 | call_details_ = new grpc_call_details; |
| 199 | grpc_call_details_init(call_details_); |
| 200 | } |
| 201 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call( |
| 202 | server, &call_, call_details_, |
| 203 | &request_metadata_, cq_, notify_cq, this)); |
| 204 | } |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 207 | bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 208 | if (!*status) { |
| 209 | grpc_completion_queue_destroy(cq_); |
| 210 | } |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 211 | if (call_details_) { |
| 212 | deadline_ = call_details_->deadline; |
| 213 | grpc_call_details_destroy(call_details_); |
| 214 | grpc_call_details_init(call_details_); |
| 215 | } |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 216 | return true; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 217 | } |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 218 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 219 | class CallData GRPC_FINAL { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 220 | public: |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 221 | explicit CallData(Server* server, SyncRequest* mrd) |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 222 | : cq_(mrd->cq_), |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 223 | call_(mrd->call_, server, &cq_, server->max_message_size_), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 224 | ctx_(mrd->deadline_, mrd->request_metadata_.metadata, |
| 225 | mrd->request_metadata_.count), |
| 226 | has_request_payload_(mrd->has_request_payload_), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 227 | request_payload_(mrd->request_payload_), |
| 228 | method_(mrd->method_) { |
yang-g | 85c04f9 | 2015-07-07 17:47:31 -0700 | [diff] [blame] | 229 | ctx_.set_call(mrd->call_); |
Yang Gao | 1205f6f | 2015-03-22 15:18:14 -0700 | [diff] [blame] | 230 | ctx_.cq_ = &cq_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 231 | GPR_ASSERT(mrd->in_flight_); |
| 232 | mrd->in_flight_ = false; |
| 233 | mrd->request_metadata_.count = 0; |
| 234 | } |
| 235 | |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 236 | ~CallData() { |
| 237 | if (has_request_payload_ && request_payload_) { |
| 238 | grpc_byte_buffer_destroy(request_payload_); |
| 239 | } |
| 240 | } |
| 241 | |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 242 | void Run(std::shared_ptr<GlobalCallbacks> global_callbacks) { |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 243 | ctx_.BeginCompletionOp(&call_); |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 244 | global_callbacks->PreSynchronousRequest(&ctx_); |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 245 | method_->handler()->RunHandler(MethodHandler::HandlerParameter( |
| 246 | &call_, &ctx_, request_payload_, call_.max_message_size())); |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 247 | global_callbacks->PostSynchronousRequest(&ctx_); |
Craig Tiller | 928ec8e | 2015-06-05 08:45:45 -0700 | [diff] [blame] | 248 | request_payload_ = nullptr; |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 249 | void* ignored_tag; |
| 250 | bool ignored_ok; |
| 251 | cq_.Shutdown(); |
| 252 | GPR_ASSERT(cq_.Next(&ignored_tag, &ignored_ok) == false); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | private: |
| 256 | CompletionQueue cq_; |
| 257 | Call call_; |
| 258 | ServerContext ctx_; |
| 259 | const bool has_request_payload_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 260 | grpc_byte_buffer* request_payload_; |
| 261 | RpcServiceMethod* const method_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | private: |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 265 | RpcServiceMethod* const method_; |
| 266 | void* const tag_; |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 267 | bool in_flight_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 268 | const bool has_request_payload_; |
Craig Tiller | 5987c70 | 2016-03-09 16:55:23 -0800 | [diff] [blame] | 269 | uint32_t incoming_flags_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 270 | grpc_call* call_; |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 271 | grpc_call_details* call_details_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 272 | gpr_timespec deadline_; |
| 273 | grpc_metadata_array request_metadata_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 274 | grpc_byte_buffer* request_payload_; |
| 275 | grpc_completion_queue* cq_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 276 | }; |
| 277 | |
David Garcia Quintas | d79ef3a | 2016-01-28 00:21:27 -0800 | [diff] [blame] | 278 | static internal::GrpcLibraryInitializer g_gli_initializer; |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 279 | Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned, |
yang-g | eb62c94 | 2016-02-17 15:37:25 -0800 | [diff] [blame] | 280 | int max_message_size, ChannelArguments* args) |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 281 | : max_message_size_(max_message_size), |
| 282 | started_(false), |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 283 | shutdown_(false), |
| 284 | num_running_cb_(0), |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 285 | sync_methods_(new std::list<SyncRequest>), |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 286 | has_generic_service_(false), |
yang-g | eb62c94 | 2016-02-17 15:37:25 -0800 | [diff] [blame] | 287 | server_(nullptr), |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 288 | thread_pool_(thread_pool), |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 289 | thread_pool_owned_(thread_pool_owned), |
| 290 | server_initializer_(new ServerInitializer(this)) { |
David Garcia Quintas | d79ef3a | 2016-01-28 00:21:27 -0800 | [diff] [blame] | 291 | g_gli_initializer.summon(); |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 292 | gpr_once_init(&g_once_init_callbacks, InitGlobalCallbacks); |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 293 | global_callbacks_ = g_callbacks; |
yang-g | eb62c94 | 2016-02-17 15:37:25 -0800 | [diff] [blame] | 294 | global_callbacks_->UpdateArguments(args); |
| 295 | grpc_channel_args channel_args; |
| 296 | args->SetChannelArgs(&channel_args); |
| 297 | server_ = grpc_server_create(&channel_args, nullptr); |
Craig Tiller | 3eee9b4 | 2016-05-19 11:28:41 -0700 | [diff] [blame] | 298 | if (thread_pool_ == nullptr) { |
| 299 | grpc_server_register_non_listening_completion_queue(server_, cq_.cq(), |
| 300 | nullptr); |
| 301 | } else { |
| 302 | grpc_server_register_completion_queue(server_, cq_.cq(), nullptr); |
| 303 | } |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 304 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 305 | |
| 306 | Server::~Server() { |
Ruyi Wang | b486ba6 | 2015-03-14 22:19:44 +0800 | [diff] [blame] | 307 | { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 308 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Ruyi Wang | b486ba6 | 2015-03-14 22:19:44 +0800 | [diff] [blame] | 309 | if (started_ && !shutdown_) { |
| 310 | lock.unlock(); |
| 311 | Shutdown(); |
yang-g | b971173 | 2016-01-15 16:53:08 -0800 | [diff] [blame] | 312 | } else if (!started_) { |
| 313 | cq_.Shutdown(); |
Ruyi Wang | b486ba6 | 2015-03-14 22:19:44 +0800 | [diff] [blame] | 314 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 315 | } |
Craig Tiller | 29f79dc | 2015-05-27 15:59:23 -0700 | [diff] [blame] | 316 | void* got_tag; |
| 317 | bool ok; |
| 318 | GPR_ASSERT(!cq_.Next(&got_tag, &ok)); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 319 | grpc_server_destroy(server_); |
| 320 | if (thread_pool_owned_) { |
| 321 | delete thread_pool_; |
| 322 | } |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 323 | delete sync_methods_; |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 326 | void Server::SetGlobalCallbacks(GlobalCallbacks* callbacks) { |
Vijay Pai | b645a2d | 2016-06-09 18:39:06 -0700 | [diff] [blame^] | 327 | GPR_ASSERT(!g_callbacks); |
| 328 | GPR_ASSERT(callbacks); |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 329 | g_callbacks.reset(callbacks); |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 332 | static grpc_server_register_method_payload_handling PayloadHandlingForMethod( |
| 333 | RpcServiceMethod* method) { |
| 334 | switch (method->method_type()) { |
| 335 | case RpcMethod::NORMAL_RPC: |
| 336 | case RpcMethod::SERVER_STREAMING: |
| 337 | return GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER; |
| 338 | case RpcMethod::CLIENT_STREAMING: |
| 339 | case RpcMethod::BIDI_STREAMING: |
| 340 | return GRPC_SRM_PAYLOAD_NONE; |
| 341 | } |
| 342 | GPR_UNREACHABLE_CODE(return GRPC_SRM_PAYLOAD_NONE;); |
| 343 | } |
| 344 | |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 345 | bool Server::RegisterService(const grpc::string* host, Service* service) { |
| 346 | bool has_async_methods = service->has_async_methods(); |
| 347 | if (has_async_methods) { |
| 348 | GPR_ASSERT(service->server_ == nullptr && |
| 349 | "Can only register an asynchronous service against one server."); |
| 350 | service->server_ = this; |
| 351 | } |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 352 | const char* method_name = nullptr; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 353 | for (auto it = service->methods_.begin(); it != service->methods_.end(); |
| 354 | ++it) { |
yang-g | 269b8be | 2016-01-15 10:46:26 -0800 | [diff] [blame] | 355 | if (it->get() == nullptr) { // Handled by generic service if any. |
| 356 | continue; |
| 357 | } |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 358 | RpcServiceMethod* method = it->get(); |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 359 | void* tag = grpc_server_register_method( |
| 360 | server_, method->name(), host ? host->c_str() : nullptr, |
| 361 | PayloadHandlingForMethod(method), 0); |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 362 | if (tag == nullptr) { |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 363 | gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", |
| 364 | method->name()); |
| 365 | return false; |
| 366 | } |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 367 | if (method->handler() == nullptr) { |
| 368 | method->set_server_tag(tag); |
| 369 | } else { |
| 370 | sync_methods_->emplace_back(method, tag); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 371 | } |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 372 | method_name = method->name(); |
| 373 | } |
| 374 | |
| 375 | // Parse service name. |
| 376 | if (method_name != nullptr) { |
| 377 | std::stringstream ss(method_name); |
| 378 | grpc::string service_name; |
| 379 | if (std::getline(ss, service_name, '/') && |
| 380 | std::getline(ss, service_name, '/')) { |
| 381 | services_.push_back(service_name); |
| 382 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 383 | } |
| 384 | return true; |
| 385 | } |
| 386 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 387 | void Server::RegisterAsyncGenericService(AsyncGenericService* service) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 388 | GPR_ASSERT(service->server_ == nullptr && |
Yang Gao | 1ad253d | 2015-03-16 13:11:29 -0700 | [diff] [blame] | 389 | "Can only register an async generic service against one server."); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 390 | service->server_ = this; |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 391 | has_generic_service_ = true; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 394 | int Server::AddListeningPort(const grpc::string& addr, |
| 395 | ServerCredentials* creds) { |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 396 | GPR_ASSERT(!started_); |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 397 | return creds->AddPortToServer(addr, server_); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 400 | bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 401 | GPR_ASSERT(!started_); |
| 402 | started_ = true; |
| 403 | grpc_server_start(server_); |
| 404 | |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 405 | if (!has_generic_service_) { |
Craig Tiller | 86d3177 | 2015-08-19 12:52:50 -0700 | [diff] [blame] | 406 | if (!sync_methods_->empty()) { |
| 407 | unknown_method_.reset(new RpcServiceMethod( |
| 408 | "unknown", RpcMethod::BIDI_STREAMING, new UnknownMethodHandler)); |
| 409 | // Use of emplace_back with just constructor arguments is not accepted |
Julien Boeuf | 5be92a3 | 2015-08-28 16:28:18 -0700 | [diff] [blame] | 410 | // here by gcc-4.4 because it can't match the anonymous nullptr with a |
Craig Tiller | 849c7ca | 2015-08-21 16:06:05 -0700 | [diff] [blame] | 411 | // proper constructor implicitly. Construct the object and use push_back. |
Craig Tiller | 86d3177 | 2015-08-19 12:52:50 -0700 | [diff] [blame] | 412 | sync_methods_->push_back(SyncRequest(unknown_method_.get(), nullptr)); |
| 413 | } |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 414 | for (size_t i = 0; i < num_cqs; i++) { |
Craig Tiller | e004958 | 2016-05-20 10:31:09 -0700 | [diff] [blame] | 415 | if (cqs[i]->IsFrequentlyPolled()) { |
| 416 | new UnimplementedAsyncRequest(this, cqs[i]); |
| 417 | } |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 418 | } |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 419 | } |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 420 | // Start processing rpcs. |
| 421 | if (!sync_methods_->empty()) { |
| 422 | for (auto m = sync_methods_->begin(); m != sync_methods_->end(); m++) { |
| 423 | m->SetupRequest(); |
Craig Tiller | 7c72adc | 2015-02-09 14:07:26 -0800 | [diff] [blame] | 424 | m->Request(server_, cq_.cq()); |
| 425 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 426 | |
| 427 | ScheduleCallback(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 430 | return true; |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 431 | } |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 432 | |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 433 | void Server::ShutdownInternal(gpr_timespec deadline) { |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 434 | grpc::unique_lock<grpc::mutex> lock(mu_); |
| 435 | if (started_ && !shutdown_) { |
| 436 | shutdown_ = true; |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 437 | grpc_server_shutdown_and_notify(server_, cq_.cq(), new ShutdownRequest()); |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 438 | cq_.Shutdown(); |
Craig Tiller | 5925603 | 2015-11-02 14:19:15 -0800 | [diff] [blame] | 439 | lock.unlock(); |
Craig Tiller | 9374ce8 | 2015-08-19 10:15:44 -0700 | [diff] [blame] | 440 | // Spin, eating requests until the completion queue is completely shutdown. |
| 441 | // If the deadline expires then cancel anything that's pending and keep |
| 442 | // spinning forever until the work is actually drained. |
Julien Boeuf | 5be92a3 | 2015-08-28 16:28:18 -0700 | [diff] [blame] | 443 | // Since nothing else needs to touch state guarded by mu_, holding it |
Craig Tiller | 681a291 | 2015-08-19 11:31:25 -0700 | [diff] [blame] | 444 | // through this loop is fine. |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 445 | SyncRequest* request; |
| 446 | bool ok; |
| 447 | while (SyncRequest::AsyncWait(&cq_, &request, &ok, deadline)) { |
| 448 | if (request == NULL) { // deadline expired |
| 449 | grpc_server_cancel_all_calls(server_); |
Craig Tiller | 9374ce8 | 2015-08-19 10:15:44 -0700 | [diff] [blame] | 450 | deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC); |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 451 | } else if (ok) { |
| 452 | SyncRequest::CallData call_data(this, request); |
| 453 | } |
| 454 | } |
Craig Tiller | 5925603 | 2015-11-02 14:19:15 -0800 | [diff] [blame] | 455 | lock.lock(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 456 | |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 457 | // Wait for running callbacks to finish. |
| 458 | while (num_running_cb_ != 0) { |
| 459 | callback_cv_.wait(lock); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 460 | } |
| 461 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 464 | void Server::Wait() { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 465 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 466 | while (num_running_cb_ != 0) { |
| 467 | callback_cv_.wait(lock); |
| 468 | } |
| 469 | } |
| 470 | |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 471 | void Server::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) { |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 472 | static const size_t MAX_OPS = 8; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 473 | size_t nops = 0; |
| 474 | grpc_op cops[MAX_OPS]; |
| 475 | ops->FillOps(cops, &nops); |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 476 | auto result = grpc_call_start_batch(call->call(), cops, nops, ops, nullptr); |
| 477 | GPR_ASSERT(GRPC_CALL_OK == result); |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 478 | } |
| 479 | |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 480 | ServerInterface::BaseAsyncRequest::BaseAsyncRequest( |
| 481 | ServerInterface* server, ServerContext* context, |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 482 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag, |
| 483 | bool delete_on_finalize) |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 484 | : server_(server), |
| 485 | context_(context), |
| 486 | stream_(stream), |
| 487 | call_cq_(call_cq), |
Craig Tiller | 5095581 | 2015-06-05 08:03:17 -0700 | [diff] [blame] | 488 | tag_(tag), |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 489 | delete_on_finalize_(delete_on_finalize), |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 490 | call_(nullptr) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 491 | memset(&initial_metadata_array_, 0, sizeof(initial_metadata_array_)); |
| 492 | } |
| 493 | |
David Garcia Quintas | f3ddb7c | 2016-01-20 16:02:22 -0800 | [diff] [blame] | 494 | bool ServerInterface::BaseAsyncRequest::FinalizeResult(void** tag, |
| 495 | bool* status) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 496 | if (*status) { |
| 497 | for (size_t i = 0; i < initial_metadata_array_.count; i++) { |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 498 | context_->client_metadata_.insert( |
| 499 | std::pair<grpc::string_ref, grpc::string_ref>( |
| 500 | initial_metadata_array_.metadata[i].key, |
| 501 | grpc::string_ref( |
| 502 | initial_metadata_array_.metadata[i].value, |
| 503 | initial_metadata_array_.metadata[i].value_length))); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
Craig Tiller | 2f4a49c | 2015-06-05 09:36:52 -0700 | [diff] [blame] | 506 | grpc_metadata_array_destroy(&initial_metadata_array_); |
yang-g | 85c04f9 | 2015-07-07 17:47:31 -0700 | [diff] [blame] | 507 | context_->set_call(call_); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 508 | context_->cq_ = call_cq_; |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 509 | Call call(call_, server_, call_cq_, server_->max_message_size()); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 510 | if (*status && call_) { |
| 511 | context_->BeginCompletionOp(&call); |
| 512 | } |
| 513 | // just the pointers inside call are copied here |
| 514 | stream_->BindCall(&call); |
Craig Tiller | 5095581 | 2015-06-05 08:03:17 -0700 | [diff] [blame] | 515 | *tag = tag_; |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 516 | if (delete_on_finalize_) { |
| 517 | delete this; |
| 518 | } |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 519 | return true; |
| 520 | } |
| 521 | |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 522 | ServerInterface::RegisteredAsyncRequest::RegisteredAsyncRequest( |
| 523 | ServerInterface* server, ServerContext* context, |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 524 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag) |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 525 | : BaseAsyncRequest(server, context, stream, call_cq, tag, true) {} |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 526 | |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 527 | void ServerInterface::RegisteredAsyncRequest::IssueRequest( |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 528 | void* registered_method, grpc_byte_buffer** payload, |
| 529 | ServerCompletionQueue* notification_cq) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 530 | grpc_server_request_registered_call( |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 531 | server_->server(), registered_method, &call_, &context_->deadline_, |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 532 | &initial_metadata_array_, payload, call_cq_->cq(), notification_cq->cq(), |
| 533 | this); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 534 | } |
| 535 | |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 536 | ServerInterface::GenericAsyncRequest::GenericAsyncRequest( |
| 537 | ServerInterface* server, GenericServerContext* context, |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 538 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 539 | ServerCompletionQueue* notification_cq, void* tag, bool delete_on_finalize) |
| 540 | : BaseAsyncRequest(server, context, stream, call_cq, tag, |
| 541 | delete_on_finalize) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 542 | grpc_call_details_init(&call_details_); |
| 543 | GPR_ASSERT(notification_cq); |
| 544 | GPR_ASSERT(call_cq); |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 545 | grpc_server_request_call(server->server(), &call_, &call_details_, |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 546 | &initial_metadata_array_, call_cq->cq(), |
| 547 | notification_cq->cq(), this); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 548 | } |
| 549 | |
David Garcia Quintas | f3ddb7c | 2016-01-20 16:02:22 -0800 | [diff] [blame] | 550 | bool ServerInterface::GenericAsyncRequest::FinalizeResult(void** tag, |
| 551 | bool* status) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 552 | // TODO(yangg) remove the copy here. |
yang-g | 3deb006 | 2015-06-23 14:06:08 -0700 | [diff] [blame] | 553 | if (*status) { |
yang-g | a58cab3 | 2015-06-23 14:37:15 -0700 | [diff] [blame] | 554 | static_cast<GenericServerContext*>(context_)->method_ = |
| 555 | call_details_.method; |
yang-g | 3deb006 | 2015-06-23 14:06:08 -0700 | [diff] [blame] | 556 | static_cast<GenericServerContext*>(context_)->host_ = call_details_.host; |
yang-g | 3deb006 | 2015-06-23 14:06:08 -0700 | [diff] [blame] | 557 | } |
yang-g | a58cab3 | 2015-06-23 14:37:15 -0700 | [diff] [blame] | 558 | gpr_free(call_details_.method); |
| 559 | gpr_free(call_details_.host); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 560 | return BaseAsyncRequest::FinalizeResult(tag, status); |
| 561 | } |
| 562 | |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 563 | bool Server::UnimplementedAsyncRequest::FinalizeResult(void** tag, |
| 564 | bool* status) { |
| 565 | if (GenericAsyncRequest::FinalizeResult(tag, status) && *status) { |
| 566 | new UnimplementedAsyncRequest(server_, cq_); |
| 567 | new UnimplementedAsyncResponse(this); |
| 568 | } else { |
| 569 | delete this; |
| 570 | } |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | Server::UnimplementedAsyncResponse::UnimplementedAsyncResponse( |
| 575 | UnimplementedAsyncRequest* request) |
| 576 | : request_(request) { |
| 577 | Status status(StatusCode::UNIMPLEMENTED, ""); |
| 578 | UnknownMethodHandler::FillOps(request_->context(), this); |
| 579 | request_->stream()->call_.PerformOps(this); |
| 580 | } |
| 581 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 582 | void Server::ScheduleCallback() { |
| 583 | { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 584 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 585 | num_running_cb_++; |
| 586 | } |
vjpai | 72a4417 | 2015-07-16 21:44:44 -0700 | [diff] [blame] | 587 | thread_pool_->Add(std::bind(&Server::RunRpc, this)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | void Server::RunRpc() { |
| 591 | // Wait for one more incoming rpc. |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 592 | bool ok; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 593 | GPR_TIMER_SCOPE("Server::RunRpc", 0); |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 594 | auto* mrd = SyncRequest::Wait(&cq_, &ok); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 595 | if (mrd) { |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 596 | ScheduleCallback(); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 597 | if (ok) { |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 598 | SyncRequest::CallData cd(this, mrd); |
Yang Gao | b8a5f86 | 2015-05-07 16:26:33 -0700 | [diff] [blame] | 599 | { |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 600 | mrd->SetupRequest(); |
Yang Gao | b8a5f86 | 2015-05-07 16:26:33 -0700 | [diff] [blame] | 601 | grpc::unique_lock<grpc::mutex> lock(mu_); |
| 602 | if (!shutdown_) { |
Craig Tiller | a33acb7 | 2015-05-08 08:02:55 -0700 | [diff] [blame] | 603 | mrd->Request(server_, cq_.cq()); |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 604 | } else { |
| 605 | // destroy the structure that was created |
| 606 | mrd->TeardownRequest(); |
Yang Gao | b8a5f86 | 2015-05-07 16:26:33 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 609 | GPR_TIMER_SCOPE("cd.Run()", 0); |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 610 | cd.Run(global_callbacks_); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 611 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 612 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 613 | |
| 614 | { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 615 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 616 | num_running_cb_--; |
| 617 | if (shutdown_) { |
| 618 | callback_cv_.notify_all(); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 623 | ServerInitializer* Server::initializer() { return server_initializer_.get(); } |
| 624 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 625 | } // namespace grpc |