channel_interface.h 6.4 KB

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