View Javadoc
1   /*
2    * Copyright 2021 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.epoll.SegmentedDatagramPacket;
20  import io.netty.channel.socket.DatagramPacket;
21  import io.netty.util.internal.ObjectUtil;
22  
23  import java.net.InetSocketAddress;
24  
25  /**
26   * Class that provides utility methods to setup {@code QUIC} when using the {@code EPOLL} transport.
27   */
28  public final class EpollQuicUtils {
29  
30      private EpollQuicUtils() { }
31  
32      /**
33       * Return a new {@link SegmentedDatagramPacketAllocator} that can be used while using
34       * {@link io.netty.channel.epoll.EpollDatagramChannel}.
35       *
36       * @param maxNumSegments the maximum number of segments that we try to send in one packet.
37       * @return a allocator.
38       */
39      public static SegmentedDatagramPacketAllocator newSegmentedAllocator(int maxNumSegments) {
40          ObjectUtil.checkInRange(maxNumSegments, 1, 64, "maxNumSegments");
41          if (SegmentedDatagramPacket.isSupported()) {
42              return new EpollSegmentedDatagramPacketAllocator(maxNumSegments);
43          }
44          return SegmentedDatagramPacketAllocator.NONE;
45      }
46  
47      private static final class EpollSegmentedDatagramPacketAllocator implements SegmentedDatagramPacketAllocator {
48  
49          private final int maxNumSegments;
50  
51          EpollSegmentedDatagramPacketAllocator(int maxNumSegments) {
52              this.maxNumSegments = maxNumSegments;
53          }
54  
55          @Override
56          public int maxNumSegments() {
57              return maxNumSegments;
58          }
59  
60          @Override
61          public DatagramPacket newPacket(ByteBuf buffer, int segmentSize, InetSocketAddress remoteAddress) {
62              return new io.netty.channel.unix.SegmentedDatagramPacket(buffer, segmentSize, remoteAddress);
63          }
64      }
65  }