PeerState

class PeerState(val numPieces: Int)

The per-connection choke/interest state: the Kotlin counterpart of the four m_choked / m_peer_choked / m_interesting / m_peer_interested flag bits in libtorrent's peer_connection (peer_connection.hpp), plus the peer's piece availability (m_have_piece).

libtorrent's naming is from "our" point of view and is easy to misread, so the mapping is spelled out here once:

this fieldlibtorrentmeaning
amChokingm_chokedwe are choking the remote peer
amInterestedm_interestingwe are interested in the remote peer
theirChokingm_peer_chokedthe remote peer is choking us
theirInterestedm_peer_interestedthe remote peer is interested in us

Both peers start out choked and not interested, exactly as peer_connection's constructor initialises m_choked(true) and leaves the interest bits clear.

This is a plain mutable holder with no I/O: PeerConnection flips these as messages arrive and are sent, and the session orchestrator reads them to drive the request scheduler / unchoke algorithm (which live above this class).

Constructors

Link copied to clipboard
constructor(numPieces: Int)

Properties

Link copied to clipboard

We are choking the remote peer (won't honour its requests). Starts true.

Link copied to clipboard

We are interested in the remote peer (want to request pieces). Starts false.

Link copied to clipboard

Whether we have received the peer's initial bitfield / have_all / have_none yet. libtorrent's m_bitfield_received; a second bitfield is a protocol error.

Link copied to clipboard

Number of pieces in the torrent; sizes theirBitfield.

Link copied to clipboard

The pieces the remote peer has, as last reported by its bitfield plus any subsequent have / have_all / have_none. Sized to numPieces; mirrors libtorrent's m_have_piece.

Link copied to clipboard

The remote peer is choking us (won't honour our requests). Starts true.

Link copied to clipboard

The remote peer is interested in us. Starts false.

Link copied to clipboard

Number of pieces the peer has, from theirBitfield.count(), cached cheaply.

Link copied to clipboard

True if the peer has advertised every piece (it is a seed).

Functions

Link copied to clipboard
fun theyHave(piece: Int): Boolean

Does the peer have piece? False for out-of-range indices.