blob: dafa8081e993288231a8da59939b5c6f16bfd719 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
19#include <grpc++/client_context.h>
20
David Garcia Quintasb523c732016-01-25 18:22:28 -080021#include <grpc/compression.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080022#include <grpc/grpc.h>
yang-gc07d9bf2015-07-28 09:19:22 -070023#include <grpc/support/alloc.h>
David Garcia Quintasc79b0652016-07-27 21:11:58 -070024#include <grpc/support/log.h>
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -070025#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080026
Garret Kelly9663d032017-06-07 16:25:36 -040027#include <grpc++/impl/grpc_library.h>
David Garcia Quintas9e9f7b62016-05-16 19:12:12 -070028#include <grpc++/security/credentials.h>
29#include <grpc++/server_context.h>
30#include <grpc++/support/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080031
32namespace grpc {
33
Vijay Paic0b2acb2016-11-01 16:31:56 -070034class DefaultGlobalClientCallbacks final
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080035 : public ClientContext::GlobalCallbacks {
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080036 public:
Vijay Paic0b2acb2016-11-01 16:31:56 -070037 ~DefaultGlobalClientCallbacks() override {}
38 void DefaultConstructor(ClientContext* context) override {}
39 void Destructor(ClientContext* context) override {}
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080040};
41
Garret Kelly9663d032017-06-07 16:25:36 -040042static internal::GrpcLibraryInitializer g_gli_initializer;
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080043static DefaultGlobalClientCallbacks g_default_client_callbacks;
44static ClientContext::GlobalCallbacks* g_client_callbacks =
45 &g_default_client_callbacks;
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080046
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047ClientContext::ClientContext()
Craig Tillercf133f42015-02-26 14:05:56 -080048 : initial_metadata_received_(false),
Mark D. Roth94777242016-09-27 13:07:00 -070049 wait_for_ready_(false),
Mark D. Roth59c9f902016-09-28 13:33:21 -070050 wait_for_ready_explicitly_set_(false),
Craig Tiller399b3c42016-04-01 12:24:18 -070051 idempotent_(false),
Makarand Dharmapurikar2b98b772016-09-07 14:24:15 -070052 cacheable_(false),
Craig Tillercf133f42015-02-26 14:05:56 -080053 call_(nullptr),
yang-ga89bf502015-11-17 14:19:17 -080054 call_canceled_(false),
Craig Tillerbb536142015-08-05 15:18:19 -070055 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
Craig Tiller05bec5c2016-06-09 23:01:24 -070056 census_context_(nullptr),
Yuxuan Li34894e42017-02-08 17:57:24 -080057 propagate_from_call_(nullptr),
58 initial_metadata_corked_(false) {
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080059 g_client_callbacks->DefaultConstructor(this);
Bogdan Drutu38d2ad62015-12-11 19:26:52 -080060}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061
62ClientContext::~ClientContext() {
63 if (call_) {
Craig Tillerdd36b152017-03-31 08:27:28 -070064 grpc_call_unref(call_);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065 }
Bogdan Drutu6af31bd2015-12-12 15:39:40 -080066 g_client_callbacks->Destructor(this);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067}
68
Craig Tiller2c3be1d2015-08-05 15:59:27 -070069std::unique_ptr<ClientContext> ClientContext::FromServerContext(
70 const ServerContext& context, PropagationOptions options) {
71 std::unique_ptr<ClientContext> ctx(new ClientContext);
72 ctx->propagate_from_call_ = context.call_;
73 ctx->propagation_options_ = options;
Craig Tillerbb536142015-08-05 15:18:19 -070074 return ctx;
75}
76
Yang Gao6baa9b62015-03-17 10:49:39 -070077void ClientContext::AddMetadata(const grpc::string& meta_key,
78 const grpc::string& meta_value) {
Yang Gao968ca532015-02-11 16:23:47 -080079 send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080}
81
Yang Gaoa8938922015-05-14 11:51:07 -070082void ClientContext::set_call(grpc_call* call,
yang-g8c2be9f2015-08-19 16:28:09 -070083 const std::shared_ptr<Channel>& channel) {
Vijay Pai320ed132016-11-01 17:16:55 -070084 std::unique_lock<std::mutex> lock(mu_);
Yang Gaoa8938922015-05-14 11:51:07 -070085 GPR_ASSERT(call_ == nullptr);
86 call_ = call;
87 channel_ = channel;
88 if (creds_ && !creds_->ApplyToCall(call_)) {
89 grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED,
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +020090 "Failed to set credentials to rpc.", nullptr);
Yang Gaoa8938922015-05-14 11:51:07 -070091 }
yang-ga89bf502015-11-17 14:19:17 -080092 if (call_canceled_) {
93 grpc_call_cancel(call_, nullptr);
94 }
Yang Gaoa8938922015-05-14 11:51:07 -070095}
96
Craig Tillerbf6abee2015-07-19 22:31:04 -070097void ClientContext::set_compression_algorithm(
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -070098 grpc_compression_algorithm algorithm) {
Yash Tibrewal9eb86722017-09-17 23:43:30 -070099 const char* algorithm_name = nullptr;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700100 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
101 gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
102 algorithm);
103 abort();
104 }
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200105 GPR_ASSERT(algorithm_name != nullptr);
David Garcia Quintas9e9f7b62016-05-16 19:12:12 -0700106 AddMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name);
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700107}
108
Yang Gao406b32f2015-02-13 16:25:33 -0800109void ClientContext::TryCancel() {
Vijay Pai320ed132016-11-01 17:16:55 -0700110 std::unique_lock<std::mutex> lock(mu_);
Yang Gao406b32f2015-02-13 16:25:33 -0800111 if (call_) {
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200112 grpc_call_cancel(call_, nullptr);
yang-ga89bf502015-11-17 14:19:17 -0800113 } else {
114 call_canceled_ = true;
Yang Gao406b32f2015-02-13 16:25:33 -0800115 }
116}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117
yang-gc07d9bf2015-07-28 09:19:22 -0700118grpc::string ClientContext::peer() const {
119 grpc::string peer;
120 if (call_) {
121 char* c_peer = grpc_call_get_peer(call_);
122 peer = c_peer;
123 gpr_free(c_peer);
124 }
125 return peer;
126}
127
Bogdan Drutu6af31bd2015-12-12 15:39:40 -0800128void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) {
129 GPR_ASSERT(g_client_callbacks == &g_default_client_callbacks);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800130 GPR_ASSERT(client_callbacks != nullptr);
Bogdan Drutu6af31bd2015-12-12 15:39:40 -0800131 GPR_ASSERT(client_callbacks != &g_default_client_callbacks);
132 g_client_callbacks = client_callbacks;
Bogdan Drutu38d2ad62015-12-11 19:26:52 -0800133}
134
Craig Tiller190d3602015-02-18 09:23:38 -0800135} // namespace grpc