channel.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/grpc.h>
  36. #include <grpc/support/log.h>
  37. #include <grpc/support/slice.h>
  38. #include <grpc++/client_context.h>
  39. #include <grpc++/completion_queue.h>
  40. #include <grpc++/security/credentials.h>
  41. #include <grpc++/impl/call.h>
  42. #include <grpc++/impl/rpc_method.h>
  43. #include <grpc++/support/channel_arguments.h>
  44. #include <grpc++/support/config.h>
  45. #include <grpc++/support/status.h>
  46. #include <grpc++/support/time.h>
  47. #include "src/core/profiling/timers.h"
  48. namespace grpc {
  49. Channel::Channel(const grpc::string& host, grpc_channel* channel)
  50. : host_(host), c_channel_(channel) {}
  51. Channel::~Channel() { grpc_channel_destroy(c_channel_); }
  52. Call Channel::CreateCall(const RpcMethod& method, ClientContext* context,
  53. CompletionQueue* cq) {
  54. const bool kRegistered = method.channel_tag() && context->authority().empty();
  55. grpc_call* c_call = NULL;
  56. if (kRegistered) {
  57. c_call = grpc_channel_create_registered_call(
  58. c_channel_, context->propagate_from_call_,
  59. context->propagation_options_.c_bitmask(), cq->cq(),
  60. method.channel_tag(), context->raw_deadline(), nullptr);
  61. } else {
  62. const char* host_str = NULL;
  63. if (!context->authority().empty()) {
  64. host_str = context->authority_.c_str();
  65. } else if (!host_.empty()) {
  66. host_str = host_.c_str();
  67. }
  68. c_call = grpc_channel_create_call(c_channel_, context->propagate_from_call_,
  69. context->propagation_options_.c_bitmask(),
  70. cq->cq(), method.name(), host_str,
  71. context->raw_deadline(), nullptr);
  72. }
  73. grpc_census_call_set_context(c_call, context->census_context());
  74. GRPC_TIMER_MARK(GRPC_PTAG_CPP_CALL_CREATED, c_call);
  75. context->set_call(c_call, shared_from_this());
  76. return Call(c_call, this, cq);
  77. }
  78. void Channel::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) {
  79. static const size_t MAX_OPS = 8;
  80. size_t nops = 0;
  81. grpc_op cops[MAX_OPS];
  82. GRPC_TIMER_BEGIN(GRPC_PTAG_CPP_PERFORM_OPS, call->call());
  83. ops->FillOps(cops, &nops);
  84. GPR_ASSERT(GRPC_CALL_OK ==
  85. grpc_call_start_batch(call->call(), cops, nops, ops, nullptr));
  86. GRPC_TIMER_END(GRPC_PTAG_CPP_PERFORM_OPS, call->call());
  87. }
  88. void* Channel::RegisterMethod(const char* method) {
  89. return grpc_channel_register_call(
  90. c_channel_, method, host_.empty() ? NULL : host_.c_str(), nullptr);
  91. }
  92. grpc_connectivity_state Channel::GetState(bool try_to_connect) {
  93. return grpc_channel_check_connectivity_state(c_channel_, try_to_connect);
  94. }
  95. namespace {
  96. class TagSaver GRPC_FINAL : public CompletionQueueTag {
  97. public:
  98. explicit TagSaver(void* tag) : tag_(tag) {}
  99. ~TagSaver() GRPC_OVERRIDE {}
  100. bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
  101. *tag = tag_;
  102. delete this;
  103. return true;
  104. }
  105. private:
  106. void* tag_;
  107. };
  108. } // namespace
  109. void Channel::NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
  110. gpr_timespec deadline,
  111. CompletionQueue* cq, void* tag) {
  112. TagSaver* tag_saver = new TagSaver(tag);
  113. grpc_channel_watch_connectivity_state(c_channel_, last_observed, deadline,
  114. cq->cq(), tag_saver);
  115. }
  116. bool Channel::WaitForStateChangeImpl(grpc_connectivity_state last_observed,
  117. gpr_timespec deadline) {
  118. CompletionQueue cq;
  119. bool ok = false;
  120. void* tag = NULL;
  121. NotifyOnStateChangeImpl(last_observed, deadline, &cq, NULL);
  122. cq.Next(&tag, &ok);
  123. GPR_ASSERT(tag == NULL);
  124. return ok;
  125. }
  126. } // namespace grpc