View Javadoc
1   /*
2    * Copyright 2020 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.quic;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.Channel;
20  import io.netty.channel.ChannelFuture;
21  import io.netty.channel.ChannelHandler;
22  import io.netty.channel.ChannelProgressivePromise;
23  import io.netty.channel.ChannelPromise;
24  import io.netty.util.concurrent.Future;
25  import io.netty.util.concurrent.Promise;
26  import org.jetbrains.annotations.Nullable;
27  
28  import javax.net.ssl.SSLEngine;
29  import java.net.SocketAddress;
30  
31  /**
32   * A QUIC {@link Channel}.
33   */
34  public interface QuicChannel extends Channel {
35  
36      @Override
37      default ChannelFuture bind(SocketAddress localAddress) {
38          return pipeline().bind(localAddress);
39      }
40  
41      @Override
42      default ChannelFuture connect(SocketAddress remoteAddress) {
43          return pipeline().connect(remoteAddress);
44      }
45  
46      @Override
47      default ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) {
48          return pipeline().connect(remoteAddress, localAddress);
49      }
50  
51      @Override
52      default ChannelFuture disconnect() {
53          return pipeline().disconnect();
54      }
55  
56      @Override
57      default ChannelFuture close() {
58          return pipeline().close();
59      }
60  
61      @Override
62      default ChannelFuture deregister() {
63          return pipeline().deregister();
64      }
65  
66      @Override
67      default ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) {
68          return pipeline().bind(localAddress, promise);
69      }
70  
71      @Override
72      default ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise) {
73          return pipeline().connect(remoteAddress, promise);
74      }
75  
76      @Override
77      default ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
78          return pipeline().connect(remoteAddress, localAddress, promise);
79      }
80  
81      @Override
82      default ChannelFuture disconnect(ChannelPromise promise) {
83          return pipeline().disconnect(promise);
84      }
85  
86      @Override
87      default ChannelFuture close(ChannelPromise promise) {
88          return pipeline().close(promise);
89      }
90  
91      @Override
92      default ChannelFuture deregister(ChannelPromise promise) {
93          return pipeline().deregister(promise);
94      }
95  
96      @Override
97      default ChannelFuture write(Object msg) {
98          return pipeline().write(msg);
99      }
100 
101     @Override
102     default ChannelFuture write(Object msg, ChannelPromise promise) {
103         return pipeline().write(msg, promise);
104     }
105 
106     @Override
107     default ChannelFuture writeAndFlush(Object msg, ChannelPromise promise) {
108         return pipeline().writeAndFlush(msg, promise);
109     }
110 
111     @Override
112     default ChannelFuture writeAndFlush(Object msg) {
113         return pipeline().writeAndFlush(msg);
114     }
115 
116     @Override
117     default ChannelPromise newPromise() {
118         return pipeline().newPromise();
119     }
120 
121     @Override
122     default ChannelProgressivePromise newProgressivePromise() {
123         return pipeline().newProgressivePromise();
124     }
125 
126     @Override
127     default ChannelFuture newSucceededFuture() {
128         return pipeline().newSucceededFuture();
129     }
130 
131     @Override
132     default ChannelFuture newFailedFuture(Throwable cause) {
133         return pipeline().newFailedFuture(cause);
134     }
135 
136     @Override
137     default ChannelPromise voidPromise() {
138         return pipeline().voidPromise();
139     }
140 
141     @Override
142     QuicChannel read();
143 
144     @Override
145     QuicChannel flush();
146 
147     /**
148      * Returns the configuration of this channel.
149      */
150     @Override
151     QuicChannelConfig config();
152 
153     /**
154      * Returns the used {@link SSLEngine} or {@code null} if none is used (yet).
155      *
156      * @return the engine.
157      */
158     @Nullable
159     SSLEngine sslEngine();
160 
161     /**
162      * Returns the number of streams that can be created before stream creation will fail
163      * with {@link QuicTransportError#STREAM_LIMIT_ERROR} error.
164      *
165      * @param type the stream type.
166      * @return the number of streams left.
167      */
168     long peerAllowedStreams(QuicStreamType type);
169 
170     /**
171      * Returns {@code true} if the connection was closed because of idle timeout.
172      *
173      * @return {@code true} if the connection was closed because of idle timeout, {@code false}.
174      */
175     boolean isTimedOut();
176 
177     /**
178      * Returns the {@link QuicTransportParameters} of the peer once received, or {@code null} if not known yet.
179      *
180      * @return peerTransportParams.
181      */
182     @Nullable
183     QuicTransportParameters peerTransportParameters();
184 
185     /**
186      * Returns the local {@link QuicConnectionAddress}. This address might change over the life-time of the
187      * channel.
188      *
189      * @return  local   the local {@link QuicConnectionAddress} or {@code null} if none is assigned yet,
190      *                  or assigned anymore.
191      */
192     @Override
193     @Nullable
194     QuicConnectionAddress localAddress();
195 
196     /**
197      * Returns the remote {@link QuicConnectionAddress}. This address might change over the life-time of the
198      * channel.
199      *
200      * @return  remote   the remote {@link QuicConnectionAddress} or {@code null} if none is assigned yet,
201      *                   or assigned anymore.
202      */
203     @Override
204     @Nullable
205     QuicConnectionAddress remoteAddress();
206 
207     /**
208      * Returns the local {@link SocketAddress} of the underlying transport that received the data.
209      * This address might change over the life-time of the channel.
210      *
211      * @return  local   the local {@link SocketAddress} of the underlying transport or {@code null} if none is assigned
212      *                  yet, or assigned anymore.
213      */
214     @Nullable
215     SocketAddress localSocketAddress();
216 
217     /**
218      * Returns the remote {@link SocketAddress} of the underlying transport to which the data is sent.
219      * This address might change over the life-time of the channel.
220      *
221      * @return  local   the remote {@link SocketAddress} of the underlying transport or {@code null} if none is assigned
222      *                  yet, or assigned anymore.
223      */
224     @Nullable
225     SocketAddress remoteSocketAddress();
226 
227     /**
228      * Creates a stream that is using this {@link QuicChannel} and notifies the {@link Future} once done.
229      * The {@link ChannelHandler} (if not {@code null}) is added to the {@link io.netty.channel.ChannelPipeline} of the
230      * {@link QuicStreamChannel} automatically.
231      *
232      * @param type      the {@link QuicStreamType} of the {@link QuicStreamChannel}.
233      * @param handler   the {@link ChannelHandler} that will be added to the {@link QuicStreamChannel}s
234      *                  {@link io.netty.channel.ChannelPipeline} during the stream creation.
235      * @return          the {@link Future} that will be notified once the operation completes.
236      */
237     default Future<QuicStreamChannel> createStream(QuicStreamType type, @Nullable ChannelHandler handler) {
238         return createStream(type, handler, eventLoop().newPromise());
239     }
240 
241     /**
242      * Creates a stream that is using this {@link QuicChannel} and notifies the {@link Promise} once done.
243      * The {@link ChannelHandler} (if not {@code null}) is added to the {@link io.netty.channel.ChannelPipeline} of the
244      * {@link QuicStreamChannel} automatically.
245      *
246      * @param type      the {@link QuicStreamType} of the {@link QuicStreamChannel}.
247      * @param handler   the {@link ChannelHandler} that will be added to the {@link QuicStreamChannel}s
248      *                  {@link io.netty.channel.ChannelPipeline} during the stream creation.
249      * @param promise   the {@link ChannelPromise} that will be notified once the operation completes.
250      * @return          the {@link Future} that will be notified once the operation completes.
251      */
252     Future<QuicStreamChannel> createStream(QuicStreamType type, @Nullable ChannelHandler handler,
253                                            Promise<QuicStreamChannel> promise);
254 
255     /**
256      * Returns a new {@link QuicStreamChannelBootstrap} which makes it easy to bootstrap new {@link QuicStreamChannel}s
257      * with custom options and attributes. For simpler use-cases you may want to consider using
258      * {@link #createStream(QuicStreamType, ChannelHandler)} or
259      * {@link #createStream(QuicStreamType, ChannelHandler, Promise)} directly.
260      *
261      * @return {@link QuicStreamChannelBootstrap} that can be used to bootstrap a {@link QuicStreamChannel}.
262      */
263     default QuicStreamChannelBootstrap newStreamBootstrap() {
264         return new QuicStreamChannelBootstrap(this);
265     }
266 
267     /**
268      * Close the {@link QuicChannel}
269      *
270      * @param applicationClose  {@code true} if an application close should be used,
271      *                          {@code false} if a normal close should be used.
272      * @param error             the application error number, or {@code 0} if no special error should be signaled.
273      * @param reason            the reason for the closure (which may be an empty {@link ByteBuf}.
274      * @return                  the future that is notified.
275      */
276     default ChannelFuture close(boolean applicationClose, int error, ByteBuf reason) {
277         return close(applicationClose, error, reason, newPromise());
278     }
279 
280     /**
281      * Close the {@link QuicChannel}
282      *
283      * @param applicationClose  {@code true} if an application close should be used,
284      *                          {@code false} if a normal close should be used.
285      * @param error             the application error number, or {@code 0} if no special error should be signaled.
286      * @param reason            the reason for the closure (which may be an empty {@link ByteBuf}.
287      * @param promise           the {@link ChannelPromise} that will be notified.
288      * @return                  the future that is notified.
289      */
290     ChannelFuture close(boolean applicationClose, int error, ByteBuf reason, ChannelPromise promise);
291 
292     /**
293      * Collects statistics about the connection and notifies the {@link Future} once done.
294      *
295      * @return the {@link Future} that is notified once the stats were collected.
296      */
297     default Future<QuicConnectionStats> collectStats() {
298         return collectStats(eventLoop().newPromise());
299     }
300 
301     /**
302      * Collects statistics about the connection and notifies the {@link Promise} once done.
303      *
304      * @param   promise the {@link ChannelPromise} that is notified once the stats were collected.
305      * @return          the {@link Future} that is notified once the stats were collected.
306      */
307     Future<QuicConnectionStats> collectStats(Promise<QuicConnectionStats> promise);
308 
309     /**
310      * Collects statistics about the path of the connection and notifies the {@link Future} once done.
311      *
312      * @return the {@link Future} that is notified once the stats were collected.
313      */
314     default Future<QuicConnectionPathStats> collectPathStats(int pathIdx) {
315         return collectPathStats(pathIdx, eventLoop().newPromise());
316     }
317 
318     /**
319      * Collects statistics about the path of the connection and notifies the {@link Promise} once done.
320      *
321      * @param   promise the {@link ChannelPromise} that is notified once the stats were collected.
322      * @return          the {@link Future} that is notified once the stats were collected.
323      */
324     Future<QuicConnectionPathStats> collectPathStats(int pathIdx, Promise<QuicConnectionPathStats> promise);
325 
326     /**
327      * Creates a new {@link QuicChannelBootstrap} that can be used to create and connect new {@link QuicChannel}s to
328      * endpoints using the given {@link Channel} as transport layer.
329      *
330      * @param channel   the {@link Channel} that is used as transport layer.
331      * @return          {@link QuicChannelBootstrap} that can be used to bootstrap a client side {@link QuicChannel}.
332      */
333     static QuicChannelBootstrap newBootstrap(Channel channel) {
334         return new QuicChannelBootstrap(channel);
335     }
336 }