Supports dynamic thread count adjustment via adjustThreads().
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classThread processing the queued packets.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicIntegerHandler threads currently reading.private final RouterContextThe router context.private final EstablishmentManagerHandler of the peer setup packets.private final CopyOnWriteArrayList<PacketHandler.Handler> The running handler threads.private final BlockingQueue<UDPPacket> Packets waiting to be processed.private booleanTrue while the handler threads keep reading.private final LogLogger for the packet handler.private static intMost handler threads allowed.private final intNetwork id the packets must match.private final AtomicIntegerPackets being processed right now.private final PeerTestManagerHandler of the peer test packets.private static final AtomicIntegerSequence number of the handler threads.private final UDPTransportThe transport the packets belong to.private static final intLargest packet queue allowed.private static final intNewest SSU version accepted.private static final intSmallest packet queue allowed.private static final intOldest SSU version accepted.private static final intQueue entry type that stops a handler.Constructor Summary
ConstructorsConstructorDescriptionPacketHandler(RouterContext ctx, UDPTransport transport, EstablishmentManager establisher, InboundMessageFragments inbound, PeerTestManager testManager, IntroductionManager introManager) Create a new packet handler.Method Summary
Modifier and TypeMethodDescription(package private) voidDynamically adjust handler thread count to match the target.private voidcountBadPacket(RemoteHostId from) Count a malformed/unrecognized SSU2 packet from a source that has no established or pending session (e.g.intReturns the current number of active handler threads.(package private) StringThe handler status string.static intReturns the current max packet handler threads.intReturns the number of handlers actively processing packets (not parked on take()).doublePacket handler pool utilization as a ratio (0.0-1.0).voidqueueReceived(UDPPacket packet) Queue a received packet for processing.Blocking call to retrieve the next inbound packet, or null if we have shut down.private booleanreceiveSSU2Packet(RemoteHostId from, UDPPacket packet, InboundEstablishState2 state) Decrypt the header and hand off to the state for processing.private booleanreceiveSSU2Packet(UDPPacket packet, OutboundEstablishState2 state) Decrypt the header and hand off to the state for processing.static voidsetMaxHandlers(int handlers) The target max packet handler threads.voidshutdown()Shut down the packet handler.voidstartup()Start the packet handler.private voidBlocking for a while.
Field Details
_context
The router context._log
Logger for the packet handler._transport
The transport the packets belong to._establisher
Handler of the peer setup packets._testManager
Handler of the peer test packets._keepReading
private volatile boolean _keepReadingTrue while the handler threads keep reading._handlers
The running handler threads._activeHandlers
Handler threads currently reading._processingCount
Packets being processed right now._inboundQueue
Packets waiting to be processed._networkID
private final int _networkIDNetwork id the packets must match.TYPE_POISON
private static final int TYPE_POISONQueue entry type that stops a handler.- See Also:
MIN_QUEUE_SIZE
private static final int MIN_QUEUE_SIZESmallest packet queue allowed.MAX_QUEUE_SIZE
private static final int MAX_QUEUE_SIZELargest packet queue allowed._maxHandlers
private static volatile int _maxHandlersMost handler threads allowed._threadNum
Sequence number of the handler threads.MIN_VERSION
MAX_VERSION
Constructor Details
PacketHandler
PacketHandler(RouterContext ctx, UDPTransport transport, EstablishmentManager establisher, InboundMessageFragments inbound, PeerTestManager testManager, IntroductionManager introManager) Create a new packet handler.- Parameters:
ctx- the router contexttransport- the UDP transportestablisher- the establishment managerinbound- the inbound message fragmentstestManager- the peer test managerintroManager- the introduction manager
Method Details
getMaxHandlers
public static int getMaxHandlers()Returns the current max packet handler threads.- Returns:
- the max handlers
setMaxHandlers
public static void setMaxHandlers(int handlers) The target max packet handler threads. Takes effect immediately viaadjustThreads().- Parameters:
handlers- the new max handlers
getActiveHandlers
public int getActiveHandlers()Returns the current number of active handler threads.- Returns:
- the active handler count
getProcessingCount
public int getProcessingCount()Returns the number of handlers actively processing packets (not parked on take()).- Returns:
- the processing count
getUtilization
public double getUtilization()Packet handler pool utilization as a ratio (0.0-1.0). Returns NaN if not started.- Returns:
- the utilization ratio
startup
public void startup()Start the packet handler.shutdown
public void shutdown()Shut down the packet handler.adjustThreads
void adjustThreads()Dynamically adjust handler thread count to match the target. Handlers are added or removed one at a time. Excess handlers receive a poison packet and exit cleanly.getHandlerStatus
String getHandlerStatus()The handler status string.- Returns:
- a summary listing how many handlers are currently active, followed by one "handler i" entry per handler in the pool
queueReceived
Queue a received packet for processing.- Parameters:
packet- the received packet to hand to the inbound thread- Throws:
InterruptedException- if interrupted while waiting
stopQueue
private void stopQueue()Blocking for a while.receiveNext
Blocking call to retrieve the next inbound packet, or null if we have shut down.- Returns:
- the next packet, or null if shut down
receiveSSU2Packet
private boolean receiveSSU2Packet(RemoteHostId from, UDPPacket packet, InboundEstablishState2 state) Decrypt the header and hand off to the state for processing. Packet is trial-decrypted, so fallback processing is possible if this returns false.Possible messages here are Session Request, Token Request, Session Confirmed, or Peer Test. Data messages out-of-order from Session Confirmed, or following a Session Confirmed that was lost, or in-order but before the Session Confirmed was processed, will handed to the state to be queued for deferred handling.
Min packet data size: 56 (token request) if state is null; 40 (data) if state is non-null.
- Parameters:
from- the remote host the packet came frompacket- the received packetstate- must be version 2, but will be null for session request unless retransmitted- Returns:
- true if the header was validated as a SSU2 packet, cannot fallback to SSU 1
receiveSSU2Packet
Decrypt the header and hand off to the state for processing. Packet is trial-decrypted, so fallback processing is possible if this returns false. But that's probably not necessary.Possible messages here are Session Created or Retry
Min packet data size: 56 (retry).
- Parameters:
packet- the received packetstate- must be version 2, non-null- Returns:
- true if the header was validated as a SSU2 packet, cannot fallback to SSU 1
countBadPacket
Count a malformed/unrecognized SSU2 packet from a source that has no established or pending session (e.g. a BAD-Destination-ConnectionID flood). Feeds the optionalBanlist.badPacket(String, String)offender tracker, which auto-blocklists an IP once it exceedsrouter.banlist.maxOffenseswithin the offense window.Only direct (IP+port) senders are tracked; a hash-only peer with no reachable address cannot be blocklisted by IP, so it is skipped. The policy is self-limiting (startup grace + maxOffenses threshold), so sporadic malformed packets from a legitimate peer are harmless.
- Parameters:
from- the source of the bad packet, never null