common

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package common provides common data structures

Index

Constants

View Source
const BitField = WireMessageType(5)

BitField is messageid for bitfield message

View Source
const Cancel = WireMessageType(8)

Cancel is messageid for a Cancel message, used to cancel a pending request

View Source
const Choke = WireMessageType(0)

Chock is message id for choke message

View Source
const Extended = WireMessageType(20)

Extended is messageid for ExtendedOptions message

View Source
const Have = WireMessageType(4)

Have is messageid for have message

View Source
const Interested = WireMessageType(2)

Interested is messageid for interested message

View Source
const Invalid = WireMessageType(255)

special for invalid

View Source
const MaxWireMessageSize = 32 * 1024
View Source
const NotInterested = WireMessageType(3)

NotInterested is messageid for not-interested message

View Source
const Piece = WireMessageType(7)

Piece is messageid for response to Request message

View Source
const Request = WireMessageType(6)

Request is messageid for piece request message

View Source
const UnChoke = WireMessageType(1)

UnChoke is message id for unchoke message

Variables

View Source
var ErrBadInfoHashLen = errors.New("bad infohash length")

ErrBadInfoHashLen is error indicating that the infohash is a bad size

View Source
var ErrBadMagnetURI = errors.New("bad magnet URI")
View Source
var ErrInvalidPiece = errors.New("invalid piece")

ErrInvalidPiece is an error for when a piece has invalid sha1sum

View Source
var ErrToBig = errors.New("message too big")
View Source
var KeepAlive = WireMessage([]byte{0, 0, 0, 0})

KeepAlive makes a WireMessage of size 0

Functions

func ReadWireMessages

func ReadWireMessages(r io.Reader, f func(WireMessage) error, msg []byte) (err error)

read wire messages from reader and call a function on each it gets reads until reader is done

Types

type Infohash

type Infohash [20]byte

Infohash is a bittorrent infohash buffer

func DecodeInfohash

func DecodeInfohash(hexstr string) (ih Infohash, err error)

DecodeInfohash decodes infohash buffer from hex string

func (Infohash) Bytes

func (ih Infohash) Bytes() []byte

Bytes gets underlying byteslice of infohash buffer

func (Infohash) Equal

func (ih Infohash) Equal(other Infohash) bool

func (Infohash) Hex

func (ih Infohash) Hex() string

Hex gets hex representation of infohash

type Peer

type Peer struct {
	Compact i2p.Base32Addr `bencode:"-"`
	IP      string         `bencode:"ip"`
	Port    int            `bencode:"port"`
	ID      PeerID         `bencode:"peer id"`
}

Peer provides info for a bittorrent swarm peer

func (*Peer) Resolve

func (p *Peer) Resolve(n network.Network) (a net.Addr, err error)

Resolve resolves network address of peer

type PeerID

type PeerID [20]byte

PeerID is a buffer for bittorrent peerid

func GeneratePeerID

func GeneratePeerID() (id PeerID)

GeneratePeerID generates a new peer id for XD

func (PeerID) Bytes

func (id PeerID) Bytes() []byte

Bytes gets buffer as byteslice

func (PeerID) String

func (id PeerID) String() string

encode to string

type PieceData

type PieceData struct {
	Index uint32
	Begin uint32
	Data  []byte
}

PieceData is a bittorrent piece response

func (*PieceData) Equals

func (pc *PieceData) Equals(other *PieceData) bool

func (PieceData) ToWireMessage

func (p PieceData) ToWireMessage() WireMessage

ToWireMessage serialize to BitTorrent wire message

type PieceRequest

type PieceRequest struct {
	Index  uint32
	Begin  uint32
	Length uint32
}

PieceRequest is a request for a bittorrent piece

func (PieceRequest) Cancel

func (pc PieceRequest) Cancel() WireMessage

func (*PieceRequest) Copy

func (pc *PieceRequest) Copy(r *PieceRequest)

func (*PieceRequest) Equals

func (r *PieceRequest) Equals(other *PieceRequest) bool

func (*PieceRequest) Matches

func (r *PieceRequest) Matches(d *PieceData) bool

return true if piecedata matches this piece request

func (PieceRequest) ToWireMessage

func (req PieceRequest) ToWireMessage() WireMessage

ToWireMessage serialize to BitTorrent wire message

type WireMessage

type WireMessage []byte

WireMessage is a serializable bittorrent wire message

func NewCancel

func NewCancel(idx, offset, length uint32) WireMessage

func NewHave

func NewHave(idx uint32) WireMessage

NewHave creates a new have message

func NewInterested

func NewInterested() WireMessage

NewInterested creates a new Interested message

func NewNotInterested

func NewNotInterested() WireMessage

NewNotInterested creates a new NotInterested message

func NewWireMessage

func NewWireMessage(id WireMessageType, bodyParts ...[]byte) (msg WireMessage)

NewWireMessage creates new wire message with id and many byteslices for the body

func (WireMessage) GetHave

func (msg WireMessage) GetHave() (h uint32)

GetHave gets the piece index of a have message

func (WireMessage) GetPieceRequest

func (msg WireMessage) GetPieceRequest() (req *PieceRequest)

GetPieceRequest gets piece request from wire message

func (WireMessage) KeepAlive

func (msg WireMessage) KeepAlive() bool

KeepAlive returns true if this message is a keepalive message

func (WireMessage) Len

func (msg WireMessage) Len() uint32

Len returns the length of the body of this message

func (WireMessage) MessageID

func (msg WireMessage) MessageID() WireMessageType

MessageID returns the id of this message

func (WireMessage) Payload

func (msg WireMessage) Payload() []byte

Payload returns a byteslice for the body of this message

func (*WireMessage) Reset

func (msg *WireMessage) Reset()

func (WireMessage) VisitPieceData

func (msg WireMessage) VisitPieceData(v func(*PieceData))

VisitPieceData gets this wire message as a PieceData if applicable

type WireMessageType

type WireMessageType byte

WireMessageType is type for wire message id

func (WireMessageType) Byte

func (t WireMessageType) Byte() byte

func (WireMessageType) String

func (t WireMessageType) String() string

String returns a string name of this wire message id

Jump to

Keyboard shortcuts

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