packets

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Reserved    byte = iota
	Connect          // 1
	Connack          // 2
	Publish          // 3
	Puback           // 4
	Pubrec           // 5
	Pubrel           // 6
	Pubcomp          // 7
	Subscribe        // 8
	Suback           // 9
	Unsubscribe      // 10
	Unsuback         // 11
	Pingreq          // 12
	Pingresp         // 13
	Disconnect       // 14

	Accepted                      byte = 0x00
	Failed                        byte = 0xFF
	CodeConnectBadProtocolVersion byte = 0x01
	CodeConnectBadClientID        byte = 0x02
	CodeConnectServerUnavailable  byte = 0x03
	CodeConnectBadAuthValues      byte = 0x04
	CodeConnectNotAuthorised      byte = 0x05
	CodeConnectNetworkError       byte = 0xFE
	CodeConnectProtocolViolation  byte = 0xFF
	ErrSubAckNetworkError         byte = 0x80
)

All of the valid packet types and their packet identifier.

Variables

View Source
var (
	// CONNECT
	ErrMalformedProtocolName    = errors.New("malformed packet: protocol name")
	ErrMalformedProtocolVersion = errors.New("malformed packet: protocol version")
	ErrMalformedFlags           = errors.New("malformed packet: flags")
	ErrMalformedKeepalive       = errors.New("malformed packet: keepalive")
	ErrMalformedClientID        = errors.New("malformed packet: client id")
	ErrMalformedWillTopic       = errors.New("malformed packet: will topic")
	ErrMalformedWillMessage     = errors.New("malformed packet: will message")
	ErrMalformedUsername        = errors.New("malformed packet: username")
	ErrMalformedPassword        = errors.New("malformed packet: password")

	// CONNACK
	ErrMalformedSessionPresent = errors.New("malformed packet: session present")
	ErrMalformedReturnCode     = errors.New("malformed packet: return code")

	// PUBLISH
	ErrMalformedTopic    = errors.New("malformed packet: topic name")
	ErrMalformedPacketID = errors.New("malformed packet: packet id")

	// SUBSCRIBE
	ErrMalformedQoS = errors.New("malformed packet: qos")

	// PACKETS
	ErrProtocolViolation        = errors.New("protocol violation")
	ErrOffsetStrOutOfRange      = errors.New("offset string out of range")
	ErrOffsetBytesOutOfRange    = errors.New("offset bytes out of range")
	ErrOffsetByteOutOfRange     = errors.New("offset byte out of range")
	ErrOffsetBoolOutOfRange     = errors.New("offset bool out of range")
	ErrOffsetUintOutOfRange     = errors.New("offset uint out of range")
	ErrOffsetStrInvalidUTF8     = errors.New("offset string invalid utf8")
	ErrInvalidFlags             = errors.New("invalid flags set for packet")
	ErrOversizedLengthIndicator = errors.New("protocol violation: oversized length indicator")
	ErrMissingPacketID          = errors.New("missing packet id")
	ErrSurplusPacketID          = errors.New("surplus packet id")
)

Functions

This section is empty.

Types

type FixedHeader

type FixedHeader struct {
	Type      byte // the type of the packet (PUBLISH, SUBSCRIBE, etc) from bits 7 - 4 (byte 1).
	Dup       bool // indicates if the packet was already sent at an earlier time.
	Qos       byte // indicates the quality of service expected.
	Retain    bool // whether the message should be retained.
	Remaining int  // the number of remaining bytes in the payload.
}

FixedHeader contains the values of the fixed header portion of the MQTT packet.

func (*FixedHeader) Decode

func (fh *FixedHeader) Decode(headerByte byte) error

decode extracts the specification bits from the header byte.

func (*FixedHeader) Encode

func (fh *FixedHeader) Encode(buf *bytes.Buffer)

Encode encodes the FixedHeader and returns a bytes buffer.

type Packet

type Packet struct {
	FixedHeader FixedHeader

	PacketID uint16

	// Connect
	ProtocolName     []byte
	ProtocolVersion  byte
	CleanSession     bool
	WillFlag         bool
	WillQos          byte
	WillRetain       bool
	UsernameFlag     bool
	PasswordFlag     bool
	ReservedBit      byte
	Keepalive        uint16
	ClientIdentifier string
	WillTopic        string
	WillMessage      []byte
	Username         []byte
	Password         []byte

	// Connack
	SessionPresent bool
	ReturnCode     byte

	// Publish
	TopicName string
	Payload   []byte

	// Subscribe, Unsubscribe
	Topics []string
	Qoss   []byte

	ReturnCodes []byte // Suback
}

Packet is an MQTT packet. Instead of providing a packet interface and variant packet structs, this is a single concrete packet type to cover all packet types, which allows us to take advantage of various compiler optimizations.

func (*Packet) ConnackDecode

func (pk *Packet) ConnackDecode(buf []byte) error

ConnackDecode decodes a Connack packet.

func (*Packet) ConnackEncode

func (pk *Packet) ConnackEncode(buf *bytes.Buffer) error

ConnackEncode encodes a Connack packet.

func (*Packet) ConnectDecode

func (pk *Packet) ConnectDecode(buf []byte) error

ConnectDecode decodes a connect packet.

func (*Packet) ConnectEncode

func (pk *Packet) ConnectEncode(buf *bytes.Buffer) error

ConnectEncode encodes a connect packet.

func (*Packet) ConnectValidate

func (pk *Packet) ConnectValidate() (b byte, err error)

ConnectValidate ensures the connect packet is compliant.

func (*Packet) DisconnectEncode

func (pk *Packet) DisconnectEncode(buf *bytes.Buffer) error

DisconnectEncode encodes a Disconnect packet.

func (*Packet) PingreqEncode

func (pk *Packet) PingreqEncode(buf *bytes.Buffer) error

PingreqEncode encodes a Pingreq packet.

func (*Packet) PingrespEncode

func (pk *Packet) PingrespEncode(buf *bytes.Buffer) error

PingrespEncode encodes a Pingresp packet.

func (*Packet) PubackDecode

func (pk *Packet) PubackDecode(buf []byte) error

PubackDecode decodes a Puback packet.

func (*Packet) PubackEncode

func (pk *Packet) PubackEncode(buf *bytes.Buffer) error

PubackEncode encodes a Puback packet.

func (*Packet) PubcompDecode

func (pk *Packet) PubcompDecode(buf []byte) error

PubcompDecode decodes a Pubcomp packet.

func (*Packet) PubcompEncode

func (pk *Packet) PubcompEncode(buf *bytes.Buffer) error

PubcompEncode encodes a Pubcomp packet.

func (*Packet) PublishCopy

func (pk *Packet) PublishCopy() Packet

PublishCopy creates a new instance of Publish packet bearing the same payload and destination topic, but with an empty header for inheriting new QoS flags, etc.

func (*Packet) PublishDecode

func (pk *Packet) PublishDecode(buf []byte) error

PublishDecode extracts the data values from the packet.

func (*Packet) PublishEncode

func (pk *Packet) PublishEncode(buf *bytes.Buffer) error

PublishEncode encodes a Publish packet.

func (*Packet) PublishValidate

func (pk *Packet) PublishValidate() (byte, error)

PublishValidate validates a publish packet.

func (*Packet) PubrecDecode

func (pk *Packet) PubrecDecode(buf []byte) error

PubrecDecode decodes a Pubrec packet.

func (*Packet) PubrecEncode

func (pk *Packet) PubrecEncode(buf *bytes.Buffer) error

PubrecEncode encodes a Pubrec packet.

func (*Packet) PubrelDecode

func (pk *Packet) PubrelDecode(buf []byte) error

PubrelDecode decodes a Pubrel packet.

func (*Packet) PubrelEncode

func (pk *Packet) PubrelEncode(buf *bytes.Buffer) error

PubrelEncode encodes a Pubrel packet.

func (*Packet) SubackDecode

func (pk *Packet) SubackDecode(buf []byte) error

SubackDecode decodes a Suback packet.

func (*Packet) SubackEncode

func (pk *Packet) SubackEncode(buf *bytes.Buffer) error

SubackEncode encodes a Suback packet.

func (*Packet) SubscribeDecode

func (pk *Packet) SubscribeDecode(buf []byte) error

SubscribeDecode decodes a Subscribe packet.

func (*Packet) SubscribeEncode

func (pk *Packet) SubscribeEncode(buf *bytes.Buffer) error

SubscribeEncode encodes a Subscribe packet.

func (*Packet) SubscribeValidate

func (pk *Packet) SubscribeValidate() (byte, error)

SubscribeValidate ensures the packet is compliant.

func (*Packet) UnsubackDecode

func (pk *Packet) UnsubackDecode(buf []byte) error

UnsubackDecode decodes an Unsuback packet.

func (*Packet) UnsubackEncode

func (pk *Packet) UnsubackEncode(buf *bytes.Buffer) error

UnsubackEncode encodes an Unsuback packet.

func (*Packet) UnsubscribeDecode

func (pk *Packet) UnsubscribeDecode(buf []byte) error

UnsubscribeDecode decodes an Unsubscribe packet.

func (*Packet) UnsubscribeEncode

func (pk *Packet) UnsubscribeEncode(buf *bytes.Buffer) error

UnsubscribeEncode encodes an Unsubscribe packet.

func (*Packet) UnsubscribeValidate

func (pk *Packet) UnsubscribeValidate() (byte, error)

UnsubscribeValidate validates an Unsubscribe packet.

Jump to

Keyboard shortcuts

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