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

source code of include/grpcpp/impl/codegen/channel_interface.h