blob: 4a5ac5af926bbc8568d71458e56a4b349529f125 [file] [log] [blame]
yangga4b6f5d2014-12-17 15:53:12 -08001/*
2 *
Jan Tattermuschc4d9f6d2016-01-23 07:50:25 -08003 * Copyright 2015-2016, 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
David Garcia Quintas2425bbb2016-01-25 17:32:48 -080036#include <grpc++/completion_queue.h>
Nicolas Noble89219162015-04-07 18:01:18 -070037#include <grpc++/impl/call.h>
38#include <grpc++/impl/sync.h>
yang-g9e2f90c2015-08-21 15:35:03 -070039#include <grpc++/support/time.h>
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -080040#include <grpc/compression.h>
41#include <grpc/grpc.h>
42#include <grpc/support/alloc.h>
43#include <grpc/support/log.h>
yangga4b6f5d2014-12-17 15:53:12 -080044
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -070045#include "src/core/channel/compress_filter.h"
yang-g3abe60b2015-07-06 14:00:36 -070046#include "src/cpp/common/create_auth_context.h"
47
Craig Tiller854a30c2015-02-11 11:44:10 -080048namespace grpc {
49
Craig Tiller492968f2015-02-18 13:14:03 -080050// CompletionOp
51
Craig Tiller50a7a682015-06-04 12:53:40 -070052class ServerContext::CompletionOp GRPC_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
62 void FillOps(grpc_op* ops, size_t* nops) GRPC_OVERRIDE;
Craig Tillercf133f42015-02-26 14:05:56 -080063 bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE;
Craig Tiller492968f2015-02-18 13:14:03 -080064
65 bool CheckCancelled(CompletionQueue* cq);
66
yang-gd45a26e2015-08-04 16:36:22 -070067 void set_tag(void* tag) {
68 has_tag_ = true;
69 tag_ = tag;
70 }
yang-gb3352562015-08-04 14:42:06 -070071
Craig Tiller492968f2015-02-18 13:14:03 -080072 void Unref();
73
74 private:
yang-gd45a26e2015-08-04 16:36:22 -070075 bool has_tag_;
yang-gb3352562015-08-04 14:42:06 -070076 void* tag_;
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +020077 grpc::mutex mu_;
Craig Tillercf133f42015-02-26 14:05:56 -080078 int refs_;
79 bool finalized_;
Craig Tiller50a7a682015-06-04 12:53:40 -070080 int cancelled_;
Craig Tiller492968f2015-02-18 13:14:03 -080081};
82
Craig Tiller492968f2015-02-18 13:14:03 -080083void ServerContext::CompletionOp::Unref() {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +020084 grpc::unique_lock<grpc::mutex> lock(mu_);
Craig Tiller492968f2015-02-18 13:14:03 -080085 if (--refs_ == 0) {
86 lock.unlock();
87 delete this;
88 }
89}
90
91bool ServerContext::CompletionOp::CheckCancelled(CompletionQueue* cq) {
Craig Tillerd24d13d2015-02-18 15:35:32 -080092 cq->TryPluck(this);
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +020093 grpc::lock_guard<grpc::mutex> g(mu_);
Craig Tiller50a7a682015-06-04 12:53:40 -070094 return finalized_ ? cancelled_ != 0 : false;
95}
96
97void ServerContext::CompletionOp::FillOps(grpc_op* ops, size_t* nops) {
98 ops->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
99 ops->data.recv_close_on_server.cancelled = &cancelled_;
Craig Tiller026e6002015-06-22 11:41:14 -0700100 ops->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200101 ops->reserved = NULL;
Craig Tiller50a7a682015-06-04 12:53:40 -0700102 *nops = 1;
Craig Tiller492968f2015-02-18 13:14:03 -0800103}
104
105bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) {
Nicolas "Pixel" Nobleff2828b2015-04-03 03:16:46 +0200106 grpc::unique_lock<grpc::mutex> lock(mu_);
Craig Tiller492968f2015-02-18 13:14:03 -0800107 finalized_ = true;
yang-gb3352562015-08-04 14:42:06 -0700108 bool ret = false;
yang-gd45a26e2015-08-04 16:36:22 -0700109 if (has_tag_) {
yang-gb3352562015-08-04 14:42:06 -0700110 *tag = tag_;
111 ret = true;
112 }
Craig Tiller50a7a682015-06-04 12:53:40 -0700113 if (!*status) cancelled_ = 1;
Craig Tiller492968f2015-02-18 13:14:03 -0800114 if (--refs_ == 0) {
115 lock.unlock();
116 delete this;
117 }
yang-gb3352562015-08-04 14:42:06 -0700118 return ret;
Craig Tiller492968f2015-02-18 13:14:03 -0800119}
120
121// ServerContext body
122
Craig Tillercf133f42015-02-26 14:05:56 -0800123ServerContext::ServerContext()
124 : completion_op_(nullptr),
yang-gd45a26e2015-08-04 16:36:22 -0700125 has_notify_when_done_tag_(false),
yang-gb3352562015-08-04 14:42:06 -0700126 async_notify_when_done_tag_(nullptr),
yang-g5ff8de32016-01-22 16:55:10 -0800127 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
Craig Tillercf133f42015-02-26 14:05:56 -0800128 call_(nullptr),
129 cq_(nullptr),
130 sent_initial_metadata_(false) {}
Craig Tillerc6453062015-02-12 17:32:57 -0800131
Craig Tiller645466e2015-02-18 09:18:33 -0800132ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata* metadata,
Craig Tiller9dcb0f82015-02-11 15:36:31 -0800133 size_t metadata_count)
Craig Tillercf133f42015-02-26 14:05:56 -0800134 : completion_op_(nullptr),
yang-gd45a26e2015-08-04 16:36:22 -0700135 has_notify_when_done_tag_(false),
yang-gb3352562015-08-04 14:42:06 -0700136 async_notify_when_done_tag_(nullptr),
Nicolas Noble89219162015-04-07 18:01:18 -0700137 deadline_(deadline),
Craig Tillercf133f42015-02-26 14:05:56 -0800138 call_(nullptr),
139 cq_(nullptr),
140 sent_initial_metadata_(false) {
Craig Tiller854a30c2015-02-11 11:44:10 -0800141 for (size_t i = 0; i < metadata_count; i++) {
yang-ge21908f2015-08-25 13:47:51 -0700142 client_metadata_.insert(std::pair<grpc::string_ref, grpc::string_ref>(
143 metadata[i].key,
144 grpc::string_ref(metadata[i].value, metadata[i].value_length)));
Craig Tiller854a30c2015-02-11 11:44:10 -0800145 }
146}
147
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800148ServerContext::~ServerContext() {
149 if (call_) {
150 grpc_call_destroy(call_);
151 }
Craig Tiller492968f2015-02-18 13:14:03 -0800152 if (completion_op_) {
153 completion_op_->Unref();
154 }
155}
156
157void ServerContext::BeginCompletionOp(Call* call) {
158 GPR_ASSERT(!completion_op_);
159 completion_op_ = new CompletionOp();
yang-gd45a26e2015-08-04 16:36:22 -0700160 if (has_notify_when_done_tag_) {
161 completion_op_->set_tag(async_notify_when_done_tag_);
162 }
Craig Tiller492968f2015-02-18 13:14:03 -0800163 call->PerformOps(completion_op_);
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800164}
165
Yang Gao2b7f5372015-02-18 00:45:53 -0800166void ServerContext::AddInitialMetadata(const grpc::string& key,
Craig Tiller645466e2015-02-18 09:18:33 -0800167 const grpc::string& value) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800168 initial_metadata_.insert(std::make_pair(key, value));
169}
170
171void ServerContext::AddTrailingMetadata(const grpc::string& key,
Craig Tiller645466e2015-02-18 09:18:33 -0800172 const grpc::string& value) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800173 trailing_metadata_.insert(std::make_pair(key, value));
174}
175
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800176void ServerContext::TryCancel() const {
177 grpc_call_error err = grpc_call_cancel_with_status(
178 call_, GRPC_STATUS_CANCELLED, "Cancelled on the server side", NULL);
179 if (err != GRPC_CALL_OK) {
180 gpr_log(GPR_INFO, "TryCancel failed with: %d", err);
181 }
182}
183
David Garcia Quintas6dd49a52015-07-15 14:58:32 -0700184bool ServerContext::IsCancelled() const {
Craig Tiller492968f2015-02-18 13:14:03 -0800185 return completion_op_ && completion_op_->CheckCancelled(cq_);
Craig Tiller645466e2015-02-18 09:18:33 -0800186}
187
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700188void ServerContext::set_compression_level(grpc_compression_level level) {
189 const grpc_compression_algorithm algorithm_for_level =
190 grpc_compression_algorithm_for_level(level);
191 set_compression_algorithm(algorithm_for_level);
192}
193
194void ServerContext::set_compression_algorithm(
195 grpc_compression_algorithm algorithm) {
196 char* algorithm_name = NULL;
197 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
198 gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
199 algorithm);
200 abort();
201 }
202 GPR_ASSERT(algorithm_name != NULL);
203 AddInitialMetadata(GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, algorithm_name);
204}
205
yang-g85c04f92015-07-07 17:47:31 -0700206void ServerContext::set_call(grpc_call* call) {
207 call_ = call;
208 auth_context_ = CreateAuthContext(call);
yang-g3abe60b2015-07-06 14:00:36 -0700209}
210
yang-g94d62252015-07-14 14:25:37 -0700211std::shared_ptr<const AuthContext> ServerContext::auth_context() const {
212 if (auth_context_.get() == nullptr) {
213 auth_context_ = CreateAuthContext(call_);
214 }
215 return auth_context_;
216}
217
yang-gf1ec3772015-07-28 22:59:50 -0700218grpc::string ServerContext::peer() const {
219 grpc::string peer;
220 if (call_) {
221 char* c_peer = grpc_call_get_peer(call_);
222 peer = c_peer;
223 gpr_free(c_peer);
224 }
225 return peer;
226}
227
Alistair Veitch100a6e12015-07-29 15:25:28 -0700228const struct census_context* ServerContext::census_context() const {
Alistair Veitchcf39e942015-07-26 17:28:26 -0700229 return grpc_census_call_get_context(call_);
230}
231
Craig Tiller854a30c2015-02-11 11:44:10 -0800232} // namespace grpc