client_context.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include <grpcpp/client_context.h>
  19. #include <grpc/compression.h>
  20. #include <grpc/grpc.h>
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include <grpc/support/string_util.h>
  24. #include <grpcpp/impl/codegen/interceptor_common.h>
  25. #include <grpcpp/impl/codegen/sync.h>
  26. #include <grpcpp/impl/grpc_library.h>
  27. #include <grpcpp/security/credentials.h>
  28. #include <grpcpp/server_context.h>
  29. #include <grpcpp/support/time.h>
  30. namespace grpc_impl {
  31. class Channel;
  32. class DefaultGlobalClientCallbacks final
  33. : public ClientContext::GlobalCallbacks {
  34. public:
  35. ~DefaultGlobalClientCallbacks() override {}
  36. void DefaultConstructor(ClientContext* /*context*/) override {}
  37. void Destructor(ClientContext* /*context*/) override {}
  38. };
  39. static grpc::internal::GrpcLibraryInitializer g_gli_initializer;
  40. static DefaultGlobalClientCallbacks* g_default_client_callbacks =
  41. new DefaultGlobalClientCallbacks();
  42. static ClientContext::GlobalCallbacks* g_client_callbacks =
  43. g_default_client_callbacks;
  44. ClientContext::ClientContext()
  45. : initial_metadata_received_(false),
  46. wait_for_ready_(false),
  47. wait_for_ready_explicitly_set_(false),
  48. idempotent_(false),
  49. cacheable_(false),
  50. call_(nullptr),
  51. call_canceled_(false),
  52. deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
  53. census_context_(nullptr),
  54. propagate_from_call_(nullptr),
  55. compression_algorithm_(GRPC_COMPRESS_NONE),
  56. initial_metadata_corked_(false) {
  57. g_client_callbacks->DefaultConstructor(this);
  58. }
  59. ClientContext::~ClientContext() {
  60. if (call_) {
  61. grpc_call_unref(call_);
  62. }
  63. g_client_callbacks->Destructor(this);
  64. }
  65. void ClientContext::set_credentials(
  66. const std::shared_ptr<grpc_impl::CallCredentials>& creds) {
  67. creds_ = creds;
  68. // If call_ is set, we have already created the call, and set the call
  69. // credentials. This should only be done before we have started the batch
  70. // for sending initial metadata.
  71. if (creds_ != nullptr && call_ != nullptr) {
  72. if (!creds_->ApplyToCall(call_)) {
  73. SendCancelToInterceptors();
  74. grpc_call_cancel_with_status(call_, GRPC_STATUS_CANCELLED,
  75. "Failed to set credentials to rpc.",
  76. nullptr);
  77. }
  78. }
  79. }
  80. std::unique_ptr<ClientContext> ClientContext::FromServerContext(
  81. const grpc::ServerContext& context, PropagationOptions options) {
  82. std::unique_ptr<ClientContext> ctx(new ClientContext);
  83. ctx->propagate_from_call_ = context.call_;
  84. ctx->propagation_options_ = options;
  85. return ctx;
  86. }
  87. void ClientContext::AddMetadata(const grpc::string& meta_key,
  88. const grpc::string& meta_value) {
  89. send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
  90. }
  91. void ClientContext::set_call(
  92. grpc_call* call, const std::shared_ptr<::grpc_impl::Channel>& channel) {
  93. grpc::internal::MutexLock lock(&mu_);
  94. GPR_ASSERT(call_ == nullptr);
  95. call_ = call;
  96. channel_ = channel;
  97. if (creds_ && !creds_->ApplyToCall(call_)) {
  98. // TODO(yashykt): should interceptors also see this status?
  99. SendCancelToInterceptors();
  100. grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED,
  101. "Failed to set credentials to rpc.", nullptr);
  102. }
  103. if (call_canceled_) {
  104. SendCancelToInterceptors();
  105. grpc_call_cancel(call_, nullptr);
  106. }
  107. }
  108. void ClientContext::set_compression_algorithm(
  109. grpc_compression_algorithm algorithm) {
  110. compression_algorithm_ = algorithm;
  111. const char* algorithm_name = nullptr;
  112. if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
  113. gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
  114. algorithm);
  115. abort();
  116. }
  117. GPR_ASSERT(algorithm_name != nullptr);
  118. AddMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name);
  119. }
  120. void ClientContext::TryCancel() {
  121. grpc::internal::MutexLock lock(&mu_);
  122. if (call_) {
  123. SendCancelToInterceptors();
  124. grpc_call_cancel(call_, nullptr);
  125. } else {
  126. call_canceled_ = true;
  127. }
  128. }
  129. void ClientContext::SendCancelToInterceptors() {
  130. grpc::internal::CancelInterceptorBatchMethods cancel_methods;
  131. for (size_t i = 0; i < rpc_info_.interceptors_.size(); i++) {
  132. rpc_info_.RunInterceptor(&cancel_methods, i);
  133. }
  134. }
  135. grpc::string ClientContext::peer() const {
  136. grpc::string peer;
  137. if (call_) {
  138. char* c_peer = grpc_call_get_peer(call_);
  139. peer = c_peer;
  140. gpr_free(c_peer);
  141. }
  142. return peer;
  143. }
  144. void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) {
  145. GPR_ASSERT(g_client_callbacks == g_default_client_callbacks);
  146. GPR_ASSERT(client_callbacks != nullptr);
  147. GPR_ASSERT(client_callbacks != g_default_client_callbacks);
  148. g_client_callbacks = client_callbacks;
  149. }
  150. } // namespace grpc_impl