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.ChannelHandlerContext;
20  import io.netty.channel.ChannelPromise;
21  import org.jetbrains.annotations.Nullable;
22  
23  import java.net.InetSocketAddress;
24  import java.net.SocketAddress;
25  import java.nio.ByteBuffer;
26  import java.util.concurrent.Executor;
27  import java.util.function.Consumer;
28  import java.util.function.Function;
29  
30  /**
31   * {@link QuicheQuicCodec} for QUIC clients.
32   */
33  final class QuicheQuicClientCodec extends QuicheQuicCodec {
34  
35      private final Function<QuicChannel, ? extends QuicSslEngine> sslEngineProvider;
36      private final Executor sslTaskExecutor;
37  
38      QuicheQuicClientCodec(QuicheConfig config, Function<QuicChannel, ? extends QuicSslEngine> sslEngineProvider,
39                            Executor sslTaskExecutor, int localConnIdLength, FlushStrategy flushStrategy) {
40          // Let's just use Quic.MAX_DATAGRAM_SIZE as the maximum size for a token on the client side. This should be
41          // safe enough and as we not have too many codecs at the same time this should be ok.
42          super(config, localConnIdLength, flushStrategy);
43          this.sslEngineProvider = sslEngineProvider;
44          this.sslTaskExecutor = sslTaskExecutor;
45      }
46  
47      @Override
48      @Nullable
49      protected QuicheQuicChannel quicPacketRead(
50              ChannelHandlerContext ctx, InetSocketAddress sender, InetSocketAddress recipient,
51              QuicPacketType type, long version, ByteBuf scid, ByteBuf dcid,
52              ByteBuf token, ByteBuf senderSockaddrMemory, ByteBuf recipientSockaddrMemory,
53              Consumer<QuicheQuicChannel> freeTask, int localConnIdLength, QuicheConfig config) {
54          ByteBuffer key = dcid.internalNioBuffer(dcid.readerIndex(), dcid.readableBytes());
55          return getChannel(key);
56      }
57  
58      @Override
59      protected void connectQuicChannel(QuicheQuicChannel channel, SocketAddress remoteAddress,
60                                        SocketAddress localAddress, ByteBuf senderSockaddrMemory,
61                                        ByteBuf recipientSockaddrMemory, Consumer<QuicheQuicChannel> freeTask,
62                                        int localConnIdLength, QuicheConfig config, ChannelPromise promise) {
63          try {
64              channel.connectNow(sslEngineProvider, sslTaskExecutor, freeTask, config.nativeAddress(),
65                      localConnIdLength, config.isDatagramSupported(),
66                      senderSockaddrMemory.internalNioBuffer(0, senderSockaddrMemory.capacity()),
67                      recipientSockaddrMemory.internalNioBuffer(0, recipientSockaddrMemory.capacity()));
68          } catch (Throwable cause) {
69              // Only fail the original promise. Cleanup will be done as part of the listener attached to it.
70              promise.setFailure(cause);
71              return;
72          }
73  
74          addChannel(channel);
75          channel.finishConnect();
76          promise.setSuccess();
77      }
78  }