packets

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Connect     = 1
	Connack     = 2
	Publish     = 3
	Puback      = 4
	Pubrec      = 5
	Pubrel      = 6
	Pubcomp     = 7
	Subscribe   = 8
	Suback      = 9
	Unsubscribe = 10
	Unsuback    = 11
	Pingreq     = 12
	Pingresp    = 13
	Disconnect  = 14
)

Mqtt消息的包类型

View Source
const (
	Accepted                        = 0x00
	ErrRefusedBadProtocolVersion    = 0x01
	ErrRefusedIDRejected            = 0x02
	ErrRefusedServerUnavailable     = 0x03
	ErrRefusedBadUsernameOrPassword = 0x04
	ErrRefusedNotAuthorised         = 0x05
	ErrNetworkError                 = 0xFE
	ErrProtocolViolation            = 0xFF
)

Variables

View Source
var PacketNames = map[uint8]string{
	1:  "CONNECT",
	2:  "CONNACK",
	3:  "PUBLISH",
	4:  "PUBACK",
	5:  "PUBREC",
	6:  "PUBREL",
	7:  "PUBCOMP",
	8:  "SUBSCRIBE",
	9:  "SUBACK",
	10: "UNSUBSCRIBE",
	11: "UNSUBACK",
	12: "PINGREQ",
	13: "PINGRESP",
	14: "DISCONNECT",
}

Functions

This section is empty.

Types

type ConnackPacket

type ConnackPacket struct {
	FixedHeader
	SessionPresent bool
	ReturnCode     byte
}

connack包,服务端对connect的回应

func (*ConnackPacket) Read

func (ca *ConnackPacket) Read(b io.Reader) error

func (*ConnackPacket) String

func (ca *ConnackPacket) String() string

func (*ConnackPacket) Write

func (ca *ConnackPacket) Write(w io.Writer) error

type ConnectPacket

type ConnectPacket struct {
	FixedHeader
	ProtocolName    string
	ProtocolVersion byte
	CleanSession    bool
	WillFlag        bool
	WillQos         byte
	WillRetain      bool
	UsernameFlag    bool
	PasswordFlag    bool
	ReservedBit     byte
	Keepalive       uint16

	ClientId    string
	WillTopic   string
	WillMessage []byte
	Username    string
	Password    []byte
}

connect包,客户端发起连接时发送

func (*ConnectPacket) Read

func (cp *ConnectPacket) Read(b io.Reader) error

func (*ConnectPacket) String

func (cp *ConnectPacket) String() string

func (*ConnectPacket) Validate added in v0.3.0

func (c *ConnectPacket) Validate() byte

Validate performs validation of the fields of a Connect packet

func (*ConnectPacket) Write

func (cp *ConnectPacket) Write(w io.Writer) error

type DisconnectPacket

type DisconnectPacket struct {
	FixedHeader
}

func (*DisconnectPacket) Read

func (d *DisconnectPacket) Read(b io.Reader) error

func (*DisconnectPacket) String

func (d *DisconnectPacket) String() string

func (*DisconnectPacket) Write

func (d *DisconnectPacket) Write(w io.Writer) error

type FixedHeader

type FixedHeader struct {
	MessageType     byte
	Dup             bool
	Qos             byte
	Retain          bool
	RemainingLength int
}

MQTT协议的固定头部协议,每条消息都需要携带

func (FixedHeader) String

func (fh FixedHeader) String() string

type MqttPacket

type MqttPacket interface {
	Write(io.Writer) error
	Read(io.Reader) error
	String() string
}

所有packet的公共接口

func NewMqttPacket

func NewMqttPacket(messageType byte) MqttPacket

func NewMqttPacketWithHeader

func NewMqttPacketWithHeader(fh FixedHeader) (MqttPacket, error)

func ReadPacket

func ReadPacket(conn net.Conn) (MqttPacket, error)

type PingreqPacket

type PingreqPacket struct {
	FixedHeader
}

func (*PingreqPacket) Read

func (pr *PingreqPacket) Read(b io.Reader) error

func (*PingreqPacket) String

func (pr *PingreqPacket) String() string

func (*PingreqPacket) Write

func (pr *PingreqPacket) Write(w io.Writer) error

type PingrespPacket

type PingrespPacket struct {
	FixedHeader
}

func (*PingrespPacket) Read

func (pr *PingrespPacket) Read(b io.Reader) error

func (*PingrespPacket) String

func (pr *PingrespPacket) String() string

func (*PingrespPacket) Write

func (pr *PingrespPacket) Write(w io.Writer) error

type PublishPacket

type PublishPacket struct {
	FixedHeader
	TopicName string
	MessageID uint16
	Payload   []byte
}

PublishPacket is an internal representation of the fields of the Publish MQTT packet

func (*PublishPacket) Copy

func (p *PublishPacket) Copy() *PublishPacket

Copy creates a new PublishPacket with the same topic and payload but an empty fixed header, useful for when you want to deliver a message with different properties such as Qos but the same content

func (*PublishPacket) Read

func (p *PublishPacket) Read(b io.Reader) error

Unpack decodes the details of a ControlPacket after the fixed header has been read

func (*PublishPacket) String

func (p *PublishPacket) String() string

func (*PublishPacket) Write

func (p *PublishPacket) Write(w io.Writer) error

type SubackPacket

type SubackPacket struct {
	FixedHeader
	MessageID   uint16
	ReturnCodes []byte
}

func (*SubackPacket) Read

func (sa *SubackPacket) Read(b io.Reader) error

func (*SubackPacket) String

func (sa *SubackPacket) String() string

func (*SubackPacket) Write

func (sa *SubackPacket) Write(w io.Writer) error

type SubscribePacket

type SubscribePacket struct {
	FixedHeader
	MessageID uint16
	Topics    []string
	Qoss      []byte
}

func (*SubscribePacket) Read

func (s *SubscribePacket) Read(b io.Reader) error

func (*SubscribePacket) String

func (s *SubscribePacket) String() string

func (*SubscribePacket) Write

func (s *SubscribePacket) Write(w io.Writer) error

type UnsubackPacket

type UnsubackPacket struct {
	FixedHeader
	MessageID uint16
}

func (*UnsubackPacket) Read

func (ua *UnsubackPacket) Read(b io.Reader) error

func (*UnsubackPacket) String

func (ua *UnsubackPacket) String() string

func (*UnsubackPacket) Write

func (ua *UnsubackPacket) Write(w io.Writer) error

type UnsubscribePacket

type UnsubscribePacket struct {
	FixedHeader
	MessageID uint16
	Topics    []string
}

func (*UnsubscribePacket) Read

func (u *UnsubscribePacket) Read(b io.Reader) error

func (*UnsubscribePacket) String

func (u *UnsubscribePacket) String() string

func (*UnsubscribePacket) Write

func (u *UnsubscribePacket) Write(w io.Writer) error

Jump to

Keyboard shortcuts

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