blob: afc9873c148ac9628282e3ba3985910ecc2c13c5 [file] [log] [blame]
yangga4b6f5d2014-12-17 15:53:12 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
yangga4b6f5d2014-12-17 15:53:12 -08004 * 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
Craig Tillerc4165772015-02-11 10:51:04 -080034#include <grpc++/server_context.h>
Craig Tillerc7625b02015-02-18 15:18:58 -080035
yang-gc42d8442017-02-15 00:05:00 -080036#include <algorithm>
Vijay Pai320ed132016-11-01 17:16:55 -070037#include <mutex>
yang-gc42d8442017-02-15 00:05:00 -080038#include <utility>
Vijay Pai320ed132016-11-01 17:16:55 -070039
David Garcia Quintas2425bbb2016-01-25 17:32:48 -080040#include <grpc++/completion_queue.h>
Nicolas Noble89219162015-04-07 18:01:18 -070041#include <grpc++/impl/call.h>
yang-g9e2f90c2015-08-21 15:35:03 -070042#include <grpc++/support/time.h>
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -080043#include <grpc/compression.h>
44#include <grpc/grpc.h>
David Garcia Quintas55c895a2017-03-30 10:43:14 -070045#include <grpc/load_reporting.h>
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -080046#include <grpc/support/alloc.h>
47#include <grpc/support/log.h>
yangga4b6f5d2014-12-17 15:53:12 -080048
Craig Tiller9533d042016-03-25 17:11:06 -070049#include "src/core/lib/surface/call.h"
yang-g3abe60b2015-07-06 14:00:36 -070050
Craig Tiller854a30c2015-02-11 11:44:10 -080051namespace grpc {
52
Craig Tiller492968f2015-02-18 13:14:03 -080053// CompletionOp
54
Vijay Paic0b2acb2016-11-01 16:31:56 -070055class ServerContext::CompletionOp final : public CallOpSetInterface {
Craig Tiller492968f2015-02-18 13:14:03 -080056 public:
Craig Tillercf133f42015-02-26 14:05:56 -080057 // initial refs: one in the server context, one in the cq
Craig Tillerd6c98df2015-08-18 09:33:44 -070058 CompletionOp()
59 : has_tag_(false),
60 tag_(nullptr),
61 refs_(2),
62 finalized_(false),
63 cancelled_(0) {}
Craig Tiller50a7a682015-06-04 12:53:40 -070064
Vijay Paic0b2acb2016-11-01 16:31:56 -070065 void FillOps(grpc_op* ops, size_t* nops) override;
66 bool FinalizeResult(void** tag, bool* status) override;
Craig Tiller492968f2015-02-18 13:14:03 -080067
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080068 bool CheckCancelled(CompletionQueue* cq) {
69 cq->TryPluck(this);
70 return CheckCancelledNoPluck();
71 }
72 bool CheckCancelledAsync() { return CheckCancelledNoPluck(); }
Craig Tiller492968f2015-02-18 13:14:03 -080073
yang-gd45a26e2015-08-04 16:36:22 -070074 void set_tag(void* tag) {
75 has_tag_ = true;
76 tag_ = tag;
77 }
yang-gb3352562015-08-04 14:42:06 -070078
Craig Tiller492968f2015-02-18 13:14:03 -080079 void Unref();
80
81 private:
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080082 bool CheckCancelledNoPluck() {
Vijay Pai320ed132016-11-01 17:16:55 -070083 std::lock_guard<std::mutex> g(mu_);
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080084 return finalized_ ? (cancelled_ != 0) : false;
85 }
86
yang-gd45a26e2015-08-04 16:36:22 -070087 bool has_tag_;
yang-gb3352562015-08-04 14:42:06 -070088 void* tag_;
Vijay Pai320ed132016-11-01 17:16:55 -070089 std::mutex mu_;
Craig Tillercf133f42015-02-26 14:05:56 -080090 int refs_;
91 bool finalized_;
Craig Tiller50a7a682015-06-04 12:53:40 -070092 int cancelled_;
Craig Tiller492968f2015-02-18 13:14:03 -080093};
94
Craig Tiller492968f2015-02-18 13:14:03 -080095void ServerContext::CompletionOp::Unref() {
Vijay Pai320ed132016-11-01 17:16:55 -070096 std::unique_lock<std::mutex> lock(mu_);
Craig Tiller492968f2015-02-18 13:14:03 -080097 if (--refs_ == 0) {
98 lock.unlock();
99 delete this;
100 }
101}
102
Craig Tiller50a7a682015-06-04 12:53:40 -0700103void ServerContext::CompletionOp::FillOps(grpc_op* ops, size_t* nops) {
104 ops->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
105 ops->data.recv_close_on_server.cancelled = &cancelled_;
Craig Tiller026e6002015-06-22 11:41:14 -0700106 ops->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200107 ops->reserved = NULL;
Craig Tiller50a7a682015-06-04 12:53:40 -0700108 *nops = 1;
Craig Tiller492968f2015-02-18 13:14:03 -0800109}
110
111bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) {
Vijay Pai320ed132016-11-01 17:16:55 -0700112 std::unique_lock<std::mutex> lock(mu_);
Craig Tiller492968f2015-02-18 13:14:03 -0800113 finalized_ = true;
yang-gb3352562015-08-04 14:42:06 -0700114 bool ret = false;
yang-gd45a26e2015-08-04 16:36:22 -0700115 if (has_tag_) {
yang-gb3352562015-08-04 14:42:06 -0700116 *tag = tag_;
117 ret = true;
118 }
Craig Tiller50a7a682015-06-04 12:53:40 -0700119 if (!*status) cancelled_ = 1;
Craig Tiller492968f2015-02-18 13:14:03 -0800120 if (--refs_ == 0) {
121 lock.unlock();
122 delete this;
123 }
yang-gb3352562015-08-04 14:42:06 -0700124 return ret;
Craig Tiller492968f2015-02-18 13:14:03 -0800125}
126
127// ServerContext body
128
Craig Tillercf133f42015-02-26 14:05:56 -0800129ServerContext::ServerContext()
130 : completion_op_(nullptr),
yang-gd45a26e2015-08-04 16:36:22 -0700131 has_notify_when_done_tag_(false),
yang-gb3352562015-08-04 14:42:06 -0700132 async_notify_when_done_tag_(nullptr),
yang-g5ff8de32016-01-22 16:55:10 -0800133 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
Craig Tillercf133f42015-02-26 14:05:56 -0800134 call_(nullptr),
135 cq_(nullptr),
David Garcia Quintas440558a2016-07-21 19:31:38 -0700136 sent_initial_metadata_(false),
137 compression_level_set_(false) {}
Craig Tillerc6453062015-02-12 17:32:57 -0800138
yang-gc42d8442017-02-15 00:05:00 -0800139ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata_array* arr)
Craig Tillercf133f42015-02-26 14:05:56 -0800140 : completion_op_(nullptr),
yang-gd45a26e2015-08-04 16:36:22 -0700141 has_notify_when_done_tag_(false),
yang-gb3352562015-08-04 14:42:06 -0700142 async_notify_when_done_tag_(nullptr),
Nicolas Noble89219162015-04-07 18:01:18 -0700143 deadline_(deadline),
Craig Tillercf133f42015-02-26 14:05:56 -0800144 call_(nullptr),
145 cq_(nullptr),
David Garcia Quintas39228382016-07-22 13:13:31 -0700146 sent_initial_metadata_(false),
147 compression_level_set_(false) {
yang-gc42d8442017-02-15 00:05:00 -0800148 std::swap(*client_metadata_.arr(), *arr);
149 client_metadata_.FillMap();
Craig Tiller854a30c2015-02-11 11:44:10 -0800150}
151
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800152ServerContext::~ServerContext() {
153 if (call_) {
154 grpc_call_destroy(call_);
155 }
Craig Tiller492968f2015-02-18 13:14:03 -0800156 if (completion_op_) {
157 completion_op_->Unref();
158 }
159}
160
161void ServerContext::BeginCompletionOp(Call* call) {
162 GPR_ASSERT(!completion_op_);
163 completion_op_ = new CompletionOp();
yang-gd45a26e2015-08-04 16:36:22 -0700164 if (has_notify_when_done_tag_) {
165 completion_op_->set_tag(async_notify_when_done_tag_);
166 }
Craig Tiller492968f2015-02-18 13:14:03 -0800167 call->PerformOps(completion_op_);
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800168}
169
Sree Kuchibhotlafa71f6f2017-04-01 17:32:34 -0700170CompletionQueueTag* ServerContext::GetCompletionOpTag() {
171 return static_cast<CompletionQueueTag*>(completion_op_);
172}
173
Yang Gao2b7f5372015-02-18 00:45:53 -0800174void ServerContext::AddInitialMetadata(const grpc::string& key,
Craig Tiller645466e2015-02-18 09:18:33 -0800175 const grpc::string& value) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800176 initial_metadata_.insert(std::make_pair(key, value));
177}
178
179void ServerContext::AddTrailingMetadata(const grpc::string& key,
Craig Tiller645466e2015-02-18 09:18:33 -0800180 const grpc::string& value) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800181 trailing_metadata_.insert(std::make_pair(key, value));
182}
183
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800184void ServerContext::TryCancel() const {
185 grpc_call_error err = grpc_call_cancel_with_status(
186 call_, GRPC_STATUS_CANCELLED, "Cancelled on the server side", NULL);
187 if (err != GRPC_CALL_OK) {
Sree Kuchibhotla369a04a2016-02-01 10:53:13 -0800188 gpr_log(GPR_ERROR, "TryCancel failed with: %d", err);
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800189 }
190}
191
David Garcia Quintas6dd49a52015-07-15 14:58:32 -0700192bool ServerContext::IsCancelled() const {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800193 if (has_notify_when_done_tag_) {
194 // when using async API, but the result is only valid
195 // if the tag has already been delivered at the completion queue
196 return completion_op_ && completion_op_->CheckCancelledAsync();
197 } else {
198 // when using sync API
199 return completion_op_ && completion_op_->CheckCancelled(cq_);
200 }
Craig Tiller645466e2015-02-18 09:18:33 -0800201}
202
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700203void ServerContext::set_compression_algorithm(
204 grpc_compression_algorithm algorithm) {
205 char* algorithm_name = NULL;
206 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
207 gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
208 algorithm);
209 abort();
210 }
211 GPR_ASSERT(algorithm_name != NULL);
David Garcia Quintas9e9f7b62016-05-16 19:12:12 -0700212 AddInitialMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name);
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700213}
214
yang-gf1ec3772015-07-28 22:59:50 -0700215grpc::string ServerContext::peer() const {
216 grpc::string peer;
217 if (call_) {
218 char* c_peer = grpc_call_get_peer(call_);
219 peer = c_peer;
220 gpr_free(c_peer);
221 }
222 return peer;
223}
224
Alistair Veitch100a6e12015-07-29 15:25:28 -0700225const struct census_context* ServerContext::census_context() const {
Alistair Veitchcf39e942015-07-26 17:28:26 -0700226 return grpc_census_call_get_context(call_);
227}
228
David Garcia Quintasf31f0962017-02-10 14:53:58 -0800229void ServerContext::SetLoadReportingCosts(
230 const std::vector<grpc::string>& cost_data) {
231 if (call_ == nullptr) return;
232 grpc_load_reporting_cost_context* cost_ctx =
233 static_cast<grpc_load_reporting_cost_context*>(
234 gpr_malloc(sizeof(*cost_ctx)));
235 cost_ctx->values_count = cost_data.size();
236 cost_ctx->values = static_cast<grpc_slice*>(
237 gpr_malloc(sizeof(*cost_ctx->values) * cost_ctx->values_count));
238 for (size_t i = 0; i < cost_ctx->values_count; ++i) {
239 cost_ctx->values[i] =
240 grpc_slice_from_copied_buffer(cost_data[i].data(), cost_data[i].size());
241 }
242 grpc_call_set_load_reporting_cost_context(call_, cost_ctx);
243}
244
Craig Tiller854a30c2015-02-11 11:44:10 -0800245} // namespace grpc