message

package
v0.0.0-...-2f17533 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package message has been literally copied code from veggiefender/torrent-client

Index

Constants

View Source
const (
	// Choke chokes the receiver
	Choke messageID = 0
	// Unchoke unchokes the receiver
	Unchoke messageID = 1
	// Interested expresses interest in receiving data
	Interested messageID = 2
	// NotInterested expresses disinterest in receiving data
	NotInterested messageID = 3
	// Have alerts the receiver that the sender has downloaded a piece
	Have messageID = 4
	// Bitfield encodes which pieces that the sender has downloaded
	Bitfield messageID = 5
	// Request requests a block of data from the receiver
	Request messageID = 6
	// Piece delivers a block of data to fulfill a request
	Piece messageID = 7
	// Cancel cancels a request
	Cancel messageID = 8
)

Variables

This section is empty.

Functions

func ParseHave

func ParseHave(msg *Message) (int, error)

ParseHave parses a HAVE message

func ParsePiece

func ParsePiece(index int, buf []byte, msg *Message) (int, error)

ParsePiece parses a PIECE message and copies its payload into a buffer

Types

type Message

type Message struct {
	ID      messageID
	Payload []byte
}

Message stores ID and payload of a message

func FormatHave

func FormatHave(index int) *Message

FormatHave creates a HAVE message

func FormatRequest

func FormatRequest(index, begin, length int) *Message

FormatRequest creates a REQUEST message

func Read

func Read(r io.Reader) (*Message, error)

Read reads a message from stream. The first 4 bytes of the stream gives the length of the message and hence we get the length then read that many bytes from string. Return nil on keep alive msg,i.e to not close the connection

func (*Message) Serialize

func (m *Message) Serialize() []byte

Serialize serializes a message into a buffer of the form <length prefix><message ID><payload> Interprets `nil` as a keep-alive message

func (*Message) String

func (m *Message) String() string

Jump to

Keyboard shortcuts

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