You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

channel_interface.h 7.1 kB

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