java.lang.Object
net.i2p.router.transport.udp.UDPSender

public class UDPSenderextends Object
Lowest level UDP packet sender that pushes packets from its queue ASAP.

Each UDPSender instance corresponds to a UDPEndpoint, owning its own dedicated sending thread and queue. Packets to be sent are enqueued by the PacketPusher.

This class utilizes a CoDel-based priority blocking queue to limit packet queueing delay and manages bandwidth throttling for sending.

Supports graceful startup and shutdown signaling using a poison packet.

  • Field Details

    • _context

      private final RouterContext _context
      The router context.
    • _log

      private final Log _log
      The log for the sender.
    • _socket

      private final DatagramSocket _socket
      The socket to send the datagrams on.
    • _name

      private String _name
      The thread name of the sender.
    • _outboundQueue

      private final BlockingQueue<UDPPacket> _outboundQueue
      The packets waiting to be sent.
    • _keepRunning

      private volatile boolean _keepRunning
      True while the sender should keep running.
    • _runner

      private final UDPSender.Runner _runner
      The thread sending the packets.
    • _dummy

      private final boolean _dummy
      True in dummy mode, which sends nothing.
    • _endpoint

      private final SocketListener _endpoint
      The listener owning the socket.
    • TYPE_POISON

      private static final int TYPE_POISON
      Poison packet message type used to signal shutdown to sender thread.
      See Also:
    • MIN_QUEUE_SIZE

      private static final int MIN_QUEUE_SIZE
      Minimum and maximum queue sizes depend on system performance. Queue sizes are tuned to compete with NTCP bandwidth requests and to optimize CoDel behavior.
      See Also:
    • MAX_QUEUE_SIZE

      private static final int MAX_QUEUE_SIZE
      Most packets queued for sending.
      See Also:
    • CODEL_TARGET

      public static final int CODEL_TARGET
      CoDel algorithm target delay in milliseconds and interval to control pacing.
      See Also:
    • CODEL_INTERVAL

      public static final int CODEL_INTERVAL
      CoDel interval in milliseconds between checks for a packet entering the queue.
      See Also:
    • PROP_CODEL_TARGET

      public static final String PROP_CODEL_TARGET
      Property name overriding the CoDel target delay.
      See Also:
    • PROP_CODEL_INTERVAL

      public static final String PROP_CODEL_INTERVAL
      Property name overriding the CoDel check interval.
      See Also:
    • RATES

      static final long[] RATES
      Time rate intervals for statistics collection.
    • CODEL_TARGET_TTL

      private static final long CODEL_TARGET_TTL
      How long a cached PROP_CODEL_TARGET value stays valid.
      See Also:
    • _codelTarget

      private volatile int _codelTarget
      Cached CoDel target, refreshed at most once per CODEL_TARGET_TTL.
    • _codelTargetRead

      private volatile long _codelTargetRead
      When the cache was last refreshed.
  • Constructor Details

    • UDPSender

      public UDPSender(RouterContext ctx, DatagramSocket socket, String name, SocketListener lsnr)
      Create a UDPSender with the given context, socket, name, and endpoint listener. Initializes the CoDel priority queue and all relevant rate statistics trackers.
      Parameters:
      ctx - the router context
      socket - the UDP datagram socket to send packets on
      name - thread name identifier for this sender
      lsnr - the associated socket listener endpoint
  • Method Details

    • fullStats

      public boolean fullStats()
      Returns whether detailed full statistics gathering is enabled.
      Returns:
      true if full stats are enabled, false otherwise
    • startup

      public void startup()
      Starts the UDP sender thread.

      This method cannot be called more than once as the socket is final.

    • shutdown

      public void shutdown()
      Gracefully shuts down the UDP sender thread.

      Clears the outbound queue and signals the runner thread to stop by enqueueing a poison packet. Waits briefly for the queue to drain.

    • clear

      public void clear()
      Clears the outbound packet queue.

      Typically used in preparation for sending destroy notifications.

    • add

      public void add(UDPPacket packet)
      Adds a UDP packet to the send queue.

      This operation blocks if the queue is full, which may back up the PacketPusher thread. Packets that are too large or too small are dropped or warned respectively. Bandwidth requests for the packet are initiated before queueing.

      Parameters:
      packet - the UDP packet to enqueue for sending
    • codelTarget

      private int codelTarget()
      The CoDel target delay in ms, from PROP_CODEL_TARGET, read at most once per second. An unparseable value logs once per read and falls back to CODEL_TARGET, as does an unset property.
      Returns:
      the CoDel target delay in ms, never null