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