Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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> |
| 35 | #include <utility> |
| 36 | |
| 37 | #include <grpc/grpc.h> |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 38 | #include <grpc/grpc_security.h> |
Yang Gao | 2a3c96a | 2015-03-11 23:32:40 -0700 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include <grpc++/completion_queue.h> |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 42 | #include <grpc++/async_generic_service.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> |
Craig Tiller | b470169 | 2015-02-09 16:12:00 -0800 | [diff] [blame] | 45 | #include <grpc++/server_context.h> |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 46 | #include <grpc++/server_credentials.h> |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 47 | #include <grpc++/thread_pool_interface.h> |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 48 | #include <grpc++/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | |
Vijay Pai | 9ffbd0c | 2015-04-15 01:02:50 -0700 | [diff] [blame] | 50 | #include "src/core/profiling/timers.h" |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 51 | #include "src/cpp/proto/proto_utils.h" |
| 52 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | namespace grpc { |
| 54 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 55 | class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 56 | public: |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 57 | SyncRequest(RpcServiceMethod* method, void* tag) |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 58 | : method_(method), |
| 59 | tag_(tag), |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 60 | in_flight_(false), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 61 | has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC || |
| 62 | method->method_type() == |
| 63 | RpcMethod::SERVER_STREAMING), |
| 64 | has_response_payload_(method->method_type() == RpcMethod::NORMAL_RPC || |
| 65 | method->method_type() == |
| 66 | RpcMethod::CLIENT_STREAMING) { |
| 67 | grpc_metadata_array_init(&request_metadata_); |
| 68 | } |
| 69 | |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 70 | static SyncRequest* Wait(CompletionQueue* cq, bool* ok) { |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 71 | void* tag = nullptr; |
Craig Tiller | 504bd33 | 2015-02-11 20:34:33 -0800 | [diff] [blame] | 72 | *ok = false; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 73 | if (!cq->Next(&tag, ok)) { |
| 74 | return nullptr; |
| 75 | } |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 76 | auto* mrd = static_cast<SyncRequest*>(tag); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 77 | GPR_ASSERT(mrd->in_flight_); |
| 78 | return mrd; |
| 79 | } |
| 80 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 81 | void Request(grpc_server* server, grpc_completion_queue* notify_cq) { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 82 | GPR_ASSERT(!in_flight_); |
| 83 | in_flight_ = true; |
| 84 | cq_ = grpc_completion_queue_create(); |
| 85 | GPR_ASSERT(GRPC_CALL_OK == |
| 86 | grpc_server_request_registered_call( |
| 87 | server, tag_, &call_, &deadline_, &request_metadata_, |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 88 | has_request_payload_ ? &request_payload_ : nullptr, cq_, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 89 | notify_cq, this)); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 92 | bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 93 | if (!*status) { |
| 94 | grpc_completion_queue_destroy(cq_); |
| 95 | } |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 96 | return true; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 97 | } |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 98 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 99 | class CallData GRPC_FINAL { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 100 | public: |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 101 | explicit CallData(Server* server, SyncRequest* mrd) |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 102 | : cq_(mrd->cq_), |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 103 | call_(mrd->call_, server, &cq_, server->max_message_size_), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 104 | ctx_(mrd->deadline_, mrd->request_metadata_.metadata, |
| 105 | mrd->request_metadata_.count), |
| 106 | has_request_payload_(mrd->has_request_payload_), |
| 107 | has_response_payload_(mrd->has_response_payload_), |
| 108 | request_payload_(mrd->request_payload_), |
| 109 | method_(mrd->method_) { |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 110 | ctx_.call_ = mrd->call_; |
Yang Gao | 1205f6f | 2015-03-22 15:18:14 -0700 | [diff] [blame] | 111 | ctx_.cq_ = &cq_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 112 | GPR_ASSERT(mrd->in_flight_); |
| 113 | mrd->in_flight_ = false; |
| 114 | mrd->request_metadata_.count = 0; |
| 115 | } |
| 116 | |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 117 | ~CallData() { |
| 118 | if (has_request_payload_ && request_payload_) { |
| 119 | grpc_byte_buffer_destroy(request_payload_); |
| 120 | } |
| 121 | } |
| 122 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 123 | void Run() { |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 124 | std::unique_ptr<grpc::protobuf::Message> req; |
| 125 | std::unique_ptr<grpc::protobuf::Message> res; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 126 | if (has_request_payload_) { |
David Garcia Quintas | bbc0b77 | 2015-04-29 14:10:05 -0700 | [diff] [blame] | 127 | GRPC_TIMER_BEGIN(GRPC_PTAG_PROTO_DESERIALIZE, call_.call()); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 128 | req.reset(method_->AllocateRequestProto()); |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 129 | if (!DeserializeProto(request_payload_, req.get(), |
| 130 | call_.max_message_size())) { |
Yang Gao | f7d05b5 | 2015-05-04 00:14:02 -0700 | [diff] [blame] | 131 | // FIXME(yangg) deal with deserialization failure |
| 132 | cq_.Shutdown(); |
| 133 | return; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 134 | } |
David Garcia Quintas | bbc0b77 | 2015-04-29 14:10:05 -0700 | [diff] [blame] | 135 | GRPC_TIMER_END(GRPC_PTAG_PROTO_DESERIALIZE, call_.call()); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 136 | } |
| 137 | if (has_response_payload_) { |
Craig Tiller | 7c2f3f7 | 2015-02-11 13:21:54 -0800 | [diff] [blame] | 138 | res.reset(method_->AllocateResponseProto()); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 139 | } |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 140 | ctx_.BeginCompletionOp(&call_); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 141 | auto status = method_->handler()->RunHandler( |
| 142 | MethodHandler::HandlerParameter(&call_, &ctx_, req.get(), res.get())); |
| 143 | CallOpBuffer buf; |
Craig Tiller | bc8e3db | 2015-02-12 09:56:02 -0800 | [diff] [blame] | 144 | if (!ctx_.sent_initial_metadata_) { |
| 145 | buf.AddSendInitialMetadata(&ctx_.initial_metadata_); |
| 146 | } |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 147 | if (has_response_payload_) { |
| 148 | buf.AddSendMessage(*res); |
| 149 | } |
Craig Tiller | 9dcb0f8 | 2015-02-11 15:36:31 -0800 | [diff] [blame] | 150 | buf.AddServerSendStatus(&ctx_.trailing_metadata_, status); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 151 | call_.PerformOps(&buf); |
| 152 | GPR_ASSERT(cq_.Pluck(&buf)); |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 153 | void* ignored_tag; |
| 154 | bool ignored_ok; |
| 155 | cq_.Shutdown(); |
| 156 | GPR_ASSERT(cq_.Next(&ignored_tag, &ignored_ok) == false); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | private: |
| 160 | CompletionQueue cq_; |
| 161 | Call call_; |
| 162 | ServerContext ctx_; |
| 163 | const bool has_request_payload_; |
| 164 | const bool has_response_payload_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 165 | grpc_byte_buffer* request_payload_; |
| 166 | RpcServiceMethod* const method_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | private: |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 170 | RpcServiceMethod* const method_; |
| 171 | void* const tag_; |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 172 | bool in_flight_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 173 | const bool has_request_payload_; |
| 174 | const bool has_response_payload_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 175 | grpc_call* call_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 176 | gpr_timespec deadline_; |
| 177 | grpc_metadata_array request_metadata_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 178 | grpc_byte_buffer* request_payload_; |
| 179 | grpc_completion_queue* cq_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 182 | static grpc_server* CreateServer(int max_message_size) { |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 183 | if (max_message_size > 0) { |
| 184 | grpc_arg arg; |
| 185 | arg.type = GRPC_ARG_INTEGER; |
| 186 | arg.key = const_cast<char*>(GRPC_ARG_MAX_MESSAGE_LENGTH); |
| 187 | arg.value.integer = max_message_size; |
| 188 | grpc_channel_args args = {1, &arg}; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 189 | return grpc_server_create(&args); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 190 | } else { |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 191 | return grpc_server_create(nullptr); |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned, |
| 196 | int max_message_size) |
| 197 | : max_message_size_(max_message_size), |
| 198 | started_(false), |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 199 | shutdown_(false), |
| 200 | num_running_cb_(0), |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 201 | sync_methods_(new std::list<SyncRequest>), |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 202 | server_(CreateServer(max_message_size)), |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 203 | thread_pool_(thread_pool), |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 204 | thread_pool_owned_(thread_pool_owned) { |
| 205 | grpc_server_register_completion_queue(server_, cq_.cq()); |
| 206 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 207 | |
| 208 | Server::~Server() { |
Ruyi Wang | b486ba6 | 2015-03-14 22:19:44 +0800 | [diff] [blame] | 209 | { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 210 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Ruyi Wang | b486ba6 | 2015-03-14 22:19:44 +0800 | [diff] [blame] | 211 | if (started_ && !shutdown_) { |
| 212 | lock.unlock(); |
| 213 | Shutdown(); |
| 214 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 215 | } |
| 216 | grpc_server_destroy(server_); |
| 217 | if (thread_pool_owned_) { |
| 218 | delete thread_pool_; |
| 219 | } |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 220 | delete sync_methods_; |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | bool Server::RegisterService(RpcService* service) { |
| 224 | for (int i = 0; i < service->GetMethodCount(); ++i) { |
| 225 | RpcServiceMethod* method = service->GetMethod(i); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 226 | void* tag = grpc_server_register_method(server_, method->name(), nullptr); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 227 | if (!tag) { |
| 228 | gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", |
| 229 | method->name()); |
| 230 | return false; |
| 231 | } |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 232 | SyncRequest request(method, tag); |
| 233 | sync_methods_->emplace_back(request); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 234 | } |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | bool Server::RegisterAsyncService(AsynchronousService* service) { |
| 239 | GPR_ASSERT(service->dispatch_impl_ == nullptr && |
Yang Gao | 1ad253d | 2015-03-16 13:11:29 -0700 | [diff] [blame] | 240 | "Can only register an asynchronous service against one server."); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 241 | service->dispatch_impl_ = this; |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 242 | service->request_args_ = new void*[service->method_count_]; |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 243 | for (size_t i = 0; i < service->method_count_; ++i) { |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 244 | void* tag = grpc_server_register_method(server_, service->method_names_[i], |
| 245 | nullptr); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 246 | if (!tag) { |
| 247 | gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", |
| 248 | service->method_names_[i]); |
| 249 | return false; |
| 250 | } |
| 251 | service->request_args_[i] = tag; |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 256 | void Server::RegisterAsyncGenericService(AsyncGenericService* service) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 257 | GPR_ASSERT(service->server_ == nullptr && |
Yang Gao | 1ad253d | 2015-03-16 13:11:29 -0700 | [diff] [blame] | 258 | "Can only register an async generic service against one server."); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 259 | service->server_ = this; |
| 260 | } |
| 261 | |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 262 | int Server::AddListeningPort(const grpc::string& addr, |
| 263 | ServerCredentials* creds) { |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 264 | GPR_ASSERT(!started_); |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 265 | return creds->AddPortToServer(addr, server_); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 268 | bool Server::Start() { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 269 | GPR_ASSERT(!started_); |
| 270 | started_ = true; |
| 271 | grpc_server_start(server_); |
| 272 | |
| 273 | // Start processing rpcs. |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 274 | if (!sync_methods_->empty()) { |
| 275 | for (auto m = sync_methods_->begin(); m != sync_methods_->end(); m++) { |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 276 | m->Request(server_, cq_.cq()); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Craig Tiller | 7c72adc | 2015-02-09 14:07:26 -0800 | [diff] [blame] | 279 | ScheduleCallback(); |
| 280 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 281 | |
| 282 | return true; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 285 | void Server::Shutdown() { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 286 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 287 | if (started_ && !shutdown_) { |
| 288 | shutdown_ = true; |
| 289 | grpc_server_shutdown(server_); |
| 290 | cq_.Shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 291 | |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 292 | // Wait for running callbacks to finish. |
| 293 | while (num_running_cb_ != 0) { |
| 294 | callback_cv_.wait(lock); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 299 | void Server::Wait() { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 300 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 301 | while (num_running_cb_ != 0) { |
| 302 | callback_cv_.wait(lock); |
| 303 | } |
| 304 | } |
| 305 | |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 306 | void Server::PerformOpsOnCall(CallOpBuffer* buf, Call* call) { |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 307 | static const size_t MAX_OPS = 8; |
| 308 | size_t nops = MAX_OPS; |
| 309 | grpc_op ops[MAX_OPS]; |
| 310 | buf->FillOps(ops, &nops); |
| 311 | GPR_ASSERT(GRPC_CALL_OK == |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 312 | grpc_call_start_batch(call->call(), ops, nops, buf)); |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 315 | class Server::AsyncRequest GRPC_FINAL : public CompletionQueueTag { |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 316 | public: |
| 317 | AsyncRequest(Server* server, void* registered_method, ServerContext* ctx, |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 318 | grpc::protobuf::Message* request, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 319 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, |
| 320 | ServerCompletionQueue* notification_cq, void* tag) |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 321 | : tag_(tag), |
| 322 | request_(request), |
| 323 | stream_(stream), |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 324 | call_cq_(call_cq), |
| 325 | notification_cq_(notification_cq), |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 326 | ctx_(ctx), |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 327 | generic_ctx_(nullptr), |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 328 | server_(server), |
| 329 | call_(nullptr), |
| 330 | payload_(nullptr) { |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 331 | memset(&array_, 0, sizeof(array_)); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 332 | grpc_call_details_init(&call_details_); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 333 | GPR_ASSERT(notification_cq); |
| 334 | GPR_ASSERT(call_cq); |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 335 | grpc_server_request_registered_call( |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 336 | server->server_, registered_method, &call_, &call_details_.deadline, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 337 | &array_, request ? &payload_ : nullptr, call_cq->cq(), |
| 338 | notification_cq->cq(), this); |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 341 | AsyncRequest(Server* server, GenericServerContext* ctx, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 342 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, |
| 343 | ServerCompletionQueue* notification_cq, void* tag) |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 344 | : tag_(tag), |
| 345 | request_(nullptr), |
| 346 | stream_(stream), |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 347 | call_cq_(call_cq), |
| 348 | notification_cq_(notification_cq), |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 349 | ctx_(nullptr), |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 350 | generic_ctx_(ctx), |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 351 | server_(server), |
| 352 | call_(nullptr), |
| 353 | payload_(nullptr) { |
| 354 | memset(&array_, 0, sizeof(array_)); |
| 355 | grpc_call_details_init(&call_details_); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 356 | GPR_ASSERT(notification_cq); |
| 357 | GPR_ASSERT(call_cq); |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 358 | grpc_server_request_call(server->server_, &call_, &call_details_, &array_, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 359 | call_cq->cq(), notification_cq->cq(), this); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 362 | ~AsyncRequest() { |
| 363 | if (payload_) { |
| 364 | grpc_byte_buffer_destroy(payload_); |
| 365 | } |
| 366 | grpc_metadata_array_destroy(&array_); |
| 367 | } |
| 368 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 369 | bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE { |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 370 | *tag = tag_; |
Vijay Pai | dbb7963 | 2015-03-03 11:54:27 -0800 | [diff] [blame] | 371 | bool orig_status = *status; |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 372 | if (*status && request_) { |
| 373 | if (payload_) { |
David Garcia Quintas | bbc0b77 | 2015-04-29 14:10:05 -0700 | [diff] [blame] | 374 | GRPC_TIMER_BEGIN(GRPC_PTAG_PROTO_DESERIALIZE, call_); |
Yang Gao | c71a9d2 | 2015-05-04 00:22:12 -0700 | [diff] [blame] | 375 | *status = |
| 376 | DeserializeProto(payload_, request_, server_->max_message_size_); |
David Garcia Quintas | bbc0b77 | 2015-04-29 14:10:05 -0700 | [diff] [blame] | 377 | GRPC_TIMER_END(GRPC_PTAG_PROTO_DESERIALIZE, call_); |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 378 | } else { |
| 379 | *status = false; |
| 380 | } |
| 381 | } |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 382 | ServerContext* ctx = ctx_ ? ctx_ : generic_ctx_; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 383 | GPR_ASSERT(ctx); |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 384 | if (*status) { |
Nicolas Noble | 8921916 | 2015-04-07 18:01:18 -0700 | [diff] [blame] | 385 | ctx->deadline_ = call_details_.deadline; |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 386 | for (size_t i = 0; i < array_.count; i++) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 387 | ctx->client_metadata_.insert(std::make_pair( |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 388 | grpc::string(array_.metadata[i].key), |
| 389 | grpc::string( |
| 390 | array_.metadata[i].value, |
| 391 | array_.metadata[i].value + array_.metadata[i].value_length))); |
| 392 | } |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 393 | if (generic_ctx_) { |
Yang Gao | 2a3c96a | 2015-03-11 23:32:40 -0700 | [diff] [blame] | 394 | // TODO(yangg) remove the copy here. |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 395 | generic_ctx_->method_ = call_details_.method; |
| 396 | generic_ctx_->host_ = call_details_.host; |
Yang Gao | 2a3c96a | 2015-03-11 23:32:40 -0700 | [diff] [blame] | 397 | gpr_free(call_details_.method); |
| 398 | gpr_free(call_details_.host); |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 399 | } |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 400 | } |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 401 | ctx->call_ = call_; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 402 | ctx->cq_ = call_cq_; |
| 403 | Call call(call_, server_, call_cq_, server_->max_message_size_); |
Vijay Pai | dbb7963 | 2015-03-03 11:54:27 -0800 | [diff] [blame] | 404 | if (orig_status && call_) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 405 | ctx->BeginCompletionOp(&call); |
Vijay Pai | 0823cb7 | 2015-03-02 15:48:51 -0800 | [diff] [blame] | 406 | } |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 407 | // just the pointers inside call are copied here |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 408 | stream_->BindCall(&call); |
| 409 | delete this; |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 410 | return true; |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 411 | } |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 412 | |
| 413 | private: |
| 414 | void* const tag_; |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 415 | grpc::protobuf::Message* const request_; |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 416 | ServerAsyncStreamingInterface* const stream_; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 417 | CompletionQueue* const call_cq_; |
| 418 | ServerCompletionQueue* const notification_cq_; |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 419 | ServerContext* const ctx_; |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 420 | GenericServerContext* const generic_ctx_; |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 421 | Server* const server_; |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 422 | grpc_call* call_; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 423 | grpc_call_details call_details_; |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 424 | grpc_metadata_array array_; |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 425 | grpc_byte_buffer* payload_; |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | void Server::RequestAsyncCall(void* registered_method, ServerContext* context, |
Yang Gao | 7694c35 | 2015-03-03 09:48:06 -0800 | [diff] [blame] | 429 | grpc::protobuf::Message* request, |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 430 | ServerAsyncStreamingInterface* stream, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 431 | CompletionQueue* call_cq, |
| 432 | ServerCompletionQueue* notification_cq, |
| 433 | void* tag) { |
| 434 | new AsyncRequest(this, registered_method, context, request, stream, call_cq, |
| 435 | notification_cq, tag); |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 436 | } |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 437 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 438 | void Server::RequestAsyncGenericCall(GenericServerContext* context, |
| 439 | ServerAsyncStreamingInterface* stream, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 440 | CompletionQueue* call_cq, |
| 441 | ServerCompletionQueue* notification_cq, |
| 442 | void* tag) { |
| 443 | new AsyncRequest(this, context, stream, call_cq, notification_cq, tag); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 444 | } |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 445 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 446 | void Server::ScheduleCallback() { |
| 447 | { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 448 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 449 | num_running_cb_++; |
| 450 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 451 | thread_pool_->ScheduleCallback(std::bind(&Server::RunRpc, this)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void Server::RunRpc() { |
| 455 | // Wait for one more incoming rpc. |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 456 | bool ok; |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 457 | auto* mrd = SyncRequest::Wait(&cq_, &ok); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 458 | if (mrd) { |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 459 | ScheduleCallback(); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 460 | if (ok) { |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 461 | SyncRequest::CallData cd(this, mrd); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame^] | 462 | mrd->Request(server_, cq_.cq()); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 463 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 464 | cd.Run(); |
| 465 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 466 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 467 | |
| 468 | { |
Nicolas "Pixel" Noble | ff2828b | 2015-04-03 03:16:46 +0200 | [diff] [blame] | 469 | grpc::unique_lock<grpc::mutex> lock(mu_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 470 | num_running_cb_--; |
| 471 | if (shutdown_) { |
| 472 | callback_cv_.notify_all(); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | } // namespace grpc |