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 1298835 : ClientContext::ClientContext()
49 : : initial_metadata_received_(false),
50 : call_(nullptr),
51 : deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
52 1298835 : propagate_from_call_(nullptr) {}
53 :
54 2596349 : ClientContext::~ClientContext() {
55 1298178 : if (call_) {
56 1299152 : grpc_call_destroy(call_);
57 : }
58 1299197 : }
59 :
60 216 : std::unique_ptr<ClientContext> ClientContext::FromServerContext(
61 : const ServerContext& context, PropagationOptions options) {
62 216 : std::unique_ptr<ClientContext> ctx(new ClientContext);
63 216 : ctx->propagate_from_call_ = context.call_;
64 216 : ctx->propagation_options_ = options;
65 216 : return ctx;
66 : }
67 :
68 417 : void ClientContext::AddMetadata(const grpc::string& meta_key,
69 : const grpc::string& meta_value) {
70 417 : send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
71 417 : }
72 :
73 1299162 : void ClientContext::set_call(grpc_call* call,
74 : const std::shared_ptr<Channel>& channel) {
75 1299162 : GPR_ASSERT(call_ == nullptr);
76 1299162 : call_ = call;
77 1299162 : channel_ = channel;
78 1299191 : if (creds_ && !creds_->ApplyToCall(call_)) {
79 : grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED,
80 1 : "Failed to set credentials to rpc.", nullptr);
81 : }
82 1299189 : }
83 :
84 405 : void ClientContext::set_compression_algorithm(
85 : grpc_compression_algorithm algorithm) {
86 405 : char* algorithm_name = nullptr;
87 405 : if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
88 : gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
89 0 : algorithm);
90 0 : abort();
91 : }
92 405 : GPR_ASSERT(algorithm_name != nullptr);
93 405 : AddMetadata(GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, algorithm_name);
94 405 : }
95 :
96 1 : std::shared_ptr<const AuthContext> ClientContext::auth_context() const {
97 1 : if (auth_context_.get() == nullptr) {
98 1 : auth_context_ = CreateAuthContext(call_);
99 : }
100 1 : return auth_context_;
101 : }
102 :
103 12 : void ClientContext::TryCancel() {
104 12 : if (call_) {
105 12 : grpc_call_cancel(call_, nullptr);
106 : }
107 12 : }
108 :
109 4 : grpc::string ClientContext::peer() const {
110 4 : grpc::string peer;
111 4 : if (call_) {
112 4 : char* c_peer = grpc_call_get_peer(call_);
113 4 : peer = c_peer;
114 4 : gpr_free(c_peer);
115 : }
116 4 : return peer;
117 : }
118 :
119 : } // namespace grpc
|