torrent

package
v0.0.0-...-cfffb21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2025 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
var (
	ErrPieceNotProgressedTo = errors.New("Piece not wanted or not downloaded yet")
	ErrBadPieceHash         = errors.New("failed hash check")
	ErrNoUsefulPieces       = errors.New("Peer has no pieces we want")
)

Functions

func ActualSize

func ActualSize(t *Torrent, num int) int

func PopCount

func PopCount(b byte) int

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

func New(metaInfoFileName string, shouldPrettyPrint bool) (*Torrent, error)

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

func (t *Torrent) CheckAllPieces() ([]int, error)

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

func (t *Torrent) DeletePiece(num int) error

Writes an uninitialized piece with data zeroed out to effectively delete a corrupted piece

func (*Torrent) GetPeersFromTracker

func (t *Torrent) GetPeersFromTracker() ([]string, error)

func (*Torrent) IsComplete

func (t *Torrent) IsComplete() bool

func (*Torrent) NumBytesDownloaded

func (t *Torrent) NumBytesDownloaded() int

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

func (t *Torrent) StartConns(peerList []string, userDesiredConns int) error

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.

func (*Torrent) String

func (t *Torrent) String() string

type TorrentFile

type TorrentFile struct {
	Path string

	Wanted bool
	// contains filtered or unexported fields
}

type TorrentMetaInfo

type TorrentMetaInfo struct {
	MetaInfoFileName string

	IsPrivate bool

	TotalSize int
	// contains filtered or unexported fields
}

type TorrentOptions

type TorrentOptions struct {
	UserDesiredConns int
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL