client_context.cc
Go to the documentation of this file.
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>
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>
42 #include <grpcpp/server_context.h>
44 #include <grpcpp/support/config.h>
45 
46 namespace grpc {
47 
48 class Channel;
49 
52  public:
54  void DefaultConstructor(ClientContext* /*context*/) override {}
55  void Destructor(ClientContext* /*context*/) override {}
56 };
57 
63 
65  : initial_metadata_received_(false),
66  wait_for_ready_(false),
67  wait_for_ready_explicitly_set_(false),
68  call_(nullptr),
69  call_canceled_(false),
71  census_context_(nullptr),
72  propagate_from_call_(nullptr),
73  compression_algorithm_(GRPC_COMPRESS_NONE),
74  initial_metadata_corked_(false) {
77 }
78 
80  if (call_) {
82  }
84 }
85 
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_)) {
96  "Failed to set credentials to rpc.",
97  nullptr);
98  }
99  }
100 }
101 
102 std::unique_ptr<ClientContext> ClientContext::FromInternalServerContext(
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,
118  return FromInternalServerContext(server_context, options);
119 }
120 
122  const std::string& meta_value) {
123  send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
124 }
125 
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?
136  "Failed to set credentials to rpc.", nullptr);
137  }
138  if (call_canceled_) {
140  grpc_call_cancel(call_, nullptr);
141  }
142 }
143 
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);
155 }
156 
158  internal::MutexLock lock(&mu_);
159  if (call_) {
161  grpc_call_cancel(call_, nullptr);
162  } else {
163  call_canceled_ = true;
164  }
165 }
166 
169  for (size_t i = 0; i < rpc_info_.interceptors_.size(); i++) {
170  rpc_info_.RunInterceptor(&cancel_methods, i);
171  }
172 }
173 
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 
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
grpc::ClientContext::send_initial_metadata_
std::multimap< std::string, std::string > send_initial_metadata_
Definition: grpcpp/impl/codegen/client_context.h:502
grpc::ClientContext::peer
std::string peer() const
Definition: client_context.cc:174
compression.h
grpc_call_cancel
GRPCAPI grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved)
Definition: call.cc:1782
grpc::ClientContext::set_call
void set_call(grpc_call *call, const std::shared_ptr< grpc::Channel > &channel)
Definition: client_context.cc:126
log.h
grpc::ClientContext::compression_algorithm_
grpc_compression_algorithm compression_algorithm_
Definition: grpcpp/impl/codegen/client_context.h:509
grpc::ClientContext::call_canceled_
bool call_canceled_
Definition: grpcpp/impl/codegen/client_context.h:496
ctx
Definition: benchmark-async.c:30
call_
grpc_call * call_
Definition: rls.cc:669
grpc::gpr_free
gpr_free(creds_file_name)
deadline_
Timestamp deadline_
Definition: channel_connectivity.cc:163
grpc
Definition: grpcpp/alarm.h:33
interceptor_common.h
grpc::PropagationOptions
Definition: grpcpp/impl/codegen/client_context.h:129
false
#define false
Definition: setup_once.h:323
grpc::ClientContext::call
grpc_call * call() const
Definition: grpcpp/impl/codegen/client_context.h:459
grpc::internal::GrpcLibraryInitializer::summon
int summon()
Definition: grpcpp/impl/grpc_library.h:54
grpc_call_get_peer
GRPCAPI char * grpc_call_get_peer(grpc_call *call)
Definition: call.cc:1774
grpc::g_gli_initializer
static grpc::internal::GrpcLibraryInitializer g_gli_initializer
Definition: channel_cc.cc:52
options
double_dict options[]
Definition: capstone_test.c:55
grpc_compression_algorithm
grpc_compression_algorithm
Definition: compression_types.h:60
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::experimental::ClientRpcInfo::interceptors_
std::vector< std::unique_ptr< experimental::Interceptor > > interceptors_
Definition: impl/codegen/client_interceptor.h:175
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
grpc::ClientContext::ClientContext
ClientContext()
Definition: client_context.cc:64
GRPC_STATUS_CANCELLED
@ GRPC_STATUS_CANCELLED
Definition: include/grpc/impl/codegen/status.h:33
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
time.h
grpc::internal::CancelInterceptorBatchMethods
Definition: interceptor_common.h:411
GRPC_COMPRESS_NONE
@ GRPC_COMPRESS_NONE
Definition: compression_types.h:61
grpc::ClientContext::AddMetadata
void AddMetadata(const std::string &meta_key, const std::string &meta_value)
Definition: client_context.cc:121
grpc::internal::MutexLock
Definition: include/grpcpp/impl/codegen/sync.h:86
call
FilterStackCall * call
Definition: call.cc:750
status.h
grpc_types.h
framework.rpc.grpc_channelz.Channel
Channel
Definition: grpc_channelz.py:32
client_interceptor.h
grpc::internal::GrpcLibraryInitializer
Instantiating this class ensures the proper initialization of gRPC.
Definition: grpcpp/impl/grpc_library.h:39
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
sync.h
grpc::ClientContext::~ClientContext
~ClientContext()
Definition: client_context.cc:79
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc::DefaultGlobalClientCallbacks
Definition: client_context.cc:50
grpc::ServerContextBase
Base class of ServerContext.
Definition: grpcpp/impl/codegen/server_context.h:126
grpc_compression_algorithm_name
GRPCAPI int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm, const char **name)
Definition: compression.cc:56
grpc_call_unref
GRPCAPI void grpc_call_unref(grpc_call *call)
Definition: call.cc:1770
grpc::ClientContext::creds_
std::shared_ptr< grpc::CallCredentials > creds_
Definition: grpcpp/impl/codegen/client_context.h:499
grpc::DefaultGlobalClientCallbacks::Destructor
void Destructor(ClientContext *) override
Definition: client_context.cc:55
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc::ClientContext::mu_
grpc::internal::Mutex mu_
Definition: grpcpp/impl/codegen/client_context.h:494
grpc.h
grpc_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
grpc::ClientContext::FromServerContext
static std::unique_ptr< ClientContext > FromServerContext(const grpc::ServerContextBase &server_context, PropagationOptions options=PropagationOptions())
Definition: client_context.cc:110
channel.h
grpc::ClientContext::SendCancelToInterceptors
void SendCancelToInterceptors()
Definition: client_context.cc:167
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
config.h
grpc::experimental::ClientRpcInfo::RunInterceptor
void RunInterceptor(experimental::InterceptorBatchMethods *interceptor_methods, size_t pos)
Definition: impl/codegen/client_interceptor.h:138
grpc::ClientContext::GlobalCallbacks::Destructor
virtual void Destructor(ClientContext *context)=0
grpc::CallbackServerContext
Definition: grpcpp/impl/codegen/server_context.h:606
grpc::g_default_client_callbacks
static DefaultGlobalClientCallbacks * g_default_client_callbacks
Definition: client_context.cc:59
grpc::g_client_callbacks
static ClientContext::GlobalCallbacks * g_client_callbacks
Definition: client_context.cc:61
gpr_types.h
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
grpc::ClientContext::rpc_info_
grpc::experimental::ClientRpcInfo rpc_info_
Definition: grpcpp/impl/codegen/client_context.h:514
compression_types.h
grpc::ClientContext::GlobalCallbacks
Definition: grpcpp/impl/codegen/client_context.h:397
grpc::ClientContext::FromInternalServerContext
static std::unique_ptr< ClientContext > FromInternalServerContext(const grpc::ServerContextBase &server_context, PropagationOptions options)
Definition: client_context.cc:102
client_context.h
credentials.h
GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY
#define GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY
Definition: compression_types.h:33
grpc_library.h
grpc::ClientContext::call_
grpc_call * call_
Definition: grpcpp/impl/codegen/client_context.h:495
grpc::ClientContext::GlobalCallbacks::DefaultConstructor
virtual void DefaultConstructor(ClientContext *context)=0
alloc.h
grpc::ClientContext::TryCancel
void TryCancel()
Definition: client_context.cc:157
server_context.h
grpc::ClientContext::FromCallbackServerContext
static std::unique_ptr< ClientContext > FromCallbackServerContext(const grpc::CallbackServerContext &server_context, PropagationOptions options=PropagationOptions())
Definition: client_context.cc:115
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
grpc::ClientContext::set_credentials
void set_credentials(const std::shared_ptr< grpc::CallCredentials > &creds)
Definition: client_context.cc:86
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
grpc::ClientContext::SetGlobalCallbacks
static void SetGlobalCallbacks(GlobalCallbacks *callbacks)
Definition: client_context.cc:184
grpc::DefaultGlobalClientCallbacks::~DefaultGlobalClientCallbacks
~DefaultGlobalClientCallbacks() override
Definition: client_context.cc:53
grpc::ClientContext::set_compression_algorithm
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Definition: client_context.cc:144
grpc_call_cancel_with_status
GRPCAPI grpc_call_error grpc_call_cancel_with_status(grpc_call *call, grpc_status_code status, const char *description, void *reserved)
Definition: call.cc:1791
grpc::ClientContext::channel_
std::shared_ptr< grpc::Channel > channel_
Definition: grpcpp/impl/codegen/client_context.h:493
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc::DefaultGlobalClientCallbacks::DefaultConstructor
void DefaultConstructor(ClientContext *) override
Definition: client_context.cc:54


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:47