client_context.cc 3.9 KB

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