packets

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2020 License: MIT Imports: 11 Imported by: 8

Documentation

Overview

Package packets is a generated GoMock package.

Index

Constants

View Source
const (
	RESERVED = iota
	CONNECT
	CONNACK
	PUBLISH
	PUBACK
	PUBREC
	PUBREL
	PUBCOMP
	SUBSCRIBE
	SUBACK
	UNSUBSCRIBE
	UNSUBACK
	PINGREQ
	PINGRESP
	DISCONNECT
	AUTH
)

Packet type

View Source
const (
	Qos0             uint8 = 0x00
	Qos1             uint8 = 0x01
	Qos2             uint8 = 0x02
	SubscribeFailure       = 0x80
)

QoS levels & Subscribe failure

View Source
const (
	FlagReserved    = 0
	FlagSubscribe   = 2
	FlagUnsubscribe = 2
	FlagPubrel      = 2
)

Flag in the FixHeader

View Source
const (
	PropPayloadFormat          byte = 0x01
	PropMessageExpiry          byte = 0x02
	PropContentType            byte = 0x03
	PropResponseTopic          byte = 0x08
	PropCorrelationData        byte = 0x09
	PropSubscriptionIdentifier byte = 0x0B
	PropSessionExpiryInterval  byte = 0x11
	PropAssignedClientID       byte = 0x12
	PropServerKeepAlive        byte = 0x13
	PropAuthMethod             byte = 0x15
	PropAuthData               byte = 0x16
	PropRequestProblemInfo     byte = 0x17
	PropWillDelayInterval      byte = 0x18
	PropRequestResponseInfo    byte = 0x19
	PropResponseInfo           byte = 0x1A
	PropServerReference        byte = 0x1C
	PropReasonString           byte = 0x1F
	PropReceiveMaximum         byte = 0x21
	PropTopicAliasMaximum      byte = 0x22
	PropTopicAlias             byte = 0x23
	PropMaximumQoS             byte = 0x24
	PropRetainAvailable        byte = 0x25
	PropUser                   byte = 0x26
	PropMaximumPacketSize      byte = 0x27
	PropWildcardSubAvailable   byte = 0x28
	PropSubIDAvailable         byte = 0x29
	PropSharedSubAvailable     byte = 0x2A
)

Variables

View Source
var (
	ErrInvalUTF8String = errors.New("invalid utf-8 string")
)

Error type

View Source
var ValidProperties = map[byte]map[byte]struct{}{
	PropPayloadFormat:          {CONNECT: {}, PUBLISH: {}},
	PropMessageExpiry:          {CONNECT: {}, PUBLISH: {}},
	PropContentType:            {CONNECT: {}, PUBLISH: {}},
	PropResponseTopic:          {CONNECT: {}, PUBLISH: {}},
	PropCorrelationData:        {CONNECT: {}, PUBLISH: {}},
	PropSubscriptionIdentifier: {SUBSCRIBE: {}},
	PropSessionExpiryInterval:  {CONNECT: {}, CONNACK: {}, DISCONNECT: {}},
	PropAssignedClientID:       {CONNACK: {}},
	PropServerKeepAlive:        {CONNACK: {}},
	PropAuthMethod:             {CONNECT: {}, CONNACK: {}, AUTH: {}},
	PropAuthData:               {CONNECT: {}, CONNACK: {}, AUTH: {}},
	PropRequestProblemInfo:     {CONNECT: {}},
	PropWillDelayInterval:      {CONNECT: {}},
	PropRequestResponseInfo:    {CONNECT: {}},
	PropResponseInfo:           {CONNACK: {}},
	PropServerReference:        {CONNACK: {}, DISCONNECT: {}},
	PropReasonString:           {CONNACK: {}, PUBACK: {}, PUBREC: {}, PUBREL: {}, PUBCOMP: {}, SUBACK: {}, UNSUBACK: {}, DISCONNECT: {}, AUTH: {}},
	PropReceiveMaximum:         {CONNECT: {}, CONNACK: {}},
	PropTopicAliasMaximum:      {CONNECT: {}, CONNACK: {}},
	PropTopicAlias:             {PUBLISH: {}},
	PropMaximumQoS:             {CONNACK: {}},
	PropRetainAvailable:        {CONNACK: {}},
	PropUser:                   {CONNECT: {}, CONNACK: {}, PUBLISH: {}, PUBACK: {}, PUBREC: {}, PUBREL: {}, PUBCOMP: {}, SUBSCRIBE: {}, UNSUBSCRIBE: {}, SUBACK: {}, UNSUBACK: {}, DISCONNECT: {}, AUTH: {}},
	PropMaximumPacketSize:      {CONNECT: {}, CONNACK: {}},
	PropWildcardSubAvailable:   {CONNACK: {}},
	PropSubIDAvailable:         {CONNACK: {}},
	PropSharedSubAvailable:     {CONNACK: {}},
}

ValidProperties is a map of the various properties and the PacketTypes that is valid for server to unpack.

Functions

func DecodeRemainLength

func DecodeRemainLength(length int) ([]byte, error)

DecodeRemainLength 将remain length 转成byte表示

DecodeRemainLength puts the length int into bytes

func DecodeUTF8String

func DecodeUTF8String(buf []byte) (b []byte, size int, err error)

DecodeUTF8String decodes the UTF-8 encoded strings into bytes, returns the decoded bytes, bytes size and error.

func EncodeRemainLength

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

EncodeRemainLength 读remainLength,如果格式错误返回 error

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

func EncodeUTF8String

func EncodeUTF8String(buf []byte) (b []byte, size int, err error)

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

func TopicMatch

func TopicMatch(topic []byte, topicFilter []byte) bool

TopicMatch 返回topic和topic filter是否

TopicMatch returns whether the topic and topic filter is matched.

func TotalBytes

func TotalBytes(p Packet) uint32

TotalBytes returns how many bytes of the packet

func ValidTopicFilter

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

ValidTopicFilter 验证主题过滤器是否合法

ValidTopicFilter returns whether the bytes is a valid topic filter. [MQTT-4.7.1-2] [MQTT-4.7.1-3] ValidTopicFilter 验证主题过滤器是否合法

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, p []byte) bool

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

func ValidUTF8

func ValidUTF8(p []byte) bool

ValidUTF8 验证是否utf8

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

func ValidV5Topic added in v0.2.0

func ValidV5Topic(p []byte) bool

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

func ValidateCode added in v0.2.0

func ValidateCode(packType byte, code byte) bool

func ValidateID added in v0.2.0

func ValidateID(packetType byte, i byte) bool

ValidateID takes a PacketType and a property name and returns a boolean indicating if that property is valid for that PacketType

Types

type Auth added in v0.2.0

type Auth struct {
	FixHeader  *FixHeader
	Code       byte
	Properties *Properties
}

func NewAuthPacket added in v0.2.0

func NewAuthPacket(fh *FixHeader, r io.Reader) (*Auth, error)

func (*Auth) Pack added in v0.2.0

func (a *Auth) Pack(w io.Writer) error

func (*Auth) String added in v0.2.0

func (a *Auth) String() string

func (*Auth) Unpack added in v0.2.0

func (a *Auth) Unpack(r io.Reader) error

type Connack

type Connack struct {
	Version        Version
	FixHeader      *FixHeader
	Code           codes.Code
	SessionPresent bool
	Properties     *Properties
}

Connack represents the MQTT Connack packet

func NewConnackPacket

func NewConnackPacket(fh *FixHeader, version Version, r io.Reader) (*Connack, error)

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

func (*Connack) Pack

func (c *Connack) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Connack) String

func (c *Connack) String() string

func (*Connack) Unpack

func (c *Connack) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct

type Connect

type Connect struct {
	Version   Version
	FixHeader *FixHeader
	//Variable header
	ProtocolLevel byte
	//Connect Flags
	UsernameFlag bool
	ProtocolName []byte
	PasswordFlag bool
	WillRetain   bool
	WillQos      uint8
	WillFlag     bool
	WillTopic    []byte
	WillMsg      []byte
	CleanStart   bool
	KeepAlive    uint16 //如果非零,1.5倍时间没收到则断开连接[MQTT-3.1.2-24]
	//if set
	ClientID []byte
	Username []byte
	Password []byte

	Properties     *Properties
	WillProperties *Properties
}

Connect represents the MQTT Connect packet

func NewConnectPacket

func NewConnectPacket(fh *FixHeader, version Version, r io.Reader) (*Connect, error)

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

func (*Connect) NewConnackPacket

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

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

func (*Connect) Pack

func (c *Connect) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Connect) String

func (c *Connect) String() string

func (*Connect) Unpack

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

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Disconnect

type Disconnect struct {
	Version   Version
	FixHeader *FixHeader
	// V5
	Code       codes.Code
	Properties *Properties
}

Disconnect represents the MQTT Disconnect packet

func NewDisConnectPackets

func NewDisConnectPackets(fh *FixHeader, version Version, 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

func (*Disconnect) Unpack

func (d *Disconnect) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type FixHeader

type FixHeader struct {
	PacketType   byte
	Flags        byte
	RemainLength int
}

FixHeader represents the FixHeader of the MQTT packet

func (*FixHeader) Pack

func (fh *FixHeader) Pack(w io.Writer) error

Pack encodes the FixHeader struct into bytes and writes it into io.Writer.

type MockPacket added in v0.2.0

type MockPacket struct {
	// contains filtered or unexported fields
}

MockPacket is a mock of Packet interface

func NewMockPacket added in v0.2.0

func NewMockPacket(ctrl *gomock.Controller) *MockPacket

NewMockPacket creates a new mock instance

func (*MockPacket) EXPECT added in v0.2.0

func (m *MockPacket) EXPECT() *MockPacketMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockPacket) Pack added in v0.2.0

func (m *MockPacket) Pack(w io.Writer) error

Pack mocks base method

func (*MockPacket) String added in v0.2.0

func (m *MockPacket) String() string

String mocks base method

func (*MockPacket) Unpack added in v0.2.0

func (m *MockPacket) Unpack(r io.Reader) error

Unpack mocks base method

type MockPacketMockRecorder added in v0.2.0

type MockPacketMockRecorder struct {
	// contains filtered or unexported fields
}

MockPacketMockRecorder is the mock recorder for MockPacket

func (*MockPacketMockRecorder) Pack added in v0.2.0

func (mr *MockPacketMockRecorder) Pack(w interface{}) *gomock.Call

Pack indicates an expected call of Pack

func (*MockPacketMockRecorder) String added in v0.2.0

func (mr *MockPacketMockRecorder) String() *gomock.Call

String indicates an expected call of String

func (*MockPacketMockRecorder) Unpack added in v0.2.0

func (mr *MockPacketMockRecorder) Unpack(r interface{}) *gomock.Call

Unpack indicates an expected call of Unpack

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

func NewPacket

func NewPacket(fh *FixHeader, version Version, r io.Reader) (Packet, error)

NewPacket returns a packet representing the decoded MQTT packet and an error.

type PacketID

type PacketID = uint16

PacketID is the type of packet identifier

const (
	MaxPacketID PacketID = 65535
	MinPacketID PacketID = 1
)

Max & min packet ID

type PayloadFormat added in v0.2.0

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

type Pingreq

type Pingreq struct {
	FixHeader *FixHeader
}

Pingreq represents the MQTT Pingreq packet

func NewPingreqPacket

func NewPingreqPacket(fh *FixHeader, r io.Reader) (*Pingreq, error)

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

func (*Pingreq) NewPingresp

func (p *Pingreq) NewPingresp() *Pingresp

NewPingresp returns a Pingresp struct

func (*Pingreq) Pack

func (p *Pingreq) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Pingreq) String

func (p *Pingreq) String() string

func (*Pingreq) Unpack

func (p *Pingreq) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Pingresp

type Pingresp struct {
	FixHeader *FixHeader
}

Pingresp represents the MQTT Pingresp packet

func NewPingrespPacket

func NewPingrespPacket(fh *FixHeader, r io.Reader) (*Pingresp, error)

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

func (*Pingresp) Pack

func (p *Pingresp) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Pingresp) String

func (p *Pingresp) String() string

func (*Pingresp) Unpack

func (p *Pingresp) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Properties added in v0.2.0

type Properties struct {
	// PayloadFormat indicates the format of the payload of the message
	// 0 is unspecified bytes
	// 1 is UTF8 encoded character data
	PayloadFormat *byte
	// MessageExpiry is the lifetime of the message in seconds
	MessageExpiry *uint32
	// ContentType is a UTF8 string describing the content of the message
	// for example it could be a MIME type
	ContentType []byte
	// ResponseTopic is a UTF8 string indicating the topic name to which any
	// response to this message should be sent
	ResponseTopic []byte
	// CorrelationData is binary data used to associate future response
	// messages with the original request message
	CorrelationData []byte
	// SubscriptionIdentifier is an identifier of the subscription to which
	// the Publish matched
	SubscriptionIdentifier []uint32
	// SessionExpiryInterval is the time in seconds after a client disconnects
	// that the server should retain the session Info (subscriptions etc)
	SessionExpiryInterval *uint32
	// AssignedClientID is the server assigned client identifier in the case
	// that a client connected without specifying a clientID the server
	// generates one and returns it in the Connack
	AssignedClientID []byte
	// ServerKeepAlive allows the server to specify in the Connack packet
	// the time in seconds to be used as the keep alive value
	ServerKeepAlive *uint16
	// AuthMethod is a UTF8 string containing the name of the authentication
	// method to be used for extended authentication
	AuthMethod []byte
	// AuthData is binary data containing authentication data
	AuthData []byte
	// RequestProblemInfo is used by the Client to indicate to the server to
	// include the Reason String and/or User Properties in case of failures
	RequestProblemInfo *byte
	// WillDelayInterval is the number of seconds the server waits after the
	// point at which it would otherwise send the will message before sending
	// it. The client reconnecting before that time expires causes the server
	// to cancel sending the will
	WillDelayInterval *uint32
	// RequestResponseInfo is used by the Client to request the Server provide
	// Response Info in the Connack
	RequestResponseInfo *byte
	// ResponseInfo is a UTF8 encoded string that can be used as the basis for
	// createing a Response Topic. The way in which the Client creates a
	// Response Topic from the Response Info is not defined. A common
	// use of this is to pass a globally unique portion of the topic tree which
	// is reserved for this Client for at least the lifetime of its Session. This
	// often cannot just be a random name as both the requesting Client and the
	// responding Client need to be authorized to use it. It is normal to use this
	// as the root of a topic tree for a particular Client. For the Server to
	// return this Info, it normally needs to be correctly configured.
	// Using this mechanism allows this configuration to be done once in the
	// Server rather than in each Client
	ResponseInfo []byte
	// ServerReference is a UTF8 string indicating another server the client
	// can use
	ServerReference []byte
	// ReasonString is a UTF8 string representing the reason associated with
	// this response, intended to be human readable for diagnostic purposes
	ReasonString []byte
	// ReceiveMaximum is the maximum number of QOS1 & 2 messages allowed to be
	// 'inflight' (not having received a PUBACK/PUBCOMP response for)
	ReceiveMaximum *uint16
	// TopicAliasMaximum is the highest value permitted as a Topic Alias
	TopicAliasMaximum *uint16
	// TopicAlias is used in place of the topic string to reduce the size of
	// packets for repeated messages on a topic
	TopicAlias *uint16
	// MaximumQoS is the highest QOS level permitted for a Publish
	MaximumQoS *byte
	// RetainAvailable indicates whether the server supports messages with the
	// retain flag set
	RetainAvailable *byte
	// User is a map of user provided properties
	User []UserProperty

	// MaximumPacketSize allows the client or server to specify the maximum packet
	// size in bytes that they support
	MaximumPacketSize *uint32
	// WildcardSubAvailable indicates whether wildcard subscriptions are permitted
	WildcardSubAvailable *byte
	// SubIDAvailable indicates whether subscription identifiers are supported
	SubIDAvailable *byte
	// SharedSubAvailable indicates whether shared subscriptions are supported
	SharedSubAvailable *byte
}

Properties is a struct representing the all the described properties allowed by the MQTT protocol, determining the validity of a property relvative to the packettype it was received in is provided by the ValidateID function

func (*Properties) Pack added in v0.2.0

func (p *Properties) Pack(bufw *bytes.Buffer, packetType byte)

Pack takes all the defined properties for an Properties and produces a slice of bytes representing the wire format for the Info

func (*Properties) PackWillProperties added in v0.2.0

func (p *Properties) PackWillProperties(bufw *bytes.Buffer)

func (*Properties) String added in v0.2.0

func (p *Properties) String() string

func (*Properties) Unpack added in v0.2.0

func (p *Properties) Unpack(bufr *bytes.Buffer, packetType byte) error

Unpack takes a buffer of bytes and reads out the defined properties filling in the appropriate entries in the struct, it returns the number of bytes used to store the Prop data and any error in decoding them

func (*Properties) UnpackWillProperties added in v0.2.0

func (p *Properties) UnpackWillProperties(bufr *bytes.Buffer) error

type Puback

type Puback struct {
	Version   Version
	FixHeader *FixHeader
	PacketID  PacketID
	// V5
	Code       codes.Code
	Properties *Properties
}

Puback represents the MQTT Puback packet

func NewPubackPacket

func NewPubackPacket(fh *FixHeader, version Version, r io.Reader) (*Puback, error)

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

func (*Puback) Pack

func (p *Puback) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Puback) String

func (p *Puback) String() string

func (*Puback) Unpack

func (p *Puback) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Pubcomp

type Pubcomp struct {
	Version    Version
	FixHeader  *FixHeader
	PacketID   PacketID
	Code       byte
	Properties *Properties
}

Pubcomp represents the MQTT Pubcomp packet

func NewPubcompPacket

func NewPubcompPacket(fh *FixHeader, version Version, r io.Reader) (*Pubcomp, error)

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

func (*Pubcomp) Pack

func (p *Pubcomp) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Pubcomp) String

func (p *Pubcomp) String() string

func (*Pubcomp) Unpack

func (p *Pubcomp) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Publish

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

Publish represents the MQTT Publish packet

func NewPublishPacket

func NewPublishPacket(fh *FixHeader, version Version, r io.Reader) (*Publish, error)

NewPublishPacket returns a Publish instance by the given FixHeader and io.Reader.

func (*Publish) NewPuback

func (p *Publish) NewPuback(code codes.Code, ppt *Properties) *Puback

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

func (*Publish) NewPubrec

func (p *Publish) NewPubrec(code codes.Code, ppt *Properties) *Pubrec

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

func (*Publish) Pack

func (p *Publish) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Publish) String

func (p *Publish) String() string

func (*Publish) Unpack

func (p *Publish) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Pubrec

type Pubrec struct {
	Version   Version
	FixHeader *FixHeader
	PacketID  PacketID
	// V5
	Code       byte
	Properties *Properties
}

Pubrec represents the MQTT Pubrec packet.

func NewPubrecPacket

func NewPubrecPacket(fh *FixHeader, version Version, r io.Reader) (*Pubrec, error)

NewPubrecPacket returns a Pubrec instance by the given FixHeader and io.Reader.

func (*Pubrec) NewPubrel

func (p *Pubrec) NewPubrel() *Pubrel

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

func (*Pubrec) Pack

func (p *Pubrec) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Pubrec) String

func (p *Pubrec) String() string

func (*Pubrec) Unpack

func (p *Pubrec) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Pubrel

type Pubrel struct {
	FixHeader *FixHeader
	PacketID  PacketID
	// V5
	Code       codes.Code
	Properties *Properties
}

Pubrel represents the MQTT Pubrel packet

func NewPubrelPacket

func NewPubrelPacket(fh *FixHeader, r io.Reader) (*Pubrel, error)

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

func (*Pubrel) NewPubcomp

func (p *Pubrel) NewPubcomp() *Pubcomp

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

func (*Pubrel) Pack

func (p *Pubrel) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Pubrel) String

func (p *Pubrel) String() string

func (*Pubrel) Unpack

func (p *Pubrel) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type QoS added in v0.2.0

type QoS = byte

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

func (r *Reader) ReadPacket() (Packet, error)

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

func (*Reader) SetVersion added in v0.2.0

func (r *Reader) SetVersion(version Version)

type SubOptions added in v0.2.0

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 uint8
	// 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
	FixHeader  *FixHeader
	PacketID   PacketID
	Payload    []codes.Code
	Properties *Properties
}

Suback represents the MQTT Suback packet.

func NewSubackPacket

func NewSubackPacket(fh *FixHeader, version Version, r io.Reader) (*Suback, error)

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

func (*Suback) Pack

func (p *Suback) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Suback) String

func (p *Suback) String() string

func (*Suback) Unpack

func (p *Suback) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Subscribe

type Subscribe struct {
	Version    Version
	FixHeader  *FixHeader
	PacketID   PacketID
	Topics     []Topic //suback响应之前填充
	Properties *Properties
}

Subscribe represents the MQTT Subscribe packet.

func NewSubscribePacket

func NewSubscribePacket(fh *FixHeader, version Version, r io.Reader) (*Subscribe, error)

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

func (*Subscribe) NewSuback added in v0.2.0

func (p *Subscribe) NewSuback() *Suback

NewSuback returns the Suback struct which is the ack packet of the Subscribe packet.

func (*Subscribe) Pack

func (p *Subscribe) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Subscribe) String

func (p *Subscribe) String() string

func (*Subscribe) Unpack

func (p *Subscribe) Unpack(r io.Reader) (err error)

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Topic

type Topic struct {
	SubOptions
	Name string
}

Topic represents the MQTT Topic

type Unsuback

type Unsuback struct {
	Version    Version
	FixHeader  *FixHeader
	PacketID   PacketID
	Properties *Properties
	Payload    []codes.Code
}

Unsuback represents the MQTT Unsuback packet.

func NewUnsubackPacket

func NewUnsubackPacket(fh *FixHeader, version Version, r io.Reader) (*Unsuback, error)

NewUnsubackPacket returns a Unsuback instance by the given FixHeader and io.Reader.

func (*Unsuback) Pack

func (p *Unsuback) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Unsuback) String

func (p *Unsuback) String() string

func (*Unsuback) Unpack

func (p *Unsuback) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type Unsubscribe

type Unsubscribe struct {
	Version    Version
	FixHeader  *FixHeader
	PacketID   PacketID
	Topics     []string
	Properties *Properties
}

Unsubscribe represents the MQTT Unsubscribe packet.

func NewUnsubscribePacket

func NewUnsubscribePacket(fh *FixHeader, version Version, r io.Reader) (*Unsubscribe, error)

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

func (*Unsubscribe) NewUnSubBack

func (u *Unsubscribe) NewUnSubBack() *Unsuback

NewUnSubBack returns the Unsuback struct which is the ack packet of the Unsubscribe packet.

func (*Unsubscribe) Pack

func (u *Unsubscribe) Pack(w io.Writer) error

Pack encodes the packet struct into bytes and writes it into io.Writer.

func (*Unsubscribe) String

func (u *Unsubscribe) String() string

func (*Unsubscribe) Unpack

func (u *Unsubscribe) Unpack(r io.Reader) error

Unpack read the packet bytes from io.Reader and decodes it into the packet struct.

type UserProperty added in v0.2.0

type UserProperty struct {
	K []byte
	V []byte
}

type Version added in v0.2.0

type Version = byte

MQTT Version

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

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

func (w *Writer) WriteAndFlush(packet Packet) error

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

func (*Writer) WritePacket

func (w *Writer) WritePacket(packet 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) WriteRaw added in v0.2.0

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

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

Jump to

Keyboard shortcuts

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