GRPC C++  1.22.0
channel_interface.h
Go to the documentation of this file.
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 
26 
27 namespace grpc_impl {
28 class ClientContext;
29 class CompletionQueue;
30 } // namespace grpc_impl
31 
32 namespace grpc {
33 class ChannelInterface;
34 
35 template <class R>
37 template <class W>
39 template <class W, class R>
41 
42 namespace internal {
43 class Call;
44 class CallOpSetInterface;
45 class RpcMethod;
46 template <class InputMessage, class OutputMessage>
48 template <class InputMessage, class OutputMessage>
50 template <class R>
52 template <class W>
54 template <class W, class R>
56 template <class R>
58 template <class W, class R>
60 template <class R>
62 template <class W>
65 class InterceptedChannel;
66 } // namespace internal
67 
70  public:
71  virtual ~ChannelInterface() {}
74  virtual grpc_connectivity_state GetState(bool try_to_connect) = 0;
75 
78  template <typename T>
79  void NotifyOnStateChange(grpc_connectivity_state last_observed, T deadline,
80  ::grpc_impl::CompletionQueue* cq, void* tag) {
81  TimePoint<T> deadline_tp(deadline);
82  NotifyOnStateChangeImpl(last_observed, deadline_tp.raw_time(), cq, tag);
83  }
84 
87  template <typename T>
88  bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline) {
89  TimePoint<T> deadline_tp(deadline);
90  return WaitForStateChangeImpl(last_observed, deadline_tp.raw_time());
91  }
92 
94  template <typename T>
95  bool WaitForConnected(T deadline) {
97  while ((state = GetState(true)) != GRPC_CHANNEL_READY) {
98  if (!WaitForStateChange(state, deadline)) return false;
99  }
100  return true;
101  }
102 
103  private:
104  template <class R>
105  friend class ::grpc::ClientReader;
106  template <class W>
107  friend class ::grpc::ClientWriter;
108  template <class W, class R>
109  friend class ::grpc::ClientReaderWriter;
110  template <class R>
111  friend class ::grpc::internal::ClientAsyncReaderFactory;
112  template <class W>
113  friend class ::grpc::internal::ClientAsyncWriterFactory;
114  template <class W, class R>
115  friend class ::grpc::internal::ClientAsyncReaderWriterFactory;
116  template <class R>
117  friend class ::grpc::internal::ClientAsyncResponseReaderFactory;
118  template <class W, class R>
119  friend class ::grpc::internal::ClientCallbackReaderWriterFactory;
120  template <class R>
121  friend class ::grpc::internal::ClientCallbackReaderFactory;
122  template <class W>
123  friend class ::grpc::internal::ClientCallbackWriterFactory;
124  friend class ::grpc::internal::ClientCallbackUnaryFactory;
125  template <class InputMessage, class OutputMessage>
126  friend class ::grpc::internal::BlockingUnaryCallImpl;
127  template <class InputMessage, class OutputMessage>
128  friend class ::grpc::internal::CallbackUnaryCallImpl;
129  friend class ::grpc::internal::RpcMethod;
130  friend class ::grpc::internal::InterceptedChannel;
131  virtual internal::Call CreateCall(const internal::RpcMethod& method,
132  ::grpc_impl::ClientContext* context,
133  ::grpc_impl::CompletionQueue* cq) = 0;
134  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
135  internal::Call* call) = 0;
136  virtual void* RegisterMethod(const char* method) = 0;
137  virtual void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
138  gpr_timespec deadline,
140  void* tag) = 0;
141  virtual bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
142  gpr_timespec deadline) = 0;
143 
144  // EXPERIMENTAL
145  // This is needed to keep codegen_test_minimal happy. InterceptedChannel needs
146  // to make use of this but can't directly call Channel's implementation
147  // because of the test.
148  // Returns an empty Call object (rather than being pure) since this is a new
149  // method and adding a new pure method to an interface would be a breaking
150  // change (even though this is private and non-API)
151  virtual internal::Call CreateCallInternal(const internal::RpcMethod& method,
152  ::grpc_impl::ClientContext* context,
154  size_t interceptor_pos) {
155  return internal::Call();
156  }
157 
158  // EXPERIMENTAL
159  // A method to get the callbackable completion queue associated with this
160  // channel. If the return value is nullptr, this channel doesn't support
161  // callback operations.
162  // TODO(vjpai): Consider a better default like using a global CQ
163  // Returns nullptr (rather than being pure) since this is a post-1.0 method
164  // and adding a new pure method to an interface would be a breaking change
165  // (even though this is private and non-API)
166  virtual ::grpc_impl::CompletionQueue* CallbackCQ() { return nullptr; }
167 };
168 } // namespace grpc
169 
170 #endif // GRPCPP_IMPL_CODEGEN_CHANNEL_INTERFACE_H
Synchronous (blocking) client-side API for doing server-streaming RPCs, where the stream of messages ...
Definition: channel_interface.h:36
::grpc_impl::ClientContext ClientContext
Definition: client_context.h:26
Definition: channel_interface.h:59
Definition: async_unary_call.h:73
Definition: channel_interface.h:63
Definition: channel_interface.h:49
gpr_timespec raw_time()
Definition: time.h:43
If you are trying to use CompletionQueue::AsyncNext with a time class that isn&#39;t either gpr_timespec ...
Definition: time.h:40
Definition: async_stream.h:315
Definition: async_stream.h:171
Descriptor of an RPC method.
Definition: rpc_method.h:29
grpc_connectivity_state
Connectivity state of a channel.
Definition: connectivity_state.h:27
An InterceptedChannel is available to client Interceptors.
Definition: intercepted_channel.h:38
::grpc_impl::CompletionQueue CompletionQueue
Definition: completion_queue.h:26
Definition: client_callback.h:998
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
void NotifyOnStateChange(grpc_connectivity_state last_observed, T deadline, ::grpc_impl::CompletionQueue *cq, void *tag)
Return the tag on cq when the channel state is changed or deadline expires.
Definition: channel_interface.h:79
Codegen interface for grpc::Channel.
Definition: channel_interface.h:69
channel is ready for work
Definition: connectivity_state.h:33
Definition: channel_interface.h:61
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call_op_set_interface.h:34
virtual ~ChannelInterface()
Definition: channel_interface.h:71
Definition: channel_interface.h:47
bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline)
Blocking wait for channel state change or deadline expiration.
Definition: channel_interface.h:88
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue_impl.h:102
A ClientContext allows the person implementing a service client to:
Definition: client_context_impl.h:178
Analogous to struct timespec.
Definition: gpr_types.h:47
Synchronous (blocking) client-side API for bi-directional streaming RPCs, where the outgoing message ...
Definition: channel_interface.h:40
bool WaitForConnected(T deadline)
Wait for this channel to be connected.
Definition: channel_interface.h:95
Synchronous (blocking) client-side API for doing client-streaming RPCs, where the outgoing message st...
Definition: channel_interface.h:38
Straightforward wrapping of the C call object.
Definition: call.h:38