Documentation ¶
Overview ¶
Package packets is a generated GoMock package.
Index ¶
- Constants
- Variables
- func DecodeRemainLength(length int) ([]byte, error)
- func DecodeUTF8String(buf []byte) (b []byte, size int, err error)
- func EncodeRemainLength(r io.ByteReader) (int, error)
- func EncodeUTF8String(buf []byte) (b []byte, size int, err error)
- func TopicMatch(topic []byte, topicFilter []byte) bool
- func TotalBytes(p Packet) uint32
- func ValidTopicFilter(mustUTF8 bool, p []byte) bool
- func ValidTopicName(mustUTF8 bool, p []byte) bool
- func ValidUTF8(p []byte) bool
- func ValidV5Topic(p []byte) bool
- func ValidateCode(packType byte, code byte) bool
- func ValidateID(packetType byte, i byte) bool
- type Auth
- type Connack
- type Connect
- type Disconnect
- type FixHeader
- type MockPacket
- type MockPacketMockRecorder
- type Packet
- type PacketID
- type PayloadFormat
- type Pingreq
- type Pingresp
- type Properties
- 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 Unsuback
- type Unsubscribe
- type UserProperty
- type Version
- type Writer
Constants ¶
const ( RESERVED = iota CONNECT CONNACK PUBLISH PUBACK PUBREC PUBREL PUBCOMP SUBSCRIBE SUBACK UNSUBSCRIBE UNSUBACK PINGREQ PINGRESP DISCONNECT AUTH )
Packet type
const ( Qos0 uint8 = 0x00 Qos1 uint8 = 0x01 Qos2 uint8 = 0x02 SubscribeFailure = 0x80 )
QoS levels & Subscribe failure
const ( FlagReserved = 0 FlagSubscribe = 2 FlagUnsubscribe = 2 FlagPubrel = 2 )
Flag in the FixHeader
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 )
Variables ¶
var (
ErrInvalUTF8String = errors.New("invalid utf-8 string")
)
Error type
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 ¶
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 ¶
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 ¶
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] 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 ¶ added in v0.2.0
ValidV5Topic returns whether the given bytes is a valid MQTT V5 topic
func ValidateCode ¶ added in v0.2.0
func ValidateID ¶ added in v0.2.0
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 }
type Connack ¶
type Connack struct { Version Version FixHeader *FixHeader Code codes.Code SessionPresent bool Properties *Properties }
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 { 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 ¶
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 { Version Version FixHeader *FixHeader // V5 Code codes.Code Properties *Properties }
Disconnect represents the MQTT Disconnect packet
func NewDisConnectPackets ¶
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 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
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
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 ¶
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 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 *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 ¶
NewPubackPacket returns a Puback instance by the given FixHeader and io.Reader
type Pubcomp ¶
type Pubcomp struct { Version Version FixHeader *FixHeader PacketID PacketID Code byte Properties *Properties }
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 { 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 ¶
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
type Pubrec ¶
type Pubrec struct { Version Version FixHeader *FixHeader PacketID PacketID // V5 Code byte Properties *Properties }
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 ¶
type Pubrel struct { FixHeader *FixHeader PacketID PacketID // V5 Code codes.Code Properties *Properties }
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
func (*Reader) SetVersion ¶ added in v0.2.0
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 ¶
NewSubackPacket returns a Suback instance by the given FixHeader and io.Reader.
type Subscribe ¶
type Subscribe struct { Version Version FixHeader *FixHeader PacketID PacketID Topics []Topic //suback响应之前填充 Properties *Properties }
Subscribe represents the MQTT Subscribe packet.
func NewSubscribePacket ¶
NewSubscribePacket returns a Subscribe instance by the given FixHeader and io.Reader.
func (*Subscribe) NewSuback ¶ added in v0.2.0
NewSuback returns the Suback struct which is the ack packet of the Subscribe packet.
type Unsuback ¶
type Unsuback struct { Version Version FixHeader *FixHeader PacketID PacketID Properties *Properties Payload []codes.Code }
Unsuback represents the MQTT Unsuback packet.
func NewUnsubackPacket ¶
NewUnsubackPacket returns a Unsuback instance by the given FixHeader and io.Reader.
type Unsubscribe ¶
type Unsubscribe struct { Version Version FixHeader *FixHeader PacketID PacketID Topics []string Properties *Properties }
Unsubscribe represents the MQTT Unsubscribe packet.
func NewUnsubscribePacket ¶
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
type UserProperty ¶ added in v0.2.0
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.