channel.cc 5.1 KB

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