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