java.lang.Object
net.i2p.router.transport.udp.UDPPacket
All Implemented Interfaces:
CDPQEntry, CDQEntry, PQEntry

class UDPPacketextends Object implements CDPQEntry
UDP datagram wrapper implementing CDPQEntry for priority queue dispatch.
  • Field Details

    • _context

      private RouterContext _context
      The router context.
    • _packet

      private final DatagramPacket _packet
      The datagram wrapper for sending.
    • _data

      private final byte[] _data
      The packet data.
    • _priority

      private int _priority
      Send priority of the packet.
    • _initializeTime

      private volatile long _initializeTime
      When the packet was initialized.
    • _markedType

      private volatile int _markedType
      Packet type read from the header.
    • _remoteHost

      private volatile RemoteHostId _remoteHost
      The peer the packet goes to or came from.
    • _released

      private volatile boolean _released
      True once the packet is returned to the cache.
    • _enqueueTime

      private long _enqueueTime
      When the packet entered the send queue.
    • _receivedTime

      private volatile long _receivedTime
      When the packet arrived.
    • _bandwidthRequest

      private final AtomicReference<FIFOBandwidthLimiter.Request> _bandwidthRequest
      The bandwidth reservation for sending.
    • _seqNum

      private long _seqNum
      Sequence number matching replies.
    • _messageType

      private int _messageType
      I2NP message type, for the statistics.
    • _fragmentCount

      private int _fragmentCount
      Fragments this message was split into.
    • CACHE

      private static final boolean CACHE
      Developer switch enabling the packet cache.
      See Also:
    • MIN_CACHE_SIZE

      private static final int MIN_CACHE_SIZE
      Packets the cache keeps at least.
      See Also:
    • MAX_CACHE_SIZE

      private static final int MAX_CACHE_SIZE
      Packets the cache keeps at most.
      See Also:
    • _packetFactory

      private static final TryCache.ObjectFactory<UDPPacket> _packetFactory
      Factory creating the cached packets.
    • _packetCache

      private static final TryCache<UDPPacket> _packetCache
      Pool of the reusable packets.
    • MAX_PACKET_SIZE

      static final int MAX_PACKET_SIZE
      Maximum SSU packet size in bytes.
      See Also:
    • IV_SIZE

      public static final int IV_SIZE
      Size of initialization vector in bytes.
      See Also:
    • MAC_SIZE

      public static final int MAC_SIZE
      Size of MAC (Message Authentication Code) in bytes.
      See Also:
    • PAYLOAD_TYPE_SESSION_REQUEST

      public static final int PAYLOAD_TYPE_SESSION_REQUEST
      Packet type: session request.
      See Also:
    • PAYLOAD_TYPE_SESSION_CREATED

      public static final int PAYLOAD_TYPE_SESSION_CREATED
      Packet type: session created.
      See Also:
    • PAYLOAD_TYPE_SESSION_CONFIRMED

      public static final int PAYLOAD_TYPE_SESSION_CONFIRMED
      Packet type: session confirmed.
      See Also:
    • PAYLOAD_TYPE_RELAY_REQUEST

      public static final int PAYLOAD_TYPE_RELAY_REQUEST
      Packet type: relay request.
      See Also:
    • PAYLOAD_TYPE_RELAY_RESPONSE

      public static final int PAYLOAD_TYPE_RELAY_RESPONSE
      Packet type: relay response.
      See Also:
    • PAYLOAD_TYPE_RELAY_INTRO

      public static final int PAYLOAD_TYPE_RELAY_INTRO
      Packet type: relay intro.
      See Also:
    • PAYLOAD_TYPE_DATA

      public static final int PAYLOAD_TYPE_DATA
      Packet type: data.
      See Also:
    • PAYLOAD_TYPE_TEST

      public static final int PAYLOAD_TYPE_TEST
      Packet type: test.
      See Also:
    • PAYLOAD_TYPE_SESSION_DESTROY

      public static final int PAYLOAD_TYPE_SESSION_DESTROY
      Packet type: session destroy.
      See Also:
    • MAX_PAYLOAD_TYPE

      public static final int MAX_PAYLOAD_TYPE
      Largest payload type value.
      See Also:
  • Constructor Details

    • UDPPacket

      private UDPPacket(RouterContext ctx)
      Used by the packet cache factory.
      Parameters:
      ctx - RouterContext for logging and timing
  • Method Details

    • payloadTypeToString

      public static String payloadTypeToString(int type)
      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

      private void init(RouterContext ctx)
      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

      public void setSeqNum(long num)
      Packet sequence number.
      Specified by:
      setSeqNum in interface PQEntry
      Parameters:
      num - sequence number to set
    • getSeqNum

      public long getSeqNum()
      Packet sequence number.
      Specified by:
      getSeqNum in interface PQEntry
      Returns:
      the outbound packet's sequence number, counting up from zero
    • getPacket

      public DatagramPacket 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:
      getPriority in interface PQEntry
      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 the RemoteHostId representing 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:
      setEnqueueTime in interface CDQEntry
      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:
      getEnqueueTime in interface CDQEntry
      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

      public FIFOBandwidthLimiter.Request 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

      public String toString()
      Returns a detailed string representation of this packet, including address, size, priority, message and mark types, fragment counts, and timing information. For logging and debugging purposes.
      Overrides:
      toString in class Object
    • acquire

      public static UDPPacket acquire(RouterContext ctx, boolean inbound)
      Inbound flag.
      Parameters:
      ctx - the context whose clock and packet cache the packet uses
      inbound - 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

      public void drop()
      Discard the packet and release it back to the cache.
      Specified by:
      drop in interface CDQEntry