client_context.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <grpc++/client_context.h>
  34. #include <grpc/grpc.h>
  35. #include <grpc/support/alloc.h>
  36. #include <grpc/support/string_util.h>
  37. #include <grpc++/credentials.h>
  38. #include <grpc++/server_context.h>
  39. #include <grpc++/time.h>
  40. #include "src/core/channel/compress_filter.h"
  41. #include "src/cpp/common/create_auth_context.h"
  42. namespace grpc {
  43. ClientContext::ClientContext()
  44. : initial_metadata_received_(false),
  45. call_(nullptr),
  46. cq_(nullptr),
  47. deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
  48. propagate_from_call_(nullptr) {}
  49. ClientContext::~ClientContext() {
  50. if (call_) {
  51. grpc_call_destroy(call_);
  52. }
  53. if (cq_) {
  54. // Drain cq_.
  55. grpc_completion_queue_shutdown(cq_);
  56. while (grpc_completion_queue_next(cq_, gpr_inf_future(GPR_CLOCK_REALTIME))
  57. .type != GRPC_QUEUE_SHUTDOWN)
  58. ;
  59. grpc_completion_queue_destroy(cq_);
  60. }
  61. }
  62. std::unique_ptr<ClientContext> ClientContext::FromServerContext(
  63. const ServerContext& context, PropagationOptions options) {
  64. std::unique_ptr<ClientContext> ctx(new ClientContext);
  65. ctx->propagate_from_call_ = context.call_;
  66. ctx->propagation_options_ = options;
  67. return ctx;
  68. }
  69. void ClientContext::AddMetadata(const grpc::string& meta_key,
  70. const grpc::string& meta_value) {
  71. send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
  72. }
  73. void ClientContext::set_call(grpc_call* call,
  74. const std::shared_ptr<ChannelInterface>& channel) {
  75. GPR_ASSERT(call_ == nullptr);
  76. call_ = call;
  77. channel_ = channel;
  78. if (creds_ && !creds_->ApplyToCall(call_)) {
  79. grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED,
  80. "Failed to set credentials to rpc.");
  81. }
  82. }
  83. void ClientContext::set_compression_algorithm(
  84. grpc_compression_algorithm algorithm) {
  85. char* algorithm_name = NULL;
  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 != NULL);
  92. AddMetadata(GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, algorithm_name);
  93. }
  94. std::shared_ptr<const AuthContext> ClientContext::auth_context() const {
  95. if (auth_context_.get() == nullptr) {
  96. auth_context_ = CreateAuthContext(call_);
  97. }
  98. return auth_context_;
  99. }
  100. void ClientContext::TryCancel() {
  101. if (call_) {
  102. grpc_call_cancel(call_);
  103. }
  104. }
  105. grpc::string ClientContext::peer() const {
  106. grpc::string peer;
  107. if (call_) {
  108. char* c_peer = grpc_call_get_peer(call_);
  109. peer = c_peer;
  110. gpr_free(c_peer);
  111. }
  112. return peer;
  113. }
  114. } // namespace grpc