View Javadoc
1   /*
2    * Copyright 2022 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.netty5.channel.unix;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.channel.socket.DatagramPacket;
20  import io.netty5.util.internal.ObjectUtil;
21  
22  import java.net.InetSocketAddress;
23  import java.net.SocketAddress;
24  
25  /**
26   * Allows to use <a href="https://blog.cloudflare.com/accelerating-udp-packet-transmission-for-quic/">GSO</a>
27   * if the underlying OS supports it. Before using this you should ensure your system support it.
28   */
29  public class SegmentedDatagramPacket extends DatagramPacket {
30  
31      private final int segmentSize;
32  
33      /**
34       * Create a new segmented datagram packet.
35       * The attached message may be sent in multiple segment-sized network packets.
36       *
37       * @param message The data to send.
38       * @param segmentSize The (positive) segment size.
39       * @param recipient The recipient address.
40       * @param sender The sender address.
41       */
42      public SegmentedDatagramPacket(Buffer message, int segmentSize, SocketAddress recipient,
43                                     SocketAddress sender) {
44          super(message, recipient, sender);
45          this.segmentSize = ObjectUtil.checkPositive(segmentSize, "segmentSize");
46      }
47  
48      /**
49       * Create a new segmented datagram packet.
50       * The attached message may be sent in multiple segment-sized network packets.
51       *
52       * @param message The data to send.
53       * @param segmentSize The (positive) segment size.
54       * @param recipient The recipient address.
55       */
56      public SegmentedDatagramPacket(Buffer message, int segmentSize, InetSocketAddress recipient) {
57          super(message, recipient);
58          this.segmentSize = ObjectUtil.checkPositive(segmentSize, "segmentSize");
59      }
60  
61      /**
62       * Return the size of each segment (the last segment can be smaller).
63       *
64       * @return size of segments.
65       */
66      public int segmentSize() {
67          return segmentSize;
68      }
69  
70      @Override
71      public SegmentedDatagramPacket replace(Buffer content) {
72          return new SegmentedDatagramPacket(content, segmentSize, recipient(), sender());
73      }
74  
75      @Override
76      public SegmentedDatagramPacket touch(Object hint) {
77          super.touch(hint);
78          return this;
79      }
80  }