Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Quintas | b523c73 | 2016-01-25 18:22:28 -0800 | [diff] [blame] | 36 | #include <grpc/compression.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <grpc/grpc.h> |
yang-g | c07d9bf | 2015-07-28 09:19:22 -0700 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
David Garcia Quintas | c79b065 | 2016-07-27 21:11:58 -0700 | [diff] [blame] | 39 | #include <grpc/support/log.h> |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 40 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | |
David Garcia Quintas | 9e9f7b6 | 2016-05-16 19:12:12 -0700 | [diff] [blame] | 42 | #include <grpc++/security/credentials.h> |
| 43 | #include <grpc++/server_context.h> |
| 44 | #include <grpc++/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | |
| 46 | namespace grpc { |
| 47 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 48 | class DefaultGlobalClientCallbacks final |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 49 | : public ClientContext::GlobalCallbacks { |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 50 | public: |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 51 | ~DefaultGlobalClientCallbacks() override {} |
| 52 | void DefaultConstructor(ClientContext* context) override {} |
| 53 | void Destructor(ClientContext* context) override {} |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 56 | static DefaultGlobalClientCallbacks g_default_client_callbacks; |
| 57 | static ClientContext::GlobalCallbacks* g_client_callbacks = |
| 58 | &g_default_client_callbacks; |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 59 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | ClientContext::ClientContext() |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 61 | : initial_metadata_received_(false), |
Mark D. Roth | 9477724 | 2016-09-27 13:07:00 -0700 | [diff] [blame] | 62 | wait_for_ready_(false), |
Mark D. Roth | 59c9f90 | 2016-09-28 13:33:21 -0700 | [diff] [blame] | 63 | wait_for_ready_explicitly_set_(false), |
Craig Tiller | 399b3c4 | 2016-04-01 12:24:18 -0700 | [diff] [blame] | 64 | idempotent_(false), |
Makarand Dharmapurikar | 2b98b77 | 2016-09-07 14:24:15 -0700 | [diff] [blame] | 65 | cacheable_(false), |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 66 | call_(nullptr), |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 67 | call_canceled_(false), |
Craig Tiller | bb53614 | 2015-08-05 15:18:19 -0700 | [diff] [blame] | 68 | deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)), |
Craig Tiller | 05bec5c | 2016-06-09 23:01:24 -0700 | [diff] [blame] | 69 | census_context_(nullptr), |
Yuxuan Li | 34894e4 | 2017-02-08 17:57:24 -0800 | [diff] [blame] | 70 | propagate_from_call_(nullptr), |
| 71 | initial_metadata_corked_(false) { |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 72 | g_client_callbacks->DefaultConstructor(this); |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 73 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | |
| 75 | ClientContext::~ClientContext() { |
| 76 | if (call_) { |
Craig Tiller | dd36b15 | 2017-03-31 08:27:28 -0700 | [diff] [blame] | 77 | grpc_call_unref(call_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | } |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 79 | g_client_callbacks->Destructor(this); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 82 | std::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 Tiller | bb53614 | 2015-08-05 15:18:19 -0700 | [diff] [blame] | 87 | return ctx; |
| 88 | } |
| 89 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 90 | void ClientContext::AddMetadata(const grpc::string& meta_key, |
| 91 | const grpc::string& meta_value) { |
Yang Gao | 968ca53 | 2015-02-11 16:23:47 -0800 | [diff] [blame] | 92 | send_initial_metadata_.insert(std::make_pair(meta_key, meta_value)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 95 | void ClientContext::set_call(grpc_call* call, |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 96 | const std::shared_ptr<Channel>& channel) { |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 97 | std::unique_lock<std::mutex> lock(mu_); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 98 | 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" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 103 | "Failed to set credentials to rpc.", nullptr); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 104 | } |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 105 | if (call_canceled_) { |
| 106 | grpc_call_cancel(call_, nullptr); |
| 107 | } |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Craig Tiller | bf6abee | 2015-07-19 22:31:04 -0700 | [diff] [blame] | 110 | void ClientContext::set_compression_algorithm( |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 111 | grpc_compression_algorithm algorithm) { |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 112 | char* algorithm_name = nullptr; |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 113 | 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" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 118 | GPR_ASSERT(algorithm_name != nullptr); |
David Garcia Quintas | 9e9f7b6 | 2016-05-16 19:12:12 -0700 | [diff] [blame] | 119 | AddMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name); |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 122 | void ClientContext::TryCancel() { |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 123 | std::unique_lock<std::mutex> lock(mu_); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 124 | if (call_) { |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 125 | grpc_call_cancel(call_, nullptr); |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 126 | } else { |
| 127 | call_canceled_ = true; |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 130 | |
yang-g | c07d9bf | 2015-07-28 09:19:22 -0700 | [diff] [blame] | 131 | grpc::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 Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 141 | void 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 Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 148 | } // namespace grpc |