packet

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ConnectAccepted int = iota
	ConnectUnacceptableProtocol
	ConnectIndentifierRejected
	ConnectServerUnavailable
	ConnectBadUserPass
	ConnectNotAuthorized
)

Variables

View Source
var (
	ErrInvalidPacketType   = errors.New("invalid packet type")
	ErrProtocolError       = errors.New("protocol error (not supported)")
	ErrInvalidPacketLength = errors.New("invalid packet Len")
	ErrUnknownPacket       = errors.New("unknown packet type")
	ErrReadFromBuf         = errors.New("error read data from buffer")
	ErrUnsupportedVersion  = errors.New("unsupported mqtt version")
	ErrConnect             = errors.New("error connect to broker")
)
View Source
var ErrInvalidQos = errors.New("invalid qos")

Functions

func MatchTopic added in v0.1.2

func MatchTopic(mask string, topic string) bool

func ReadBytes

func ReadBytes(buf []byte, offset int, length int) ([]byte, int, error)

func ReadInt16

func ReadInt16(buf []byte, offset int) (uint16, int, error)

func ReadInt8

func ReadInt8(buf []byte, offset int) (uint8, int, error)

func ReadString

func ReadString(buf []byte, offset int, length int) (string, int, error)

func WriteBytes added in v0.1.2

func WriteBytes(buf []byte, offset int, bytes []byte) int

Write bytes array length and array itself into buffer return offset after write

func WriteInt16

func WriteInt16(buf []byte, offset int, value uint16) int

func WriteInt8

func WriteInt8(buf []byte, offset int, value uint8) int

func WriteLength added in v0.1.2

func WriteLength(len int) []byte

func WritePacket added in v0.1.2

func WritePacket(conn net.Conn, pkt Packet, debug bool) error

func WriteString added in v0.1.2

func WriteString(buf []byte, offset int, value string) int

Write string len and string itself into buffer return offset after write

Types

type ConnAckPacket

type ConnAckPacket struct {
	Header     byte
	Session    bool
	ReturnCode uint8
}

func CreateConnAck

func CreateConnAck(buf byte) *ConnAckPacket

func NewConnAck

func NewConnAck() *ConnAckPacket

func (*ConnAckPacket) Length

func (cack *ConnAckPacket) Length() int

func (*ConnAckPacket) Pack

func (cack *ConnAckPacket) Pack() []byte

func (*ConnAckPacket) String

func (cack *ConnAckPacket) String() string

func (*ConnAckPacket) Type

func (cack *ConnAckPacket) Type() Type

func (*ConnAckPacket) Unpack

func (cack *ConnAckPacket) Unpack(buf []byte) error

type ConnPacket

type ConnPacket struct {
	Header       byte
	ClientID     string
	KeepAlive    uint16
	Username     string
	Password     string
	CleanSession bool
	Will         *Message
	Version      byte
	VersionName  string
}

func CreateConnect

func CreateConnect(buf byte) *ConnPacket

func NewConnect

func NewConnect() *ConnPacket

func (*ConnPacket) Length

func (c *ConnPacket) Length() int

func (*ConnPacket) Pack

func (c *ConnPacket) Pack() []byte

func (*ConnPacket) String

func (c *ConnPacket) String() string

func (*ConnPacket) Type

func (c *ConnPacket) Type() Type

func (*ConnPacket) Unpack

func (c *ConnPacket) Unpack(buf []byte) error

type DisconnectPacket

type DisconnectPacket struct {
	Header byte
}

func CreateDisconnect

func CreateDisconnect(buf byte) *DisconnectPacket

func NewDisconnect

func NewDisconnect() *DisconnectPacket

func (*DisconnectPacket) Length

func (cack *DisconnectPacket) Length() int

func (*DisconnectPacket) Pack

func (cack *DisconnectPacket) Pack() []byte

func (*DisconnectPacket) String

func (cack *DisconnectPacket) String() string

func (*DisconnectPacket) Type

func (cack *DisconnectPacket) Type() Type

func (*DisconnectPacket) Unpack

func (cack *DisconnectPacket) Unpack(buf []byte) error

type Message

type Message struct {
	Flag      bool
	QoS       QoS
	Retain    bool
	Dublicate bool
	Topic     string
	Payload   string
}

func (*Message) Length

func (m *Message) Length() int

func (*Message) Pack

func (m *Message) Pack() []byte

func (*Message) String

func (m *Message) String() string

type Packet

type Packet interface {
	Type() Type
	Length() int
	Unpack(buf []byte) error
	Pack() []byte
	String() string
}

func Create

func Create(buf byte) Packet

func ReadPacket added in v0.1.2

func ReadPacket(conn net.Conn, debug bool) (Packet, error)

type PingPacket

type PingPacket struct {
	Header byte
}

func CreatePing

func CreatePing(buf byte) *PingPacket

func NewPing

func NewPing() *PingPacket

func (*PingPacket) Length

func (p *PingPacket) Length() int

func (*PingPacket) Pack

func (p *PingPacket) Pack() []byte

func (*PingPacket) String

func (p *PingPacket) String() string

func (*PingPacket) Type

func (p *PingPacket) Type() Type

func (*PingPacket) Unpack

func (p *PingPacket) Unpack(buf []byte) error

type PongPacket

type PongPacket struct {
	Header byte
}

func CreatePong

func CreatePong(buf byte) *PongPacket

func NewPong

func NewPong() *PongPacket

func (*PongPacket) Length

func (p *PongPacket) Length() int

func (*PongPacket) Pack

func (p *PongPacket) Pack() []byte

func (*PongPacket) String

func (p *PongPacket) String() string

func (*PongPacket) Type

func (p *PongPacket) Type() Type

func (*PongPacket) Unpack

func (p *PongPacket) Unpack(buf []byte) error

type PubAckPacket

type PubAckPacket struct {
	Header byte
	Id     uint16
}

func CreatePubAck

func CreatePubAck(buf byte) *PubAckPacket

func NewPubAck

func NewPubAck() *PubAckPacket

func (*PubAckPacket) Length

func (pack *PubAckPacket) Length() int

func (*PubAckPacket) Pack

func (pack *PubAckPacket) Pack() []byte

func (*PubAckPacket) String

func (pack *PubAckPacket) String() string

func (*PubAckPacket) Type

func (pack *PubAckPacket) Type() Type

func (*PubAckPacket) Unpack

func (pack *PubAckPacket) Unpack(buf []byte) error

type PubCompPacket

type PubCompPacket struct {
	Header byte
	Id     uint16
}

func CreatePubComp

func CreatePubComp(buf byte) *PubCompPacket

func NewPubComp

func NewPubComp() *PubCompPacket

func (*PubCompPacket) Length

func (p *PubCompPacket) Length() int

func (*PubCompPacket) Pack

func (p *PubCompPacket) Pack() []byte

func (*PubCompPacket) String

func (p *PubCompPacket) String() string

func (*PubCompPacket) Type

func (p *PubCompPacket) Type() Type

func (*PubCompPacket) Unpack

func (p *PubCompPacket) Unpack(buf []byte) error

type PubRecPacket

type PubRecPacket struct {
	Header byte
	Id     uint16
}

func CreatePubRec

func CreatePubRec(buf byte) *PubRecPacket

func NewPubRec

func NewPubRec() *PubRecPacket

func (*PubRecPacket) Length

func (p *PubRecPacket) Length() int

func (*PubRecPacket) Pack

func (p *PubRecPacket) Pack() []byte

func (*PubRecPacket) String

func (p *PubRecPacket) String() string

func (*PubRecPacket) Type

func (p *PubRecPacket) Type() Type

func (*PubRecPacket) Unpack

func (p *PubRecPacket) Unpack(buf []byte) error

type PubRelPacket

type PubRelPacket struct {
	Header byte
	Id     uint16
}

func CreatePubRel

func CreatePubRel(buf byte) *PubRelPacket

func NewPubRel

func NewPubRel() *PubRelPacket

func (*PubRelPacket) Length

func (p *PubRelPacket) Length() int

func (*PubRelPacket) Pack

func (p *PubRelPacket) Pack() []byte

func (*PubRelPacket) String

func (p *PubRelPacket) String() string

func (*PubRelPacket) Type

func (p *PubRelPacket) Type() Type

func (*PubRelPacket) Unpack

func (p *PubRelPacket) Unpack(buf []byte) error

type PublishPacket

type PublishPacket struct {
	Header  byte
	Id      uint16
	QoS     QoS
	Retain  bool
	DUP     bool
	Topic   string
	Payload string
}

func CreatePublish

func CreatePublish(buf byte) *PublishPacket

func NewPublish

func NewPublish() *PublishPacket

func (*PublishPacket) Length

func (p *PublishPacket) Length() int

func (*PublishPacket) Pack

func (p *PublishPacket) Pack() []byte

func (*PublishPacket) String

func (p *PublishPacket) String() string

func (*PublishPacket) Type

func (p *PublishPacket) Type() Type

func (*PublishPacket) Unpack

func (p *PublishPacket) Unpack(buf []byte) error

type QoS

type QoS byte
const (
	AtMostOnce  QoS = 0x00
	AtLeastOnce QoS = 0x01
	ExactlyOnce QoS = 0x02
)

func (QoS) Int

func (q QoS) Int() int

func (QoS) String

func (q QoS) String() string

func (QoS) Valid

func (q QoS) Valid() bool

type SubAckPacket

type SubAckPacket struct {
	Header      byte
	Id          uint16
	ReturnCodes []QoS
}

func CreateSubAck

func CreateSubAck(buf byte) *SubAckPacket

func NewSubAck

func NewSubAck() *SubAckPacket

func (*SubAckPacket) Length

func (sack *SubAckPacket) Length() int

func (*SubAckPacket) Pack

func (sack *SubAckPacket) Pack() []byte

func (*SubAckPacket) String

func (sack *SubAckPacket) String() string

func (*SubAckPacket) Type

func (sack *SubAckPacket) Type() Type

func (*SubAckPacket) Unpack

func (sack *SubAckPacket) Unpack(buf []byte) error

type SubscribePacket

type SubscribePacket struct {
	Header byte
	Id     uint16
	Topics []SubscribePayload
}

func CreateSubscribe

func CreateSubscribe(buf byte) *SubscribePacket

func NewSubscribe

func NewSubscribe() *SubscribePacket

func (*SubscribePacket) Length

func (s *SubscribePacket) Length() int

func (*SubscribePacket) Pack

func (s *SubscribePacket) Pack() []byte

func (*SubscribePacket) String

func (s *SubscribePacket) String() string

func (*SubscribePacket) Type

func (s *SubscribePacket) Type() Type

func (*SubscribePacket) Unpack

func (s *SubscribePacket) Unpack(buf []byte) error

type SubscribePayload

type SubscribePayload struct {
	QoS   QoS
	Topic string
}

func (*SubscribePayload) Length

func (p *SubscribePayload) Length() int

func (*SubscribePayload) Pack

func (p *SubscribePayload) Pack() []byte

func (*SubscribePayload) String

func (p *SubscribePayload) String() string

type Type

type Type byte
const (
	RESERVED Type = iota
	CONNECT
	CONNACK
	PUBLISH
	PUBACK
	PUBREC
	PUBREL
	PUBCOMP
	SUBSCRIBE
	SUBACK
	UNSUBSCRIBE
	UNSUBACK
	PING
	PONG
	DISCONNECT
)

func (Type) String

func (t Type) String() string

type UnSubAckPacket

type UnSubAckPacket struct {
	Header byte
	Id     uint16
}

func CreateUnSubAck

func CreateUnSubAck(buf byte) *UnSubAckPacket

func NewUnSubAck

func NewUnSubAck() *UnSubAckPacket

func (*UnSubAckPacket) Length

func (uack *UnSubAckPacket) Length() int

func (*UnSubAckPacket) Pack

func (uack *UnSubAckPacket) Pack() []byte

func (*UnSubAckPacket) String

func (uack *UnSubAckPacket) String() string

func (*UnSubAckPacket) Type

func (uack *UnSubAckPacket) Type() Type

func (*UnSubAckPacket) Unpack

func (uack *UnSubAckPacket) Unpack(buf []byte) error

type UnSubscribePacket

type UnSubscribePacket struct {
	Header byte
	Id     uint16
	Topics []SubscribePayload
}

func CreateUnSubscribe

func CreateUnSubscribe(buf byte) *UnSubscribePacket

func NewUnSub

func NewUnSub() *UnSubscribePacket

func (*UnSubscribePacket) Length

func (u *UnSubscribePacket) Length() int

func (*UnSubscribePacket) Pack

func (u *UnSubscribePacket) Pack() []byte

func (*UnSubscribePacket) String

func (u *UnSubscribePacket) String() string

func (*UnSubscribePacket) Type

func (u *UnSubscribePacket) Type() Type

func (*UnSubscribePacket) Unpack

func (u *UnSubscribePacket) Unpack(buf []byte) error

Jump to

Keyboard shortcuts

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