Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include <grpc++/client_context.h>
35 :
36 : #include <grpc/grpc.h>
37 : #include <grpc/support/alloc.h>
38 : #include <grpc/support/string_util.h>
39 : #include <grpc++/security/credentials.h>
40 : #include <grpc++/server_context.h>
41 : #include <grpc++/support/time.h>
42 :
43 : #include "src/core/channel/compress_filter.h"
44 : #include "src/cpp/common/create_auth_context.h"
45 :
46 : namespace grpc {
47 :
48 1645646 : ClientContext::ClientContext()
49 : : initial_metadata_received_(false),
50 : call_(nullptr),
51 : call_canceled_(false),
52 : deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
53 1645646 : propagate_from_call_(nullptr) {}
54 :
55 3289980 : ClientContext::~ClientContext() {
56 1645013 : if (call_) {
57 1646787 : grpc_call_destroy(call_);
58 : }
59 1646808 : }
60 :
61 216 : std::unique_ptr<ClientContext> ClientContext::FromServerContext(
62 : const ServerContext& context, PropagationOptions options) {
63 216 : std::unique_ptr<ClientContext> ctx(new ClientContext);
64 216 : ctx->propagate_from_call_ = context.call_;
65 216 : ctx->propagation_options_ = options;
66 216 : return ctx;
67 : }
68 :
69 417 : void ClientContext::AddMetadata(const grpc::string& meta_key,
70 : const grpc::string& meta_value) {
71 417 : send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
72 417 : }
73 :
74 1646419 : void ClientContext::set_call(grpc_call* call,
75 : const std::shared_ptr<Channel>& channel) {
76 1646419 : grpc::unique_lock<grpc::mutex> lock(mu_);
77 1646772 : GPR_ASSERT(call_ == nullptr);
78 1646772 : call_ = call;
79 1646772 : channel_ = channel;
80 1646813 : if (creds_ && !creds_->ApplyToCall(call_)) {
81 : grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED,
82 0 : "Failed to set credentials to rpc.", nullptr);
83 : }
84 1646479 : if (call_canceled_) {
85 2 : grpc_call_cancel(call_, nullptr);
86 1646479 : }
87 1646762 : }
88 :
89 405 : void ClientContext::set_compression_algorithm(
90 : grpc_compression_algorithm algorithm) {
91 405 : char* algorithm_name = nullptr;
92 405 : if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
93 : gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
94 0 : algorithm);
95 0 : abort();
96 : }
97 405 : GPR_ASSERT(algorithm_name != nullptr);
98 405 : AddMetadata(GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, algorithm_name);
99 405 : }
100 :
101 1 : std::shared_ptr<const AuthContext> ClientContext::auth_context() const {
102 1 : if (auth_context_.get() == nullptr) {
103 1 : auth_context_ = CreateAuthContext(call_);
104 : }
105 1 : return auth_context_;
106 : }
107 :
108 14 : void ClientContext::TryCancel() {
109 14 : grpc::unique_lock<grpc::mutex> lock(mu_);
110 14 : if (call_) {
111 12 : grpc_call_cancel(call_, nullptr);
112 : } else {
113 2 : call_canceled_ = true;
114 14 : }
115 14 : }
116 :
117 4 : grpc::string ClientContext::peer() const {
118 4 : grpc::string peer;
119 4 : if (call_) {
120 4 : char* c_peer = grpc_call_get_peer(call_);
121 4 : peer = c_peer;
122 4 : gpr_free(c_peer);
123 : }
124 4 : return peer;
125 : }
126 :
127 : } // namespace grpc
|