channel_interface.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. //
  3. // Copyright 2016 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_IMPL_CHANNEL_INTERFACE_H
  19. #define GRPCPP_IMPL_CHANNEL_INTERFACE_H
  20. #include <grpc/impl/connectivity_state.h>
  21. #include <grpcpp/impl/call.h>
  22. #include <grpcpp/support/status.h>
  23. #include <grpcpp/support/time.h>
  24. namespace grpc {
  25. template <class R>
  26. class ClientReader;
  27. template <class W>
  28. class ClientWriter;
  29. template <class W, class R>
  30. class ClientReaderWriter;
  31. namespace internal {
  32. template <class InputMessage, class OutputMessage>
  33. class CallbackUnaryCallImpl;
  34. template <class R>
  35. class ClientAsyncReaderFactory;
  36. template <class W>
  37. class ClientAsyncWriterFactory;
  38. template <class W, class R>
  39. class ClientAsyncReaderWriterFactory;
  40. class ClientAsyncResponseReaderHelper;
  41. template <class W, class R>
  42. class ClientCallbackReaderWriterFactory;
  43. template <class R>
  44. class ClientCallbackReaderFactory;
  45. template <class W>
  46. class ClientCallbackWriterFactory;
  47. class ClientCallbackUnaryFactory;
  48. } // namespace internal
  49. class ChannelInterface;
  50. class ClientContext;
  51. class CompletionQueue;
  52. namespace experimental {
  53. class DelegatingChannel;
  54. }
  55. namespace internal {
  56. class Call;
  57. class CallOpSetInterface;
  58. class RpcMethod;
  59. class InterceptedChannel;
  60. template <class InputMessage, class OutputMessage>
  61. class BlockingUnaryCallImpl;
  62. } // namespace internal
  63. /// Codegen interface for \a grpc::Channel.
  64. class ChannelInterface {
  65. public:
  66. virtual ~ChannelInterface() {}
  67. /// Get the current channel state. If the channel is in IDLE and
  68. /// \a try_to_connect is set to true, try to connect.
  69. virtual grpc_connectivity_state GetState(bool try_to_connect) = 0;
  70. /// Return the \a tag on \a cq when the channel state is changed or \a
  71. /// deadline expires. \a GetState needs to called to get the current state.
  72. template <typename T>
  73. void NotifyOnStateChange(grpc_connectivity_state last_observed, T deadline,
  74. grpc::CompletionQueue* cq, void* tag) {
  75. TimePoint<T> deadline_tp(deadline);
  76. NotifyOnStateChangeImpl(last_observed, deadline_tp.raw_time(), cq, tag);
  77. }
  78. /// Blocking wait for channel state change or \a deadline expiration.
  79. /// \a GetState needs to called to get the current state.
  80. template <typename T>
  81. bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline) {
  82. TimePoint<T> deadline_tp(deadline);
  83. return WaitForStateChangeImpl(last_observed, deadline_tp.raw_time());
  84. }
  85. /// Wait for this channel to be connected
  86. template <typename T>
  87. bool WaitForConnected(T deadline) {
  88. grpc_connectivity_state state;
  89. while ((state = GetState(true)) != GRPC_CHANNEL_READY) {
  90. if (!WaitForStateChange(state, deadline)) return false;
  91. }
  92. return true;
  93. }
  94. private:
  95. template <class R>
  96. friend class grpc::ClientReader;
  97. template <class W>
  98. friend class grpc::ClientWriter;
  99. template <class W, class R>
  100. friend class grpc::ClientReaderWriter;
  101. template <class R>
  102. friend class grpc::internal::ClientAsyncReaderFactory;
  103. template <class W>
  104. friend class grpc::internal::ClientAsyncWriterFactory;
  105. template <class W, class R>
  106. friend class grpc::internal::ClientAsyncReaderWriterFactory;
  107. friend class grpc::internal::ClientAsyncResponseReaderHelper;
  108. template <class W, class R>
  109. friend class grpc::internal::ClientCallbackReaderWriterFactory;
  110. template <class R>
  111. friend class grpc::internal::ClientCallbackReaderFactory;
  112. template <class W>
  113. friend class grpc::internal::ClientCallbackWriterFactory;
  114. friend class grpc::internal::ClientCallbackUnaryFactory;
  115. template <class InputMessage, class OutputMessage>
  116. friend class grpc::internal::BlockingUnaryCallImpl;
  117. template <class InputMessage, class OutputMessage>
  118. friend class grpc::internal::CallbackUnaryCallImpl;
  119. friend class grpc::internal::RpcMethod;
  120. friend class grpc::experimental::DelegatingChannel;
  121. friend class grpc::internal::InterceptedChannel;
  122. virtual internal::Call CreateCall(const internal::RpcMethod& method,
  123. grpc::ClientContext* context,
  124. grpc::CompletionQueue* cq) = 0;
  125. virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
  126. internal::Call* call) = 0;
  127. virtual void* RegisterMethod(const char* method) = 0;
  128. virtual void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
  129. gpr_timespec deadline,
  130. grpc::CompletionQueue* cq,
  131. void* tag) = 0;
  132. virtual bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
  133. gpr_timespec deadline) = 0;
  134. // EXPERIMENTAL
  135. // This is needed to keep codegen_test_minimal happy. InterceptedChannel needs
  136. // to make use of this but can't directly call Channel's implementation
  137. // because of the test.
  138. // Returns an empty Call object (rather than being pure) since this is a new
  139. // method and adding a new pure method to an interface would be a breaking
  140. // change (even though this is private and non-API)
  141. virtual internal::Call CreateCallInternal(
  142. const internal::RpcMethod& /*method*/, grpc::ClientContext* /*context*/,
  143. grpc::CompletionQueue* /*cq*/, size_t /*interceptor_pos*/) {
  144. return internal::Call();
  145. }
  146. // A method to get the callbackable completion queue associated with this
  147. // channel. If the return value is nullptr, this channel doesn't support
  148. // callback operations.
  149. // TODO(vjpai): Consider a better default like using a global CQ
  150. // Returns nullptr (rather than being pure) since this is a post-1.0 method
  151. // and adding a new pure method to an interface would be a breaking change
  152. // (even though this is private and non-API)
  153. virtual grpc::CompletionQueue* CallbackCQ() { return nullptr; }
  154. };
  155. } // namespace grpc
  156. #endif // GRPCPP_IMPL_CHANNEL_INTERFACE_H