| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 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 |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 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. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
#include <map> |
| 22 |
#include <memory> |
| 23 |
#include <string> |
| 24 |
#include <utility> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include <grpc/compression.h> |
| 28 |
#include <grpc/grpc.h> |
| 29 |
#include <grpc/impl/codegen/compression_types.h> |
| 30 |
#include <grpc/impl/codegen/gpr_types.h> |
| 31 |
#include <grpc/impl/codegen/grpc_types.h> |
| 32 |
#include <grpc/status.h> |
| 33 |
#include <grpc/support/alloc.h> |
| 34 |
#include <grpc/support/log.h> |
| 35 |
#include <grpc/support/time.h> |
| 36 |
#include <grpcpp/channel.h> |
| 37 |
#include <grpcpp/client_context.h> |
| 38 |
#include <grpcpp/impl/codegen/interceptor_common.h> |
| 39 |
#include <grpcpp/impl/codegen/sync.h> |
| 40 |
#include <grpcpp/impl/grpc_library.h> |
| 41 |
#include <grpcpp/security/credentials.h> |
| 42 |
#include <grpcpp/server_context.h> |
| 43 |
#include <grpcpp/support/client_interceptor.h> |
| 44 |
|
| 45 |
namespace grpc { |
| 46 |
|
| 47 |
class Channel; |
| 48 |
|
| 49 |
class DefaultGlobalClientCallbacks final |
| 50 |
: public ClientContext::GlobalCallbacks { |
| 51 |
public: |
| 52 |
~DefaultGlobalClientCallbacks() override {} |
| 53 |
void DefaultConstructor(ClientContext* /*context*/) override {} |
| 54 |
void Destructor(ClientContext* /*context*/) override {} |
| 55 |
}; |
| 56 |
|
| 57 |
static internal::GrpcLibraryInitializer g_gli_initializer; |
| 58 |
static DefaultGlobalClientCallbacks* g_default_client_callbacks = |
| 59 |
new DefaultGlobalClientCallbacks(); |
| 60 |
static ClientContext::GlobalCallbacks* g_client_callbacks = |
| 61 |
g_default_client_callbacks; |
| 62 |
|
| 63 |
ClientContext::ClientContext() |
| 64 |
: initial_metadata_received_(false), |
| 65 |
wait_for_ready_(false), |
| 66 |
wait_for_ready_explicitly_set_(false), |
| 67 |
call_(nullptr), |
| 68 |
call_canceled_(false), |
| 69 |
deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)), |
| 70 |
census_context_(nullptr), |
| 71 |
propagate_from_call_(nullptr), |
| 72 |
compression_algorithm_(GRPC_COMPRESS_NONE), |
| 73 |
initial_metadata_corked_(false) { |
| 74 |
g_gli_initializer.summon(); |
| 75 |
g_client_callbacks->DefaultConstructor(this); |
| 76 |
} |
| 77 |
|
| 78 |
ClientContext::~ClientContext() { |
| 79 |
if (call_) { |
| 80 |
grpc_call_unref(call_); |
| 81 |
call_ = nullptr; |
| 82 |
} |
| 83 |
g_client_callbacks->Destructor(this); |
| 84 |
} |
| 85 |
|
| 86 |
void ClientContext::set_credentials( |
| 87 |
const std::shared_ptr<CallCredentials>& creds) { |
| 88 |
creds_ = creds; |
| 89 |
// If call_ is set, we have already created the call, and set the call |
| 90 |
// credentials. This should only be done before we have started the batch |
| 91 |
// for sending initial metadata. |
| 92 |
if (creds_ != nullptr && call_ != nullptr) { |
| 93 |
if (!creds_->ApplyToCall(call_)) { |
| 94 |
SendCancelToInterceptors(); |
| 95 |
grpc_call_cancel_with_status(call_, GRPC_STATUS_CANCELLED, |
| 96 |
"Failed to set credentials to rpc.", |
| 97 |
nullptr); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
std::unique_ptr<ClientContext> ClientContext::FromInternalServerContext( |
| 103 |
const grpc::ServerContextBase& context, PropagationOptions options) { |
| 104 |
std::unique_ptr<ClientContext> ctx(new ClientContext); |
| 105 |
ctx->propagate_from_call_ = context.call_.call; |
| 106 |
ctx->propagation_options_ = options; |
| 107 |
return ctx; |
| 108 |
} |
| 109 |
|
| 110 |
std::unique_ptr<ClientContext> ClientContext::FromServerContext( |
| 111 |
const grpc::ServerContextBase& server_context, PropagationOptions options) { |
| 112 |
return FromInternalServerContext(server_context, options); |
| 113 |
} |
| 114 |
|
| 115 |
std::unique_ptr<ClientContext> ClientContext::FromCallbackServerContext( |
| 116 |
const grpc::CallbackServerContext& server_context, |
| 117 |
PropagationOptions options) { |
| 118 |
return FromInternalServerContext(server_context, options); |
| 119 |
} |
| 120 |
|
| 121 |
void ClientContext::AddMetadata(const std::string& meta_key, |
| 122 |
const std::string& meta_value) { |
| 123 |
send_initial_metadata_.insert(std::make_pair(meta_key, meta_value)); |
| 124 |
} |
| 125 |
|
| 126 |
void ClientContext::set_call(grpc_call* call, |
| 127 |
const std::shared_ptr<Channel>& channel) { |
| 128 |
internal::MutexLock lock(&mu_); |
| 129 |
GPR_ASSERT(call_ == nullptr); |
| 130 |
call_ = call; |
| 131 |
channel_ = channel; |
| 132 |
if (creds_ && !creds_->ApplyToCall(call_)) { |
| 133 |
// TODO(yashykt): should interceptors also see this status? |
| 134 |
SendCancelToInterceptors(); |
| 135 |
grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED, |
| 136 |
"Failed to set credentials to rpc.", nullptr); |
| 137 |
} |
| 138 |
if (call_canceled_) { |
| 139 |
SendCancelToInterceptors(); |
| 140 |
grpc_call_cancel(call_, nullptr); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void ClientContext::set_compression_algorithm( |
| 145 |
grpc_compression_algorithm algorithm) { |
| 146 |
compression_algorithm_ = algorithm; |
| 147 |
const char* algorithm_name = nullptr; |
| 148 |
if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) { |
| 149 |
gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.", |
| 150 |
algorithm); |
| 151 |
abort(); |
| 152 |
} |
| 153 |
GPR_ASSERT(algorithm_name != nullptr); |
| 154 |
AddMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name); |
| 155 |
} |
| 156 |
|
| 157 |
void ClientContext::TryCancel() { |
| 158 |
internal::MutexLock lock(&mu_); |
| 159 |
if (call_) { |
| 160 |
SendCancelToInterceptors(); |
| 161 |
grpc_call_cancel(call_, nullptr); |
| 162 |
} else { |
| 163 |
call_canceled_ = true; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
void ClientContext::SendCancelToInterceptors() { |
| 168 |
internal::CancelInterceptorBatchMethods cancel_methods; |
| 169 |
for (size_t i = 0; i < rpc_info_.interceptors_.size(); i++) { |
| 170 |
rpc_info_.RunInterceptor(&cancel_methods, i); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
std::string ClientContext::peer() const { |
| 175 |
std::string peer; |
| 176 |
if (call_) { |
| 177 |
char* c_peer = grpc_call_get_peer(call_); |
| 178 |
peer = c_peer; |
| 179 |
gpr_free(c_peer); |
| 180 |
} |
| 181 |
return peer; |
| 182 |
} |
| 183 |
|
| 184 |
void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) { |
| 185 |
GPR_ASSERT(g_client_callbacks == g_default_client_callbacks); |
| 186 |
GPR_ASSERT(client_callbacks != nullptr); |
| 187 |
GPR_ASSERT(client_callbacks != g_default_client_callbacks); |
| 188 |
g_client_callbacks = client_callbacks; |
| 189 |
} |
| 190 |
|
| 191 |
} // namespace grpc |