mq

package module
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2023 License: MIT Imports: 8 Imported by: 4

README

Build Status codecov Maintainability

gregoryv/mq - package implements mqtt-v5.0 control packets

For a low level client implementation refer to gregoryv/tt

Naming concepts follow the specification as much as possible.

Initial work and design is discussed in etc/blog.md.

Documentation

Overview

Package mq provides a mqtt-v5.0 protocol implementation

The specification is found at https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html

Example (PacketReadWrite)
package main

import (
	"bytes"
	"fmt"

	"github.com/gregoryv/mq"
)

func main() {
	var buf bytes.Buffer
	{ // create and write packet
		p := mq.NewConnect()
		p.SetClientID("pink")
		p.SetUsername("gopher")
		p.SetPassword([]byte("cute"))
		p.SetWill(mq.Pub(1, "client/gone", "pink"), 7)
		p.WriteTo(&buf)
	}
	{ // read the packet
		p, _ := mq.ReadPacket(&buf)
		fmt.Print(p)
	}
}
Output:

CONNECT ---- up--1w-- MQTT5 pink 0s 58 bytes

Index

Examples

Constants

View Source
const (
	Reserved byte = 1 << iota
	CleanStart
	WillFlag
	WillQoS1
	WillQoS2
	WillRetain
	PasswordFlag
	UsernameFlag
)

CONNECT flags used in Connect.HasFlag()

View Source
const (
	UNDEFINED   byte = (iota << 4) // 0 Forbidden Reserved
	CONNECT                        // 1 Client to Server Connection request
	CONNACK                        // 2 Server to Client Connect acknowledgment
	PUBLISH                        // 3 Client to Server or Publish message
	PUBACK                         // 4 Client to Server or Publish acknowledgment (QoS 1)
	PUBREC                         // 5 Client to Server or Publish received (QoS 2 delivery part 1)
	PUBREL                         // 6 Client to Server or Publish release (QoS 2 delivery part 2)
	PUBCOMP                        // 7 Client to Server or Publish complete (QoS 2 delivery part 3)
	SUBSCRIBE                      // 8 Client to Server Subscribe request
	SUBACK                         // 9 Server to Client Subscribe acknowledgment
	UNSUBSCRIBE                    // 10 Client to Server Unsubscribe request
	UNSUBACK                       // 11 Server to Client Unsubscribe acknowledgment
	PINGREQ                        // 12 Client to Server PING request
	PINGRESP                       // 13 Server to Client PING response
	DISCONNECT                     // 14 Client to Server or Disconnect notification
	AUTH                           // 15 Client to Server or Server to Client Authentication exchange
)

2.1.2 MQTT Control Packet type

https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_MQTT_Control_Packet

View Source
const (
	RETAIN byte = 0b0000_0001
	QoS1   byte = 0b0000_0010
	QoS2   byte = 0b0000_0100
	QoS3   byte = 0b0000_0110 // malformed!
	DUP    byte = 0b0000_1000
)

firstByte header flags

View Source
const (
	SessionPresent uint8 = 1
)

Variables

View Source
var ErrMissingData = fmt.Errorf("missing data")

Functions

func Dump added in v0.24.0

func Dump(w io.Writer, p Packet)

Dump writes all packet fields to the given writer, including empty value ones.

Example (Unsubscribe)
s := NewUnsubscribe()
s.AddUserProp("color", "purple")
s.AddFilter("a/b/c")
s.AddFilter("d/e")
Dump(os.Stdout, s)
Output:

PacketID: 0
Filters
  0. a/b/c
  1. d/e
UserProperties
  0. color: "purple"

Types

type Auth

type Auth struct {
	UserProperties
	// contains filtered or unexported fields
}

func NewAuth

func NewAuth() *Auth

func (*Auth) AuthData added in v0.24.0

func (p *Auth) AuthData() []byte

func (*Auth) AuthMethod added in v0.24.0

func (p *Auth) AuthMethod() string

func (*Auth) ReasonCode

func (p *Auth) ReasonCode() ReasonCode

func (*Auth) ReasonString added in v0.24.0

func (p *Auth) ReasonString() string

func (*Auth) SetAuthData added in v0.24.0

func (p *Auth) SetAuthData(v []byte)

func (*Auth) SetAuthMethod added in v0.24.0

func (p *Auth) SetAuthMethod(v string)

func (*Auth) SetReasonCode

func (p *Auth) SetReasonCode(v ReasonCode)

func (*Auth) SetReasonString added in v0.24.0

func (p *Auth) SetReasonString(v string)

func (*Auth) String

func (p *Auth) String() string
Example
p := NewAuth()
fmt.Println(p)
fmt.Print(DocumentFlags(p))
Output:

AUTH ---- 2 bytes
     3210 Size

3-0 reserved

func (*Auth) UnmarshalBinary

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

func (*Auth) WriteTo

func (p *Auth) WriteTo(w io.Writer) (int64, error)

type ConnAck

type ConnAck struct {
	UserProperties
	// contains filtered or unexported fields
}
Example
a := NewConnAck()
a.SetSessionPresent(true)

fmt.Print(a.String())
Output:

CONNACK ---- -------s  5 bytes
Example (WithReasonCode)
a := NewConnAck()
a.SetSessionPresent(true)
a.SetReasonCode(NotAuthorized)

fmt.Print(a.String())
Output:

CONNACK ---- -------s  5 bytes NotAuthorized!

func NewConnAck

func NewConnAck() *ConnAck

func (*ConnAck) AssignedClientID

func (p *ConnAck) AssignedClientID() string

func (*ConnAck) AuthData

func (p *ConnAck) AuthData() []byte

func (*ConnAck) AuthMethod

func (p *ConnAck) AuthMethod() string

func (*ConnAck) HasFlag

func (p *ConnAck) HasFlag(v byte) bool

func (*ConnAck) MaxPacketSize

func (p *ConnAck) MaxPacketSize() uint32

func (*ConnAck) MaxQoS

func (p *ConnAck) MaxQoS() uint8

func (*ConnAck) ReasonCode

func (p *ConnAck) ReasonCode() ReasonCode

func (*ConnAck) ReasonString

func (p *ConnAck) ReasonString() string

func (*ConnAck) ReceiveMax

func (p *ConnAck) ReceiveMax() uint16

func (*ConnAck) ResponseInformation

func (p *ConnAck) ResponseInformation() string

func (*ConnAck) RetainAvailable

func (p *ConnAck) RetainAvailable() bool

func (*ConnAck) ServerKeepAlive

func (p *ConnAck) ServerKeepAlive() uint16

func (*ConnAck) ServerReference

func (p *ConnAck) ServerReference() string

func (*ConnAck) SessionExpiryInterval

func (p *ConnAck) SessionExpiryInterval() uint32

func (*ConnAck) SessionPresent

func (p *ConnAck) SessionPresent() bool

func (*ConnAck) SetAssignedClientID

func (p *ConnAck) SetAssignedClientID(v string)

func (*ConnAck) SetAuthData

func (p *ConnAck) SetAuthData(v []byte)

func (*ConnAck) SetAuthMethod

func (p *ConnAck) SetAuthMethod(v string)

func (*ConnAck) SetMaxPacketSize

func (p *ConnAck) SetMaxPacketSize(v uint32)

func (*ConnAck) SetMaxQoS

func (p *ConnAck) SetMaxQoS(v uint8)

func (*ConnAck) SetReasonCode

func (p *ConnAck) SetReasonCode(v ReasonCode)

func (*ConnAck) SetReasonString

func (p *ConnAck) SetReasonString(v string)

func (*ConnAck) SetReceiveMax

func (p *ConnAck) SetReceiveMax(v uint16)

func (*ConnAck) SetResponseInformation

func (p *ConnAck) SetResponseInformation(v string)

func (*ConnAck) SetRetainAvailable

func (p *ConnAck) SetRetainAvailable(v bool)

func (*ConnAck) SetServerKeepAlive

func (p *ConnAck) SetServerKeepAlive(v uint16)

func (*ConnAck) SetServerReference

func (p *ConnAck) SetServerReference(v string)

func (*ConnAck) SetSessionExpiryInterval

func (p *ConnAck) SetSessionExpiryInterval(v uint32)

func (*ConnAck) SetSessionPresent

func (p *ConnAck) SetSessionPresent(v bool)

func (*ConnAck) SetSharedSubAvailable

func (p *ConnAck) SetSharedSubAvailable(v bool)

func (*ConnAck) SetSubIdentifiersAvailable

func (p *ConnAck) SetSubIdentifiersAvailable(v bool)

func (*ConnAck) SetTopicAliasMax

func (p *ConnAck) SetTopicAliasMax(v uint16)

func (*ConnAck) SetWildcardSubAvailable

func (p *ConnAck) SetWildcardSubAvailable(v bool)

func (*ConnAck) SharedSubAvailable

func (p *ConnAck) SharedSubAvailable() bool

func (*ConnAck) String

func (p *ConnAck) String() string
Example
a := NewConnAck()
a.SetSessionPresent(true)
a.SetAssignedClientID("pink")
a.SetReasonCode(QoSNotSupported)
a.SetReasonString("please select max 1")

fmt.Println(a.String())
fmt.Print(DocumentFlags(a))
Output:

CONNACK ---- -------s pink 34 bytes QoSNotSupported! please select max 1
        3210 76543210 AssignedClientID Size [ReasonCode and ReasonString if error]

3-0 reserved

7-1 reserved
0 s Session present

func (*ConnAck) SubIdentifiersAvailable

func (p *ConnAck) SubIdentifiersAvailable() bool

func (*ConnAck) TopicAliasMax

func (p *ConnAck) TopicAliasMax() uint16

func (*ConnAck) UnmarshalBinary

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

func (*ConnAck) WildcardSubAvailable

func (p *ConnAck) WildcardSubAvailable() bool

func (*ConnAck) WriteTo

func (p *ConnAck) WriteTo(w io.Writer) (int64, error)

WriteTo writes this connect control packet in wire format to the given writer.

type Connect

type Connect struct {
	UserProperties
	// contains filtered or unexported fields
}
Example
c := NewConnect()
c.SetClientID("macy")
c.SetKeepAlive(299)
c.SetUsername("john.doe")
c.SetPassword([]byte("123"))

fmt.Print(c.String())
Output:

CONNECT ---- up------ MQTT5 macy 4m59s 34 bytes

func NewConnect

func NewConnect() *Connect

NewConnect returns an empty MQTT v5 connect packet.

func (*Connect) AuthData

func (p *Connect) AuthData() []byte

func (*Connect) AuthMethod

func (p *Connect) AuthMethod() string

func (*Connect) CleanStart added in v0.24.0

func (p *Connect) CleanStart() bool

func (*Connect) ClientID

func (p *Connect) ClientID() string

func (*Connect) HasFlag

func (p *Connect) HasFlag(v byte) bool

func (*Connect) KeepAlive

func (p *Connect) KeepAlive() uint16

func (*Connect) MaxPacketSize

func (p *Connect) MaxPacketSize() uint32

func (*Connect) Password

func (p *Connect) Password() []byte

func (*Connect) ProtocolName

func (p *Connect) ProtocolName() string

func (*Connect) ProtocolVersion

func (p *Connect) ProtocolVersion() uint8

func (*Connect) ReceiveMax

func (p *Connect) ReceiveMax() uint16

func (*Connect) RequestProblemInfo

func (p *Connect) RequestProblemInfo() bool

func (*Connect) RequestResponseInfo

func (p *Connect) RequestResponseInfo() bool

func (*Connect) SessionExpiryInterval

func (p *Connect) SessionExpiryInterval() uint32

func (*Connect) SetAuthData

func (p *Connect) SetAuthData(v []byte)

func (*Connect) SetAuthMethod

func (p *Connect) SetAuthMethod(v string)

func (*Connect) SetCleanStart

func (p *Connect) SetCleanStart(v bool)

func (*Connect) SetClientID

func (p *Connect) SetClientID(v string)

func (*Connect) SetKeepAlive

func (p *Connect) SetKeepAlive(v uint16)

func (*Connect) SetMaxPacketSize

func (p *Connect) SetMaxPacketSize(v uint32)

func (*Connect) SetPassword

func (p *Connect) SetPassword(v []byte)

func (*Connect) SetProtocolName

func (p *Connect) SetProtocolName(v string)

func (*Connect) SetProtocolVersion

func (p *Connect) SetProtocolVersion(v uint8)

func (*Connect) SetReceiveMax

func (p *Connect) SetReceiveMax(v uint16)

func (*Connect) SetRequestProblemInfo

func (p *Connect) SetRequestProblemInfo(v bool)

The Client uses this value to indicate whether the ReasonString String or User Properties are sent in the case of failures.

func (*Connect) SetRequestResponseInfo

func (p *Connect) SetRequestResponseInfo(v bool)

The Client uses this value to request the Server to return Response Information in the CONNACK

func (*Connect) SetSessionExpiryInterval

func (p *Connect) SetSessionExpiryInterval(v uint32)

func (*Connect) SetTopicAliasMax

func (p *Connect) SetTopicAliasMax(v uint16)

This value indicates the highest value that the Client will accept as a Topic Alias sent by the Server. The Client uses this value to limit the number of Topic Aliases that it is willing to hold on this Connection.

func (*Connect) SetUsername

func (p *Connect) SetUsername(v string)

func (*Connect) SetWill added in v0.21.0

func (p *Connect) SetWill(will *Publish, delayInterval uint32)

SetWill sets the will message and delay in seconds. The Server delays publishing the Client’s Will Message until the Will Delay Interval has passed or the Session ends, whichever happens first.

func (*Connect) String

func (p *Connect) String() string

String returns a short string describing the connect packet.

Example
p := NewConnect()
p.SetClientID("pink")
p.SetUsername("gopher")
p.SetPassword([]byte("cute"))
p.SetWill(Pub(1, "client/gone", "pink"), 3)

fmt.Println(p.String())
fmt.Println(DocumentFlags(p))
Output:

CONNECT ---- up--1w-- MQTT5 pink 0s 58 bytes
        3210 76543210 ProtocolVersion ClientID KeepAlive Size

3-0 reserved

7 u   User Name Flag
6 p   Password Flag
5 r   Will Retain
4 2|! Will QoS
3 1|! Will QoS
2 w   Will Flag
1 s   Clean Start
0     reserved

func (*Connect) TopicAliasMax

func (p *Connect) TopicAliasMax() uint16

func (*Connect) UnmarshalBinary

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

func (*Connect) Username

func (p *Connect) Username() string

func (*Connect) Will added in v0.21.0

func (p *Connect) Will() *Publish

func (*Connect) WriteTo

func (p *Connect) WriteTo(w io.Writer) (int64, error)

WriteTo writes this connect control packet in wire format to the given writer.

type ControlPacket

type ControlPacket interface {
	// Write the packet in wireformat to a writer
	io.WriterTo

	// Unmarshal wireformat
	encoding.BinaryUnmarshaler

	// Return a short readable string suitable for logging
	fmt.Stringer
}

func ReadPacket

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

ReadPacket reads one packet from the reader. Returns a io.EOF or Malformed error on failure.

type Disconnect

type Disconnect struct {
	UserProperties
	// contains filtered or unexported fields
}

func NewDisconnect

func NewDisconnect() *Disconnect

NewDisconnect returns a disconnect packet with reason code NormalDisconnect.

func (*Disconnect) ReasonCode

func (p *Disconnect) ReasonCode() ReasonCode

func (*Disconnect) SetReasonCode

func (p *Disconnect) SetReasonCode(v ReasonCode)

func (*Disconnect) String

func (p *Disconnect) String() string
Example
p := NewDisconnect()
fmt.Println(p)
fmt.Print(DocumentFlags(p))
Output:

DISCONNECT ---- 2 bytes
           3210 Size

3-0 reserved

func (*Disconnect) UnmarshalBinary

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

func (*Disconnect) WriteTo

func (p *Disconnect) WriteTo(w io.Writer) (int64, error)

type FilterOption added in v0.16.0

type FilterOption byte

Filter option, used in Subscribe

type HasPacketID added in v0.17.0

type HasPacketID interface {
	PacketID() uint16
}

HasPacketID is implemented by packets carrying a packet ID.

type HasReason added in v0.25.0

type HasReason interface {
	ReasonCode() ReasonCode
}

HasReason is implemented by packets carrying a reason code.

type HasWellFormed added in v0.26.0

type HasWellFormed interface {
	WellFormed() *Malformed
}

HasWellFormed is implemented by packets that implement WellFormed.

type Ident

type Ident uint8

MQTT Packet UserProp identifier codes Ident is the same as wuint16 but is used to name the identifier codes

const (
	PayloadFormatIndicator Ident = 0x01
	MessageExpiryInterval  Ident = 0x02
	ContentType            Ident = 0x03
	ResponseTopic          Ident = 0x08
	CorrelationData        Ident = 0x09
	SubscriptionID         Ident = 0x0b
	SessionExpiryInterval  Ident = 0x11
	AssignedClientID       Ident = 0x12
	ServerKeepAlive        Ident = 0x13
	AuthMethod             Ident = 0x15
	AuthData               Ident = 0x16
	RequestProblemInfo     Ident = 0x17
	WillDelayInterval      Ident = 0x18
	RequestResponseInfo    Ident = 0x19
	ResponseInformation    Ident = 0x1a
	ServerReference        Ident = 0x1c
	ReasonString           Ident = 0x1f
	ReceiveMax             Ident = 0x21
	TopicAliasMax          Ident = 0x22
	TopicAlias             Ident = 0x23
	MaxQoS                 Ident = 0x24
	RetainAvailable        Ident = 0x25
	UserProperty           Ident = 0x26
	MaxPacketSize          Ident = 0x27
	WildcardSubAvailable   Ident = 0x28
	SubIDsAvailable        Ident = 0x29
	SharedSubAvailable     Ident = 0x2a
)

func (*Ident) UnmarshalBinary

func (v *Ident) UnmarshalBinary(data []byte) error

type Malformed

type Malformed struct {
	// contains filtered or unexported fields
}

func (*Malformed) Error

func (e *Malformed) Error() string
Example
var e Malformed
e.SetPacket(NewConnect())
e.SetReasonString("missing data")
fmt.Println(e.Error())
Output:

malformed *mq.Connect: missing data

func (*Malformed) SetPacket added in v0.24.0

func (e *Malformed) SetPacket(p Packet)

func (*Malformed) SetReasonString added in v0.25.0

func (e *Malformed) SetReasonString(v string)

type Opt added in v0.16.0

type Opt = FilterOption
const (
	OptQoS1    Opt = 1
	OptQoS2    Opt = 2
	OptQoS3    Opt = 3 // malformed
	OptNL      Opt = 1 << 2
	OptRAP     Opt = 1 << 3
	OptRetain1 Opt = 1 << 4
	OptRetain2 Opt = 2 << 4
	OptRetain3 Opt = 3 << 4 // malformed
)

type Packet

type Packet = ControlPacket

Packet and ControlPacket can be used interchangebly.

type PingReq

type PingReq struct {
	// contains filtered or unexported fields
}

func NewPingReq

func NewPingReq() *PingReq

func (*PingReq) String

func (p *PingReq) String() string
Example
p := NewPingReq()
fmt.Println(p)
fmt.Print(DocumentFlags(p))
Output:

PINGREQ ---- 2 bytes
        3210 Size

3-0 reserved

func (*PingReq) UnmarshalBinary

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

func (*PingReq) WriteTo

func (p *PingReq) WriteTo(w io.Writer) (int64, error)

type PingResp

type PingResp struct {
	// contains filtered or unexported fields
}

func NewPingResp

func NewPingResp() *PingResp

func (*PingResp) String

func (p *PingResp) String() string
Example
p := NewPingResp()
fmt.Println(p)
fmt.Print(DocumentFlags(p))
Output:

PINGRESP ---- 2 bytes
         3210 Size

3-0 reserved

func (*PingResp) UnmarshalBinary

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

func (*PingResp) WriteTo

func (p *PingResp) WriteTo(w io.Writer) (int64, error)

type PubAck

type PubAck struct {
	UserProperties
	// contains filtered or unexported fields
}

A PubAck packet is the response to a Publish packets, depending on the fixed header it can be one of PUBACK, PUBREC, PUBREL or PUBCOMP

func NewPubAck

func NewPubAck() *PubAck

NewPubAck returns control packet with type PUBACK

func (*PubAck) PacketID

func (p *PubAck) PacketID() uint16

func (*PubAck) ReasonCode

func (p *PubAck) ReasonCode() ReasonCode

func (*PubAck) ReasonString added in v0.25.0

func (p *PubAck) ReasonString() string

func (*PubAck) SetPacketID

func (p *PubAck) SetPacketID(v uint16)

func (*PubAck) SetReasonCode

func (p *PubAck) SetReasonCode(v ReasonCode)

func (*PubAck) SetReasonString added in v0.25.0

func (p *PubAck) SetReasonString(v string)

func (*PubAck) String

func (p *PubAck) String() string
Example
p := NewPubAck()
p.SetPacketID(9)
fmt.Println(p)
fmt.Print(DocumentFlags(p))
Output:

PUBACK ---- p9 4 bytes
       3210 PacketID ReasonString Size [reason text]

3-0 reserved

func (*PubAck) UnmarshalBinary

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

func (*PubAck) WriteTo

func (p *PubAck) WriteTo(w io.Writer) (int64, error)

type PubComp added in v0.23.0

type PubComp struct {
	UserProperties
	// contains filtered or unexported fields
}

A PubComp packet is the response to a Publish packets, depending on the fixed header it can be one of PUBACK, PUBREC, PUBREL or PUBCOMP

func NewPubComp

func NewPubComp() *PubComp

NewPubComp returns control packet with type PUBCOMP

Example
p := NewPubComp()
p.SetPacketID(9)
fmt.Println(p)
Output:

PUBCOMP ---- p9 Success 4 bytes

func (*PubComp) PacketID added in v0.23.0

func (p *PubComp) PacketID() uint16

func (*PubComp) ReasonCode added in v0.23.0

func (p *PubComp) ReasonCode() ReasonCode

func (*PubComp) ReasonString added in v0.25.0

func (p *PubComp) ReasonString() string

func (*PubComp) SetPacketID added in v0.23.0

func (p *PubComp) SetPacketID(v uint16)

func (*PubComp) SetReasonCode added in v0.23.0

func (p *PubComp) SetReasonCode(v ReasonCode)

func (*PubComp) SetReasonString added in v0.25.0

func (p *PubComp) SetReasonString(v string)

func (*PubComp) String added in v0.23.0

func (p *PubComp) String() string

func (*PubComp) UnmarshalBinary added in v0.23.0

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

func (*PubComp) WriteTo added in v0.23.0

func (p *PubComp) WriteTo(w io.Writer) (int64, error)

type PubRec added in v0.23.0

type PubRec struct {
	UserProperties
	// contains filtered or unexported fields
}

A PubRec packet is the response to a Publish packets, depending on the fixed header it can be one of PUBACK, PUBREC, PUBREL or PUBCOMP

func NewPubRec

func NewPubRec() *PubRec

NewPubRec returns control packet with type PUBREC

Example
p := NewPubRec()
p.SetPacketID(9)
fmt.Println(p)
Output:

PUBREC ---- p9 Success 4 bytes

func (*PubRec) PacketID added in v0.23.0

func (p *PubRec) PacketID() uint16

func (*PubRec) ReasonCode added in v0.23.0

func (p *PubRec) ReasonCode() ReasonCode

func (*PubRec) ReasonString added in v0.25.0

func (p *PubRec) ReasonString() string

func (*PubRec) SetPacketID added in v0.23.0

func (p *PubRec) SetPacketID(v uint16)

func (*PubRec) SetReasonCode added in v0.23.0

func (p *PubRec) SetReasonCode(v ReasonCode)

func (*PubRec) SetReasonString added in v0.25.0

func (p *PubRec) SetReasonString(v string)

func (*PubRec) String added in v0.23.0

func (p *PubRec) String() string

func (*PubRec) UnmarshalBinary added in v0.23.0

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

func (*PubRec) WriteTo added in v0.23.0

func (p *PubRec) WriteTo(w io.Writer) (int64, error)

type PubRel added in v0.21.0

type PubRel struct {
	UserProperties
	// contains filtered or unexported fields
}

A PubRel packet is the response to a Publish packets, depending on the fixed header it can be one of PUBACK, PUBREC, PUBREL or PUBCOMP

func NewPubRel

func NewPubRel() *PubRel

NewPubRel returns control packet with type PUBREL

Example
p := NewPubRel()
p.SetPacketID(9)
p.SetReasonCode(PacketIdentifierNotFound)
fmt.Println(p)
Output:

PUBREL --1- p9 5 bytes PacketIdentifierNotFound!

func (*PubRel) PacketID added in v0.23.0

func (p *PubRel) PacketID() uint16

func (*PubRel) ReasonCode added in v0.23.0

func (p *PubRel) ReasonCode() ReasonCode

func (*PubRel) ReasonString added in v0.25.0

func (p *PubRel) ReasonString() string

func (*PubRel) SetPacketID added in v0.23.0

func (p *PubRel) SetPacketID(v uint16)

func (*PubRel) SetReasonCode added in v0.23.0

func (p *PubRel) SetReasonCode(v ReasonCode)

func (*PubRel) SetReasonString added in v0.25.0

func (p *PubRel) SetReasonString(v string)

func (*PubRel) String added in v0.23.0

func (p *PubRel) String() string

func (*PubRel) UnmarshalBinary added in v0.23.0

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

func (*PubRel) WriteTo added in v0.23.0

func (p *PubRel) WriteTo(w io.Writer) (int64, error)

type Publish

type Publish struct {
	UserProperties
	// contains filtered or unexported fields
}
Example (StringMalformed)
fmt.Println(Pub(0, "", "gopher"))
fmt.Println(Pub(3, "a/b", "gopher"))
fmt.Println(Pub(1, "a/b", "gopher"))
Output:

PUBLISH ---- p0  11 bytes, malformed! empty topic name
PUBLISH -!!- p0 a/b 14 bytes, malformed! invalid QoS
PUBLISH --1- p0 a/b 16 bytes, malformed! empty packet ID

func NewPublish

func NewPublish() *Publish

func Pub added in v0.19.0

func Pub(qos uint8, topic, payload string) *Publish

Pub is a convenience method for creating a publish packet.

func (*Publish) AddSubscriptionID

func (p *Publish) AddSubscriptionID(v uint32)

func (*Publish) ContentType

func (p *Publish) ContentType() string

func (*Publish) CorrelationData

func (p *Publish) CorrelationData() []byte

func (*Publish) Duplicate

func (p *Publish) Duplicate() bool

func (*Publish) MessageExpiryInterval

func (p *Publish) MessageExpiryInterval() uint32

func (*Publish) PacketID

func (p *Publish) PacketID() uint16

func (*Publish) Payload

func (p *Publish) Payload() []byte

func (*Publish) PayloadFormat

func (p *Publish) PayloadFormat() bool

func (*Publish) QoS

func (p *Publish) QoS() uint8

func (*Publish) ResponseTopic

func (p *Publish) ResponseTopic() string

func (*Publish) Retain

func (p *Publish) Retain() bool

func (*Publish) SetContentType

func (p *Publish) SetContentType(v string)

The value of the Content Type is defined by the sending and receiving application, e.g. it may be a mime type like application/json.

func (*Publish) SetCorrelationData

func (p *Publish) SetCorrelationData(v []byte)

func (*Publish) SetDuplicate

func (p *Publish) SetDuplicate(v bool)

func (*Publish) SetMessageExpiryInterval

func (p *Publish) SetMessageExpiryInterval(v uint32)

func (*Publish) SetPacketID

func (p *Publish) SetPacketID(v uint16)

func (*Publish) SetPayload

func (p *Publish) SetPayload(v []byte)

func (*Publish) SetPayloadFormat

func (p *Publish) SetPayloadFormat(v bool)

SetPayloadFormat, false indicates that the message is unspecified bytes. True indicates that the message is UTF-8 encoded character data.

func (*Publish) SetQoS

func (p *Publish) SetQoS(v uint8)

SetQoS, 0,1,2 or 3 other values unset the QoS. 3 is malformed but allowed to be set here.

func (*Publish) SetResponseTopic

func (p *Publish) SetResponseTopic(v string)

func (*Publish) SetRetain

func (p *Publish) SetRetain(v bool)

func (*Publish) SetTopicAlias

func (p *Publish) SetTopicAlias(v uint16)

func (*Publish) SetTopicName

func (p *Publish) SetTopicName(v string)

func (*Publish) String

func (p *Publish) String() string
Example
p := Pub(2, "a/b/1", "gopher")
p.SetPacketID(3)
p.SetRetain(true)
p.SetCorrelationData([]byte("1111-222222-3333333"))
fmt.Println(p)
fmt.Print(DocumentFlags(p))
Output:

PUBLISH -2-r p3 a/b/1 1111-222222-3333333 40 bytes
        3210 PacketID Topic [CorrelationData] Size

3 d   Duplicate
2 2|! QoS
1 1|! QoS
0 r   Retain

func (*Publish) SubscriptionIDs

func (p *Publish) SubscriptionIDs() []uint32

func (*Publish) TopicAlias

func (p *Publish) TopicAlias() uint16

func (*Publish) TopicName

func (p *Publish) TopicName() string

func (*Publish) UnmarshalBinary

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

func (*Publish) WellFormed added in v0.21.0

func (p *Publish) WellFormed() *Malformed

func (*Publish) WriteTo

func (p *Publish) WriteTo(w io.Writer) (int64, error)

type ReasonCode

type ReasonCode byte
const (
	Success               ReasonCode = 0x00 // ConnAck, PubAck, PubRec, PubRel, PubComp, UnsubAck, Auth
	NormalDisconnect      ReasonCode = 0x00 // Disconnect
	GrantedQoS0           ReasonCode = 0x00 // SubAck
	GrantedQoS1           ReasonCode = 0x01 // SubAck
	GrantedQoS2           ReasonCode = 0x02 // SubAck
	DisconnectWithWill    ReasonCode = 0x04 // Disconnect
	NoMatchingSubscribers ReasonCode = 0x10 // PubAck, PubRec
	NoSubscriptionExisted ReasonCode = 0x11 // UnsubAck
	ContinueAuth          ReasonCode = 0x18 // Auth
	ReAuthenticate        ReasonCode = 0x19 // Auth

	// failures >= 0x80
	UnspecifiedError                    ReasonCode = 0x80 // ConnAck, PubAck, PubRec, SubAck, UnsubAck, Disconnect
	MalformedPacket                     ReasonCode = 0x81 // ConnAck, Disconnect
	ProtocolError                       ReasonCode = 0x82 // ConnAck, Disconnect
	ImplementationSpecificError         ReasonCode = 0x83 // ConnAck, PubAck, PubRec, SubAck, UnsubAck, Disconnect
	UnsupportedProtocolVersion          ReasonCode = 0x84 // ConnAck
	ClientIdentifierNotValid            ReasonCode = 0x85 // ConnAck
	BadUserNameOrPassword               ReasonCode = 0x86 // ConnAck
	NotAuthorized                       ReasonCode = 0x87 // ConnAck, PubAck, PubRec, SubAck, UnsubAck, Disconnect
	ServerUnavailable                   ReasonCode = 0x88 // ConnAck
	ServerBusy                          ReasonCode = 0x89 // ConnAck, Disconnect
	Banned                              ReasonCode = 0x8A // ConnAck
	ServerShuttingDown                  ReasonCode = 0x8B // Disconnect
	BadAuthenticationMethod             ReasonCode = 0x8C // ConnAck, Disconnect
	KeepAliveTimeout                    ReasonCode = 0x8D // Disconnect
	SessionTakenOver                    ReasonCode = 0x8E // Disconnect
	TopicFilterInvalid                  ReasonCode = 0x8F // SubAck, UnsubAck, Disconnect
	TopicNameInvalid                    ReasonCode = 0x90 // ConnAck, PubAck, PubRec, Disconnect
	PacketIdentifierInUse               ReasonCode = 0x91 // PubAck, PubRec, SubAck, UnsubAck
	PacketIdentifierNotFound            ReasonCode = 0x92 // PubRel, PubComp
	ReceiveMaximumExceeded              ReasonCode = 0x93 // Disconnect
	TopicAliasInvalid                   ReasonCode = 0x94 // Disconnect
	PacketTooLarge                      ReasonCode = 0x95 // ConnAck, Disconnect
	MessageRateToHigh                   ReasonCode = 0x96 // Disconnect
	QuotaExceeded                       ReasonCode = 0x97 // ConnAck, PubAck, PubRec, SubAck, Disconnect
	AdministrativeAction                ReasonCode = 0x98 // Disconnect
	PayloadFormatInvalid                ReasonCode = 0x99 // ConnAck, PubAck, PubRec, Disconnect
	RetainNotSupported                  ReasonCode = 0x9A // ConnAck, Disconnect
	QoSNotSupported                     ReasonCode = 0x9B // ConnAck, Disconnect
	UseAnotherServer                    ReasonCode = 0x9C // ConnAck, Disconnect
	ServerMoved                         ReasonCode = 0x9D // ConnAck, Disconnect
	SharedSubscriptionsNotSupported     ReasonCode = 0x9E // SubAck, Disconnect
	ConnectionRateExceeded              ReasonCode = 0x9F // ConnAck, Disconnect
	MaximumConnectTime                  ReasonCode = 0xA0 // Disconnect
	SubscriptionIdentifiersNotSupported ReasonCode = 0xA1 // SubAck, Disconnect
	WildcardSubscriptionsNotSupported   ReasonCode = 0xA2 // SubAck, Disconnect
)

func (ReasonCode) String

func (i ReasonCode) String() string

type SubAck

type SubAck struct {
	UserProperties
	// contains filtered or unexported fields
}

func NewSubAck

func NewSubAck() *SubAck

NewSubAck returns a suback packet without reason codes.

func (*SubAck) AddReasonCode

func (p *SubAck) AddReasonCode(v ReasonCode)

func (*SubAck) PacketID

func (p *SubAck) PacketID() uint16

func (*SubAck) ReasonCodes

func (p *SubAck) ReasonCodes() []uint8

func (*SubAck) ReasonString

func (p *SubAck) ReasonString() string

func (*SubAck) SetPacketID

func (p *SubAck) SetPacketID(v uint16)

func (*SubAck) SetReasonString

func (p *SubAck) SetReasonString(v string)

func (*SubAck) String

func (p *SubAck) String() string
Example
p := NewSubAck()
p.SetPacketID(3)
fmt.Println(p)
fmt.Print(DocumentFlags(p))
Output:

SUBACK ---- p3 5 bytes
       3210 PacketID Size

3-0 reserved

func (*SubAck) UnmarshalBinary

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

func (*SubAck) WriteTo

func (p *SubAck) WriteTo(w io.Writer) (int64, error)

type Subscribe

type Subscribe struct {
	UserProperties
	// contains filtered or unexported fields
}
Example (Malformed)
// no filters
fmt.Println(NewSubscribe())
{ // bad qos
	s := NewSubscribe()
	s.SetSubscriptionID(1)
	s.AddFilters(NewTopicFilter("a/b", OptQoS3))
	fmt.Println(s)
}
{ // empty filter
	s := NewSubscribe()
	s.SetSubscriptionID(1)
	s.AddFilters(NewTopicFilter("", OptQoS1))
	fmt.Println(s)
}
{ // missing subscription id
	s := NewSubscribe()
	s.SetSubscriptionID(0)
	s.AddFilters(NewTopicFilter("#", OptQoS1))
	fmt.Println(s)
}
{ // too large subscription id
	s := NewSubscribe()
	s.SetSubscriptionID(268_435_455 + 1)
	s.AddFilters(NewTopicFilter("#", OptQoS1))
	fmt.Println(s)
}
Output:

SUBSCRIBE --1- p0  5 bytes, malformed! no filters
SUBSCRIBE --1- p0 a/b --r0--!! 13 bytes, malformed! invalid QoS
SUBSCRIBE --1- p0  --r0---1 10 bytes, malformed! empty filter
SUBSCRIBE --1- p0 # --r0---1 9 bytes, malformed! missing sub ID
SUBSCRIBE --1- p0 # --r0---1 15 bytes, malformed! too large sub ID

func NewSubscribe

func NewSubscribe() *Subscribe

func (*Subscribe) AddFilters added in v0.26.0

func (p *Subscribe) AddFilters(v ...TopicFilter)

func (*Subscribe) Filters added in v0.24.0

func (p *Subscribe) Filters() []TopicFilter

func (*Subscribe) PacketID

func (p *Subscribe) PacketID() uint16

func (*Subscribe) SetPacketID

func (p *Subscribe) SetPacketID(v uint16)

func (*Subscribe) SetSubscriptionID

func (p *Subscribe) SetSubscriptionID(v int)

func (*Subscribe) String

func (p *Subscribe) String() string
Example
s := NewSubscribe()
s.SetSubscriptionID(39)
s.AddUserProp("color", "purple")
s.AddFilters(
	NewTopicFilter("a/b/c", OptQoS2|OptNL|OptRAP),
	NewTopicFilter("d/e", OptQoS1),
)
fmt.Println(s)
Output:

SUBSCRIBE --1- p0 a/b/c --r0pn2- 37 bytes

func (*Subscribe) SubscriptionID

func (p *Subscribe) SubscriptionID() int

func (*Subscribe) UnmarshalBinary

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

func (*Subscribe) WellFormed added in v0.26.0

func (p *Subscribe) WellFormed() *Malformed

func (*Subscribe) WriteTo

func (p *Subscribe) WriteTo(w io.Writer) (int64, error)

type TopicFilter

type TopicFilter struct {
	// contains filtered or unexported fields
}

func NewTopicFilter

func NewTopicFilter(filter string, options Opt) TopicFilter

func (*TopicFilter) Filter added in v0.26.0

func (c *TopicFilter) Filter() string

func (*TopicFilter) Options added in v0.26.0

func (c *TopicFilter) Options() Opt

func (*TopicFilter) SetFilter added in v0.26.0

func (c *TopicFilter) SetFilter(v string)

func (*TopicFilter) SetOptions added in v0.26.0

func (c *TopicFilter) SetOptions(v Opt)

func (TopicFilter) String

func (c TopicFilter) String() string
Example
fmt.Println(NewTopicFilter("gopher/pink/#", OptQoS1|OptRetain1))
Output:

gopher/pink/# --r1---1

func (*TopicFilter) WellFormed added in v0.26.0

func (c *TopicFilter) WellFormed() *Malformed

type Undefined

type Undefined struct {
	// contains filtered or unexported fields
}

Undefined represents a packet with type value of 0.

func (*Undefined) Data added in v0.24.0

func (p *Undefined) Data() []byte

func (*Undefined) String

func (p *Undefined) String() string

func (*Undefined) UnmarshalBinary

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

func (*Undefined) WriteTo

func (p *Undefined) WriteTo(w io.Writer) (int64, error)

type UnsubAck

type UnsubAck struct {
	UserProperties
	// contains filtered or unexported fields
}

func NewUnsubAck

func NewUnsubAck() *UnsubAck

func (*UnsubAck) AddReasonCode

func (p *UnsubAck) AddReasonCode(v ReasonCode)

func (*UnsubAck) PacketID

func (p *UnsubAck) PacketID() uint16

func (*UnsubAck) ReasonCodes

func (p *UnsubAck) ReasonCodes() []uint8

func (*UnsubAck) ReasonString

func (p *UnsubAck) ReasonString() string

func (*UnsubAck) SetPacketID

func (p *UnsubAck) SetPacketID(v uint16)

func (*UnsubAck) SetReasonString

func (p *UnsubAck) SetReasonString(v string)

func (*UnsubAck) String

func (p *UnsubAck) String() string

func (*UnsubAck) UnmarshalBinary

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

func (*UnsubAck) WriteTo

func (p *UnsubAck) WriteTo(w io.Writer) (int64, error)

type Unsubscribe

type Unsubscribe struct {
	UserProperties
	// contains filtered or unexported fields
}

func NewUnsubscribe

func NewUnsubscribe() *Unsubscribe

func (*Unsubscribe) AddFilter

func (p *Unsubscribe) AddFilter(filter string)

func (*Unsubscribe) PacketID

func (p *Unsubscribe) PacketID() uint16

func (*Unsubscribe) SetPacketID

func (p *Unsubscribe) SetPacketID(v uint16)

func (*Unsubscribe) String

func (p *Unsubscribe) String() string

func (*Unsubscribe) UnmarshalBinary

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

func (*Unsubscribe) WriteTo

func (p *Unsubscribe) WriteTo(w io.Writer) (int64, error)

type UserProp added in v0.21.0

type UserProp [2]string

https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901013

func (UserProp) String added in v0.21.0

func (v UserProp) String() string

func (*UserProp) UnmarshalBinary added in v0.21.0

func (v *UserProp) UnmarshalBinary(data []byte) error

type UserProperties added in v0.21.0

type UserProperties []UserProp

func (*UserProperties) AddUserProp added in v0.21.0

func (p *UserProperties) AddUserProp(kvPair ...string)

AddUserProp adds key value pair user properties. The same key is is allowed to appear more than once.

Example
var u UserProperties
u.AddUserProp(
	"size", "large",
	"color", "red",
)
fmt.Println(u)
Output:

[size:large color:red]

Jump to

Keyboard shortcuts

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