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 <grpcpp/client_context.h>
20
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>
26
27 #include <grpcpp/impl/grpc_library.h>
28 #include <grpcpp/security/credentials.h>
29 #include <grpcpp/server_context.h>
30 #include <grpcpp/support/time.h>
31
32 namespace grpc {
33
34 class DefaultGlobalClientCallbacks final
35 : public ClientContext::GlobalCallbacks {
36 public:
~DefaultGlobalClientCallbacks()37 ~DefaultGlobalClientCallbacks() override {}
DefaultConstructor(ClientContext * context)38 void DefaultConstructor(ClientContext* context) override {}
Destructor(ClientContext * context)39 void Destructor(ClientContext* context) override {}
40 };
41
42 static internal::GrpcLibraryInitializer g_gli_initializer;
43 static DefaultGlobalClientCallbacks g_default_client_callbacks;
44 static ClientContext::GlobalCallbacks* g_client_callbacks =
45 &g_default_client_callbacks;
46
ClientContext()47 ClientContext::ClientContext()
48 : initial_metadata_received_(false),
49 wait_for_ready_(false),
50 wait_for_ready_explicitly_set_(false),
51 idempotent_(false),
52 cacheable_(false),
53 call_(nullptr),
54 call_canceled_(false),
55 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
56 census_context_(nullptr),
57 propagate_from_call_(nullptr),
58 initial_metadata_corked_(false) {
59 g_client_callbacks->DefaultConstructor(this);
60 }
61
~ClientContext()62 ClientContext::~ClientContext() {
63 if (call_) {
64 grpc_call_unref(call_);
65 }
66 g_client_callbacks->Destructor(this);
67 }
68
FromServerContext(const ServerContext & context,PropagationOptions options)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;
74 return ctx;
75 }
76
AddMetadata(const grpc::string & meta_key,const grpc::string & meta_value)77 void ClientContext::AddMetadata(const grpc::string& meta_key,
78 const grpc::string& meta_value) {
79 send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
80 }
81
set_call(grpc_call * call,const std::shared_ptr<Channel> & channel)82 void ClientContext::set_call(grpc_call* call,
83 const std::shared_ptr<Channel>& channel) {
84 std::unique_lock<std::mutex> lock(mu_);
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,
90 "Failed to set credentials to rpc.", nullptr);
91 }
92 if (call_canceled_) {
93 grpc_call_cancel(call_, nullptr);
94 }
95 }
96
set_compression_algorithm(grpc_compression_algorithm algorithm)97 void ClientContext::set_compression_algorithm(
98 grpc_compression_algorithm algorithm) {
99 compression_algorithm_ = algorithm;
100 const char* algorithm_name = nullptr;
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 }
106 GPR_ASSERT(algorithm_name != nullptr);
107 AddMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name);
108 }
109
TryCancel()110 void ClientContext::TryCancel() {
111 std::unique_lock<std::mutex> lock(mu_);
112 if (call_) {
113 grpc_call_cancel(call_, nullptr);
114 } else {
115 call_canceled_ = true;
116 }
117 }
118
peer() const119 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
SetGlobalCallbacks(GlobalCallbacks * client_callbacks)129 void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) {
130 GPR_ASSERT(g_client_callbacks == &g_default_client_callbacks);
131 GPR_ASSERT(client_callbacks != nullptr);
132 GPR_ASSERT(client_callbacks != &g_default_client_callbacks);
133 g_client_callbacks = client_callbacks;
134 }
135
136 } // namespace grpc
137