MonoTorrent Documentation

repository·master·Indexed 22 days ago

https://github.com/alanmcgovern/monotorrent

A comprehensive BitTorrent implementation for .NET providing both a high-performance client and a standard tracker server. It supports a wide range of BitTorrent Enhancement Proposals (BEPs), including BEP 3, BEP 5 (DHT), BEP 15 (UDP Tracker), and BEP 52 (v2). Key features include selective and sequential downloading, rarest-first piece picking, IPv4/IPv6 support, and HTTP/UDP tracker functionality. The library includes classes like TorrentFileInfo for file metadata, TrackerManager for peer discovery, and PieceHashesV2 for V2 specification hash management.

Tokens
2.1K
Snippets
0
Records
15
Agent score
73%

What's inside MonoTorrent

  1. Overview of MonoTorrent capabilities

    master

    MonoTorrent is a BitTorrent implementation that provides both client and tracker functionality.

    Client Capabilities

    • File Management: Prioritize specific files, selective downloading (including DoNotDownload status), and sequential downloading for media.
    • Performance: Rarest-first piece picking, end-game mode, in-memory caching, and incremental piece hashing to reduce disk reads.
    • Network & Connectivity: Supports IPv4, IPv6, UPnP/NAT-PMP port forwarding, and Magnet URIs.
    • Resource Control: Per-torrent and overall download/upload rate limiting, and auto-throttling based on disk write rates.
    • Data Integrity: Fast resume data support and partial hash checking (skipping files marked as DoNotDownload).

    Tracker Capabilities

    • Protocols: Supports both HTTP and UDP announce and scrape requests.
    • Efficiency: Implements compact peer responses to reduce bandwidth.
    • Flexibility: Optionally allows unregistered torrents, where the tracker begins maintaining peer lists upon the first announce request.
  2. Manage trackers with TrackerManager

    master

    The TrackerManager (implementing ITrackerManager) is responsible for managing connections to trackers for a torrent. It handles adding/removing trackers, performing announces (notifying trackers of client state), and performing scrapes (requesting information from trackers).

    Key behaviors:

    • Private Torrents: If the Private property is true, calling AddTrackerAsync or RemoveTrackerAsync will throw an InvalidOperationException.
    • Concurrency: The manager enforces concurrency limits to prevent overwhelming the engine or trackers.
    • Events: You can subscribe to AnnounceComplete and ScrapeComplete to react to tracker communication results.
  3. Supported BitTorrent Specifications (BEPs)

    master

    MonoTorrent implements various BitTorrent Enhancement Proposals (BEPs).

    Final/Active BEPs

    • BEP 3: The BitTorrent Protocol Specification
    • BEP 20: Peer ID Conventions

    Accepted BEPs

    • BEP 5: DHT Protocol
    • BEP 6: Fast Extension
    • BEP 7: IPv6 Tracker Extension
    • BEP 9: Extension for Peers to Send Metadata Files
    • BEP 10: Extension Protocol
    • BEP 11: Peer Exchange (PEX)
    • BEP 12: Multitracker Metadata Extension
    • BEP 14: Local Service/Peer Discovery
    • BEP 15: UDP Tracker Protocol
    • BEP 19: HTTP/FTP/Web Seeding
    • BEP 23: Tracker Returns Compact Peer Lists
    • BEP 27: Private Torrents

    Draft BEPs

    • BEP 16: Superseeding
    • BEP 48: Tracker Protocol Extension: Scrape
    • BEP 47: Padding files and extended file attributes
    • BEP 52: The BitTorrent Protocol Specification v2

    Others

    • Message Stream Encryption (Vuze)
  4. Access file information via TorrentFileInfo

    master

    The TorrentFileInfo class provides metadata and state for individual files within a torrent managed by MonoTorrent. It tracks file paths (complete, incomplete, and current), download progress via a BitField, and file properties like size and piece indices.

    Key properties include:

    • Path: The relative path of the file within the torrent.
    • Length: The expected size of the file once fully downloaded.
    • CachedActualLength: The actual size of the file on-disk (used during fast resume or hashing).
    • Priority: The download priority of the file (defaults to Priority.Normal).
    • FullPath, DownloadCompleteFullPath, and DownloadIncompleteFullPath: The various filesystem paths associated with the file.
    • BitField: A representation of which pieces of the file have been downloaded.
    • IncompleteFileSuffix: The suffix used for partial files, which is .!mt.
  5. Perform tracker announces

    master

    Announcing notifies the tracker of the client's current state (e.g., peer list, upload/download status). You can trigger a general announce for all trackers or a specific announce for a single tracker.

    Available methods:

    • AnnounceAsync(CancellationToken token): Triggers a regular announce cycle for all applicable trackers.
    • AnnounceAsync(TorrentEvent clientEvent, CancellationToken token): Triggers an announce associated with a specific TorrentEvent.
    • AnnounceAsync(ITracker tracker, CancellationToken token): Triggers an announce specifically for the provided ITracker.
  6. Perform tracker scrapes

    master

    Scraping requests information from the tracker (such as the number of seeders and leechers) without updating the client's state.

    Available methods:

    • ScrapeAsync(CancellationToken token): Scrapes all applicable trackers.
    • ScrapeAsync(ITracker tracker, CancellationToken token): Scrapes a specific tracker. This will throw a TorrentException if the tracker does not support scraping.
  7. Inspect Peer properties and state

    master

    The PeerId class provides access to the state and metadata of a connected peer. You can use these properties to monitor connection status, encryption, and data transfer progress.

    Key properties include:

    • IsConnected: Indicates if the peer is currently connected.
    • IsSeeder: Returns true if the peer has all pieces (based on BitField.AllTrue or Peer.IsSeeder).
    • BitField: The ReadOnlyBitField representing the pieces the peer possesses.
    • PeerID: The BEncodedString representing the remote peer's ID.
    • Uri: The Uri of the peer's connection.
    • ConnectionDirection: The direction of the connection (Direction.Incoming or Direction.Outgoing).
    • EncryptionType: The type of encryption being used by the peer.
    • PiecesSent: The number of pieces sent to this peer.
    • PiecesReceived: The number of pieces received from this peer.
    • IsRequestingPiecesCount: The number of pieces the peer is currently requesting.
    • AmChoking: Whether this client is currently choking the peer.
    • IsChoking: Whether the peer is currently choking this client.
    • AmInterested: Whether this client is interested in the peer's data.
    • IsInterested: Whether the peer is interested in this client's data.