mqtt

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2022 License: GPL-3.0 Imports: 6 Imported by: 10

README

mqtt packets

Installation

go get -u github.com/hkloudou/xtransport/packets/mqtt

Documentation

Index

Constants

View Source
const (
	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
)

Below are the constants assigned to each of the MQTT packet types

Variables

View Source
var (
	ErrorRefusedBadProtocolVersion    = errors.New("unacceptable protocol version")
	ErrorRefusedIDRejected            = errors.New("identifier rejected")
	ErrorRefusedServerUnavailable     = errors.New("server Unavailable")
	ErrorRefusedBadUsernameOrPassword = errors.New("bad user name or password")
	ErrorRefusedNotAuthorised         = errors.New("not Authorized")
	ErrorNetworkError                 = errors.New("network Error")
	ErrorProtocolViolation            = errors.New("protocol Violation")
)

ConnErrors is a map of the errors codes constants for Connect() to a Go error

View Source
var ConnackReturnCodes = map[ConnackReturnCode]string{
	0:   "Connection Accepted",
	1:   "Connection Refused: Bad Protocol Version",
	2:   "Connection Refused: Client Identifier Rejected",
	3:   "Connection Refused: Server Unavailable",
	4:   "Connection Refused: Username or Password in unknown format",
	5:   "Connection Refused: Not Authorised",
	254: "Connection Error",
	255: "Connection Refused: Protocol Violation",
}

ConnackReturnCodes is a map of the error codes constants for Connect() to a string representation of the error

View Source
var ErrInvalidTopicEmptyString = errors.New("invalid Topic; empty string")

ErrInvalidTopicEmptyString is the error returned when a topic string is passed in that is 0 length

View Source
var ErrInvalidTopicMultilevel = errors.New("invalid Topic; multi-level wildcard must be last level")

ErrInvalidTopicMultilevel is the error returned when a topic string is passed in that has the multi level wildcard in any position but the last

View Source
var ErrInvalidWildcardTopic = errors.New("invalid Topic; topic should not contain wildcard")
View Source
var PacketNames = map[uint8]string{
	1:  "CONNECT",
	2:  "CONNACK",
	3:  "PUBLISH",
	4:  "PUBACK",
	5:  "PUBREC",
	6:  "PUBREL",
	7:  "PUBCOMP",
	8:  "SUBSCRIBE",
	9:  "SUBACK",
	10: "UNSUBSCRIBE",
	11: "UNSUBACK",
	12: "PINGREQ",
	13: "PINGRESP",
	14: "DISCONNECT",
}

PacketNames maps the constants for each of the MQTT packet types to a string representation of their name.

Functions

func NewPacketError added in v1.1.3

func NewPacketError(code, desc string) *packetError

func ValidatePattern added in v1.1.3

func ValidatePattern(pattern string) error

Topic Names and Topic Filters The MQTT v3.1.1 spec clarifies a number of ambiguities with regard to the validity of Topic strings.

  • A Topic must be between 1 and 65535 bytes.
  • A Topic is case sensitive.
  • A Topic may contain whitespace.
  • A Topic containing a leading forward slash is different than a Topic without.
  • A Topic may be "/" (two levels, both empty string).
  • A Topic must be UTF-8 encoded.
  • A Topic may contain any number of levels.
  • A Topic may contain an empty level (two forward slashes in a row).
  • A TopicName may not contain a wildcard.
  • A TopicFilter may only have a # (multi-level) wildcard as the last level.
  • A TopicFilter may contain any number of + (single-level) wildcards.
  • A TopicFilter with a # will match the absence of a level Example: a subscription to "foo/#" will match messages published to "foo".

func ValidateTopic added in v1.1.3

func ValidateTopic(topic string) error

Types

type ConnackPacket

type ConnackPacket struct {
	FixedHeader
	SessionPresent bool
	ReturnCode     ConnackReturnCode
}

ConnackPacket is an internal representation of the fields of the Connack MQTT packet

func (*ConnackPacket) Details

func (ca *ConnackPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*ConnackPacket) String

func (ca *ConnackPacket) String() string

func (*ConnackPacket) Unpack

func (ca *ConnackPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*ConnackPacket) WriteTo added in v1.1.2

func (ca *ConnackPacket) WriteTo(w io.Writer) (n int64, err error)

type ConnackReturnCode added in v1.1.1

type ConnackReturnCode byte
const (
	Accepted                        ConnackReturnCode = 0x00
	ErrRefusedBadProtocolVersion    ConnackReturnCode = 0x01
	ErrRefusedIDRejected            ConnackReturnCode = 0x02
	ErrRefusedServerUnavailable     ConnackReturnCode = 0x03
	ErrRefusedBadUsernameOrPassword ConnackReturnCode = 0x04
	ErrRefusedNotAuthorised         ConnackReturnCode = 0x05
	ErrNetworkError                 ConnackReturnCode = 0xFE
	ErrProtocolViolation            ConnackReturnCode = 0xFF
)

Below are the const definitions for error codes returned by Connect()

type ConnectPacket

type ConnectPacket struct {
	FixedHeader
	ProtocolName    string
	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         string
	Password         []byte
}

ConnectPacket is an internal representation of the fields of the Connect MQTT packet

func (*ConnectPacket) Details

func (c *ConnectPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*ConnectPacket) String

func (c *ConnectPacket) String() string

func (*ConnectPacket) Unpack

func (c *ConnectPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*ConnectPacket) Validate

func (c *ConnectPacket) Validate() ConnackReturnCode

Validate performs validation of the fields of a Connect packet

func (*ConnectPacket) WriteTo added in v1.1.2

func (c *ConnectPacket) WriteTo(w io.Writer) (n int64, err error)

type ControlPacket

type ControlPacket interface {
	// Write(io.Writer) error
	io.WriterTo
	Unpack(io.Reader) error
	String() string
	Details() Details
	Type() byte
}

ControlPacket defines the interface for structs intended to hold decoded MQTT packets, either from being read or before being written

func NewControlPacket

func NewControlPacket(packetType byte) ControlPacket

NewControlPacket is used to create a new ControlPacket of the type specified by packetType, this is usually done by reference to the packet type constants defined in packets.go. The newly created ControlPacket is empty and a pointer is returned.

func NewControlPacketWithHeader

func NewControlPacketWithHeader(fh FixedHeader) (ControlPacket, error)

NewControlPacketWithHeader is used to create a new ControlPacket of the type specified within the FixedHeader that is passed to the function. The newly created ControlPacket is empty and a pointer is returned.

func ReadPacket

func ReadPacket(r io.Reader) (ControlPacket, error)

ReadPacket takes an instance of an io.Reader (such as net.Conn) and attempts to read an MQTT packet from the stream. It returns a ControlPacket representing the decoded MQTT packet and an error. One of these returns will always be nil, a nil ControlPacket indicating an error occurred.

type Details

type Details struct {
	Qos       byte
	MessageID uint16
}

Details struct returned by the Details() function called on ControlPackets to present details of the Qos and MessageID of the ControlPacket

type DisconnectPacket

type DisconnectPacket struct {
	FixedHeader
}

DisconnectPacket is an internal representation of the fields of the Disconnect MQTT packet

func (*DisconnectPacket) Details

func (d *DisconnectPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*DisconnectPacket) String

func (d *DisconnectPacket) String() string

func (*DisconnectPacket) Unpack

func (d *DisconnectPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*DisconnectPacket) WriteTo added in v1.1.2

func (d *DisconnectPacket) WriteTo(w io.Writer) (n int64, err error)

type FixedHeader

type FixedHeader struct {
	MessageType     byte
	Dup             bool
	Qos             byte
	Retain          bool
	RemainingLength int
}

FixedHeader is a struct to hold the decoded information from the fixed header of an MQTT ControlPacket

func (FixedHeader) String

func (fh FixedHeader) String() string

func (FixedHeader) Type

func (fh FixedHeader) Type() byte

type PingreqPacket

type PingreqPacket struct {
	FixedHeader
}

PingreqPacket is an internal representation of the fields of the Pingreq MQTT packet

func (*PingreqPacket) Details

func (pr *PingreqPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*PingreqPacket) String

func (pr *PingreqPacket) String() string

func (*PingreqPacket) Unpack

func (pr *PingreqPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*PingreqPacket) WriteTo added in v1.1.2

func (pr *PingreqPacket) WriteTo(w io.Writer) (n int64, err error)

type PingrespPacket

type PingrespPacket struct {
	FixedHeader
}

PingrespPacket is an internal representation of the fields of the Pingresp MQTT packet

func (*PingrespPacket) Details

func (pr *PingrespPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*PingrespPacket) String

func (pr *PingrespPacket) String() string

func (*PingrespPacket) Unpack

func (pr *PingrespPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*PingrespPacket) WriteTo added in v1.1.2

func (pr *PingrespPacket) WriteTo(w io.Writer) (n int64, err error)

type PubackPacket

type PubackPacket struct {
	FixedHeader
	MessageID uint16
}

PubackPacket is an internal representation of the fields of the Puback MQTT packet

func (*PubackPacket) Details

func (pa *PubackPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*PubackPacket) String

func (pa *PubackPacket) String() string

func (*PubackPacket) Unpack

func (pa *PubackPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*PubackPacket) WriteTo added in v1.1.2

func (pa *PubackPacket) WriteTo(w io.Writer) (n int64, err error)

type PubcompPacket

type PubcompPacket struct {
	FixedHeader
	MessageID uint16
}

PubcompPacket is an internal representation of the fields of the Pubcomp MQTT packet

func (*PubcompPacket) Details

func (pc *PubcompPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*PubcompPacket) String

func (pc *PubcompPacket) String() string

func (*PubcompPacket) Unpack

func (pc *PubcompPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*PubcompPacket) WriteTo added in v1.1.2

func (pc *PubcompPacket) WriteTo(w io.Writer) (n int64, err error)

type PublishPacket

type PublishPacket struct {
	FixedHeader
	TopicName string
	MessageID uint16
	Payload   []byte
}

PublishPacket is an internal representation of the fields of the Publish MQTT packet

func (*PublishPacket) Copy

func (p *PublishPacket) Copy() *PublishPacket

Copy creates a new PublishPacket with the same topic and payload but an empty fixed header, useful for when you want to deliver a message with different properties such as Qos but the same content

func (*PublishPacket) Details

func (p *PublishPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*PublishPacket) StrictValidate added in v1.1.3

func (s *PublishPacket) StrictValidate() error

func (*PublishPacket) String

func (p *PublishPacket) String() string

func (*PublishPacket) Unpack

func (p *PublishPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*PublishPacket) Validate added in v1.1.3

func (p *PublishPacket) Validate() error

func (*PublishPacket) WriteTo added in v1.1.2

func (p *PublishPacket) WriteTo(w io.Writer) (n int64, err error)

type PubrecPacket

type PubrecPacket struct {
	FixedHeader
	MessageID uint16
}

PubrecPacket is an internal representation of the fields of the Pubrec MQTT packet

func (*PubrecPacket) Details

func (pr *PubrecPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*PubrecPacket) String

func (pr *PubrecPacket) String() string

func (*PubrecPacket) Unpack

func (pr *PubrecPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*PubrecPacket) WriteTo added in v1.1.2

func (pr *PubrecPacket) WriteTo(w io.Writer) (n int64, err error)

type PubrelPacket

type PubrelPacket struct {
	FixedHeader
	MessageID uint16
}

PubrelPacket is an internal representation of the fields of the Pubrel MQTT packet

func (*PubrelPacket) Details

func (pr *PubrelPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*PubrelPacket) String

func (pr *PubrelPacket) String() string

func (*PubrelPacket) Unpack

func (pr *PubrelPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*PubrelPacket) WriteTo added in v1.1.2

func (pr *PubrelPacket) WriteTo(w io.Writer) (n int64, err error)

type SubackPacket

type SubackPacket struct {
	FixedHeader
	MessageID   uint16
	ReturnCodes []byte
}

SubackPacket is an internal representation of the fields of the Suback MQTT packet

func (*SubackPacket) Details

func (sa *SubackPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*SubackPacket) String

func (sa *SubackPacket) String() string

func (*SubackPacket) Unpack

func (sa *SubackPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*SubackPacket) WriteTo added in v1.1.2

func (sa *SubackPacket) WriteTo(w io.Writer) (n int64, err error)

type SubscribePacket

type SubscribePacket struct {
	FixedHeader
	MessageID uint16
	Topics    []string
	Qoss      []byte
}

SubscribePacket is an internal representation of the fields of the Subscribe MQTT packet

func (*SubscribePacket) Details

func (s *SubscribePacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*SubscribePacket) StrictValidate added in v1.1.3

func (s *SubscribePacket) StrictValidate() error

func (*SubscribePacket) String

func (s *SubscribePacket) String() string

func (*SubscribePacket) Unpack

func (s *SubscribePacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*SubscribePacket) Validate added in v1.1.3

func (s *SubscribePacket) Validate() error

func (*SubscribePacket) WriteTo added in v1.1.2

func (s *SubscribePacket) WriteTo(w io.Writer) (n int64, err error)

type UnsubackPacket

type UnsubackPacket struct {
	FixedHeader
	MessageID uint16
}

UnsubackPacket is an internal representation of the fields of the Unsuback MQTT packet

func (*UnsubackPacket) Details

func (ua *UnsubackPacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*UnsubackPacket) String

func (ua *UnsubackPacket) String() string

func (*UnsubackPacket) Unpack

func (ua *UnsubackPacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*UnsubackPacket) WriteTo added in v1.1.2

func (ua *UnsubackPacket) WriteTo(w io.Writer) (n int64, err error)

type UnsubscribePacket

type UnsubscribePacket struct {
	FixedHeader
	MessageID uint16
	Topics    []string
}

UnsubscribePacket is an internal representation of the fields of the Unsubscribe MQTT packet

func (*UnsubscribePacket) Details

func (u *UnsubscribePacket) Details() Details

Details returns a Details struct containing the Qos and MessageID of this ControlPacket

func (*UnsubscribePacket) StrictValidate added in v1.1.3

func (s *UnsubscribePacket) StrictValidate() error

func (*UnsubscribePacket) String

func (u *UnsubscribePacket) String() string

func (*UnsubscribePacket) Unpack

func (u *UnsubscribePacket) Unpack(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*UnsubscribePacket) Validate added in v1.1.3

func (s *UnsubscribePacket) Validate() error

func (*UnsubscribePacket) WriteTo added in v1.1.2

func (u *UnsubscribePacket) WriteTo(w io.Writer) (n int64, err error)

Jump to

Keyboard shortcuts

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