Error is logger for errors
var Error *log.Logger
Info is logger for information
var Info *log.Logger
MaxTry is the maximum number of times we should try to connect to a tracker
var MaxTry int = 1
ReadTimeout is the maximum time for which one must wait for the nect message from the peer. If no message arrives till this point, handshake again
var ReadTimeout time.Duration = 150
TCPTimeout is the maximum time for which one must wait for connection to a peer
var TCPTimeout time.Duration = 15
func BitFieldHandler(peer tracker.Peer, conn net.Conn, pieces *piece.PieceTracker, queue *queue.Queue, payload Payload) (err error)
BitFieldHandler handles bitfield protocol
func BuildCancel(payload parser.PieceBlock) (cancelBuf *bytes.Buffer, err error)
BuildCancel returns pointer to a buffer. Takes parser.PieceBlock object as arg
uint32 : length - Length of the remaining message = 13 uint8 : messageType - for cancel, messageType = 8 uint32 : piece index - parser.PieceBlock.Index for payload uint32 : piece begin - parser.PieceBlock.Begin for payload uint32 : piece length - parser.PieceBlock.Length for payload
func BuildChoke() (choke *bytes.Buffer, err error)
BuildChoke returns pointer to a buffer. Buffer looks like:
uint32 : length - Length of remaining part(message) = 1 uint8 : messageType - For choke, messageType = 0
func BuildHandshake(report tracker.ClientStatusReport) (handshake *bytes.Buffer, err error)
BuildHandshake returns a pointer to a buffer. Buffer looks like:
uint8 : pstrlen - Length of pstr [pstrlen]byte : pstr - pstr, the string identifier of the protocol [8]byte : reserved - 8 reserved bytes [20]byte : infohash - SHA1 hash of the info key in the metainfo file. Same as the info hash transmitted in tracker requests [20]byte : peerID - 20 byte unique ID for the client. Usually the same peerID transmitted in tracker requests
In version 1.0 of the BitTorrent protocol, pstrlen = 19, and pstr = "BitTorrent protocol"
func BuildHave(payload uint32) (have *bytes.Buffer, err error)
BuildHave returns pointer to a buffer. This takes uint32 payload(piece index) as an argument Buffer looks like:
uint32 : length - Length of remaining part(message) = 5 uint8 : messageType - for have, messageType = 4 uint32 : piece index - payload
func BuildInterested() (interested *bytes.Buffer, err error)
BuildInterested returns pointer to a buffer. Buffer looks like:
uint32 : length - Length of remaining part(message) = 1 uint8 : messageType - For interested, messageType = 2
func BuildKeepAlive() (keepAlive *bytes.Buffer)
BuildKeepAlive returns pointer to an empty buffer (4 bytes)
func BuildPort(port uint16) (portBuf *bytes.Buffer, err error)
BuildPort returns a pointer to a buffer. Takes uint16 port as arg
uint32 : length - length of remaining message = 3 uint8 : messageType - for port, messageType = 9 uint16 : port - the argument, port
func BuildRequest(payload parser.PieceBlock) (request *bytes.Buffer, err error)
BuildRequest returns pointer to a buffer. This takes parser.PieceBlock as an argument
uint32 : length - Length of remaining part(message) = 13 uint8 : messageType - for request, message = 5 uint32 : piece index - parser.PieceBlock.Index for payload uint32 : piece begin - parser.PieceBlock.Begin for payload uint32 : piece length - parser.PieceBlock.Length for payload
func BuildUnchoke() (unchoke *bytes.Buffer, err error)
BuildUnchoke returns pointer to a buffer. Buffer looks like:
uint32 : length - Length of remaining part(message) = 1 uint8 : messageType - For unchoke, messageType = 1
func BuildUninterested() (uninterested *bytes.Buffer, err error)
BuildUninterested returns pointer to a buffer. Buffer looks like:
uint32 : length - Length of remaining part(message) = 1 uint8 : messageType - For uninterested, messageType = 3
func ChokeHandler(peer tracker.Peer, conn net.Conn, pieces *piece.PieceTracker, report *tracker.ClientStatusReport)
ChokeHandler handles choking protocol
func DownloadFromFile(path string, port int)
DownloadFromFile downloads torrent from path using port
func DownloadFromPeer(peer tracker.Peer, report *tracker.ClientStatusReport, pieces *piece.PieceTracker) error
DownloadFromPeer is a function that handshakes with a peer specified by peer object. Concurrently call this function to establish parallel connections to many peers.
func GetRandomPiece() (piece parser.PieceBlock)
GetRandomPiece returns a test piece object with random data.
func HaveHandler(peer tracker.Peer, conn net.Conn, pieces *piece.PieceTracker, queue *queue.Queue, payload Payload) (pieceIndex uint32, err error)
HaveHandler handles Have protocol
func PieceHandler(peer tracker.Peer, conn net.Conn, pieces *piece.PieceTracker, queue *queue.Queue, report *tracker.ClientStatusReport, pieceResp parser.PieceBlock)
PieceHandler - TODO Write comment
func RequestPiece(peer tracker.Peer, conn net.Conn, pieces *piece.PieceTracker, queue *queue.Queue) (err error)
RequestPiece requests a piece
func UnchokeHandler(peer tracker.Peer, conn net.Conn, pieces *piece.PieceTracker, queue *queue.Queue)
UnchokeHandler handles unchoking protocol
Payload refers to the message payload that is to be sent.
type Payload map[string]interface{}
func ParseMsg(msg *bytes.Buffer) (size uint32, id uint8, payload Payload)
ParseMsg parses a message