Documentation
¶
Index ¶
- Constants
- Variables
- func ActualSize(t *Torrent, num int) int
- func PopCount(b byte) int
- type Torrent
- func (t *Torrent) CheckAllPieces() ([]int, error)
- func (t *Torrent) CreateFiles() ([]*TorrentFile, error)
- func (t *Torrent) DeletePiece(num int) error
- func (t *Torrent) GetPeersFromTracker() ([]string, error)
- func (t *Torrent) IsComplete() bool
- func (t *Torrent) NumBytesDownloaded() int
- func (t *Torrent) SetWantedBitfield()
- func (t *Torrent) StartConns(peerList []string, userDesiredConns int) error
- func (t *Torrent) String() string
- type TorrentFile
- type TorrentMetaInfo
- type TorrentOptions
Constants ¶
const ( CLIENT_BLOCK_SIZE = 16 * 1024 PIECE_MESSAGE_HEADER_SIZE = 13 MAX_MESSAGE_SIZE = CLIENT_BLOCK_SIZE + PIECE_MESSAGE_HEADER_SIZE /* "Implementer's Note: Even 30 peers is plenty, the official client version 3 in fact only actively forms new connections if it has less than 30 peers and will refuse connections if it has 55. This value is important to performance. When a new piece has completed download, HAVE messages (see below) will need to be sent to most active peers. As a result the cost of broadcast traffic grows in direct proportion to the number of peers. Above 25, new peers are highly unlikely to increase download speed. UI designers are strongly advised to make this obscure and hard to change as it is very rare to be useful to do so." */ EFFECTIVE_MAX_PEER_CONNS = 25 DIAL_TIMEOUT = 3 * time.Second // Generally 2 minutes: https://wiki.theory.org/BitTorrentSpecification#keep-alive:_.3Clen.3D0000.3E MESSAGE_TIMEOUT = 5 * time.Second // Decides how long handleConn waits for incoming messages (from the peer) // before checking for outbound messages (from chooseResponse) // or whether MESSAGE_TIMEOUT has expired // Note: directly affects throughput CONN_READ_INTERVAL = 100 * time.Millisecond CLIENT_PEER_ID = "edededededededededed" )
Variables ¶
Functions ¶
func ActualSize ¶
Types ¶
type Torrent ¶
type Torrent struct { TorrentMetaInfo TorrentOptions NumBytesUploaded int Bitfield []byte Files []*TorrentFile Seeders int Leechers int sync.Mutex Logbuf bytes.Buffer Logger slog.Logger // contains filtered or unexported fields }
func New ¶
Initializes the Torrent state with nil file descriptors and no notion of which files are wanted (user input has not been read yet)
func (*Torrent) CheckAllPieces ¶
Reads and verifies existing data on disk when the user adds a torrent.
Also sets the number of pieces downloaded so far, which is used for reporting to the tracker and to determine if the torrent is complete.
TODO: Benchmark a multithreaded version
func (*Torrent) CreateFiles ¶
func (t *Torrent) CreateFiles() ([]*TorrentFile, error)
Creates and initializes file descriptors for all files, even unwanted ones, in case a piece crosses file boundaries and needs to write to/read from the subsequent file.
NOTE: Prior to calling this, torrentFiles have a nil file descriptior.
func (*Torrent) DeletePiece ¶
Writes an uninitialized piece with data zeroed out to effectively delete a corrupted piece
func (*Torrent) GetPeersFromTracker ¶
func (*Torrent) IsComplete ¶
func (*Torrent) NumBytesDownloaded ¶
func (*Torrent) SetWantedBitfield ¶
func (t *Torrent) SetWantedBitfield()
Populates a bitfield representing the pieces the user wants. Doesn't need to acquire a lock because it only runs on initialization (i.e. we don't support user changing desired pieces mid-download)
func (*Torrent) StartConns ¶
StartConns sends the "started" message to the tracker and then runs for the lifetime of the program.
Will attempt to start as many connections as desired by the user, as long as there are enough peers in the swarm.
It also kicks off a separate connection listener goroutine.
type TorrentFile ¶
type TorrentMetaInfo ¶
type TorrentOptions ¶
type TorrentOptions struct {
UserDesiredConns int
}