java.lang.Object
org.klomp.snark.PeerConnectionIn
All Implemented Interfaces:
Runnable

class PeerConnectionInextends Object implements Runnable
Handles incoming BitTorrent protocol messages from a peer.
This class runs in a separate thread to continuously read and parse incoming messages, delegating them to the appropriate PeerState handlers.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Log
    Logger for this class.
    private final DataInputStream
    The stream read from the peer.
    private static final long
    Flood guard: length of the counting window.
    private final boolean
    True when running outside the router.
    (package private) long
    Last rcvd.
    private static final int
    Flood guard: maximum inbound messages per window, generous to tolerate bursts.
    private static final int
    Largest complete message accepted, in bytes.
    private final Peer
    The peer being read from.
    private boolean
    Set to end the read loop.
    private Thread
    The thread reading messages, null once stopped.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a handler for incoming messages from a peer.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    Disconnect from the peer and release resources.
    private static int
    minLengthFor(byte type)
    The minimum complete message length in bytes for a message type, including the type byte.
    void
    run()
    Read and dispatch incoming BitTorrent messages until disconnect.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • _log

      private final Log _log
      Logger for this class.
    • peer

      private final Peer peer
      The peer being read from.
    • din

      private final DataInputStream din
      The stream read from the peer.
    • MAX_MSG_SIZE

      private static final int MAX_MSG_SIZE
      Largest complete message accepted, in bytes. The biggest is the piece message, the request size (32K) plus 9; the bitfield message could exceed it only if Storage.MAX_PIECES / 8 grew past the current 625.
    • MAX_MESSAGES_PER_WINDOW

      private static final int MAX_MESSAGES_PER_WINDOW
      Flood guard: maximum inbound messages per window, generous to tolerate bursts.
      See Also:
    • FLOOD_WINDOW_MS

      private static final long FLOOD_WINDOW_MS
      Flood guard: length of the counting window.
      See Also:
    • thread

      private volatile Thread thread
      The thread reading messages, null once stopped.
    • quit

      private volatile boolean quit
      Set to end the read loop.
    • isStandalone

      private final boolean isStandalone
      True when running outside the router.
    • lastRcvd

      long lastRcvd
      Last rcvd.
  • Constructor Details

    • PeerConnectionIn

      public PeerConnectionIn(Peer peer, DataInputStream din)
      Create a handler for incoming messages from a peer.
      Parameters:
      peer - the remote peer this connection belongs to
      din - the stream positioned at the start of the peer's message stream
  • Method Details

    • minLengthFor

      private static int minLengthFor(byte type)
      The minimum complete message length in bytes for a message type, including the type byte. Messages with variable-length data (PIECE, BITFIELD, EXTENSION) have a minimum of just their fixed header.
      Parameters:
      type - the message type byte
      Returns:
      the minimum length for the given type
    • disconnect

      void disconnect()
      Disconnect from the peer and release resources.
    • run

      public void run()
      Read and dispatch incoming BitTorrent messages until disconnect.
      Specified by:
      run in interface Runnable