OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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/compression.h> |
| 37 #include <grpc/grpc.h> |
| 38 #include <grpc/support/alloc.h> |
| 39 #include <grpc/support/string_util.h> |
| 40 #include <grpc++/security/credentials.h> |
| 41 #include <grpc++/server_context.h> |
| 42 #include <grpc++/support/time.h> |
| 43 |
| 44 #include "src/core/channel/compress_filter.h" |
| 45 #include "src/cpp/common/create_auth_context.h" |
| 46 |
| 47 namespace grpc { |
| 48 |
| 49 class DefaultGlobalClientCallbacks GRPC_FINAL |
| 50 : public ClientContext::GlobalCallbacks { |
| 51 public: |
| 52 ~DefaultGlobalClientCallbacks() GRPC_OVERRIDE {} |
| 53 void DefaultConstructor(ClientContext* context) GRPC_OVERRIDE {} |
| 54 void Destructor(ClientContext* context) GRPC_OVERRIDE {} |
| 55 }; |
| 56 |
| 57 static DefaultGlobalClientCallbacks g_default_client_callbacks; |
| 58 static ClientContext::GlobalCallbacks* g_client_callbacks = |
| 59 &g_default_client_callbacks; |
| 60 |
| 61 ClientContext::ClientContext() |
| 62 : initial_metadata_received_(false), |
| 63 call_(nullptr), |
| 64 call_canceled_(false), |
| 65 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)), |
| 66 propagate_from_call_(nullptr) { |
| 67 g_client_callbacks->DefaultConstructor(this); |
| 68 } |
| 69 |
| 70 ClientContext::~ClientContext() { |
| 71 if (call_) { |
| 72 grpc_call_destroy(call_); |
| 73 } |
| 74 g_client_callbacks->Destructor(this); |
| 75 } |
| 76 |
| 77 std::unique_ptr<ClientContext> ClientContext::FromServerContext( |
| 78 const ServerContext& context, PropagationOptions options) { |
| 79 std::unique_ptr<ClientContext> ctx(new ClientContext); |
| 80 ctx->propagate_from_call_ = context.call_; |
| 81 ctx->propagation_options_ = options; |
| 82 return ctx; |
| 83 } |
| 84 |
| 85 void ClientContext::AddMetadata(const grpc::string& meta_key, |
| 86 const grpc::string& meta_value) { |
| 87 send_initial_metadata_.insert(std::make_pair(meta_key, meta_value)); |
| 88 } |
| 89 |
| 90 void ClientContext::set_call(grpc_call* call, |
| 91 const std::shared_ptr<Channel>& channel) { |
| 92 grpc::unique_lock<grpc::mutex> lock(mu_); |
| 93 GPR_ASSERT(call_ == nullptr); |
| 94 call_ = call; |
| 95 channel_ = channel; |
| 96 if (creds_ && !creds_->ApplyToCall(call_)) { |
| 97 grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED, |
| 98 "Failed to set credentials to rpc.", nullptr); |
| 99 } |
| 100 if (call_canceled_) { |
| 101 grpc_call_cancel(call_, nullptr); |
| 102 } |
| 103 } |
| 104 |
| 105 void ClientContext::set_compression_algorithm( |
| 106 grpc_compression_algorithm algorithm) { |
| 107 char* algorithm_name = nullptr; |
| 108 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) { |
| 109 gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.", |
| 110 algorithm); |
| 111 abort(); |
| 112 } |
| 113 GPR_ASSERT(algorithm_name != nullptr); |
| 114 AddMetadata(GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, algorithm_name); |
| 115 } |
| 116 |
| 117 std::shared_ptr<const AuthContext> ClientContext::auth_context() const { |
| 118 if (auth_context_.get() == nullptr) { |
| 119 auth_context_ = CreateAuthContext(call_); |
| 120 } |
| 121 return auth_context_; |
| 122 } |
| 123 |
| 124 void ClientContext::TryCancel() { |
| 125 grpc::unique_lock<grpc::mutex> lock(mu_); |
| 126 if (call_) { |
| 127 grpc_call_cancel(call_, nullptr); |
| 128 } else { |
| 129 call_canceled_ = true; |
| 130 } |
| 131 } |
| 132 |
| 133 grpc::string ClientContext::peer() const { |
| 134 grpc::string peer; |
| 135 if (call_) { |
| 136 char* c_peer = grpc_call_get_peer(call_); |
| 137 peer = c_peer; |
| 138 gpr_free(c_peer); |
| 139 } |
| 140 return peer; |
| 141 } |
| 142 |
| 143 void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) { |
| 144 GPR_ASSERT(g_client_callbacks == &g_default_client_callbacks); |
| 145 GPR_ASSERT(client_callbacks != NULL); |
| 146 GPR_ASSERT(client_callbacks != &g_default_client_callbacks); |
| 147 g_client_callbacks = client_callbacks; |
| 148 } |
| 149 |
| 150 } // namespace grpc |
OLD | NEW |