packet

package
v0.0.0-...-80ab217 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2022 License: MIT Imports: 7 Imported by: 8

Documentation

Overview

Package packet defines the MQTT packet types

Index

Constants

View Source
const (
	CONNECT     = 1  // Client request to connect to Server
	CONNACK     = 2  // Connect acknowledgment
	PUBLISH     = 3  // Publish message
	PUBACK      = 4  // Publish acknowledgment
	PUBREC      = 5  // Publish received (assured delivery part 1)
	PUBREL      = 6  // Publish release (assured delivery part 2)
	PUBCOMP     = 7  // Publish complete (assured delivery part 3)
	SUBSCRIBE   = 8  // Subscribe request
	SUBACK      = 9  // Subscribe acknowledgment
	UNSUBSCRIBE = 10 // Unsubscribe request
	UNSUBACK    = 11 // Unsubscribe acknowledgment
	PINGREQ     = 12 // PING request
	PINGRESP    = 13 // PING response
	DISCONNECT  = 14 // Client is disconnecting
	AUTH        = 15 // Authentication exchange
)

Packet types

View Source
const (
	AtMostOnce  = 0
	AtLeastOnce = 1
	ExactlyOnce = 2
)

QoS Levels

Variables

View Source
var ErrInvalidFlags = errors.New("Invalid flags")

ErrInvalidFlags is returned if the flags of a packet are invalid

View Source
var ErrInvalidLength = errors.New("Invalid Length")

ErrInvalidLength indicates an invalid length

View Source
var ErrInvalidPacketIdentifier = errors.New("Invalid packet identifier")

ErrInvalidPacketIdentifier is returned if an expected packet identifier was empty

View Source
var ErrInvalidPacketType = errors.New("Invalid packet type")

ErrInvalidPacketType is returned when the control packet type is invalid

View Source
var ErrInvalidQoS = errors.New("Invalid QoS")

ErrInvalidQoS is returned if the QoS of a publish packet is invalid

View Source
var ErrInvalidRemainingLength = errors.New("Invalid Remaining Length")

ErrInvalidRemainingLength when attempting to encode an invalid remaining length field

View Source
var ErrMalformedRemainingLength = errors.New("Malformed Remaining Length")

ErrMalformedRemainingLength is returned when attempting to read a malformed remaining length

View Source
var ErrProtocolViolation = errors.New("Protocol Violation")

ErrProtocolViolation is returned when a message violates the protocol specification

View Source
var Name = map[byte]string{
	CONNECT:     "connect",
	CONNACK:     "connack",
	PUBLISH:     "publish",
	PUBACK:      "puback",
	PUBREC:      "pubrec",
	PUBREL:      "pubrel",
	PUBCOMP:     "pubcomp",
	SUBSCRIBE:   "subscribe",
	SUBACK:      "suback",
	UNSUBSCRIBE: "unsubscribe",
	UNSUBACK:    "unsuback",
	PINGREQ:     "pingreq",
	PINGRESP:    "pingresp",
	DISCONNECT:  "disconnect",
	AUTH:        "auth",
}
View Source
var SubscribeRejected byte = 0x80

SubscribeRejected response code

Functions

func ReadByte

func ReadByte(r io.Reader) (b byte, err error)

ReadByte reads a byte from the given Reader

func ReadBytes

func ReadBytes(r io.Reader) (b []byte, err error)

ReadBytes reads bytes form the given Reader

func ReadRemainingLength

func ReadRemainingLength(r io.Reader) (value int, err error)

ReadRemainingLength returns the decoded remaining length field

func ReadString

func ReadString(r io.Reader) (string, error)

ReadString reads a string from the given Reader

func ReadStringPair

func ReadStringPair(r io.Reader) (string, string, error)

ReadStringPair reads a string pair from the given Reader

func ReadUint16

func ReadUint16(r io.Reader) (i uint16, err error)

ReadUint16 reads an uint16 from the given Reader

func ReadUint32

func ReadUint32(r io.Reader) (i uint32, err error)

ReadUint32 reads an uint32 from the given Reader

func Write

func Write(w io.Writer, p ControlPacket) (err error)

Write a control packet to the writer

func WriteByte

func WriteByte(w io.Writer, b byte) (err error)

WriteByte writes a byte to the given Writer

func WriteBytes

func WriteBytes(w io.Writer, b []byte) (err error)

WriteBytes writes bytes to the given Writer

func WriteRemainingLength

func WriteRemainingLength(w io.Writer, x int) (err error)

WriteRemainingLength writes a remaining length field to the given Writer

func WriteString

func WriteString(w io.Writer, s string) error

WriteString writes a string to the given Writer

func WriteStringPair

func WriteStringPair(w io.Writer, k, v string) error

WriteStringPair writes a string pair to the given Writer

func WriteUint16

func WriteUint16(w io.Writer, i uint16) (err error)

WriteUint16 writes an uint16 to the given Writer

func WriteUint32

func WriteUint32(w io.Writer, i uint32) (err error)

WriteUint32 writes an uint32 to the given Writer

Types

type ConnackPacket

type ConnackPacket struct {
	ReturnCode     ConnectReturnCode
	SessionPresent bool
}

ConnackPacket is the CONNACK packet

func (ConnackPacket) MarshalBinary

func (p ConnackPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (ConnackPacket) PacketType

func (ConnackPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (*ConnackPacket) UnmarshalBinary

func (p *ConnackPacket) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (ConnackPacket) Validate

func (p ConnackPacket) Validate() error

Validate the packet contents

type ConnectPacket

type ConnectPacket struct {
	ProtocolName  string
	ProtocolLevel byte
	CleanStart    bool
	Will          bool
	WillQoS       byte
	WillRetain    bool
	KeepAlive     uint16
	ClientID      string
	WillTopic     string
	WillMessage   []byte
	Username      string
	Password      []byte
}

ConnectPacket is the CONNECT packet

func (ConnectPacket) MarshalBinary

func (p ConnectPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (ConnectPacket) PacketType

func (ConnectPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (ConnectPacket) Response

func (p ConnectPacket) Response() *ConnackPacket

Response to the packet

func (*ConnectPacket) UnmarshalBinary

func (p *ConnectPacket) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (ConnectPacket) Validate

func (p ConnectPacket) Validate() error

Validate the packet contents

type ConnectReturnCode

type ConnectReturnCode byte

ConnectReturnCode is returned in the Connack

var (
	ConnectAccepted                    ConnectReturnCode = 0x00
	ConnectUnacceptableProtocolVersion ConnectReturnCode = 0x01
	ConnectIdentifierRejected          ConnectReturnCode = 0x02
	ConnectServerUnavailable           ConnectReturnCode = 0x03
	ConnectMalformedUsernameOrPassword ConnectReturnCode = 0x04
	ConnectNotAuthorized               ConnectReturnCode = 0x05
)

Connect return codes

func (ConnectReturnCode) Error

func (c ConnectReturnCode) Error() string

type ControlPacket

type ControlPacket interface {
	encoding.BinaryMarshaler   // without fixed header
	encoding.BinaryUnmarshaler // without fixed header
	PacketType() byte

	Validate() error
	// contains filtered or unexported methods
}

ControlPacket represents an MQTT Control Packet

func Read

func Read(r io.Reader) (p ControlPacket, err error)

Read a control packet from the Reader

type DisconnectPacket

type DisconnectPacket struct {
}

DisconnectPacket is the DISCONNECT packet

func (DisconnectPacket) MarshalBinary

func (p DisconnectPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (DisconnectPacket) PacketType

func (DisconnectPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (*DisconnectPacket) UnmarshalBinary

func (p *DisconnectPacket) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (DisconnectPacket) Validate

func (p DisconnectPacket) Validate() error

Validate the packet contents (noop)

type PacketType

type PacketType byte

PacketType contains the MQTT packet type

type PingreqPacket

type PingreqPacket struct {
}

PingreqPacket is the PINGREQ packet

func (PingreqPacket) MarshalBinary

func (p PingreqPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (PingreqPacket) PacketType

func (PingreqPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (PingreqPacket) Response

func (p PingreqPacket) Response() *PingrespPacket

Response to the packet

func (*PingreqPacket) UnmarshalBinary

func (p *PingreqPacket) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (PingreqPacket) Validate

func (p PingreqPacket) Validate() error

Validate the packet contents (noop)

type PingrespPacket

type PingrespPacket struct {
}

PingrespPacket is the PINGRESP packet

func (PingrespPacket) MarshalBinary

func (p PingrespPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (PingrespPacket) PacketType

func (PingrespPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (*PingrespPacket) UnmarshalBinary

func (p *PingrespPacket) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (PingrespPacket) Validate

func (p PingrespPacket) Validate() error

Validate the packet contents (noop)

type PubackPacket

type PubackPacket struct {
	PacketIdentifier uint16
}

PubackPacket is the PUBACK packet

func (PubackPacket) MarshalBinary

func (p PubackPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (PubackPacket) PacketType

func (PubackPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (*PubackPacket) UnmarshalBinary

func (p *PubackPacket) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (PubackPacket) Validate

func (p PubackPacket) Validate() error

Validate the packet contents

type PubcompPacket

type PubcompPacket struct {
	PacketIdentifier uint16
}

PubcompPacket is the PUBCOMP packet

func (PubcompPacket) MarshalBinary

func (p PubcompPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (PubcompPacket) PacketType

func (PubcompPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (*PubcompPacket) UnmarshalBinary

func (p *PubcompPacket) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (PubcompPacket) Validate

func (p PubcompPacket) Validate() error

Validate the packet contents

type PublishPacket

type PublishPacket struct {
	Received         time.Time `json:"received"`
	Retain           bool      `json:"retained"`
	QoS              byte      `json:"qos"`
	Duplicate        bool      `json:"-"`
	PacketIdentifier uint16    `json:"-"`
	TopicName        string    `json:"topic"`
	TopicParts       []string  `json:"-"`
	Message          []byte    `json:"message"`
}

PublishPacket is the PUBLISH packet

func (PublishPacket) MarshalBinary

func (p PublishPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (PublishPacket) PacketType

func (PublishPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (PublishPacket) Response

func (p PublishPacket) Response() ControlPacket

Response to the packet

func (*PublishPacket) UnmarshalBinary

func (p *PublishPacket) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (PublishPacket) Validate

func (p PublishPacket) Validate() error

Validate the packet contents

type PubrecPacket

type PubrecPacket struct {
	PacketIdentifier uint16
}

PubrecPacket is the PUBREC packet

func (PubrecPacket) MarshalBinary

func (p PubrecPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (PubrecPacket) PacketType

func (PubrecPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (PubrecPacket) Response

func (p PubrecPacket) Response() *PubrelPacket

Response to the packet

func (*PubrecPacket) UnmarshalBinary

func (p *PubrecPacket) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (PubrecPacket) Validate

func (p PubrecPacket) Validate() error

Validate the packet contents

type PubrelPacket

type PubrelPacket struct {
	PacketIdentifier uint16
}

PubrelPacket is the PUBREL packet

func (PubrelPacket) MarshalBinary

func (p PubrelPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (PubrelPacket) PacketType

func (PubrelPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (PubrelPacket) Response

func (p PubrelPacket) Response() *PubcompPacket

Response to the packet

func (*PubrelPacket) UnmarshalBinary

func (p *PubrelPacket) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (PubrelPacket) Validate

func (p PubrelPacket) Validate() error

Validate the packet contents

type SubackPacket

type SubackPacket struct {
	PacketIdentifier uint16
	ReturnCodes      []byte
}

SubackPacket is the SUBACK packet

func (SubackPacket) MarshalBinary

func (p SubackPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (SubackPacket) PacketType

func (SubackPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (*SubackPacket) UnmarshalBinary

func (p *SubackPacket) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (SubackPacket) Validate

func (p SubackPacket) Validate() error

Validate the packet contents

type SubscribePacket

type SubscribePacket struct {
	PacketIdentifier uint16
	Topics           []string
	QoSs             []byte
}

SubscribePacket is the SUBSCRIBE packet

func (SubscribePacket) MarshalBinary

func (p SubscribePacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (SubscribePacket) PacketType

func (SubscribePacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (SubscribePacket) Response

func (p SubscribePacket) Response() *SubackPacket

Response to the packet

func (*SubscribePacket) UnmarshalBinary

func (p *SubscribePacket) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (SubscribePacket) Validate

func (p SubscribePacket) Validate() (err error)

Validate the packet contents

type UnsubackPacket

type UnsubackPacket struct {
	PacketIdentifier uint16
}

UnsubackPacket is the UNSUBACK packet

func (UnsubackPacket) MarshalBinary

func (p UnsubackPacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (UnsubackPacket) PacketType

func (UnsubackPacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (*UnsubackPacket) UnmarshalBinary

func (p *UnsubackPacket) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (UnsubackPacket) Validate

func (p UnsubackPacket) Validate() error

Validate the packet contents

type UnsubscribePacket

type UnsubscribePacket struct {
	PacketIdentifier uint16
	Topics           []string
}

UnsubscribePacket is the UNSUBSCRIBE packet

func (UnsubscribePacket) MarshalBinary

func (p UnsubscribePacket) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler

func (UnsubscribePacket) PacketType

func (UnsubscribePacket) PacketType() byte

PacketType returns the MQTT packet type of this packet

func (UnsubscribePacket) Response

func (p UnsubscribePacket) Response() *UnsubackPacket

Response to the packet

func (*UnsubscribePacket) UnmarshalBinary

func (p *UnsubscribePacket) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements encoding.BinaryUnmarshaler

func (UnsubscribePacket) Validate

func (p UnsubscribePacket) Validate() (err error)

Validate the packet contents

Jump to

Keyboard shortcuts

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