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