UdpTracker

class UdpTracker(udp: UdpSocket, random: Random = Random.Default, maxAttempts: Int = 4, baseTimeoutMs: Long, connectionIdTtlMs: Long, nowMs: () -> Long = { 0L })

A BEP-15 UDP tracker client: the live half of libtorrent's udp_tracker_connection (src/udp_tracker_connection.cpp), driven over the proven UdpSocket. The fixed-layout packet build/parse lives in UdpTrackerCodec; this class owns the transaction: the mandatory two-step connect→announce handshake, the random transaction ids, connection-id caching, and the "ignore datagrams that aren't ours" filtering.

libtorrent retransmits a UDP request a few times with a growing timeout before giving up (tracker_completion_timeout / tracker_receive_timeout). We do the same: each request is attempted up to maxAttempts times, with the per-attempt receive bounded by an exponentially growing timeout starting at baseTimeoutMs. That is the BEP-15 recommended 15 * 2^n back-off, capped so a single announce cannot block the caller indefinitely. Cancellation propagates through structured concurrency: cancel the calling coroutine and every receive() unwinds.

BEP-15 caches a tracker's connection_id for ~60 seconds; libtorrent keeps a process-wide m_connection_cache keyed by tracker address. We keep an instance-level cache keyed by host:port, refreshed by connect and reused by announce/scrape until it expires after connectionIdTtlMs.

Constructors

Link copied to clipboard
constructor(udp: UdpSocket, random: Random = Random.Default, maxAttempts: Int = 4, baseTimeoutMs: Long, connectionIdTtlMs: Long, nowMs: () -> Long = { 0L })

Functions

Link copied to clipboard
suspend fun announce(host: String, port: Int, req: AnnounceRequest): AnnounceResponse

Announce to the UDP tracker at host:port, performing the BEP-15 connect→announce two-step (reusing a cached connection id when one is still valid), and return the parsed AnnounceResponse.

Link copied to clipboard
suspend fun scrape(host: String, port: Int, req: AnnounceRequest): ScrapeResponse

Scrape the swarm statistics for req's info-hash from the UDP tracker at host:port. Mirrors send_udp_scrapeon_scrape_response.