Documentation ¶
Index ¶
- Constants
- Variables
- func DecodeRemainLength(length int) ([]byte, error)
- func DecodeUTF8String(buf []byte) (b []byte, size int, err error)
- func EncodeRemainLength(r *bufio.Reader) (int, error)
- func EncodeUTF8String(buf []byte) (b []byte, size int, err error)
- func TopicMatch(topic []byte, topicFilter []byte) bool
- func TotalBytes(p Packet) uint
- func ValidTopicFilter(p []byte) bool
- func ValidTopicName(p []byte) bool
- func ValidUTF8(p []byte) bool
- type Connack
- type Connect
- type Disconnect
- type FixHeader
- type Message
- type Packet
- type PacketID
- type Pingreq
- type Pingresp
- type Puback
- type Pubcomp
- type Publish
- type Pubrec
- type Pubrel
- type ReadWriter
- type Reader
- type Suback
- type Subscribe
- type Topic
- type Unsuback
- type Unsubscribe
- type Writer
Constants ¶
const ( CodeAccepted = 0x00 CodeUnacceptableProtocolVersion = 0x01 CodeIdentifierRejected = 0x02 CodeBadUsernameorPsw = 0x04 CodeNotAuthorized = 0x05 )
There are the possible code in the connack packet.
const ( RESERVED = iota CONNECT CONNACK PUBLISH PUBACK PUBREC PUBREL PUBCOMP SUBSCRIBE SUBACK UNSUBSCRIBE UNSUBACK PINGREQ PINGRESP DISCONNECT )
Packet type
const ( FLAG_RESERVED = 0 FLAG_SUBSCRIBE = 2 FLAG_UNSUBSCRIBE = 2 FLAG_PUBREL = 2 )
Flag in the FixHeader
const ( QOS_0 uint8 = 0x00 QOS_1 uint8 = 0x01 QOS_2 uint8 = 0x02 SUBSCRIBE_FAILURE = 0x80 )
QoS levels & Subscribe failure
Variables ¶
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 ¶
DecodeRemainLength 将remain length 转成byte表示
DecodeRemainLength puts the length int into bytes
func DecodeUTF8String ¶
DecodeUTF8String decodes the UTF-8 encoded strings into bytes, returns the decoded bytes, bytes size and error.
func EncodeRemainLength ¶
EncodeRemainLength 读remainLength,如果格式错误返回 error
EncodeRemainLength reads the remain length bytes from bufio.Reader and returns length int.
func EncodeUTF8String ¶
EncodeUTF8String encodes the bytes into UTF-8 encoded strings, returns the encoded bytes, bytes size and error.
func TopicMatch ¶
TopicMatch 返回topic和topic filter是否
TopicMatch returns whether the topic and topic filter is matched.
func ValidTopicFilter ¶
ValidTopicFilter 验证主题过滤器是否合法
ValidTopicFilter returns whether the bytes is a valid topic filter. [MQTT-4.7.1-2] [MQTT-4.7.1-3]
func ValidTopicName ¶
ValidTopicName 验证主题名是否合法 [MQTT-4.7.1-1]
ValidTopicName returns whether the bytes is a valid topic name.[MQTT-4.7.1-1].
Types ¶
type Connack ¶
Connack represents the MQTT Connack packet
func NewConnackPacket ¶
NewConnackPacket returns a Connack instance by the given FixHeader and io.Reader
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 ¶
NewConnectPacket 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 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
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
type Pingreq ¶
type Pingreq struct {
FixHeader *FixHeader
}
Pingreq represents the MQTT Pingreq packet
func NewPingreqPacket ¶
NewPingreqPacket returns a Pingreq instance by the given FixHeader and io.Reader
func (*Pingreq) NewPingresp ¶
NewPingresp returns a Pingresp struct
type Pingresp ¶
type Pingresp struct {
FixHeader *FixHeader
}
Pingresp represents the MQTT Pingresp packet
func NewPingrespPacket ¶
NewPingrespPacket returns a Pingresp instance by the given FixHeader and io.Reader
type Puback ¶
Puback represents the MQTT Puback packet
func NewPubackPacket ¶
NewPubackPacket returns a Puback instance by the given FixHeader and io.Reader
type Pubcomp ¶
Pubcomp represents the MQTT Pubcomp packet
func NewPubcompPacket ¶
NewPubcompPacket returns a Pubcomp instance by the given FixHeader and io.Reader
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 ¶
NewPublishPacket returns a Publish instance by the given FixHeader and io.Reader.
func (*Publish) CopyPublish ¶
CopyPublish 将 publish 复制一份
CopyPublish returns the copied publish struct for distribution
func (*Publish) NewPuback ¶
NewPuback returns the puback struct related to the publish struct in QoS 1
func (*Publish) NewPubrec ¶
NewPubrec returns the pubrec struct related to the publish struct in QoS 2
type Pubrec ¶
Pubrec represents the MQTT Pubrec packet.
func NewPubrecPacket ¶
NewPubrecPacket returns a Pubrec instance by the given FixHeader and io.Reader.
func (*Pubrec) NewPubrel ¶
NewPubrel returns the Pubrel struct related to the Pubrec struct in QoS 2.
type Pubrel ¶
Pubrel represents the MQTT Pubrel packet
func NewPubrelPacket ¶
NewPubrelPacket returns a Pubrel instance by the given FixHeader and io.Reader.
func (*Pubrel) NewPubcomp ¶
NewPubcomp 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.
func (*Reader) ReadPacket ¶
ReadPacket reads data from Reader and returns a Packet instance. If any errors occurs, returns nil, error
type Suback ¶
Suback represents the MQTT Suback packet.
func NewSubackPacket ¶
NewSubackPacket returns a Suback instance by the given FixHeader and io.Reader.
type Subscribe ¶
Subscribe represents the MQTT Subscribe packet.
func NewSubscribePacket ¶
NewSubscribePacket returns a Subscribe instance by the given FixHeader and io.Reader.
func (*Subscribe) NewSubBack ¶
NewSubBack returns the Suback struct which is the ack packet of the Subscribe packet.
type Unsuback ¶
Unsuback represents the MQTT Unsuback packet.
func NewUnsubackPacket ¶
NewUnsubackPacket returns a Unsuback instance by the given FixHeader and io.Reader.
type Unsubscribe ¶
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
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) WriteAndFlush ¶
WriteAndFlush writes and flush the packet bytes 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.