The muonengine package provides functionality for a Go project related to a download client for a peer-to-peer network.
MsgChoke: Represents the "Choke" message ID.MsgUnchoke: Represents the "Unchoke" message ID.MsgInterested: Represents the "Interested" message ID.MsgNotInterested: Represents the "Not Interested" message ID.MsgHave: Represents the "Have" message ID.MsgBitfield: Represents the "Bitfield" message ID.MsgRequest: Represents the "Request" message ID.MsgPiece: Represents the "Piece" message ID.MsgCancel: Represents the "Cancel" message ID.
type Message struct {
ID messageID
Data []byte
}The Message struct represents a message used in the peer-to-peer protocol.
ID: The message ID.Data: The data associated with the message.
func (m *Message) Serialize() []byteThe Serialize method serializes the message into a byte slice.
- The serialized message as a byte slice.
func MessageDeserialize(r io.Reader) (*Message, error)The MessageDeserialize function deserializes a message from an io.Reader and returns a Message object.
r: Theio.Readerto read the message from.
- A pointer to the deserialized
Messageobject. - An error if deserialization fails.
func FormatRequest(index, begin, length int) *MessageThe FormatRequest function formats a "Request" message with the specified index, begin offset, and length.
index: The index of the piece being requested.begin: The beginning offset of the requested block.length: The length of the requested block.
- A pointer to the formatted "Request" message.
func FormatHave(index int) *MessageThe FormatHave function formats a "Have" message with the specified index.
index: The index of the piece that the sender has.
- A pointer to the formatted "Have" message.
func ParsePiece(index int, buf []byte, msg *Message) (int, error)The ParsePiece function parses a "Piece" message and copies the data to the specified buffer.
index: The index of the piece being received.buf: The buffer to copy the data into.msg: The received "Piece" message.
- The number of bytes copied to the buffer.
- An error if parsing or copying fails.
func ParseHave(msg *Message) (int, error)The ParseHave function parses a "Have" message and returns the index of the piece.
msg: The received "Have" message.
- The index of the piece that the sender has.
- An error if parsing fails.
type Bitfield []byteThe Bitfield type represents a bitfield indicating the availability of pieces.
HasPiece(index int) bool: Checks if a specific piece is available in the bitfield.SetPiece(index int): Sets the availability of a specific piece in the bitfield.