Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <grpc++/client_context.h> |
| 20 | |
David Garcia Quintas | b523c73 | 2016-01-25 18:22:28 -0800 | [diff] [blame] | 21 | #include <grpc/compression.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 22 | #include <grpc/grpc.h> |
yang-g | c07d9bf | 2015-07-28 09:19:22 -0700 | [diff] [blame] | 23 | #include <grpc/support/alloc.h> |
David Garcia Quintas | c79b065 | 2016-07-27 21:11:58 -0700 | [diff] [blame] | 24 | #include <grpc/support/log.h> |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 25 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 26 | |
Garret Kelly | 9663d03 | 2017-06-07 16:25:36 -0400 | [diff] [blame] | 27 | #include <grpc++/impl/grpc_library.h> |
David Garcia Quintas | 9e9f7b6 | 2016-05-16 19:12:12 -0700 | [diff] [blame] | 28 | #include <grpc++/security/credentials.h> |
| 29 | #include <grpc++/server_context.h> |
| 30 | #include <grpc++/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 31 | |
| 32 | namespace grpc { |
| 33 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 34 | class DefaultGlobalClientCallbacks final |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 35 | : public ClientContext::GlobalCallbacks { |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 36 | public: |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 37 | ~DefaultGlobalClientCallbacks() override {} |
| 38 | void DefaultConstructor(ClientContext* context) override {} |
| 39 | void Destructor(ClientContext* context) override {} |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
Garret Kelly | 9663d03 | 2017-06-07 16:25:36 -0400 | [diff] [blame] | 42 | static internal::GrpcLibraryInitializer g_gli_initializer; |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 43 | static DefaultGlobalClientCallbacks g_default_client_callbacks; |
| 44 | static ClientContext::GlobalCallbacks* g_client_callbacks = |
| 45 | &g_default_client_callbacks; |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 46 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | ClientContext::ClientContext() |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 48 | : initial_metadata_received_(false), |
Mark D. Roth | 9477724 | 2016-09-27 13:07:00 -0700 | [diff] [blame] | 49 | wait_for_ready_(false), |
Mark D. Roth | 59c9f90 | 2016-09-28 13:33:21 -0700 | [diff] [blame] | 50 | wait_for_ready_explicitly_set_(false), |
Craig Tiller | 399b3c4 | 2016-04-01 12:24:18 -0700 | [diff] [blame] | 51 | idempotent_(false), |
Makarand Dharmapurikar | 2b98b77 | 2016-09-07 14:24:15 -0700 | [diff] [blame] | 52 | cacheable_(false), |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 53 | call_(nullptr), |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 54 | call_canceled_(false), |
Craig Tiller | bb53614 | 2015-08-05 15:18:19 -0700 | [diff] [blame] | 55 | deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)), |
Craig Tiller | 05bec5c | 2016-06-09 23:01:24 -0700 | [diff] [blame] | 56 | census_context_(nullptr), |
Yuxuan Li | 34894e4 | 2017-02-08 17:57:24 -0800 | [diff] [blame] | 57 | propagate_from_call_(nullptr), |
| 58 | initial_metadata_corked_(false) { |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 59 | g_client_callbacks->DefaultConstructor(this); |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 60 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | |
| 62 | ClientContext::~ClientContext() { |
| 63 | if (call_) { |
Craig Tiller | dd36b15 | 2017-03-31 08:27:28 -0700 | [diff] [blame] | 64 | grpc_call_unref(call_); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | } |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 66 | g_client_callbacks->Destructor(this); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Craig Tiller | 2c3be1d | 2015-08-05 15:59:27 -0700 | [diff] [blame] | 69 | std::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 Tiller | bb53614 | 2015-08-05 15:18:19 -0700 | [diff] [blame] | 74 | return ctx; |
| 75 | } |
| 76 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 77 | void ClientContext::AddMetadata(const grpc::string& meta_key, |
| 78 | const grpc::string& meta_value) { |
Yang Gao | 968ca53 | 2015-02-11 16:23:47 -0800 | [diff] [blame] | 79 | send_initial_metadata_.insert(std::make_pair(meta_key, meta_value)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 82 | void ClientContext::set_call(grpc_call* call, |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 83 | const std::shared_ptr<Channel>& channel) { |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 84 | std::unique_lock<std::mutex> lock(mu_); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 85 | 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" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 90 | "Failed to set credentials to rpc.", nullptr); |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 91 | } |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 92 | if (call_canceled_) { |
| 93 | grpc_call_cancel(call_, nullptr); |
| 94 | } |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Craig Tiller | bf6abee | 2015-07-19 22:31:04 -0700 | [diff] [blame] | 97 | void ClientContext::set_compression_algorithm( |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 98 | grpc_compression_algorithm algorithm) { |
David Garcia Quintas | a21ab2b | 2018-02-01 16:24:00 -0800 | [diff] [blame] | 99 | compression_algorithm_ = algorithm; |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 100 | const char* algorithm_name = nullptr; |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 101 | if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) { |
| 102 | gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.", |
| 103 | algorithm); |
| 104 | abort(); |
| 105 | } |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 106 | GPR_ASSERT(algorithm_name != nullptr); |
David Garcia Quintas | 9e9f7b6 | 2016-05-16 19:12:12 -0700 | [diff] [blame] | 107 | AddMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name); |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 110 | void ClientContext::TryCancel() { |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 111 | std::unique_lock<std::mutex> lock(mu_); |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 112 | if (call_) { |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 113 | grpc_call_cancel(call_, nullptr); |
yang-g | a89bf50 | 2015-11-17 14:19:17 -0800 | [diff] [blame] | 114 | } else { |
| 115 | call_canceled_ = true; |
Yang Gao | 406b32f | 2015-02-13 16:25:33 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | |
yang-g | c07d9bf | 2015-07-28 09:19:22 -0700 | [diff] [blame] | 119 | grpc::string ClientContext::peer() const { |
| 120 | grpc::string peer; |
| 121 | if (call_) { |
| 122 | char* c_peer = grpc_call_get_peer(call_); |
| 123 | peer = c_peer; |
| 124 | gpr_free(c_peer); |
| 125 | } |
| 126 | return peer; |
| 127 | } |
| 128 | |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 129 | void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) { |
| 130 | GPR_ASSERT(g_client_callbacks == &g_default_client_callbacks); |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 131 | GPR_ASSERT(client_callbacks != nullptr); |
Bogdan Drutu | 6af31bd | 2015-12-12 15:39:40 -0800 | [diff] [blame] | 132 | GPR_ASSERT(client_callbacks != &g_default_client_callbacks); |
| 133 | g_client_callbacks = client_callbacks; |
Bogdan Drutu | 38d2ad6 | 2015-12-11 19:26:52 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 136 | } // namespace grpc |