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.channel.unix;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.socket.DatagramPacket;
20  import io.netty.util.internal.ObjectUtil;
21  
22  import java.net.InetSocketAddress;
23  
24  /**
25   * Allows to use <a href="https://blog.cloudflare.com/accelerating-udp-packet-transmission-for-quic/">GSO</a>
26   * if the underlying OS supports it. Before using this you should ensure your system support it.
27   */
28  public class SegmentedDatagramPacket extends DatagramPacket {
29  
30      private final int segmentSize;
31  
32      /**
33       * Create a new instance.
34       *
35       * @param data          the {@link ByteBuf} which must be continguous.
36       * @param segmentSize   the segment size.
37       * @param recipient     the recipient.
38       */
39      public SegmentedDatagramPacket(ByteBuf data, int segmentSize, InetSocketAddress recipient) {
40          super(data, recipient);
41          this.segmentSize = ObjectUtil.checkPositive(segmentSize, "segmentSize");
42      }
43  
44      /**
45       * Create a new instance.
46       *
47       * @param data          the {@link ByteBuf} which must be continguous.
48       * @param segmentSize   the segment size.
49       * @param recipient     the recipient.
50       */
51      public SegmentedDatagramPacket(ByteBuf data, int segmentSize,
52                                     InetSocketAddress recipient, InetSocketAddress sender) {
53          super(data, recipient, sender);
54          this.segmentSize = ObjectUtil.checkPositive(segmentSize, "segmentSize");
55      }
56  
57      /**
58       * Return the size of each segment (the last segment can be smaller).
59       *
60       * @return size of segments.
61       */
62      public int segmentSize() {
63          return segmentSize;
64      }
65  
66      @Override
67      public SegmentedDatagramPacket copy() {
68          return new SegmentedDatagramPacket(content().copy(), segmentSize, recipient(), sender());
69      }
70  
71      @Override
72      public SegmentedDatagramPacket duplicate() {
73          return new SegmentedDatagramPacket(content().duplicate(), segmentSize, recipient(), sender());
74      }
75  
76      @Override
77      public SegmentedDatagramPacket retainedDuplicate() {
78          return new SegmentedDatagramPacket(content().retainedDuplicate(), segmentSize, recipient(), sender());
79      }
80  
81      @Override
82      public SegmentedDatagramPacket replace(ByteBuf content) {
83          return new SegmentedDatagramPacket(content, segmentSize, recipient(), sender());
84      }
85  
86      @Override
87      public SegmentedDatagramPacket retain() {
88          super.retain();
89          return this;
90      }
91  
92      @Override
93      public SegmentedDatagramPacket retain(int increment) {
94          super.retain(increment);
95          return this;
96      }
97  
98      @Override
99      public SegmentedDatagramPacket touch() {
100         super.touch();
101         return this;
102     }
103 
104     @Override
105     public SegmentedDatagramPacket touch(Object hint) {
106         super.touch(hint);
107         return this;
108     }
109 }