GRPC C++  1.17.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 
27 
28 namespace grpc {
29 class ChannelInterface;
30 class ClientContext;
31 class CompletionQueue;
32 
33 template <class R>
35 template <class W>
37 template <class W, class R>
39 
40 namespace internal {
41 class Call;
42 class CallOpSetInterface;
43 class RpcMethod;
44 template <class InputMessage, class OutputMessage>
46 template <class InputMessage, class OutputMessage>
48 template <class R>
50 template <class W>
52 template <class W, class R>
54 template <class R>
56 class InterceptedChannel;
57 } // namespace internal
58 
61  public:
62  virtual ~ChannelInterface() {}
65  virtual grpc_connectivity_state GetState(bool try_to_connect) = 0;
66 
69  template <typename T>
70  void NotifyOnStateChange(grpc_connectivity_state last_observed, T deadline,
71  CompletionQueue* cq, void* tag) {
72  TimePoint<T> deadline_tp(deadline);
73  NotifyOnStateChangeImpl(last_observed, deadline_tp.raw_time(), cq, tag);
74  }
75 
78  template <typename T>
79  bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline) {
80  TimePoint<T> deadline_tp(deadline);
81  return WaitForStateChangeImpl(last_observed, deadline_tp.raw_time());
82  }
83 
85  template <typename T>
86  bool WaitForConnected(T deadline) {
88  while ((state = GetState(true)) != GRPC_CHANNEL_READY) {
89  if (!WaitForStateChange(state, deadline)) return false;
90  }
91  return true;
92  }
93 
94  private:
95  template <class R>
96  friend class ::grpc::ClientReader;
97  template <class W>
98  friend class ::grpc::ClientWriter;
99  template <class W, class R>
100  friend class ::grpc::ClientReaderWriter;
101  template <class R>
102  friend class ::grpc::internal::ClientAsyncReaderFactory;
103  template <class W>
104  friend class ::grpc::internal::ClientAsyncWriterFactory;
105  template <class W, class R>
106  friend class ::grpc::internal::ClientAsyncReaderWriterFactory;
107  template <class R>
108  friend class ::grpc::internal::ClientAsyncResponseReaderFactory;
109  template <class InputMessage, class OutputMessage>
110  friend class ::grpc::internal::BlockingUnaryCallImpl;
111  template <class InputMessage, class OutputMessage>
112  friend class ::grpc::internal::CallbackUnaryCallImpl;
113  friend class ::grpc::internal::RpcMethod;
114  friend class ::grpc::internal::InterceptedChannel;
115  virtual internal::Call CreateCall(const internal::RpcMethod& method,
116  ClientContext* context,
117  CompletionQueue* cq) = 0;
118  virtual void PerformOpsOnCall(internal::CallOpSetInterface* ops,
119  internal::Call* call) = 0;
120  virtual void* RegisterMethod(const char* method) = 0;
121  virtual void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
122  gpr_timespec deadline,
123  CompletionQueue* cq, void* tag) = 0;
124  virtual bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
125  gpr_timespec deadline) = 0;
126 
127  // EXPERIMENTAL
128  // This is needed to keep codegen_test_minimal happy. InterceptedChannel needs
129  // to make use of this but can't directly call Channel's implementation
130  // because of the test.
131  // Returns an empty Call object (rather than being pure) since this is a new
132  // method and adding a new pure method to an interface would be a breaking
133  // change (even though this is private and non-API)
134  virtual internal::Call CreateCallInternal(const internal::RpcMethod& method,
135  ClientContext* context,
136  CompletionQueue* cq,
137  size_t interceptor_pos) {
138  return internal::Call();
139  }
140 
141  // EXPERIMENTAL
142  // A method to get the callbackable completion queue associated with this
143  // channel. If the return value is nullptr, this channel doesn't support
144  // callback operations.
145  // TODO(vjpai): Consider a better default like using a global CQ
146  // Returns nullptr (rather than being pure) since this is a post-1.0 method
147  // and adding a new pure method to an interface would be a breaking change
148  // (even though this is private and non-API)
149  virtual CompletionQueue* CallbackCQ() { return nullptr; }
150 };
151 } // namespace grpc
152 
153 #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:34
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:70
Definition: channel_interface.h:47
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:165
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
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:33
Codegen interface for grpc::Channel.
Definition: channel_interface.h:60
channel is ready for work
Definition: connectivity_state.h:33
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:62
Definition: channel_interface.h:45
bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline)
Blocking wait for channel state change or deadline expiration.
Definition: channel_interface.h:79
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:38
bool WaitForConnected(T deadline)
Wait for this channel to be connected.
Definition: channel_interface.h:86
Synchronous (blocking) client-side API for doing client-streaming RPCs, where the outgoing message st...
Definition: channel_interface.h:36
Straightforward wrapping of the C call object.
Definition: call.h:36