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