blob: a7aaa255724378cf939c9327c37b304d6d220d24 [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
Vijay Pai320ed132016-11-01 17:16:55 -070036#include <mutex>
37
David Garcia Quintas2425bbb2016-01-25 17:32:48 -080038#include <grpc++/completion_queue.h>
Nicolas Noble89219162015-04-07 18:01:18 -070039#include <grpc++/impl/call.h>
yang-g9e2f90c2015-08-21 15:35:03 -070040#include <grpc++/support/time.h>
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -080041#include <grpc/compression.h>
42#include <grpc/grpc.h>
43#include <grpc/support/alloc.h>
44#include <grpc/support/log.h>
yangga4b6f5d2014-12-17 15:53:12 -080045
Craig Tiller9533d042016-03-25 17:11:06 -070046#include "src/core/lib/surface/call.h"
yang-g3abe60b2015-07-06 14:00:36 -070047
Craig Tiller854a30c2015-02-11 11:44:10 -080048namespace grpc {
49
Craig Tiller492968f2015-02-18 13:14:03 -080050// CompletionOp
51
Vijay Paic0b2acb2016-11-01 16:31:56 -070052class ServerContext::CompletionOp final : public CallOpSetInterface {
Craig Tiller492968f2015-02-18 13:14:03 -080053 public:
Craig Tillercf133f42015-02-26 14:05:56 -080054 // initial refs: one in the server context, one in the cq
Craig Tillerd6c98df2015-08-18 09:33:44 -070055 CompletionOp()
56 : has_tag_(false),
57 tag_(nullptr),
58 refs_(2),
59 finalized_(false),
60 cancelled_(0) {}
Craig Tiller50a7a682015-06-04 12:53:40 -070061
Vijay Paic0b2acb2016-11-01 16:31:56 -070062 void FillOps(grpc_op* ops, size_t* nops) override;
63 bool FinalizeResult(void** tag, bool* status) override;
Craig Tiller492968f2015-02-18 13:14:03 -080064
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080065 bool CheckCancelled(CompletionQueue* cq) {
66 cq->TryPluck(this);
67 return CheckCancelledNoPluck();
68 }
69 bool CheckCancelledAsync() { return CheckCancelledNoPluck(); }
Craig Tiller492968f2015-02-18 13:14:03 -080070
yang-gd45a26e2015-08-04 16:36:22 -070071 void set_tag(void* tag) {
72 has_tag_ = true;
73 tag_ = tag;
74 }
yang-gb3352562015-08-04 14:42:06 -070075
Craig Tiller492968f2015-02-18 13:14:03 -080076 void Unref();
77
78 private:
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080079 bool CheckCancelledNoPluck() {
Vijay Pai320ed132016-11-01 17:16:55 -070080 std::lock_guard<std::mutex> g(mu_);
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080081 return finalized_ ? (cancelled_ != 0) : false;
82 }
83
yang-gd45a26e2015-08-04 16:36:22 -070084 bool has_tag_;
yang-gb3352562015-08-04 14:42:06 -070085 void* tag_;
Vijay Pai320ed132016-11-01 17:16:55 -070086 std::mutex mu_;
Craig Tillercf133f42015-02-26 14:05:56 -080087 int refs_;
88 bool finalized_;
Craig Tiller50a7a682015-06-04 12:53:40 -070089 int cancelled_;
Craig Tiller492968f2015-02-18 13:14:03 -080090};
91
Craig Tiller492968f2015-02-18 13:14:03 -080092void ServerContext::CompletionOp::Unref() {
Vijay Pai320ed132016-11-01 17:16:55 -070093 std::unique_lock<std::mutex> lock(mu_);
Craig Tiller492968f2015-02-18 13:14:03 -080094 if (--refs_ == 0) {
95 lock.unlock();
96 delete this;
97 }
98}
99
Craig Tiller50a7a682015-06-04 12:53:40 -0700100void ServerContext::CompletionOp::FillOps(grpc_op* ops, size_t* nops) {
101 ops->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
102 ops->data.recv_close_on_server.cancelled = &cancelled_;
Craig Tiller026e6002015-06-22 11:41:14 -0700103 ops->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200104 ops->reserved = NULL;
Craig Tiller50a7a682015-06-04 12:53:40 -0700105 *nops = 1;
Craig Tiller492968f2015-02-18 13:14:03 -0800106}
107
108bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) {
Vijay Pai320ed132016-11-01 17:16:55 -0700109 std::unique_lock<std::mutex> lock(mu_);
Craig Tiller492968f2015-02-18 13:14:03 -0800110 finalized_ = true;
yang-gb3352562015-08-04 14:42:06 -0700111 bool ret = false;
yang-gd45a26e2015-08-04 16:36:22 -0700112 if (has_tag_) {
yang-gb3352562015-08-04 14:42:06 -0700113 *tag = tag_;
114 ret = true;
115 }
Craig Tiller50a7a682015-06-04 12:53:40 -0700116 if (!*status) cancelled_ = 1;
Craig Tiller492968f2015-02-18 13:14:03 -0800117 if (--refs_ == 0) {
118 lock.unlock();
119 delete this;
120 }
yang-gb3352562015-08-04 14:42:06 -0700121 return ret;
Craig Tiller492968f2015-02-18 13:14:03 -0800122}
123
124// ServerContext body
125
Craig Tillercf133f42015-02-26 14:05:56 -0800126ServerContext::ServerContext()
127 : completion_op_(nullptr),
yang-gd45a26e2015-08-04 16:36:22 -0700128 has_notify_when_done_tag_(false),
yang-gb3352562015-08-04 14:42:06 -0700129 async_notify_when_done_tag_(nullptr),
yang-g5ff8de32016-01-22 16:55:10 -0800130 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
Craig Tillercf133f42015-02-26 14:05:56 -0800131 call_(nullptr),
132 cq_(nullptr),
David Garcia Quintas440558a2016-07-21 19:31:38 -0700133 sent_initial_metadata_(false),
134 compression_level_set_(false) {}
Craig Tillerc6453062015-02-12 17:32:57 -0800135
Craig Tiller645466e2015-02-18 09:18:33 -0800136ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata* metadata,
Craig Tiller9dcb0f82015-02-11 15:36:31 -0800137 size_t metadata_count)
Craig Tillercf133f42015-02-26 14:05:56 -0800138 : completion_op_(nullptr),
yang-gd45a26e2015-08-04 16:36:22 -0700139 has_notify_when_done_tag_(false),
yang-gb3352562015-08-04 14:42:06 -0700140 async_notify_when_done_tag_(nullptr),
Nicolas Noble89219162015-04-07 18:01:18 -0700141 deadline_(deadline),
Craig Tillercf133f42015-02-26 14:05:56 -0800142 call_(nullptr),
143 cq_(nullptr),
David Garcia Quintas39228382016-07-22 13:13:31 -0700144 sent_initial_metadata_(false),
145 compression_level_set_(false) {
Craig Tiller854a30c2015-02-11 11:44:10 -0800146 for (size_t i = 0; i < metadata_count; i++) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800147 client_metadata_.map()->insert(
148 std::pair<grpc::string_ref, grpc::string_ref>(
149 StringRefFromSlice(&metadata[i].key),
150 StringRefFromSlice(&metadata[i].value)));
Craig Tiller854a30c2015-02-11 11:44:10 -0800151 }
152}
153
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800154ServerContext::~ServerContext() {
155 if (call_) {
156 grpc_call_destroy(call_);
157 }
Craig Tiller492968f2015-02-18 13:14:03 -0800158 if (completion_op_) {
159 completion_op_->Unref();
160 }
161}
162
163void ServerContext::BeginCompletionOp(Call* call) {
164 GPR_ASSERT(!completion_op_);
165 completion_op_ = new CompletionOp();
yang-gd45a26e2015-08-04 16:36:22 -0700166 if (has_notify_when_done_tag_) {
167 completion_op_->set_tag(async_notify_when_done_tag_);
168 }
Craig Tiller492968f2015-02-18 13:14:03 -0800169 call->PerformOps(completion_op_);
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800170}
171
Yang Gao2b7f5372015-02-18 00:45:53 -0800172void ServerContext::AddInitialMetadata(const grpc::string& key,
Craig Tiller645466e2015-02-18 09:18:33 -0800173 const grpc::string& value) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800174 initial_metadata_.insert(std::make_pair(key, value));
175}
176
177void ServerContext::AddTrailingMetadata(const grpc::string& key,
Craig Tiller645466e2015-02-18 09:18:33 -0800178 const grpc::string& value) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800179 trailing_metadata_.insert(std::make_pair(key, value));
180}
181
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800182void ServerContext::TryCancel() const {
183 grpc_call_error err = grpc_call_cancel_with_status(
184 call_, GRPC_STATUS_CANCELLED, "Cancelled on the server side", NULL);
185 if (err != GRPC_CALL_OK) {
Sree Kuchibhotla369a04a2016-02-01 10:53:13 -0800186 gpr_log(GPR_ERROR, "TryCancel failed with: %d", err);
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800187 }
188}
189
David Garcia Quintas6dd49a52015-07-15 14:58:32 -0700190bool ServerContext::IsCancelled() const {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800191 if (has_notify_when_done_tag_) {
192 // when using async API, but the result is only valid
193 // if the tag has already been delivered at the completion queue
194 return completion_op_ && completion_op_->CheckCancelledAsync();
195 } else {
196 // when using sync API
197 return completion_op_ && completion_op_->CheckCancelled(cq_);
198 }
Craig Tiller645466e2015-02-18 09:18:33 -0800199}
200
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700201void ServerContext::set_compression_algorithm(
202 grpc_compression_algorithm algorithm) {
203 char* algorithm_name = NULL;
204 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
205 gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
206 algorithm);
207 abort();
208 }
209 GPR_ASSERT(algorithm_name != NULL);
David Garcia Quintas9e9f7b62016-05-16 19:12:12 -0700210 AddInitialMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name);
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700211}
212
yang-gf1ec3772015-07-28 22:59:50 -0700213grpc::string ServerContext::peer() const {
214 grpc::string peer;
215 if (call_) {
216 char* c_peer = grpc_call_get_peer(call_);
217 peer = c_peer;
218 gpr_free(c_peer);
219 }
220 return peer;
221}
222
Alistair Veitch100a6e12015-07-29 15:25:28 -0700223const struct census_context* ServerContext::census_context() const {
Alistair Veitchcf39e942015-07-26 17:28:26 -0700224 return grpc_census_call_get_context(call_);
225}
226
Craig Tiller854a30c2015-02-11 11:44:10 -0800227} // namespace grpc