channel_cc.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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++/channel.h>
  34. #include <memory>
  35. #include <grpc++/client_context.h>
  36. #include <grpc++/completion_queue.h>
  37. #include <grpc++/impl/call.h>
  38. #include <grpc++/impl/codegen/completion_queue_tag.h>
  39. #include <grpc++/impl/grpc_library.h>
  40. #include <grpc++/impl/rpc_method.h>
  41. #include <grpc++/security/credentials.h>
  42. #include <grpc++/support/channel_arguments.h>
  43. #include <grpc++/support/config.h>
  44. #include <grpc++/support/status.h>
  45. #include <grpc++/support/time.h>
  46. #include <grpc/grpc.h>
  47. #include <grpc/slice.h>
  48. #include <grpc/support/alloc.h>
  49. #include <grpc/support/log.h>
  50. #include "src/core/lib/profiling/timers.h"
  51. namespace grpc {
  52. static internal::GrpcLibraryInitializer g_gli_initializer;
  53. Channel::Channel(const grpc::string& host, grpc_channel* channel)
  54. : host_(host), c_channel_(channel) {
  55. g_gli_initializer.summon();
  56. }
  57. Channel::~Channel() { grpc_channel_destroy(c_channel_); }
  58. namespace {
  59. grpc::string GetChannelInfoField(grpc_channel* channel,
  60. grpc_channel_info* channel_info,
  61. char*** channel_info_field) {
  62. char* value = NULL;
  63. memset(channel_info, 0, sizeof(*channel_info));
  64. *channel_info_field = &value;
  65. grpc_channel_get_info(channel, channel_info);
  66. if (value == NULL) return "";
  67. grpc::string result = value;
  68. gpr_free(value);
  69. return result;
  70. }
  71. } // namespace
  72. grpc::string Channel::GetLoadBalancingPolicyName() const {
  73. grpc_channel_info channel_info;
  74. return GetChannelInfoField(c_channel_, &channel_info,
  75. &channel_info.lb_policy_name);
  76. }
  77. grpc::string Channel::GetServiceConfigJSON() const {
  78. grpc_channel_info channel_info;
  79. return GetChannelInfoField(c_channel_, &channel_info,
  80. &channel_info.service_config_json);
  81. }
  82. Call Channel::CreateCall(const RpcMethod& method, ClientContext* context,
  83. CompletionQueue* cq) {
  84. const bool kRegistered = method.channel_tag() && context->authority().empty();
  85. grpc_call* c_call = NULL;
  86. if (kRegistered) {
  87. c_call = grpc_channel_create_registered_call(
  88. c_channel_, context->propagate_from_call_,
  89. context->propagation_options_.c_bitmask(), cq->cq(),
  90. method.channel_tag(), context->raw_deadline(), nullptr);
  91. } else {
  92. const char* host_str = NULL;
  93. if (!context->authority().empty()) {
  94. host_str = context->authority_.c_str();
  95. } else if (!host_.empty()) {
  96. host_str = host_.c_str();
  97. }
  98. c_call = grpc_channel_create_call(c_channel_, context->propagate_from_call_,
  99. context->propagation_options_.c_bitmask(),
  100. cq->cq(), method.name(), host_str,
  101. context->raw_deadline(), nullptr);
  102. }
  103. grpc_census_call_set_context(c_call, context->census_context());
  104. context->set_call(c_call, shared_from_this());
  105. return Call(c_call, this, cq);
  106. }
  107. void Channel::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) {
  108. static const size_t MAX_OPS = 8;
  109. size_t nops = 0;
  110. grpc_op cops[MAX_OPS];
  111. ops->FillOps(cops, &nops);
  112. GPR_ASSERT(GRPC_CALL_OK ==
  113. grpc_call_start_batch(call->call(), cops, nops, ops, nullptr));
  114. }
  115. void* Channel::RegisterMethod(const char* method) {
  116. return grpc_channel_register_call(
  117. c_channel_, method, host_.empty() ? NULL : host_.c_str(), nullptr);
  118. }
  119. grpc_connectivity_state Channel::GetState(bool try_to_connect) {
  120. return grpc_channel_check_connectivity_state(c_channel_, try_to_connect);
  121. }
  122. namespace {
  123. class TagSaver final : public CompletionQueueTag {
  124. public:
  125. explicit TagSaver(void* tag) : tag_(tag) {}
  126. ~TagSaver() override {}
  127. bool FinalizeResult(void** tag, bool* status) override {
  128. *tag = tag_;
  129. delete this;
  130. return true;
  131. }
  132. private:
  133. void* tag_;
  134. };
  135. } // namespace
  136. void Channel::NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
  137. gpr_timespec deadline,
  138. CompletionQueue* cq, void* tag) {
  139. TagSaver* tag_saver = new TagSaver(tag);
  140. grpc_channel_watch_connectivity_state(c_channel_, last_observed, deadline,
  141. cq->cq(), tag_saver);
  142. }
  143. bool Channel::WaitForStateChangeImpl(grpc_connectivity_state last_observed,
  144. gpr_timespec deadline) {
  145. CompletionQueue cq;
  146. bool ok = false;
  147. void* tag = NULL;
  148. NotifyOnStateChangeImpl(last_observed, deadline, &cq, NULL);
  149. cq.Next(&tag, &ok);
  150. GPR_ASSERT(tag == NULL);
  151. return ok;
  152. }
  153. } // namespace grpc