packets

package
v0.1.4-0...-09d983b Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeAccepted                    = 0x00
	CodeUnacceptableProtocolVersion = 0x01
	CodeIdentifierRejected          = 0x02
	CodeServerUnavaliable           = 0x03
	CodeBadUsernameorPsw            = 0x04
	CodeNotAuthorized               = 0x05
)

There are the possible code in the connack packet.

View Source
const (
	RESERVED = iota
	CONNECT
	CONNACK
	PUBLISH
	PUBACK
	PUBREC
	PUBREL
	PUBCOMP
	SUBSCRIBE
	SUBACK
	UNSUBSCRIBE
	UNSUBACK
	PINGREQ
	PINGRESP
	DISCONNECT
)

Packet type

View Source
const (
	FLAG_RESERVED    = 0
	FLAG_SUBSCRIBE   = 2
	FLAG_UNSUBSCRIBE = 2
	FLAG_PUBREL      = 2
)

Flag in the FixHeader

View Source
const (
	QOS_0             uint8 = 0x00
	QOS_1             uint8 = 0x01
	QOS_2             uint8 = 0x02
	SUBSCRIBE_FAILURE       = 0x80
)

QoS levels & Subscribe failure

Variables

View Source
var (
	ErrInvalPacketType           = errors.New("invalid Packet Type")
	ErrInvalFlags                = errors.New("invalid Flags")
	ErrInvalConnFlags            = errors.New("invalid Connect Flags")
	ErrInvalConnAcknowledgeFlags = errors.New("invalid Connect Acknowledge Flags")
	ErrInvalSessionPresent       = errors.New("invalid Session Present")
	ErrInvalRemainLength         = errors.New("Malformed Remaining Length")
	ErrInvalProtocolName         = errors.New("invalid protocol name")
	ErrInvalUtf8                 = errors.New("invalid utf-8 string")
	ErrInvalTopicName            = errors.New("invalid topic name")
	ErrInvalTopicFilter          = errors.New("invalid topic filter")
	ErrInvalQos                  = errors.New("invalid Qos,only support qos0 | qos1 | qos2")
	ErrInvalWillQos              = errors.New("invalid Will Qos")
	ErrInvalWillRetain           = errors.New("invalid Will Retain")
	ErrInvalUTF8String           = errors.New("invalid utf-8 string")
)

Error type

Functions

func DecodeRemainLength

func DecodeRemainLength(length int) ([]byte, error)

DecodeRemainLength 将remain length 转成byte表示

DecodeRemainLength puts the length int into bytes

func DecodeUTF8String

func DecodeUTF8String(buf []byte) (b []byte, size int, err error)

DecodeUTF8String decodes the UTF-8 encoded strings into bytes, returns the decoded bytes, bytes size and error.

func EncodeRemainLength

func EncodeRemainLength(r *bufio.Reader) (int, error)

EncodeRemainLength 读remainLength,如果格式错误返回 error

EncodeRemainLength reads the remain length bytes from bufio.Reader and returns length int.

func EncodeUTF8String

func EncodeUTF8String(buf []byte) (b []byte, size int, err error)

EncodeUTF8String encodes the bytes into UTF-8 encoded strings, returns the encoded bytes, bytes size and error.

func TopicMatch

func TopicMatch(topic []byte, topicFilter []byte) bool

TopicMatch 返回topic和topic filter是否

TopicMatch returns whether the topic and topic filter is matched.

func TotalBytes

func TotalBytes(p Packet) uint

TotalBytes returns how many bytes of the packet

func ValidTopicFilter

func ValidTopicFilter(p []byte) bool

ValidTopicFilter 验证主题过滤器是否合法

ValidTopicFilter returns whether the bytes is a valid topic filter. [MQTT-4.7.1-2] [MQTT-4.7.1-3]

func ValidTopicName

func ValidTopicName(p []byte) bool

ValidTopicName 验证主题名是否合法 [MQTT-4.7.1-1]

ValidTopicName returns whether the bytes is a valid topic name.[MQTT-4.7.1-1].

func ValidUTF8

func ValidUTF8(p []byte) bool

ValidUTF8 验证是否utf8

ValidUTF8 returns whether the given bytes is in UTF-8 form.

Types

type Connack

type Connack struct {
	FixHeader      *FixHeader
	Code           byte
	SessionPresent int
}

Connack represents the MQTT Connack packet

func NewConnackPacket

func NewConnackPacket(fh *FixHeader, r io.Reader) (*Connack, error)

NewConnackPacket returns a Connack instance by the given FixHeader and io.Reader

func (*Connack) Pack

func (c *Connack) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Connack) String

func (c *Connack) String() string

func (*Connack) Unpack

func (c *Connack) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct

type Connect

type Connect struct {
	FixHeader *FixHeader
	//Variable header
	ProtocolLevel byte
	//Connect Flags
	UsernameFlag bool
	ProtocolName []byte
	PasswordFlag bool
	WillRetain   bool
	WillQos      uint8
	WillFlag     bool
	WillTopic    []byte
	WillMsg      []byte
	CleanSession bool
	KeepAlive    uint16 //如果非零,1.5倍时间没收到则断开连接[MQTT-3.1.2-24]
	//if set
	ClientID []byte
	Username []byte
	Password []byte
	AckCode  uint8 //ack的返回码
}

Connect represents the MQTT Connect packet

func NewConnectPacket

func NewConnectPacket(fh *FixHeader, r io.Reader) (*Connect, error)

NewConnectPacket returns a Connect instance by the given FixHeader and io.Reader

func (*Connect) NewConnackPacket

func (c *Connect) NewConnackPacket(sessionReuse bool) *Connack

NewConnackPacket returns the Connack struct which is the ack packet of the Connect packet.

func (*Connect) Pack

func (c *Connect) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Connect) String

func (c *Connect) String() string

String is mainly used in logging, debugging and testing.

func (*Connect) Unpack

func (c *Connect) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Disconnect

type Disconnect struct {
	FixHeader *FixHeader
}

Disconnect represents the MQTT Disconnect packet

func NewDisConnectPackets

func NewDisConnectPackets(fh *FixHeader, r io.Reader) (*Disconnect, error)

NewDisConnectPackets returns a Disconnect instance by the given FixHeader and io.Reader

func (*Disconnect) Pack

func (d *Disconnect) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Disconnect) String

func (d *Disconnect) String() string

func (*Disconnect) Unpack

func (d *Disconnect) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type FixHeader

type FixHeader struct {
	PacketType   byte
	Flags        byte
	RemainLength int
}

FixHeader represents the FixHeader of the MQTT packet

func (*FixHeader) Pack

func (fh *FixHeader) Pack(w io.Writer) error

Pack encodes the FixHeader struct into bytes and writes it into io.Writer.

type Message

type Message interface {
	Dup() bool
	Qos() uint8
	Retained() bool
	Topic() string
	PacketID() PacketID
	Payload() []byte
}

type Packet

type Packet interface {
	// Pack encodes the packet struct into bytes and writes it into io.Writer.
	Pack(w io.Writer) error
	// Unpack read the packet bytes from io.Reader and decodes it into the packet struct
	Unpack(r io.Reader) error
	// String is mainly used in logging, debugging and testing.
	String() string
}

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

func NewPacket

func NewPacket(fh *FixHeader, r io.Reader) (Packet, error)

NewPacket returns a packet representing the decoded MQTT packet and an error.

type PacketID

type PacketID = uint16

PacketID is the type of packet identifier

const (
	MAX_PACKET_ID PacketID = 65535
	MIN_PACKET_ID PacketID = 1
)

Max & min packet ID

type Pingreq

type Pingreq struct {
	FixHeader *FixHeader
}

Pingreq represents the MQTT Pingreq packet

func NewPingreqPacket

func NewPingreqPacket(fh *FixHeader, r io.Reader) (*Pingreq, error)

NewPingreqPacket returns a Pingreq instance by the given FixHeader and io.Reader

func (*Pingreq) NewPingresp

func (p *Pingreq) NewPingresp() *Pingresp

NewPingresp returns a Pingresp struct

func (*Pingreq) Pack

func (p *Pingreq) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Pingreq) String

func (p *Pingreq) String() string

func (*Pingreq) Unpack

func (p *Pingreq) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Pingresp

type Pingresp struct {
	FixHeader *FixHeader
}

Pingresp represents the MQTT Pingresp packet

func NewPingrespPacket

func NewPingrespPacket(fh *FixHeader, r io.Reader) (*Pingresp, error)

NewPingrespPacket returns a Pingresp instance by the given FixHeader and io.Reader

func (*Pingresp) Pack

func (p *Pingresp) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Pingresp) String

func (p *Pingresp) String() string

func (*Pingresp) Unpack

func (p *Pingresp) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Puback

type Puback struct {
	FixHeader *FixHeader
	PacketID
}

Puback represents the MQTT Puback packet

func NewPubackPacket

func NewPubackPacket(fh *FixHeader, r io.Reader) (*Puback, error)

NewPubackPacket returns a Puback instance by the given FixHeader and io.Reader

func (*Puback) Pack

func (p *Puback) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Puback) String

func (p *Puback) String() string

func (*Puback) Unpack

func (p *Puback) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Pubcomp

type Pubcomp struct {
	FixHeader *FixHeader
	PacketID
}

Pubcomp represents the MQTT Pubcomp packet

func NewPubcompPacket

func NewPubcompPacket(fh *FixHeader, r io.Reader) (*Pubcomp, error)

NewPubcompPacket returns a Pubcomp instance by the given FixHeader and io.Reader

func (*Pubcomp) Pack

func (p *Pubcomp) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Pubcomp) String

func (p *Pubcomp) String() string

func (*Pubcomp) Unpack

func (p *Pubcomp) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Publish

type Publish struct {
	FixHeader *FixHeader
	Dup       bool   //是否重发 [MQTT-3.3.1.-1]
	Qos       uint8  //qos等级
	Retain    bool   //是否保留消息
	TopicName []byte //主题名
	PacketID         //报文标识符
	Payload   []byte
}

Publish represents the MQTT Publish packet

func NewPublishPacket

func NewPublishPacket(fh *FixHeader, r io.Reader) (*Publish, error)

NewPublishPacket returns a Publish instance by the given FixHeader and io.Reader.

func (*Publish) CopyPublish

func (p *Publish) CopyPublish() *Publish

CopyPublish 将 publish 复制一份

CopyPublish returns the copied publish struct for distribution

func (*Publish) NewPuback

func (p *Publish) NewPuback() *Puback

NewPuback returns the puback struct related to the publish struct in QoS 1

func (*Publish) NewPubrec

func (p *Publish) NewPubrec() *Pubrec

NewPubrec returns the pubrec struct related to the publish struct in QoS 2

func (*Publish) Pack

func (p *Publish) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Publish) String

func (p *Publish) String() string

func (*Publish) Unpack

func (p *Publish) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Pubrec

type Pubrec struct {
	FixHeader *FixHeader
	PacketID  PacketID
}

Pubrec represents the MQTT Pubrec packet.

func NewPubrecPacket

func NewPubrecPacket(fh *FixHeader, r io.Reader) (*Pubrec, error)

NewPubrecPacket returns a Pubrec instance by the given FixHeader and io.Reader.

func (*Pubrec) NewPubrel

func (p *Pubrec) NewPubrel() *Pubrel

NewPubrel returns the Pubrel struct related to the Pubrec struct in QoS 2.

func (*Pubrec) Pack

func (p *Pubrec) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Pubrec) String

func (p *Pubrec) String() string

func (*Pubrec) Unpack

func (p *Pubrec) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Pubrel

type Pubrel struct {
	FixHeader *FixHeader
	PacketID  PacketID
}

Pubrel represents the MQTT Pubrel packet

func NewPubrelPacket

func NewPubrelPacket(fh *FixHeader, r io.Reader) (*Pubrel, error)

NewPubrelPacket returns a Pubrel instance by the given FixHeader and io.Reader.

func (*Pubrel) NewPubcomp

func (p *Pubrel) NewPubcomp() *Pubcomp

NewPubcomp returns the Pubcomp struct related to the Pubrel struct in QoS 2.

func (*Pubrel) Pack

func (p *Pubrel) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Pubrel) String

func (p *Pubrel) String() string

func (*Pubrel) Unpack

func (p *Pubrel) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type ReadWriter

type ReadWriter struct {
	*Reader
	*Writer
}

ReadWriter warps Reader and Writer.

type Reader

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

Reader is used to read data from bufio.Reader and create MQTT packet instance.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new Reader.

func (*Reader) ReadPacket

func (r *Reader) ReadPacket() (Packet, error)

ReadPacket reads data from Reader and returns a Packet instance. If any errors occurs, returns nil, error

type Suback

type Suback struct {
	FixHeader *FixHeader
	PacketID  PacketID
	Payload   []byte
}

Suback represents the MQTT Suback packet.

func NewSubackPacket

func NewSubackPacket(fh *FixHeader, r io.Reader) (*Suback, error)

NewSubackPacket returns a Suback instance by the given FixHeader and io.Reader.

func (*Suback) Pack

func (p *Suback) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Suback) String

func (p *Suback) String() string

func (*Suback) Unpack

func (p *Suback) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Subscribe

type Subscribe struct {
	FixHeader *FixHeader
	PacketID  PacketID

	Topics []Topic //suback响应之前填充
}

Subscribe represents the MQTT Subscribe packet.

func NewSubscribePacket

func NewSubscribePacket(fh *FixHeader, r io.Reader) (*Subscribe, error)

NewSubscribePacket returns a Subscribe instance by the given FixHeader and io.Reader.

func (*Subscribe) NewSubBack

func (p *Subscribe) NewSubBack() *Suback

NewSubBack returns the Suback struct which is the ack packet of the Subscribe packet.

func (*Subscribe) Pack

func (p *Subscribe) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Subscribe) String

func (p *Subscribe) String() string

func (*Subscribe) Unpack

func (p *Subscribe) Unpack(r io.Reader) (err error)

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Topic

type Topic struct {
	Qos  uint8
	Name string
}

Topic represents the MQTT Topic

type Unsuback

type Unsuback struct {
	FixHeader *FixHeader
	PacketID  PacketID
}

Unsuback represents the MQTT Unsuback packet.

func NewUnsubackPacket

func NewUnsubackPacket(fh *FixHeader, r io.Reader) (*Unsuback, error)

NewUnsubackPacket returns a Unsuback instance by the given FixHeader and io.Reader.

func (*Unsuback) Pack

func (p *Unsuback) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Unsuback) String

func (p *Unsuback) String() string

func (*Unsuback) Unpack

func (p *Unsuback) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Unsubscribe

type Unsubscribe struct {
	FixHeader *FixHeader
	PacketID  PacketID

	Topics []string
}

Unsubscribe represents the MQTT Unsubscribe packet.

func NewUnsubscribePacket

func NewUnsubscribePacket(fh *FixHeader, r io.Reader) (*Unsubscribe, error)

NewUnsubscribePacket returns a Unsubscribe instance by the given FixHeader and io.Reader.

func (*Unsubscribe) NewUnSubBack

func (p *Unsubscribe) NewUnSubBack() *Unsuback

NewUnSubBack returns the Unsuback struct which is the ack packet of the Unsubscribe packet.

func (*Unsubscribe) Pack

func (p *Unsubscribe) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Unsubscribe) String

func (p *Unsubscribe) String() string

func (*Unsubscribe) Unpack

func (p *Unsubscribe) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Writer

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

Writer is used to encode MQTT packet into bytes and write it to bufio.Writer.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Writer.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush writes any buffered data to the underlying io.Writer.

func (*Writer) WriteAndFlush

func (w *Writer) WriteAndFlush(packet Packet) error

WriteAndFlush writes and flush the packet bytes to the underlying io.Writer.

func (*Writer) WritePacket

func (w *Writer) WritePacket(packet Packet) error

WritePacket writes the packet bytes to the Writer. Call Flush after WritePacket to flush buffered data to the underlying io.Writer.

Jump to

Keyboard shortcuts

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