blob: 251623284089e4afa130deaa23e09a8d53db37fd [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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
34#include <grpc++/client_context.h>
35
David Garcia Quintasb523c732016-01-25 18:22:28 -080036#include <grpc/compression.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <grpc/grpc.h>
yang-gc07d9bf2015-07-28 09:19:22 -070038#include <grpc/support/alloc.h>
David Garcia Quintasc79b0652016-07-27 21:11:58 -070039#include <grpc/support/log.h>
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -070040#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041
David Garcia Quintas9e9f7b62016-05-16 19:12:12 -070042#include <grpc++/security/credentials.h>
43#include <grpc++/server_context.h>
44#include <grpc++/support/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045
46namespace grpc {
47
Vijay Paic0b2acb2016-11-01 16:31:56 -070048class DefaultGlobalClientCallbacks final
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080049 : public ClientContext::GlobalCallbacks {
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080050 public:
Vijay Paic0b2acb2016-11-01 16:31:56 -070051 ~DefaultGlobalClientCallbacks() override {}
52 void DefaultConstructor(ClientContext* context) override {}
53 void Destructor(ClientContext* context) override {}
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080054};
55
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080056static DefaultGlobalClientCallbacks g_default_client_callbacks;
57static ClientContext::GlobalCallbacks* g_client_callbacks =
58 &g_default_client_callbacks;
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080059
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060ClientContext::ClientContext()
Craig Tillercf133f42015-02-26 14:05:56 -080061 : initial_metadata_received_(false),
Mark D. Roth94777242016-09-27 13:07:00 -070062 wait_for_ready_(false),
Mark D. Roth59c9f902016-09-28 13:33:21 -070063 wait_for_ready_explicitly_set_(false),
Craig Tiller399b3c42016-04-01 12:24:18 -070064 idempotent_(false),
Makarand Dharmapurikar2b98b772016-09-07 14:24:15 -070065 cacheable_(false),
Craig Tillercf133f42015-02-26 14:05:56 -080066 call_(nullptr),
yang-ga89bf502015-11-17 14:19:17 -080067 call_canceled_(false),
Craig Tillerbb536142015-08-05 15:18:19 -070068 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
Craig Tiller05bec5c2016-06-09 23:01:24 -070069 census_context_(nullptr),
Yuxuan Li34894e42017-02-08 17:57:24 -080070 propagate_from_call_(nullptr),
71 initial_metadata_corked_(false) {
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080072 g_client_callbacks->DefaultConstructor(this);
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080073}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074
75ClientContext::~ClientContext() {
76 if (call_) {
Craig Tillerdd36b152017-03-31 08:27:28 -070077 grpc_call_unref(call_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 }
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080079 g_client_callbacks->Destructor(this);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080}
81
Craig Tiller2c3be1d2015-08-05 15:59:27 -070082std::unique_ptr<ClientContext> ClientContext::FromServerContext(
83 const ServerContext& context, PropagationOptions options) {
84 std::unique_ptr<ClientContext> ctx(new ClientContext);
85 ctx->propagate_from_call_ = context.call_;
86 ctx->propagation_options_ = options;
Craig Tillerbb536142015-08-05 15:18:19 -070087 return ctx;
88}
89
Yang Gao6baa9b62015-03-17 10:49:39 -070090void ClientContext::AddMetadata(const grpc::string& meta_key,
91 const grpc::string& meta_value) {
Yang Gao968ca532015-02-11 16:23:47 -080092 send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093}
94
Yang Gaoa8938922015-05-14 11:51:07 -070095void ClientContext::set_call(grpc_call* call,
yang-g8c2be9f2015-08-19 16:28:09 -070096 const std::shared_ptr<Channel>& channel) {
Vijay Pai320ed132016-11-01 17:16:55 -070097 std::unique_lock<std::mutex> lock(mu_);
Yang Gaoa8938922015-05-14 11:51:07 -070098 GPR_ASSERT(call_ == nullptr);
99 call_ = call;
100 channel_ = channel;
101 if (creds_ && !creds_->ApplyToCall(call_)) {
102 grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED,
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200103 "Failed to set credentials to rpc.", nullptr);
Yang Gaoa8938922015-05-14 11:51:07 -0700104 }
yang-ga89bf502015-11-17 14:19:17 -0800105 if (call_canceled_) {
106 grpc_call_cancel(call_, nullptr);
107 }
Yang Gaoa8938922015-05-14 11:51:07 -0700108}
109
Craig Tillerbf6abee2015-07-19 22:31:04 -0700110void ClientContext::set_compression_algorithm(
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700111 grpc_compression_algorithm algorithm) {
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200112 char* algorithm_name = nullptr;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700113 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
114 gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
115 algorithm);
116 abort();
117 }
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200118 GPR_ASSERT(algorithm_name != nullptr);
David Garcia Quintas9e9f7b62016-05-16 19:12:12 -0700119 AddMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name);
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700120}
121
Yang Gao406b32f2015-02-13 16:25:33 -0800122void ClientContext::TryCancel() {
Vijay Pai320ed132016-11-01 17:16:55 -0700123 std::unique_lock<std::mutex> lock(mu_);
Yang Gao406b32f2015-02-13 16:25:33 -0800124 if (call_) {
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200125 grpc_call_cancel(call_, nullptr);
yang-ga89bf502015-11-17 14:19:17 -0800126 } else {
127 call_canceled_ = true;
Yang Gao406b32f2015-02-13 16:25:33 -0800128 }
129}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130
yang-gc07d9bf2015-07-28 09:19:22 -0700131grpc::string ClientContext::peer() const {
132 grpc::string peer;
133 if (call_) {
134 char* c_peer = grpc_call_get_peer(call_);
135 peer = c_peer;
136 gpr_free(c_peer);
137 }
138 return peer;
139}
140
Bogdan Drutu6af31bd2015-12-12 15:39:40 -0800141void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) {
142 GPR_ASSERT(g_client_callbacks == &g_default_client_callbacks);
143 GPR_ASSERT(client_callbacks != NULL);
144 GPR_ASSERT(client_callbacks != &g_default_client_callbacks);
145 g_client_callbacks = client_callbacks;
Bogdan Drutu38d2ad62015-12-11 19:26:52 -0800146}
147
Craig Tiller190d3602015-02-18 09:23:38 -0800148} // namespace grpc