downloader

package
v0.0.0-...-e294334 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package downloader is used to download the torrent or the real file from the peer node by the peer wire protocol.

Index

Constants

View Source
const BlockSize = 16384 // 16KiB.

BlockSize is the size of a block of the piece.

Variables

This section is empty.

Functions

This section is empty.

Types

type TorrentDownloader

type TorrentDownloader struct {
	// contains filtered or unexported fields
}

TorrentDownloader is used to download the torrent file from the peer.

func NewTorrentDownloader

func NewTorrentDownloader(c ...TorrentDownloaderConfig) *TorrentDownloader

NewTorrentDownloader returns a new TorrentDownloader.

If id is ZERO, it is reset to a random id. workerNum is 128 by default.

func (*TorrentDownloader) Close

func (d *TorrentDownloader) Close()

Close closes the downloader and releases the underlying resources.

func (*TorrentDownloader) OnDHTNode

func (d *TorrentDownloader) OnDHTNode(cb func(host string, port uint16))

OnDHTNode sets the DHT node callback, which will enable DHT extenstion bit.

In the callback function, you maybe ping it like DHT by UDP. If the node responds, you can add the node in DHT routing table.

BEP 5

func (*TorrentDownloader) Request

func (d *TorrentDownloader) Request(host string, port uint16, infohash metainfo.Hash)

Request submits a download request.

Notice: the remote peer must support the "ut_metadata" extenstion. Or downloading fails.

func (*TorrentDownloader) Response

func (d *TorrentDownloader) Response() <-chan TorrentResponse

Response returns a response channel to get the downloaded torrent info.

type TorrentDownloaderConfig

type TorrentDownloaderConfig struct {
	// ID is the id of the downloader peer node.
	//
	// The default is a random id.
	ID metainfo.Hash

	// WorkerNum is the number of the worker downloading the torrent concurrently.
	//
	// The default is 128.
	WorkerNum int

	// DialTimeout is the timeout used by dialing to the peer on TCP.
	DialTimeout time.Duration

	// ErrorLog is used to log the error.
	//
	// The default is log.Printf.
	ErrorLog func(format string, args ...interface{})
}

TorrentDownloaderConfig is used to configure the TorrentDownloader.

type TorrentResponse

type TorrentResponse struct {
	Host      string        // Which host the torrent is downloaded from
	Port      uint16        // Which port the torrent is downloaded from
	PeerID    metainfo.Hash // ID of the peer where torrent is downloaded from
	InfoHash  metainfo.Hash // The SHA-1 hash of the torrent to be downloaded
	InfoBytes []byte        // The content of the info part in the torrent
}

TorrentResponse represents a torrent info response.

Jump to

Keyboard shortcuts

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