client_context.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <grpc++/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 <grpc++/impl/grpc_library.h>
  25. #include <grpc++/security/credentials.h>
  26. #include <grpc++/server_context.h>
  27. #include <grpc++/support/time.h>
  28. namespace grpc {
  29. class DefaultGlobalClientCallbacks final
  30. : public ClientContext::GlobalCallbacks {
  31. public:
  32. ~DefaultGlobalClientCallbacks() override {}
  33. void DefaultConstructor(ClientContext* context) override {}
  34. void Destructor(ClientContext* context) override {}
  35. };
  36. static internal::GrpcLibraryInitializer g_gli_initializer;
  37. static DefaultGlobalClientCallbacks g_default_client_callbacks;
  38. static ClientContext::GlobalCallbacks* g_client_callbacks =
  39. &g_default_client_callbacks;
  40. ClientContext::ClientContext()
  41. : initial_metadata_received_(false),
  42. wait_for_ready_(false),
  43. wait_for_ready_explicitly_set_(false),
  44. idempotent_(false),
  45. cacheable_(false),
  46. call_(nullptr),
  47. call_canceled_(false),
  48. deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
  49. census_context_(nullptr),
  50. propagate_from_call_(nullptr),
  51. initial_metadata_corked_(false) {
  52. g_client_callbacks->DefaultConstructor(this);
  53. }
  54. ClientContext::~ClientContext() {
  55. if (call_) {
  56. grpc_call_unref(call_);
  57. }
  58. g_client_callbacks->Destructor(this);
  59. }
  60. std::unique_ptr<ClientContext> ClientContext::FromServerContext(
  61. const ServerContext& context, PropagationOptions options) {
  62. std::unique_ptr<ClientContext> ctx(new ClientContext);
  63. ctx->propagate_from_call_ = context.call_;
  64. ctx->propagation_options_ = options;
  65. return ctx;
  66. }
  67. void ClientContext::AddMetadata(const grpc::string& meta_key,
  68. const grpc::string& meta_value) {
  69. send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
  70. }
  71. void ClientContext::set_call(grpc_call* call,
  72. const std::shared_ptr<Channel>& channel) {
  73. std::unique_lock<std::mutex> lock(mu_);
  74. GPR_ASSERT(call_ == nullptr);
  75. call_ = call;
  76. channel_ = channel;
  77. if (creds_ && !creds_->ApplyToCall(call_)) {
  78. grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED,
  79. "Failed to set credentials to rpc.", nullptr);
  80. }
  81. if (call_canceled_) {
  82. grpc_call_cancel(call_, nullptr);
  83. }
  84. }
  85. void ClientContext::set_compression_algorithm(
  86. grpc_compression_algorithm algorithm) {
  87. const char* algorithm_name = nullptr;
  88. if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
  89. gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
  90. algorithm);
  91. abort();
  92. }
  93. GPR_ASSERT(algorithm_name != nullptr);
  94. AddMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name);
  95. }
  96. void ClientContext::TryCancel() {
  97. std::unique_lock<std::mutex> lock(mu_);
  98. if (call_) {
  99. grpc_call_cancel(call_, nullptr);
  100. } else {
  101. call_canceled_ = true;
  102. }
  103. }
  104. grpc::string ClientContext::peer() const {
  105. grpc::string peer;
  106. if (call_) {
  107. char* c_peer = grpc_call_get_peer(call_);
  108. peer = c_peer;
  109. gpr_free(c_peer);
  110. }
  111. return peer;
  112. }
  113. void ClientContext::SetGlobalCallbacks(GlobalCallbacks* client_callbacks) {
  114. GPR_ASSERT(g_client_callbacks == &g_default_client_callbacks);
  115. GPR_ASSERT(client_callbacks != NULL);
  116. GPR_ASSERT(client_callbacks != &g_default_client_callbacks);
  117. g_client_callbacks = client_callbacks;
  118. }
  119. } // namespace grpc