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