java.lang.Object
net.i2p.router.transport.udp.UDPPacket
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classFactory for UDP packet caching.Field Summary
Modifier and TypeFieldDescriptionprivate final AtomicReference<FIFOBandwidthLimiter.Request> The bandwidth reservation for sending.private RouterContextThe router context.private final byte[]The packet data.private longWhen the packet entered the send queue.private intFragments this message was split into.private longWhen the packet was initialized.private intPacket type read from the header.private intI2NP message type, for the statistics.private final DatagramPacketThe datagram wrapper for sending.Pool of the reusable packets.private static final TryCache.ObjectFactory<UDPPacket> Factory creating the cached packets.private intSend priority of the packet.private longWhen the packet arrived.private booleanTrue once the packet is returned to the cache.private RemoteHostIdThe peer the packet goes to or came from.private longSequence number matching replies.private static final booleanDeveloper switch enabling the packet cache.static final intSize of initialization vector in bytes.static final intSize of MAC (Message Authentication Code) in bytes.private static final intPackets the cache keeps at most.(package private) static final intMaximum SSU packet size in bytes.static final intLargest payload type value.private static final intPackets the cache keeps at least.static final intPacket type: data.static final intPacket type: relay intro.static final intPacket type: relay request.static final intPacket type: relay response.static final intPacket type: session confirmed.static final intPacket type: session created.static final intPacket type: session destroy.static final intPacket type: session request.static final intPacket type: test.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic UDPPacketacquire(RouterContext ctx, boolean inbound) Inbound flag.static voidClear cache.voiddrop()Discard the packet and release it back to the cache.Bandwidth request.longgetBegin()Initialization time.longEnqueue time.(package private) intFragment count.longPacket lifetime.intMarked type.(package private) intMessage type.Underlying packet.intSend priority.(package private) RemoteHostIdLazily obtains theRemoteHostIdrepresenting the sender's address and port.longPacket sequence number.(package private) longReturns the amount of time elapsed since the packet was received.private voidinit(RouterContext ctx) Initialize or reset the packet state for reuse.voidmarkType(int type) Mark type.static StringpayloadTypeToString(int type) Packet type enumeration.(package private) voidreceived()Marks the packet as received, recording the current time.static voidReduce cache size.voidrelease()Return the packet to the object pool for reuse.voidRequest outbound bandwidth.voidReset initialization time.voidsetEnqueueTime(long now) Enqueue timestamp.(package private) voidsetFragmentCount(int count) Fragment count.(package private) voidsetMessageType(int type) Message type.voidsetPriority(int pri) Priority level.voidsetSeqNum(long num) Packet sequence number.toString()Returns a detailed string representation of this packet, including address, size, priority, message and mark types, fragment counts, and timing information.private voidLogs an error if an operation is attempted on a packet that has already been released.
Field Details
_context
The router context._packet
The datagram wrapper for sending._data
private final byte[] _dataThe packet data._priority
private int _prioritySend priority of the packet._initializeTime
private volatile long _initializeTimeWhen the packet was initialized._markedType
private volatile int _markedTypePacket type read from the header._remoteHost
The peer the packet goes to or came from._released
private volatile boolean _releasedTrue once the packet is returned to the cache._enqueueTime
private long _enqueueTimeWhen the packet entered the send queue._receivedTime
private volatile long _receivedTimeWhen the packet arrived._bandwidthRequest
The bandwidth reservation for sending._seqNum
private long _seqNumSequence number matching replies._messageType
private int _messageTypeI2NP message type, for the statistics._fragmentCount
private int _fragmentCountFragments this message was split into.CACHE
private static final boolean CACHEDeveloper switch enabling the packet cache.- See Also:
MIN_CACHE_SIZE
private static final int MIN_CACHE_SIZEPackets the cache keeps at least.- See Also:
MAX_CACHE_SIZE
private static final int MAX_CACHE_SIZEPackets the cache keeps at most.- See Also:
_packetFactory
Factory creating the cached packets._packetCache
MAX_PACKET_SIZE
IV_SIZE
public static final int IV_SIZESize of initialization vector in bytes.- See Also:
MAC_SIZE
public static final int MAC_SIZESize of MAC (Message Authentication Code) in bytes.- See Also:
PAYLOAD_TYPE_SESSION_REQUEST
public static final int PAYLOAD_TYPE_SESSION_REQUESTPacket type: session request.- See Also:
PAYLOAD_TYPE_SESSION_CREATED
public static final int PAYLOAD_TYPE_SESSION_CREATEDPacket type: session created.- See Also:
PAYLOAD_TYPE_SESSION_CONFIRMED
public static final int PAYLOAD_TYPE_SESSION_CONFIRMEDPacket type: session confirmed.- See Also:
PAYLOAD_TYPE_RELAY_REQUEST
public static final int PAYLOAD_TYPE_RELAY_REQUESTPacket type: relay request.- See Also:
PAYLOAD_TYPE_RELAY_RESPONSE
public static final int PAYLOAD_TYPE_RELAY_RESPONSEPacket type: relay response.- See Also:
PAYLOAD_TYPE_RELAY_INTRO
public static final int PAYLOAD_TYPE_RELAY_INTROPacket type: relay intro.- See Also:
PAYLOAD_TYPE_DATA
PAYLOAD_TYPE_TEST
PAYLOAD_TYPE_SESSION_DESTROY
public static final int PAYLOAD_TYPE_SESSION_DESTROYPacket type: session destroy.- See Also:
MAX_PAYLOAD_TYPE
Constructor Details
UDPPacket
Used by the packet cache factory.- Parameters:
ctx- RouterContext for logging and timing
Method Details
payloadTypeToString
Packet type enumeration.- Parameters:
type- one of the PAYLOAD_TYPE_* constants- Returns:
- the human-readable name for the type, or a string naming the unrecognised value if it is not a known payload type
init
Initialize or reset the packet state for reuse. Resets metadata and timestamps. Data buffer is not zeroed — callers fully overwrite before send and the DatagramPacket length limits reads.- Parameters:
ctx- RouterContext used for timing and logging
setSeqNum
getSeqNum
getPacket
Underlying packet.- Returns:
- the datagram carrying the payload, reflecting the buffer and length currently in use
getPriority
public int getPriority()Send priority.- Specified by:
getPriorityin interfacePQEntry- Returns:
- the priority value
setPriority
public void setPriority(int pri) Priority level.- Parameters:
pri- the send priority on the OutNetMessage scale of 100 to 1000, where a larger value means a more urgent packet
getBegin
public long getBegin()Initialization time.- Returns:
- the router clock time at which this packet was created or reset
getLifetime
public long getLifetime()Packet lifetime.- Returns:
- milliseconds elapsed on the router clock since initialization
resetBegin
public void resetBegin()Reset initialization time.markType
public void markType(int type) Mark type.- Parameters:
type- 1 to flag the packet as an ack, which lets the sender record ack send timings separately; any other value is simply stored
getMarkedType
public int getMarkedType()Marked type.- Returns:
- the mark set by
markType(int), or -1 if this packet has not been marked
getMessageType
int getMessageType()Message type.- Returns:
- the SSU2 message type this packet carries, or -1 when none has been set yet
setMessageType
void setMessageType(int type) Message type.- Parameters:
type- the SSU2 message type being carried, recorded for debug logging
getFragmentCount
int getFragmentCount()Fragment count.- Returns:
- the number of data fragments this packet carries, or -1 when it is an ack rather than a data fragment
setFragmentCount
void setFragmentCount(int count) Fragment count.- Parameters:
count- the number of data fragments this packet carries, or -1 to mark it as an ack with no fragment data
getRemoteHost
RemoteHostId getRemoteHost()Lazily obtains theRemoteHostIdrepresenting the sender's address and port. This is initialized once per packet and cached thereafter.- Returns:
- remote host identifier or null if address unavailable
setEnqueueTime
public void setEnqueueTime(long now) Enqueue timestamp.- Specified by:
setEnqueueTimein interfaceCDQEntry- Parameters:
now- the enqueue timestamp
received
void received()Marks the packet as received, recording the current time.getEnqueueTime
public long getEnqueueTime()Enqueue time.- Specified by:
getEnqueueTimein interfaceCDQEntry- Returns:
- the enqueue timestamp
getTimeSinceReceived
long getTimeSinceReceived()Returns the amount of time elapsed since the packet was received.- Returns:
- milliseconds since received, or 0 if never received
requestOutboundBandwidth
public void requestOutboundBandwidth()Request outbound bandwidth.getBandwidthRequest
Bandwidth request.- Returns:
- the limiter request recorded by
requestOutboundBandwidth(), or null if no bandwidth has been requested for this packet yet
release
public void release()Return the packet to the object pool for reuse.clearCache
public static void clearCache()Clear cache.reduceCacheSize
public static void reduceCacheSize()Reduce cache size.verifyNotReleased
private void verifyNotReleased()Logs an error if an operation is attempted on a packet that has already been released. This helps prevent bugs caused by reusing or accessing stale packet instances.toString
acquire
Inbound flag.- Parameters:
ctx- the context whose clock and packet cache the packet usesinbound- true if the packet is being prepared to be received- Returns:
- a pooled packet with its state initialized for ctx, ready to be filled in and sent or received
drop