GRPC C++  1.18.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 {
28 class ChannelInterface;
29 class ClientContext;
30 class CompletionQueue;
31 
32 template <class R>
34 template <class W>
36 template <class W, class R>
38 
39 namespace internal {
40 class Call;
41 class CallOpSetInterface;
42 class RpcMethod;
43 template <class InputMessage, class OutputMessage>
45 template <class InputMessage, class OutputMessage>
47 template <class R>
49 template <class W>
51 template <class W, class R>
53 template <class R>
55 template <class W, class R>
57 template <class R>
59 template <class W>
61 class InterceptedChannel;
62 } // namespace internal
63 
66  public:
67  virtual ~ChannelInterface() {}
70  virtual grpc_connectivity_state GetState(bool try_to_connect) = 0;
71 
74  template <typename T>
75  void NotifyOnStateChange(grpc_connectivity_state last_observed, T deadline,
76  CompletionQueue* cq, void* tag) {
77  TimePoint<T> deadline_tp(deadline);
78  NotifyOnStateChangeImpl(last_observed, deadline_tp.raw_time(), cq, tag);
79  }
80 
83  template <typename T>
84  bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline) {
85  TimePoint<T> deadline_tp(deadline);
86  return WaitForStateChangeImpl(last_observed, deadline_tp.raw_time());
87  }
88 
90  template <typename T>
91  bool WaitForConnected(T deadline) {
93  while ((state = GetState(true)) != GRPC_CHANNEL_READY) {
94  if (!WaitForStateChange(state, deadline)) return false;
95  }
96  return true;
97  }
98 
99  private:
100  template <class R>
101  friend class ::grpc::ClientReader;
102  template <class W>
103  friend class ::grpc::ClientWriter;
104  template <class W, class R>
105  friend class ::grpc::ClientReaderWriter;
106  template <class R>
107  friend class ::grpc::internal::ClientAsyncReaderFactory;
108  template <class W>
109  friend class ::grpc::internal::ClientAsyncWriterFactory;
110  template <class W, class R>
111  friend class ::grpc::internal::ClientAsyncReaderWriterFactory;
112  template <class R>
113  friend class ::grpc::internal::ClientAsyncResponseReaderFactory;
114  template <class W, class R>
115  friend class ::grpc::internal::ClientCallbackReaderWriterFactory;
116  template <class R>
117  friend class ::grpc::internal::ClientCallbackReaderFactory;
118  template <class W>
119  friend class ::grpc::internal::ClientCallbackWriterFactory;
120  template <class InputMessage, class OutputMessage>
121  friend class ::grpc::internal::BlockingUnaryCallImpl;
122  template <class InputMessage, class OutputMessage>
123  friend class ::grpc::internal::CallbackUnaryCallImpl;
124  friend class ::grpc::internal::RpcMethod;
125  friend class ::grpc::internal::InterceptedChannel;
126  virtual internal::Call CreateCall(const internal::RpcMethod& method,
127  ClientContext* context,
128  CompletionQueue* cq) = 0;
129  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
130  internal::Call* call) = 0;
131  virtual void* RegisterMethod(const char* method) = 0;
132  virtual void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
133  gpr_timespec deadline,
134  CompletionQueue* cq, void* tag) = 0;
135  virtual bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
136  gpr_timespec deadline) = 0;
137 
138  // EXPERIMENTAL
139  // This is needed to keep codegen_test_minimal happy. InterceptedChannel needs
140  // to make use of this but can't directly call Channel's implementation
141  // because of the test.
142  // Returns an empty Call object (rather than being pure) since this is a new
143  // method and adding a new pure method to an interface would be a breaking
144  // change (even though this is private and non-API)
145  virtual internal::Call CreateCallInternal(const internal::RpcMethod& method,
146  ClientContext* context,
147  CompletionQueue* cq,
148  size_t interceptor_pos) {
149  return internal::Call();
150  }
151 
152  // EXPERIMENTAL
153  // A method to get the callbackable completion queue associated with this
154  // channel. If the return value is nullptr, this channel doesn't support
155  // callback operations.
156  // TODO(vjpai): Consider a better default like using a global CQ
157  // Returns nullptr (rather than being pure) since this is a post-1.0 method
158  // and adding a new pure method to an interface would be a breaking change
159  // (even though this is private and non-API)
160  virtual CompletionQueue* CallbackCQ() { return nullptr; }
161 };
162 } // namespace grpc
163 
164 #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:33
Definition: channel_interface.h:56
Definition: async_unary_call.h:74
void NotifyOnStateChange(grpc_connectivity_state last_observed, T deadline, CompletionQueue *cq, void *tag)
Return the tag on cq when the channel state is changed or deadline expires.
Definition: channel_interface.h:75
Definition: channel_interface.h:60
Definition: channel_interface.h:46
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
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:174
Definition: async_stream.h:317
Definition: async_stream.h:173
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:34
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Codegen interface for grpc::Channel.
Definition: channel_interface.h:65
channel is ready for work
Definition: connectivity_state.h:33
Definition: channel_interface.h:58
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:95
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:67
Definition: channel_interface.h:44
bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline)
Blocking wait for channel state change or deadline expiration.
Definition: channel_interface.h:84
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:37
bool WaitForConnected(T deadline)
Wait for this channel to be connected.
Definition: channel_interface.h:91
Synchronous (blocking) client-side API for doing client-streaming RPCs, where the outgoing message st...
Definition: channel_interface.h:35
Straightforward wrapping of the C call object.
Definition: call.h:36