Documentation
¶
Index ¶
- Constants
- Variables
- func ConcatMultipleSlices[T any](slices [][]T) []T
- func CreateBitfield(bitfield []byte) []byte
- func CreateChoke() []byte
- func CreateHandshake(infoHash []byte, peerId []byte) []byte
- func CreateHave(pieceNum uint32) []byte
- func CreateInterested() []byte
- func CreatePiece(pieceNum, offset uint32, block []byte) []byte
- func CreateRequest(pieceNum, offset, length uint32) []byte
- func CreateUnchoke() []byte
- type PeerMessage
Constants ¶
const ( Choke messageKinds = iota Unchoke Interested Uninterested Have Bitfield Request Piece Cancel Port Keepalive Handshake // Not part of the BT protocol; used here to mean an incomplete "piece" message split into blocks Fragment )
Variables ¶
Functions ¶
func ConcatMultipleSlices ¶
func ConcatMultipleSlices[T any](slices [][]T) []T
Credit: https://freshman.tech/snippets/go/concatenate-slices/#concatenating-multiple-slices-at-once
func CreateBitfield ¶
<len=0001+X><id=5><bitfield>
"The bitfield message may only be sent immediately after the handshaking sequence is completed, and before any other messages are sent. It is optional, and need not be sent if a client has no pieces.
The bitfield message is variable length, where X is the length of the bitfield. The payload is a bitfield representing the pieces that have been successfully downloaded. The high bit in the first byte corresponds to piece index 0. Bits that are cleared indicated a missing piece, and set bits indicate a valid and available piece. Spare bits at the end are set to zero."
func CreateChoke ¶
func CreateChoke() []byte
func CreateHandshake ¶
<pstrlen><pstr><reserved><info_hash><peer_id>
func CreateHave ¶
have: <len=0005><id=4><piece index> "The have message is fixed length. The payload is the zero-based index of a piece that has just been successfully downloaded and verified via the hash."
func CreatePiece ¶
<len=0009+X><id=7><index><begin><block>
func CreateRequest ¶
<len=0013><id=6><index><begin><length>
func CreateUnchoke ¶
func CreateUnchoke() []byte
Types ¶
type PeerMessage ¶
type PeerMessage struct { Kind messageKinds TotalSize int PeerId []byte PieceNum int Bitfield []byte BlockOffset int BlockSize int BlockData []byte }
TODO: Move away from single type for all messages
func ParseMultiMessage ¶
func ParseMultiMessage(buf, infoHash []byte) ([]PeerMessage, error)
parseMultiMessage will parse a chunk of bytes for multiple sequential messages until empty