packet

package
v0.0.0-...-d5ac831 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CleanSessionTure

	WillQos2
)
View Source
const (
	Version31  Version = 0x03
	Version311 Version = 0x04
	Version5   Version = 0x05
	// MaximumSize The maximum packet size of a MQTT packet
	MaximumSize = 268435456

	// SubscribeFailure 订阅失败
	SubscribeFailure = 0x80

	MaxPacketID Id = 65535
	MinPacketID Id = 1
	// UTF8EncodedStringsMaxLen There is a limit on the size of a string that can be passed in one of these UTF-8 encoded string components; you cannot use a string that would encode to more than 65535 bytes.
	// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.2_-
	// 2 byte = uint16
	UTF8EncodedStringsMaxLen = 1<<16 - 1
	// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.4_Size
	RemainLength1ByteMin = 0
	RemainLength1ByteMax = 1<<7 - 1
	RemainLength2ByteMin = 1 << 7
	RemainLength2ByteMax = 1<<14 - 1
	RemainLength3ByteMin = 1 << 14
	RemainLength3ByteMax = 1<<21 - 1
	RemainLength4ByteMin = 1 << 21
	RemainLength4ByteMax = 1<<28 - 1
)
View Source
const (
	PropPayloadFormat          byte = 1
	PropMessageExpiry          byte = 2
	PropContentType            byte = 3
	PropResponseTopic          byte = 8
	PropCorrelationData        byte = 9
	PropSubscriptionIdentifier byte = 11
	PropSessionExpiryInterval  byte = 17
	PropAssignedClientID       byte = 18
	PropServerKeepAlive        byte = 19
	PropAuthMethod             byte = 21
	PropAuthData               byte = 22
	PropRequestProblemInfo     byte = 23
	PropWillDelayInterval      byte = 24
	PropRequestResponseInfo    byte = 25
	PropResponseInfo           byte = 26
	PropServerReference        byte = 28
	PropReasonString           byte = 31
	PropReceiveMaximum         byte = 33
	PropTopicAliasMaximum      byte = 34
	PropTopicAlias             byte = 35
	PropMaximumQOS             byte = 36
	PropRetainAvailable        byte = 37
	PropUser                   byte = 38
	PropMaximumPacketSize      byte = 39
	PropWildcardSubAvailable   byte = 40
	PropSubIDAvailable         byte = 41
	PropSharedSubAvailable     byte = 42
)
View Source
const (
	DupTure    byte = 1 << 3
	QoS3       byte = 1 << 3
	RetainTure byte = 1
)
View Source
const TopicMaxLen = 65535

Variables

View Source
var (
	ProtocolNamePrefix = []byte{0x00, 0x04}
)

Functions

func DecodeRemainLength

func DecodeRemainLength(r io.ByteReader) (int, error)

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

func EncodeRemainLength

func EncodeRemainLength(length int) (result []byte, err error)

EncodeRemainLength puts the length int into bytes

func IsInternalTopic

func IsInternalTopic(topic string) bool

IsInternalTopic 内部主题

func IsVersion3

func IsVersion3(version Version) bool

func IsVersion5

func IsVersion5(version Version) bool

func UTF8DecodedStrings

func UTF8DecodedStrings(mustUTF8 bool, r *bytes.Buffer) (b []byte, err error)

func UTF8EncodedStrings

func UTF8EncodedStrings(data []byte) (b []byte, size int, err error)

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

func ValidTopicFilter

func ValidTopicFilter(mustUTF8 bool, topic []byte) bool

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

func ValidTopicName

func ValidTopicName(mustUTF8 bool, topic []byte) bool

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

func ValidUTF8

func ValidUTF8(b []byte) bool

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

func ValidV5Topic

func ValidV5Topic(p []byte) bool

ValidV5Topic returns whether the given bytes is a valid MQTT V5 topic

Types

type BasePub

type BasePub struct {
	Version     Version
	FixedHeader *FixedHeader
	PacketId    Id
}

type Connack

type Connack struct {
	Version        Version
	FixedHeader    *FixedHeader
	SessionPresent bool
	Code           code.Code
}

func NewConnack

func NewConnack(fixedHeader *FixedHeader, version Version, r io.Reader) (*Connack, error)

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

func (*Connack) Decode

func (c *Connack) Decode(r io.Reader) (err error)

Decode 解码r中可变报头

func (*Connack) Encode

func (c *Connack) Encode(w io.Writer) (err error)

Encode the packet struct into bytes and writes it into io.Writer.

func (*Connack) String

func (c *Connack) String() string

type Connect

type Connect struct {
	Version     Version
	FixedHeader *FixedHeader

	ProtocolName  []byte
	ProtocolLevel byte
	// The ConnectFlags byte contains a number of parameters specifying the behavior of the MQTT connection.
	// It also indicates the presence or absence of fields in the payload.
	ConnectFlags
	// The KeepAlive is a time interval measured in seconds.
	// Expressed as a 16-bit word, it is the maximum time interval that is permitted
	// to elapse between the point at which the Client finishes transmitting one Control Packet
	// and the point it starts sending the next.
	KeepAlive uint16

	WillTopic   []byte
	WillMessage []byte

	//auth
	ClientId []byte
	Username []byte
	Password []byte
	// contains filtered or unexported fields
}

Connect represents the MQTT Connect packet.

func NewConnect

func NewConnect(fixedHeader *FixedHeader, version Version, r io.Reader) (*Connect, error)

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

func (*Connect) Context

func (c *Connect) Context() context.Context

func (*Connect) Decode

func (c *Connect) Decode(r io.Reader) (err error)

Decode 解码可变报头的长度(10字节)加上有效载荷

func (*Connect) Encode

func (c *Connect) Encode(w io.Writer) (err error)

func (*Connect) NewConnackPacket

func (c *Connect) NewConnackPacket(cd code.Code, sessionReuse bool) *Connack

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

func (*Connect) String

func (c *Connect) String() string

type ConnectFlags

type ConnectFlags struct {

	// CleanSession: bit 1 of the ConnectFlags byte.
	// This bit specifies the handling of the Session state.
	CleanSession bool
	// WillFlag: bit 2 of the ConnectFlags.
	WillFlag bool
	// WillQoS bits 4 and 3 of the ConnectFlags.
	// These two bits specify the QoS level to be used when publishing the Will Message.
	WillQoS byte
	// WillRetain:bit 5 of the ConnectFlags.
	WillRetain bool
	// PasswordFlag:bit 7 of the ConnectFlags.
	PasswordFlag bool
	// UsernameFlag
	UsernameFlag bool
}

type Disconnect

type Disconnect struct {
	Version     Version
	FixedHeader *FixedHeader
}

func NewDisconnect

func NewDisconnect(fixedHeader *FixedHeader, version Version, r io.Reader) (*Disconnect, error)

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

func (*Disconnect) Decode

func (d *Disconnect) Decode(_ io.Reader) (err error)

func (*Disconnect) Encode

func (d *Disconnect) Encode(w io.Writer) (err error)

func (*Disconnect) String

func (d *Disconnect) String() string

type FixedHeader

type FixedHeader struct {
	PacketType   Type
	Flags        byte
	RemainLength int
}

FixedHeader represents the FixedHeader of the MQTT packet

func (*FixedHeader) Encode

func (fixedHeader *FixedHeader) Encode(w io.Writer) error

Encode encodes the FixedHeader struct into bytes and writes it into io.Writer.

type Id

type Id = uint16

Id 数据包ID

type Packet

type Packet interface {
	// Encode encodes the packet struct into bytes and writes it into io.Writer.
	Encode(w io.Writer) (err error)
	// Decode read the packet bytes from io.Reader and decodes it into the packet struct
	Decode(r io.Reader) (err 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(fixedHeader *FixedHeader, version Version, r io.Reader) (Packet, error)

type PayloadFormat

type PayloadFormat = byte
const (
	PayloadFormatBytes  PayloadFormat = 0
	PayloadFormatString PayloadFormat = 1
)

type Pingreq

type Pingreq struct {
	FixedHeader *FixedHeader
}

func NewPingreq

func NewPingreq(fixedHeader *FixedHeader, r io.Reader) (*Pingreq, error)

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

func (*Pingreq) CreatePingresp

func (p *Pingreq) CreatePingresp() *Pingresp

func (*Pingreq) Decode

func (p *Pingreq) Decode(_ io.Reader) (err error)

func (*Pingreq) Encode

func (p *Pingreq) Encode(w io.Writer) (err error)

func (*Pingreq) String

func (p *Pingreq) String() string

type Pingresp

type Pingresp struct {
	FixedHeader *FixedHeader
}

func NewPingresp

func NewPingresp(fixedHeader *FixedHeader, r io.Reader) (*Pingresp, error)

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

func (*Pingresp) Decode

func (p *Pingresp) Decode(_ io.Reader) (err error)

func (*Pingresp) Encode

func (p *Pingresp) Encode(w io.Writer) (err error)

func (*Pingresp) String

func (p *Pingresp) String() string

type Puback

type Puback struct {
	Version     Version
	FixedHeader *FixedHeader
	PacketId    Id
}

func NewPuback

func NewPuback(fixedHeader *FixedHeader, version Version, r io.Reader) (*Puback, error)

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

func (*Puback) Decode

func (bp *Puback) Decode(r io.Reader) (err error)

func (*Puback) Encode

func (bp *Puback) Encode(w io.Writer) (err error)

func (*Puback) String

func (bp *Puback) String() string

type Pubcomp

type Pubcomp struct {
	Version     Version
	FixedHeader *FixedHeader
	PacketId    Id
}

func NewPubcomp

func NewPubcomp(fixedHeader *FixedHeader, version Version, r io.Reader) (*Pubcomp, error)

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

func (*Pubcomp) Decode

func (pb *Pubcomp) Decode(r io.Reader) (err error)

func (*Pubcomp) Encode

func (pb *Pubcomp) Encode(w io.Writer) (err error)

func (*Pubcomp) String

func (pb *Pubcomp) String() string

String returns string.

type Publish

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

func NewPublish

func NewPublish(fixedHeader *FixedHeader, version Version, r io.Reader) (*Publish, error)

NewPublish returns a Publishing instance by the given FixHeader and io.Reader.

func (*Publish) CreatePuback

func (p *Publish) CreatePuback() *Puback

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

func (*Publish) CreatePubrec

func (p *Publish) CreatePubrec() *Pubrec

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

func (*Publish) Decode

func (p *Publish) Decode(r io.Reader) (err error)

func (*Publish) Encode

func (p *Publish) Encode(w io.Writer) (err error)

func (*Publish) String

func (p *Publish) String() string

type Pubrec

type Pubrec struct {
	Version     Version
	FixedHeader *FixedHeader
	PacketId    Id
}

func NewPubrec

func NewPubrec(fixedHeader *FixedHeader, version Version, r io.Reader) (*Pubrec, error)

func (*Pubrec) CreateNewPubrel

func (p *Pubrec) CreateNewPubrel() *Pubrel

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

func (*Pubrec) Decode

func (p *Pubrec) Decode(r io.Reader) (err error)

func (*Pubrec) Encode

func (p *Pubrec) Encode(w io.Writer) (err error)

func (*Pubrec) String

func (p *Pubrec) String() string

type Pubrel

type Pubrel struct {
	Version     Version
	FixedHeader *FixedHeader
	PacketId    Id
}

func NewPubrel

func NewPubrel(fixedHeader *FixedHeader, version Version, r io.Reader) (*Pubrel, error)

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

func (*Pubrel) CreatePubcomp

func (p *Pubrel) CreatePubcomp() *Pubcomp

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

func (*Pubrel) Decode

func (p *Pubrel) Decode(r io.Reader) (err error)

func (*Pubrel) Encode

func (p *Pubrel) Encode(w io.Writer) (err error)

func (*Pubrel) String

func (p *Pubrel) String() string

type QoS

type QoS = byte

QoS 消息质量

const (
	QoS0 QoS = 0x00
	QoS1 QoS = 0x01
	QoS2 QoS = 0x02
)

QoS levels

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) Read

func (r *Reader) Read() (p Packet, err error)

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

type SubOptions

type SubOptions struct {
	// QoS is the QoS level of the subscription.
	// 0 = At most once delivery
	// 1 = At least once delivery
	// 2 = Exactly once delivery
	QoS QoS
	// RetainHandling specifies whether retained messages are sent when the subscription is established.
	// 0 = Send retained messages at the time of the subscribe
	// 1 = Send retained messages at subscribe only if the subscription does not currently exist
	// 2 = Do not send retained messages at the time of the subscribe
	RetainHandling byte
	// NoLocal is the No Local option.
	//  If the value is 1, Application Messages MUST NOT be forwarded to a connection with a ClientId equal to the ClientId of the publishing connection
	NoLocal bool
	// RetainAsPublished is the Retain As Published option.
	// If 1, Application Messages forwarded using this subscription keep the RETAIN flag they were published with.
	// If 0, Application Messages forwarded using this subscription have the RETAIN flag set to 0. Retained messages sent when the subscription is established have the RETAIN flag set to 1.
	RetainAsPublished bool
}

SubOptions is the subscription option of subscriptions. For details: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Subscription_Options

type Suback

type Suback struct {
	Version     Version
	FixedHeader *FixedHeader
	PacketId    Id
	Payload     []code.Code
}

func NewSuback

func NewSuback(fixedHeader *FixedHeader, version Version, r io.Reader) (*Suback, error)

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

func (*Suback) Decode

func (s *Suback) Decode(r io.Reader) (err error)

func (*Suback) Encode

func (s *Suback) Encode(w io.Writer) (err error)

func (*Suback) String

func (s *Suback) String() string

type Subscribe

type Subscribe struct {
	Version     Version
	FixedHeader *FixedHeader
	PacketId    Id
	Topics      []*Topic //suback响应之前填充
}

func NewSubscribe

func NewSubscribe(fh *FixedHeader, version Version, r io.Reader) (*Subscribe, error)

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

func (*Subscribe) Decode

func (s *Subscribe) Decode(r io.Reader) (err error)

func (*Subscribe) Encode

func (s *Subscribe) Encode(w io.Writer) (err error)

func (*Subscribe) String

func (s *Subscribe) String() string

type Topic

type Topic struct {
	SubOptions
	Name string
}

Topic represents the MQTT Topic

func (*Topic) String

func (t *Topic) String() string

type Type

type Type = byte
const (
	// RESERVED Forbidden
	RESERVED Type = iota
	// CONNECT Client request to connect to Server
	CONNECT
	// CONNACK Connect acknowledgment
	CONNACK
	// PUBLISH message
	PUBLISH
	// PUBACK Publish acknowledgment
	PUBACK
	// PUBREC Publish received (assured delivery part 1)
	PUBREC
	// PUBREL Publish release (assured delivery part 2)
	PUBREL
	// PUBCOMP Publish complete (assured delivery part 3)
	PUBCOMP
	// SUBSCRIBE Client subscribe request
	SUBSCRIBE
	// SUBACK Subscribe acknowledgment
	SUBACK
	// UNSUBSCRIBE Unsubscribe request
	UNSUBSCRIBE
	// UNSUBACK Unsubscribe acknowledgment
	UNSUBACK
	// PINGREQ PING request
	PINGREQ
	// PINGRESP PING response
	PINGRESP
	// DISCONNECT Client is disconnecting
	DISCONNECT
	// AUTHReserved Forbidden
	AUTHReserved

	FixedHeaderFlagReserved    = 0
	FixedHeaderFlagSubscribe   = 2
	FixedHeaderFlagUnsubscribe = 2
	FixedHeaderFlagPubrel      = 2
)

Packet type

type Unsuback

type Unsuback struct {
	Version     Version
	FixedHeader *FixedHeader
	PacketId    Id
	Payload     []code.Code
}

func (*Unsuback) Decode

func (u *Unsuback) Decode(r io.Reader) (err error)

func (*Unsuback) Encode

func (u *Unsuback) Encode(w io.Writer) (err error)

func (*Unsuback) String

func (u *Unsuback) String() string

type Unsubscribe

type Unsubscribe struct {
	Version     Version
	FixedHeader *FixedHeader
	PacketId    Id
	Topics      []string
}

func NewUnsubscribe

func NewUnsubscribe(fixedHeader *FixedHeader, version Version, r io.Reader) (*Unsubscribe, error)

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

func (*Unsubscribe) Decode

func (u *Unsubscribe) Decode(r io.Reader) (err error)

func (*Unsubscribe) Encode

func (u *Unsubscribe) Encode(w io.Writer) (err error)

func (*Unsubscribe) String

func (u *Unsubscribe) String() string

type Version

type Version byte

Version MQTT版本

func (Version) String

func (v Version) String() string

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) Write

func (w *Writer) Write(b []byte) error

Write raw bytes to the Writer. Call Flush after Write to flush buffered data to the underlying io.Writer.

func (*Writer) WritePacket

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

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

func (*Writer) WritePacketAndFlush

func (w *Writer) WritePacketAndFlush(p Packet) error

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

Jump to

Keyboard shortcuts

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