Documentation ¶
Overview ¶
All of the remaining messages in the protocol take the form of <length prefix><message ID><payload>.
The length prefix is a four byte big-endian value. The message ID is a single decimal byte. The payload is message dependent.
Message <length prefix> <message ID> <payload> Keep-alive 0000 0 none Choke 0001 0 none Unchoke 0001 1 none Interested 0001 2 none Not-interested 0001 3 none Have 0005 4 Piece index Bitfield 0001+X 5 Bitfield Request 0013 6 <index><begin><length> Piece 0009+X 7 <index><begin><block> Cancel 0013 8 <index><begin><length> port 0003 9 <listen-port>
Index ¶
- Constants
- func ParseHave(msg *Message) (int, error)
- func ParsePiece(index int, buf []byte, msg *Message) (int, error)
- type Handshake
- type Message
- type PeerConnection
- func (c *PeerConnection) DoHandshake(ih, pid []byte) (*Handshake, error)
- func (c *PeerConnection) Interested() error
- func (p *PeerConnection) Read() (*Message, error)
- func (c *PeerConnection) SendHave(idx int) error
- func (c *PeerConnection) SendRequest(index, begin, length int) error
- func (c *PeerConnection) Unchoke() error
Constants ¶
const ( KeepAliveID = 0 ChokeID = 0 UnchokeID = 1 InterestedID = 2 NotInterestedID = 3 HaveID = 4 BitfieldID = 5 RequestID = 6 PieceID = 7 CancelID = 8 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Handshake ¶
type Handshake struct { Pstr string //protocol identifier which is always BitTorrent protocol InfoHash []byte PeerID []byte //identify ourselves }
Handshake represent the tcp handshake between us and the tracker.
func Deserialize ¶
Deserialize into an handshake structure.
func NewHandshake ¶
func (*Handshake) Serialize ¶
* SerializeMsg the handshake structure into an array of bytes.
- The length of the protocol identifier, which is always 19 -> 1 byte
- The name of the protocol (BitTorrent protocol)
- Eight reserved bytes, all set to 0 -> 8 bytes
- The infohash to identify which file we want -> 20 bytes
- The Peer ID to identify ourselves -> 20 bytes 49 bytes
type Message ¶
Message represents a message sent from client to Peer
func DeserializeMsg ¶
func FormatRequest ¶
FormatRequest creates a REQUEST message
func NewHaveMessage ¶
NewHaveMessage generates a create have message
func NewInterestedMessage ¶
func NewInterestedMessage() *Message
NewInterestedMessage generates a create interested message
func NewUnchokeMessage ¶
func NewUnchokeMessage() *Message
NewUnchokeMessage generate a create unchoke message
type PeerConnection ¶
PeerConnection is a tcp connection between a Peer and us
func Dial ¶
func Dial(p peers.Peer) (*PeerConnection, error)
Dial starts a tcp connection with the given Peer.
func (*PeerConnection) DoHandshake ¶
func (c *PeerConnection) DoHandshake(ih, pid []byte) (*Handshake, error)
DoHandshake completes a two-way BitTorrent handshake
func (*PeerConnection) Interested ¶
func (c *PeerConnection) Interested() error
Interested in obtaining pieces of a file the Peer has
func (*PeerConnection) Read ¶
func (p *PeerConnection) Read() (*Message, error)
func (*PeerConnection) SendHave ¶
func (c *PeerConnection) SendHave(idx int) error
SendHave sends a Have message to the peer
func (*PeerConnection) SendRequest ¶
func (c *PeerConnection) SendRequest(index, begin, length int) error
SendRequest sends a Request message to the peer
func (*PeerConnection) Unchoke ¶
func (c *PeerConnection) Unchoke() error
Unchoke Peer. Choking is a temporary refusal to upload. It is one of BitTorrent’s most powerful idea to deal with those who only download but never upload.