Documentation
¶
Index ¶
- Constants
- Variables
- func DecodeRemainLength(r io.ByteReader) (int, error)
- func EncodeRemainLength(length int) (result []byte, err error)
- func IsInternalTopic(topic string) bool
- func IsVersion3(version Version) bool
- func IsVersion5(version Version) bool
- func UTF8DecodedStrings(mustUTF8 bool, r *bytes.Buffer) (b []byte, err error)
- func UTF8EncodedStrings(data []byte) (b []byte, size int, err error)
- func ValidTopicFilter(mustUTF8 bool, topic []byte) bool
- func ValidTopicName(mustUTF8 bool, topic []byte) bool
- func ValidUTF8(b []byte) bool
- func ValidV5Topic(p []byte) bool
- type BasePub
- type Connack
- type Connect
- type ConnectFlags
- type Disconnect
- type FixedHeader
- type Id
- type Packet
- type PayloadFormat
- type Pingreq
- type Pingresp
- type Puback
- type Pubcomp
- type Publish
- type Pubrec
- type Pubrel
- type QoS
- type ReadWriter
- type Reader
- type SubOptions
- type Suback
- type Subscribe
- type Topic
- type Type
- type Unsuback
- type Unsubscribe
- type Version
- type Writer
Constants ¶
const ( CleanSessionTure WillQos2 )
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 )
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 )
const ( DupTure byte = 1 << 3 QoS3 byte = 1 << 3 RetainTure byte = 1 )
const TopicMaxLen = 65535
Variables ¶
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 ¶
EncodeRemainLength puts the length int into bytes
func IsVersion3 ¶
func IsVersion5 ¶
func UTF8DecodedStrings ¶
func UTF8EncodedStrings ¶
UTF8EncodedStrings encodes the bytes into UTF-8 encoded strings, returns the encoded bytes, bytes size and error.
func ValidTopicFilter ¶
ValidTopicFilter returns whether the bytes is a valid topic filter. [MQTT-4.7.1-2] [MQTT-4.7.1-3]
func ValidTopicName ¶
ValidTopicName returns whether the bytes is a valid non-shared topic filter.[MQTT-4.7.1-1].
func ValidV5Topic ¶
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 ¶
NewConnack returns a Connack instance by the given FixHeader and io.Reader
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 ¶
NewConnect returns a Connect instance by the given FixHeader and io.Reader
func (*Connect) NewConnackPacket ¶
NewConnackPacket returns the Connack struct which is the ack packet of the Connect packet.
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) String ¶
func (d *Disconnect) String() string
type FixedHeader ¶
FixedHeader represents the FixedHeader of the MQTT packet
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
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 ¶
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
type Puback ¶
type Puback struct { Version Version FixedHeader *FixedHeader PacketId Id }
type Pubcomp ¶
type Pubcomp struct { Version Version FixedHeader *FixedHeader PacketId Id }
func NewPubcomp ¶
NewPubcomp returns a Pubcomp instance by the given FixHeader and io.Reader
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 ¶
NewPublish returns a Publishing instance by the given FixHeader and io.Reader.
func (*Publish) CreatePuback ¶
CreatePuback returns the puback struct related to the publish struct in QoS 1
func (*Publish) CreatePubrec ¶
CreatePubrec returns the pubrec struct related to the publish struct in QoS 2
type Pubrec ¶
type Pubrec struct { Version Version FixedHeader *FixedHeader PacketId Id }
func (*Pubrec) CreateNewPubrel ¶
CreateNewPubrel returns the Pubrel struct related to the Pubrec struct in QoS 2.
type Pubrel ¶
type Pubrel struct { Version Version FixedHeader *FixedHeader PacketId Id }
func (*Pubrel) CreatePubcomp ¶
CreatePubcomp returns the Pubcomp struct related to the Pubrel struct in QoS 2.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader is used to read data from bufio.Reader and create MQTT packet instance.
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 }
type Subscribe ¶
type Subscribe struct { Version Version FixedHeader *FixedHeader PacketId Id Topics []*Topic //suback响应之前填充 }
func NewSubscribe ¶
NewSubscribe returns a Subscribe instance by the given FixHeader and io.Reader.
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 }
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) String ¶
func (u *Unsubscribe) 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 (*Writer) Write ¶
Write raw bytes to the Writer. Call Flush after Write to flush buffered data to the underlying io.Writer.
func (*Writer) WritePacket ¶
WritePacket writes the packet bytes to the Writer. Call Flush after WritePacket to flush buffered data to the underlying io.Writer.
func (*Writer) WritePacketAndFlush ¶
WritePacketAndFlush writes and flush the packet bytes to the underlying io.Writer.