Class PacketBuilder
java.lang.Object
io.github.mahmoudimus.quic.packet.PacketBuilder
Builds a datagram of one or more coalesced packets: write a header, fill the frame space, then
pad and protect. A client's Initial datagram must reach 1200 bytes
(RFC 9000 section 14.1), which
finish(int) pads to.
PacketBuilder b = new PacketBuilder(ByteBuffer.allocate(1500), 1500);
ByteBuffer frames = b.begin(new PacketHeader.Initial(V1, dcid, scid, new byte[0], 0, 1), keys.local());
Frame.Crypto.encode(frames, 0, clientHello, frames.remaining());
b.finish(PacketBuilder.MIN_INITIAL_DATAGRAM);
ByteBuffer datagram = b.datagram();
Instances are not thread-safe.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe smallest datagram that may carry a client's Initial packet (RFC 9000 section 14.1). -
Constructor Summary
ConstructorsConstructorDescriptionPacketBuilder(ByteBuffer buffer, int maxDatagramSize) A builder that writes from the buffer's position. -
Method Summary
Modifier and TypeMethodDescriptionbegin(PacketHeader.Numbered header, DirectionalKeys keys) Starts a packet after any already finished in this datagram.datagram()The datagram built so far: every finished packet.intfinish()Finishes the current packet without padding beyond what header protection needs.intfinish(int minDatagramSize) Finishes the current packet: pads it so that header protection can sample it and, ifminDatagramSizeasks for it, so that the datagram reaches that size; then protects it.intThe frame space left in the current packet.
-
Field Details
-
MIN_INITIAL_DATAGRAM
public static final int MIN_INITIAL_DATAGRAMThe smallest datagram that may carry a client's Initial packet (RFC 9000 section 14.1).- See Also:
-
-
Constructor Details
-
PacketBuilder
A builder that writes from the buffer's position.- Parameters:
buffer- the datagram buffer; packets are written from its position onmaxDatagramSize- the largest datagram to build, such as the path MTU's UDP payload
-
-
Method Details
-
begin
Starts a packet after any already finished in this datagram.- Parameters:
header- the header, with the packet number to sendkeys- the sender's keys for the header's level- Returns:
- the frame space: a buffer whose position is where frames go and whose limit leaves room for the AEAD tag; write frames at its position
- Throws:
IllegalStateException- if a packet is already in progressBufferOverflowException- if the header does not fit; the builder is unchanged
-
remaining
public int remaining()The frame space left in the current packet.- Returns:
- the bytes that frames may still use
-
finish
public int finish(int minDatagramSize) Finishes the current packet: pads it so that header protection can sample it and, ifminDatagramSizeasks for it, so that the datagram reaches that size; then protects it.- Parameters:
minDatagramSize- the size the datagram must reach with this packet, or 0- Returns:
- the index just past the protected packet
- Throws:
IllegalStateException- if no packet is in progress or the padding does not fit
-
finish
public int finish()Finishes the current packet without padding beyond what header protection needs.- Returns:
- the index just past the protected packet
-
datagram
The datagram built so far: every finished packet.- Returns:
- a view from the first packet to the end of the last finished one
-