channel.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifndef GRPCPP_CHANNEL_H
  19. #define GRPCPP_CHANNEL_H
  20. #include <memory>
  21. #include <grpc/grpc.h>
  22. #include <grpcpp/completion_queue.h>
  23. #include <grpcpp/impl/call.h>
  24. #include <grpcpp/impl/channel_interface.h>
  25. #include <grpcpp/impl/grpc_library.h>
  26. #include <grpcpp/impl/sync.h>
  27. #include <grpcpp/support/client_interceptor.h>
  28. #include <grpcpp/support/config.h>
  29. struct grpc_channel;
  30. namespace grpc {
  31. namespace testing {
  32. class ChannelTestPeer;
  33. } // namespace testing
  34. std::shared_ptr<Channel> CreateChannelInternal(
  35. const TString& host, grpc_channel* c_channel,
  36. std::vector<
  37. std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
  38. interceptor_creators);
  39. namespace experimental {
  40. /// Resets the channel's connection backoff.
  41. /// TODO(roth): Once we see whether this proves useful, either create a gRFC
  42. /// and change this to be a method of the Channel class, or remove it.
  43. void ChannelResetConnectionBackoff(Channel* channel);
  44. } // namespace experimental
  45. /// Channels represent a connection to an endpoint. Created by \a CreateChannel.
  46. class Channel final : public grpc::ChannelInterface,
  47. public grpc::internal::CallHook,
  48. public std::enable_shared_from_this<Channel>,
  49. private grpc::internal::GrpcLibrary {
  50. public:
  51. ~Channel() override;
  52. /// Get the current channel state. If the channel is in IDLE and
  53. /// \a try_to_connect is set to true, try to connect.
  54. grpc_connectivity_state GetState(bool try_to_connect) override;
  55. /// Returns the LB policy name, or the empty string if not yet available.
  56. TString GetLoadBalancingPolicyName() const;
  57. /// Returns the service config in JSON form, or the empty string if
  58. /// not available.
  59. TString GetServiceConfigJSON() const;
  60. private:
  61. template <class InputMessage, class OutputMessage>
  62. friend class grpc::internal::BlockingUnaryCallImpl;
  63. friend class grpc::testing::ChannelTestPeer;
  64. friend void experimental::ChannelResetConnectionBackoff(Channel* channel);
  65. friend std::shared_ptr<Channel> grpc::CreateChannelInternal(
  66. const TString& host, grpc_channel* c_channel,
  67. std::vector<std::unique_ptr<
  68. grpc::experimental::ClientInterceptorFactoryInterface>>
  69. interceptor_creators);
  70. friend class grpc::internal::InterceptedChannel;
  71. Channel(const TString& host, grpc_channel* c_channel,
  72. std::vector<std::unique_ptr<
  73. grpc::experimental::ClientInterceptorFactoryInterface>>
  74. interceptor_creators);
  75. grpc::internal::Call CreateCall(const grpc::internal::RpcMethod& method,
  76. grpc::ClientContext* context,
  77. grpc::CompletionQueue* cq) override;
  78. void PerformOpsOnCall(grpc::internal::CallOpSetInterface* ops,
  79. grpc::internal::Call* call) override;
  80. void* RegisterMethod(const char* method) override;
  81. void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
  82. gpr_timespec deadline, grpc::CompletionQueue* cq,
  83. void* tag) override;
  84. bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
  85. gpr_timespec deadline) override;
  86. grpc::CompletionQueue* CallbackCQ() override;
  87. grpc::internal::Call CreateCallInternal(
  88. const grpc::internal::RpcMethod& method, grpc::ClientContext* context,
  89. grpc::CompletionQueue* cq, size_t interceptor_pos) override;
  90. const TString host_;
  91. grpc_channel* const c_channel_; // owned
  92. // mu_ protects callback_cq_ (the per-channel callbackable completion queue)
  93. grpc::internal::Mutex mu_;
  94. // callback_cq_ references the callbackable completion queue associated
  95. // with this channel (if any). It is set on the first call to CallbackCQ().
  96. // It is _not owned_ by the channel; ownership belongs with its internal
  97. // shutdown callback tag (invoked when the CQ is fully shutdown).
  98. std::atomic<CompletionQueue*> callback_cq_{nullptr};
  99. std::vector<
  100. std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
  101. interceptor_creators_;
  102. };
  103. } // namespace grpc
  104. #endif // GRPCPP_CHANNEL_H