torrent

package module
v0.0.0-...-f7faaa5 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2022 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Index

Constants

View Source
const (
	BLOCKSIZE  = 1024 * 16 // block = sub-piece
	MAXBACKLOG = 5         // 一个peer协程最多同时发送5个请求
)
View Source
const (
	Reserved = 8
	HsMsgLen = Reserved + SHALEN + IDLen
)
View Source
const (
	PeerPort = 6666
	IpLen    = 4
	PortLen  = 2
	PeerLen  = IpLen + PortLen
	IDLen    = 20
)
View Source
const LenByte = 4
View Source
const SHALEN int = 20

Variables

This section is empty.

Functions

func CopyPieceData

func CopyPieceData(index int, buf []byte, msg *PeerMsg) (int, error)

CopyPieceData 把通信消息中对应分片的子分片内容拷贝到内存buf中

func Download

func Download(task *TorrentTask) error

func GetIndex

func GetIndex(msg *PeerMsg) (int, error)

GetIndex 获取消息中的信息:分片序号

func WriteHandShake

func WriteHandShake(w io.Writer, msg *HandShakeMsg) (int, error)

Types

type Bitfield

type Bitfield []byte

func (Bitfield) HasPiece

func (b Bitfield) HasPiece(index int) bool

func (Bitfield) SetPiece

func (b Bitfield) SetPiece(index int)

func (Bitfield) String

func (b Bitfield) String() string

type HandShakeMsg

type HandShakeMsg struct {
	PreStr  string
	InfoSHA [SHALEN]byte
	PeerID  [IDLen]byte
}

HandShakeMsg 握手消息格式:协议长度 + 协议名 + SHA-1哈希值 + peer_id

func NewHandShakeMsg

func NewHandShakeMsg(infoSHA [SHALEN]byte, peerID [IDLen]byte) *HandShakeMsg

func ReadHandShake

func ReadHandShake(r io.Reader) (*HandShakeMsg, error)

type MsgID

type MsgID uint8
const (
	MsgChoke       MsgID = 0
	MsgUnchoke     MsgID = 1
	MsgInterested  MsgID = 2
	MsgNotInterest MsgID = 3
	MsgHave        MsgID = 4
	MsgBitfield    MsgID = 5
	MsgRequest     MsgID = 6
	MsgPiece       MsgID = 7
	MsgCancel      MsgID = 8
)

type PeerConn

type PeerConn struct {
	net.Conn
	Choked bool
	Field  Bitfield
	// contains filtered or unexported fields
}

func NewPeerConn

func NewPeerConn(peer PeerInfo, infoSHA [SHALEN]byte, peerID [SHALEN]byte) (*PeerConn, error)

func (*PeerConn) ReadMsg

func (c *PeerConn) ReadMsg() (*PeerMsg, error)

func (*PeerConn) WriteMsg

func (c *PeerConn) WriteMsg(msg *PeerMsg) (int, error)

WriteMsg 写入格式:消息长度(id + payload) + id + payload Peer约定消息格式:前4字节是消息长度,后面1字节是消息id,再往后是消息内容

type PeerInfo

type PeerInfo struct {
	IP   net.IP
	Port uint16
}

func FindPeers

func FindPeers(tf *TorrentFile, peerID [IDLen]byte) []PeerInfo

type PeerMsg

type PeerMsg struct {
	ID      MsgID
	Payload []byte
}

func NewRequestMsg

func NewRequestMsg(index, offset, length int) *PeerMsg

type TorrentFile

type TorrentFile struct {
	Announce string
	InfoSHA  [SHALEN]byte
	FileName string
	FileLen  int
	PieceLen int
	PieceSHA [][SHALEN]byte
}

func ParseFile

func ParseFile(r io.Reader) (*TorrentFile, error)

type TorrentTask

type TorrentTask struct {
	PeerID   [IDLen]byte    // 客户端id
	PeerList []PeerInfo     // 获取到的peer列表
	InfoSHA  [SHALEN]byte   // 文件哈希值
	FileName string         // 文件名
	FileLen  int            // 文件长度
	PieceLen int            // 分片长度
	PieceSHA [][SHALEN]byte // 所有分片哈希值
}

TorrentTask 下载任务的抽象

type TrackerResp

type TrackerResp struct {
	Interval int    `bencode:"interval"`
	Peers    string `bencode:"peers"`
}

Jump to

Keyboard shortcuts

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