Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 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> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc++/completion_queue.h> |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 41 | #include <grpc++/impl/rpc_service_method.h> |
Craig Tiller | b470169 | 2015-02-09 16:12:00 -0800 | [diff] [blame] | 42 | #include <grpc++/server_context.h> |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 43 | #include <grpc++/server_credentials.h> |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 44 | #include <grpc++/thread_pool_interface.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 46 | #include "src/cpp/proto/proto_utils.h" |
| 47 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | namespace grpc { |
| 49 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 50 | Server::Server(ThreadPoolInterface *thread_pool, bool thread_pool_owned, |
| 51 | ServerCredentials *creds) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | : started_(false), |
| 53 | shutdown_(false), |
| 54 | num_running_cb_(0), |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 55 | thread_pool_(thread_pool), |
| 56 | thread_pool_owned_(thread_pool_owned), |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 57 | secure_(creds != nullptr) { |
| 58 | if (creds) { |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 59 | server_ = grpc_secure_server_create(creds->GetRawCreds(), cq_.cq(), nullptr); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 60 | } else { |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 61 | server_ = grpc_server_create(cq_.cq(), nullptr); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 62 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | Server::Server() { |
| 66 | // Should not be called. |
| 67 | GPR_ASSERT(false); |
| 68 | } |
| 69 | |
| 70 | Server::~Server() { |
| 71 | std::unique_lock<std::mutex> lock(mu_); |
| 72 | if (started_ && !shutdown_) { |
| 73 | lock.unlock(); |
| 74 | Shutdown(); |
Craig Tiller | 061754a | 2015-02-09 14:56:49 -0800 | [diff] [blame] | 75 | } else { |
| 76 | lock.unlock(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | } |
| 78 | grpc_server_destroy(server_); |
| 79 | if (thread_pool_owned_) { |
| 80 | delete thread_pool_; |
| 81 | } |
| 82 | } |
| 83 | |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 84 | bool Server::RegisterService(RpcService *service) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 85 | for (int i = 0; i < service->GetMethodCount(); ++i) { |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 86 | RpcServiceMethod *method = service->GetMethod(i); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 87 | void *tag = grpc_server_register_method(server_, method->name(), nullptr); |
| 88 | if (!tag) { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 89 | gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", |
| 90 | method->name()); |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 91 | return false; |
| 92 | } |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 93 | methods_.emplace_back(method, tag); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 94 | } |
Craig Tiller | 7c72adc | 2015-02-09 14:07:26 -0800 | [diff] [blame] | 95 | return true; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 98 | int Server::AddPort(const grpc::string &addr) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | GPR_ASSERT(!started_); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 100 | if (secure_) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 101 | return grpc_server_add_secure_http2_port(server_, addr.c_str()); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 102 | } else { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 103 | return grpc_server_add_http2_port(server_, addr.c_str()); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 104 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 107 | class Server::MethodRequestData final : public CompletionQueueTag { |
| 108 | public: |
| 109 | MethodRequestData(RpcServiceMethod *method, void *tag) |
| 110 | : method_(method), |
| 111 | tag_(tag), |
| 112 | has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC || |
| 113 | method->method_type() == |
| 114 | RpcMethod::SERVER_STREAMING), |
| 115 | has_response_payload_(method->method_type() == RpcMethod::NORMAL_RPC || |
| 116 | method->method_type() == |
| 117 | RpcMethod::CLIENT_STREAMING) { |
| 118 | grpc_metadata_array_init(&request_metadata_); |
| 119 | } |
| 120 | |
| 121 | static MethodRequestData *Wait(CompletionQueue *cq, bool *ok) { |
| 122 | void *tag; |
| 123 | if (!cq->Next(&tag, ok)) { |
| 124 | return nullptr; |
| 125 | } |
| 126 | auto *mrd = static_cast<MethodRequestData *>(tag); |
| 127 | GPR_ASSERT(mrd->in_flight_); |
| 128 | return mrd; |
| 129 | } |
| 130 | |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 131 | void Request(grpc_server *server) { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 132 | GPR_ASSERT(!in_flight_); |
| 133 | in_flight_ = true; |
| 134 | cq_ = grpc_completion_queue_create(); |
| 135 | GPR_ASSERT(GRPC_CALL_OK == |
| 136 | grpc_server_request_registered_call( |
| 137 | server, tag_, &call_, &deadline_, &request_metadata_, |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 138 | has_request_payload_ ? &request_payload_ : nullptr, |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 139 | cq_, this)); |
| 140 | } |
| 141 | |
Craig Tiller | 04c8ff0 | 2015-02-11 11:01:07 -0800 | [diff] [blame] | 142 | void FinalizeResult(void **tag, bool *status) override {} |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 143 | |
| 144 | class CallData { |
| 145 | public: |
| 146 | explicit CallData(MethodRequestData *mrd) |
| 147 | : cq_(mrd->cq_), |
| 148 | call_(mrd->call_, nullptr, &cq_), |
| 149 | ctx_(mrd->deadline_, mrd->request_metadata_.metadata, |
| 150 | mrd->request_metadata_.count), |
| 151 | has_request_payload_(mrd->has_request_payload_), |
| 152 | has_response_payload_(mrd->has_response_payload_), |
| 153 | request_payload_(mrd->request_payload_), |
| 154 | method_(mrd->method_) { |
| 155 | GPR_ASSERT(mrd->in_flight_); |
| 156 | mrd->in_flight_ = false; |
| 157 | mrd->request_metadata_.count = 0; |
| 158 | } |
| 159 | |
| 160 | void Run() { |
| 161 | std::unique_ptr<google::protobuf::Message> req; |
| 162 | std::unique_ptr<google::protobuf::Message> res; |
| 163 | if (has_request_payload_) { |
| 164 | req.reset(method_->AllocateRequestProto()); |
| 165 | if (!DeserializeProto(request_payload_, req.get())) { |
| 166 | abort(); // for now |
| 167 | } |
| 168 | } |
| 169 | if (has_response_payload_) { |
Craig Tiller | 7c2f3f7 | 2015-02-11 13:21:54 -0800 | [diff] [blame^] | 170 | res.reset(method_->AllocateResponseProto()); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 171 | } |
| 172 | auto status = method_->handler()->RunHandler( |
| 173 | MethodHandler::HandlerParameter(&call_, &ctx_, req.get(), res.get())); |
| 174 | CallOpBuffer buf; |
| 175 | buf.AddServerSendStatus(nullptr, status); |
| 176 | if (has_response_payload_) { |
| 177 | buf.AddSendMessage(*res); |
| 178 | } |
| 179 | call_.PerformOps(&buf); |
| 180 | GPR_ASSERT(cq_.Pluck(&buf)); |
| 181 | } |
| 182 | |
| 183 | private: |
| 184 | CompletionQueue cq_; |
| 185 | Call call_; |
| 186 | ServerContext ctx_; |
| 187 | const bool has_request_payload_; |
| 188 | const bool has_response_payload_; |
| 189 | grpc_byte_buffer *request_payload_; |
| 190 | RpcServiceMethod *const method_; |
| 191 | }; |
| 192 | |
| 193 | private: |
| 194 | RpcServiceMethod *const method_; |
| 195 | void *const tag_; |
| 196 | bool in_flight_ = false; |
| 197 | const bool has_request_payload_; |
| 198 | const bool has_response_payload_; |
| 199 | grpc_call *call_; |
| 200 | gpr_timespec deadline_; |
| 201 | grpc_metadata_array request_metadata_; |
| 202 | grpc_byte_buffer *request_payload_; |
| 203 | grpc_completion_queue *cq_; |
| 204 | }; |
| 205 | |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 206 | bool Server::Start() { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 207 | GPR_ASSERT(!started_); |
| 208 | started_ = true; |
| 209 | grpc_server_start(server_); |
| 210 | |
| 211 | // Start processing rpcs. |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 212 | if (!methods_.empty()) { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 213 | for (auto &m : methods_) { |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 214 | m.Request(server_); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Craig Tiller | 7c72adc | 2015-02-09 14:07:26 -0800 | [diff] [blame] | 217 | ScheduleCallback(); |
| 218 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 219 | |
| 220 | return true; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 223 | void Server::Shutdown() { |
| 224 | { |
| 225 | std::unique_lock<std::mutex> lock(mu_); |
| 226 | if (started_ && !shutdown_) { |
| 227 | shutdown_ = true; |
| 228 | grpc_server_shutdown(server_); |
| 229 | |
| 230 | // Wait for running callbacks to finish. |
| 231 | while (num_running_cb_ != 0) { |
| 232 | callback_cv_.wait(lock); |
| 233 | } |
| 234 | } |
| 235 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void Server::ScheduleCallback() { |
| 239 | { |
| 240 | std::unique_lock<std::mutex> lock(mu_); |
| 241 | num_running_cb_++; |
| 242 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 243 | thread_pool_->ScheduleCallback(std::bind(&Server::RunRpc, this)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void Server::RunRpc() { |
| 247 | // Wait for one more incoming rpc. |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 248 | bool ok; |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 249 | auto *mrd = MethodRequestData::Wait(&cq_, &ok); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 250 | if (mrd) { |
| 251 | MethodRequestData::CallData cd(mrd); |
| 252 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 253 | if (ok) { |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 254 | mrd->Request(server_); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 255 | ScheduleCallback(); |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 256 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 257 | cd.Run(); |
| 258 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 259 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 260 | |
| 261 | { |
| 262 | std::unique_lock<std::mutex> lock(mu_); |
| 263 | num_running_cb_--; |
| 264 | if (shutdown_) { |
| 265 | callback_cv_.notify_all(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | } // namespace grpc |