p2p

package
v0.0.0-...-8c5f22a Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ChunkSize = 16 * 1024

	HandshakeMessageLength               = 68
	ReadDeadline           time.Duration = time.Second * 10
	WriteDeadline          time.Duration = time.Second * 10
)

Variables

View Source
var (
	ErrInvalidHandshakeFormat = errors.New("p2p: invalid handshake format")
	ErrNoPeers                = errors.New("p2p: no peers")
	ErrWriteConn              = errors.New("p2p: failed to write to the connection")
	ErrPieceNotFound          = errors.New("p2p: downloaded piece was not found in the buffer") // Should technically never happen?
	ErrInvalidPieceHash       = errors.New("p2p: invalid downloaded piece hash")
	ErrNoCommand              = errors.New("p2p: command from the connection was nil")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the structure for receiving and sending commands between bittorrent clients.

func NewClient

func NewClient(log *slog.Logger, peerID []byte, torrent *bencode.Torrent) (*Client, error)

NewClient returns a new client that immediately tries to initiate a handshake with the peer.

func (*Client) Announce

func (c *Client) Announce(ctx context.Context) (*bencode.AnnounceResponse, error)

Announce sends the request to the tracker to get the latest announce message.

func (*Client) Close

func (c *Client) Close() error

Close closes all of the clients connections and stops the refetch announce goroutine.

func (*Client) ConnectionCount

func (c *Client) ConnectionCount() int64

func (*Client) Connections

func (c *Client) Connections() []*Connection

Connections returns all the current connections as a slice.

func (*Client) DiscoverPeers

func (c *Client) DiscoverPeers(ctx context.Context) ([]bencode.Peer, error)

DiscoverPeers returns the peers from the announce message.

func (*Client) Download

func (c *Client) Download(w io.Writer) error

Download starts the download and blocks until the download finished or errors out.

func (*Client) HasConnection

func (c *Client) HasConnection(addr string) bool

HasConnection returns whether or not the current connection pool includes the provided connection (addr).

func (*Client) PieceLengths

func (c *Client) PieceLengths() []int

func (*Client) Pieces

func (c *Client) Pieces() []Piece

type Command

type Command struct {
	Length    uint32
	MessageID MessageID
	Payload   []byte
}

func (*Command) MarshalBinary

func (cmd *Command) MarshalBinary() (data []byte, err error)

func (Command) String

func (cmd Command) String() string

func (*Command) UnmarshalBinary

func (cmd *Command) UnmarshalBinary(data []byte) error

type CommandDecoder

type CommandDecoder struct {
	// contains filtered or unexported fields
}

func NewCommandDecoder

func NewCommandDecoder(r io.Reader) *CommandDecoder

func (CommandDecoder) Decode

func (dec CommandDecoder) Decode() (*Command, error)

type CommandEncoder

type CommandEncoder struct {
	// contains filtered or unexported fields
}

func NewCommandEncoder

func NewCommandEncoder(w io.Writer) *CommandEncoder

func (CommandEncoder) Encode

func (enc CommandEncoder) Encode(cmd *Command) error

type Connection

type Connection struct {
	net.Conn
	// contains filtered or unexported fields
}

func (*Connection) Addr

func (c *Connection) Addr() string

func (*Connection) PeerID

func (c *Connection) PeerID() string

type HandshakeDecoder

type HandshakeDecoder struct {
	// contains filtered or unexported fields
}

func NewHandshakeDecoder

func NewHandshakeDecoder(r io.Reader) *HandshakeDecoder

func (*HandshakeDecoder) Decode

func (dec *HandshakeDecoder) Decode() (*HandshakeMessage, error)

type HandshakeEncoder

type HandshakeEncoder struct {
	// contains filtered or unexported fields
}

func NewHandshakeEncoder

func NewHandshakeEncoder(w io.Writer) *HandshakeEncoder

func (*HandshakeEncoder) Encode

func (enc *HandshakeEncoder) Encode(msg *HandshakeMessage) error

type HandshakeMessage

type HandshakeMessage struct {
	ProtocolLength uint8
	Protocol       string
	Reserved       [8]byte
	InfoHash       []byte
	PeerID         []byte
}

func (*HandshakeMessage) MarshalBinary

func (msg *HandshakeMessage) MarshalBinary() (data []byte, err error)

func (*HandshakeMessage) UnmarshalBinary

func (msg *HandshakeMessage) UnmarshalBinary(data []byte) error

type MessageID

type MessageID byte
const (
	CommandChoke MessageID = iota
	CommandUnchoke
	CommandInterested
	CommandNotInterested
	CommandHave
	CommandBitfield
	CommandRequest
	CommandPiece
	CommandCancel
)

func (MessageID) String

func (id MessageID) String() string

type Piece

type Piece struct {
	Hash           string
	Chunks         []int
	TotalSize      int
	DownloadedSize int
	Index          uint32
}

Jump to

Keyboard shortcuts

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