3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpcpp/client_context.h>
21 #include <grpc/compression.h>
22 #include <grpc/grpc.h>
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/string_util.h>
27 #include <grpcpp/impl/codegen/interceptor_common.h>
28 #include <grpcpp/impl/codegen/sync.h>
29 #include <grpcpp/impl/grpc_library.h>
30 #include <grpcpp/security/credentials.h>
31 #include <grpcpp/server_context.h>
32 #include <grpcpp/support/time.h>
38 class DefaultGlobalClientCallbacks final
39 : public ClientContext::GlobalCallbacks {
41 ~DefaultGlobalClientCallbacks() override {}
42 void DefaultConstructor(ClientContext* /*context*/) override {}
43 void Destructor(ClientContext* /*context*/) override {}
46 static internal::GrpcLibraryInitializer g_gli_initializer;
47 static DefaultGlobalClientCallbacks* g_default_client_callbacks =
48 new DefaultGlobalClientCallbacks();
49 static ClientContext::GlobalCallbacks* g_client_callbacks =
50 g_default_client_callbacks;
52 ClientContext::ClientContext()
53 : initial_metadata_received_(false),
54 wait_for_ready_(false),
55 wait_for_ready_explicitly_set_(false),
59 call_canceled_(false),
60 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
61 census_context_(nullptr),
62 propagate_from_call_(nullptr),
63 compression_algorithm_(GRPC_COMPRESS_NONE),
64 initial_metadata_corked_(false) {
65 g_gli_initializer.summon();
66 g_client_callbacks->DefaultConstructor(this);
69 ClientContext::~ClientContext() {
71 grpc_call_unref(call_);
73 g_client_callbacks->Destructor(this);
76 void ClientContext::set_credentials(
77 const std::shared_ptr<CallCredentials>& creds) {
79 // If call_ is set, we have already created the call, and set the call
80 // credentials. This should only be done before we have started the batch
81 // for sending initial metadata.
82 if (creds_ != nullptr && call_ != nullptr) {
83 if (!creds_->ApplyToCall(call_)) {
84 SendCancelToInterceptors();
85 grpc_call_cancel_with_status(call_, GRPC_STATUS_CANCELLED,
86 "Failed to set credentials to rpc.",
92 std::unique_ptr<ClientContext> ClientContext::FromInternalServerContext(
93 const grpc::ServerContextBase& context, PropagationOptions options) {
94 std::unique_ptr<ClientContext> ctx(new ClientContext);
95 ctx->propagate_from_call_ = context.call_.call;
96 ctx->propagation_options_ = options;
100 std::unique_ptr<ClientContext> ClientContext::FromServerContext(
101 const grpc::ServerContextBase& server_context, PropagationOptions options) {
102 return FromInternalServerContext(server_context, options);
105 std::unique_ptr<ClientContext> ClientContext::FromCallbackServerContext(
106 const grpc::CallbackServerContext& server_context,
107 PropagationOptions options) {
108 return FromInternalServerContext(server_context, options);
111 void ClientContext::AddMetadata(const std::string& meta_key,
112 const std::string& meta_value) {
113 send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
116 void ClientContext::set_call(grpc_call* call,
117 const std::shared_ptr<Channel>& channel) {
118 internal::MutexLock lock(&mu_);
119 GPR_ASSERT(call_ == nullptr);
122 if (creds_ && !creds_->ApplyToCall(call_)) {
123 // TODO(yashykt): should interceptors also see this status?
124 SendCancelToInterceptors();
125 grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED,
126 "Failed to set credentials to rpc.", nullptr);
128 if (call_canceled_) {
129 SendCancelToInterceptors();
130 grpc_call_cancel(call_, nullptr);
134 void ClientContext::set_compression_algorithm(
135 grpc_compression_algorithm algorithm) {
136 compression_algorithm_ = algorithm;
137 const char* algorithm_name = nullptr;
138 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
139 gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
143 GPR_ASSERT(algorithm_name != nullptr);
144 AddMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name);
147 void ClientContext::TryCancel() {
148 internal::MutexLock lock(&mu_);
150 SendCancelToInterceptors();
151 grpc_call_cancel(call_, nullptr);
153 call_canceled_ = true;
157 void ClientContext::SendCancelToInterceptors() {
158 internal::CancelInterceptorBatchMethods cancel_methods;
159 for (size_t i = 0; i < rpc_info_.interceptors_.size(); i++) {
160 rpc_info_.RunInterceptor(&cancel_methods, i);
164 std::string ClientContext::peer() const {
167 char* c_peer = grpc_call_get_peer(call_);
174 void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) {
175 GPR_ASSERT(g_client_callbacks == g_default_client_callbacks);
176 GPR_ASSERT(client_callbacks != nullptr);
177 GPR_ASSERT(client_callbacks != g_default_client_callbacks);
178 g_client_callbacks = client_callbacks;