Documentation ¶
Overview ¶
Package packet implements functionality for encoding and decoding MQTT packets.
Index ¶
- Constants
- Variables
- func Fuzz(data []byte) int
- type Connack
- type ConnackCode
- type Connect
- type Disconnect
- type Error
- type Generic
- type ID
- type Message
- type Pingreq
- type Pingresp
- type Puback
- type Pubcomp
- type Publish
- type Pubrec
- type Pubrel
- type QOS
- type Suback
- type Subscribe
- type Subscription
- type Type
- type Unsuback
- type Unsubscribe
Constants ¶
const ( Version31 byte = 3 Version311 byte = 4 )
Variables ¶
var ErrInvalidPacketType = errors.New("invalid packet type")
ErrInvalidPacketType is returned by New if the packet type is invalid.
var MaxVarint = uint64(268435455)
Functions ¶
func Fuzz ¶
Fuzz is a basic fuzzing test that works with https://github.com/dvyukov/go-fuzz:
$ go-fuzz-build github.com/gomqtt/packet $ go-fuzz -bin=./packet-fuzz.zip -workdir=./fuzz
Types ¶
type Connack ¶
type Connack struct { SessionPresent bool ReturnCode ConnackCode }
func NewConnack ¶
func NewConnack() *Connack
type ConnackCode ¶
type ConnackCode uint8
const ( ConnectionAccepted ConnackCode = iota InvalidProtocolVersion IdentifierRejected BadUsernameOrPassword NotAuthorized )
func (ConnackCode) String ¶
func (cc ConnackCode) String() string
func (ConnackCode) Valid ¶
func (cc ConnackCode) Valid() bool
type Connect ¶
type Connect struct { ClientID string KeepAlive uint16 Username string Password string CleanSession bool Will *Message Version byte }
func NewConnect ¶
func NewConnect() *Connect
type Disconnect ¶
type Disconnect struct{}
A Disconnect packet is sent from the client to the server. It indicates that the client is disconnecting cleanly.
func NewDisconnect ¶
func NewDisconnect() *Disconnect
NewDisconnect creates a new Disconnect packet.
func (*Disconnect) Decode ¶
func (d *Disconnect) Decode(src []byte) (int, error)
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Disconnect) Encode ¶
func (d *Disconnect) Encode(dst []byte) (int, error)
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
func (*Disconnect) Len ¶
func (d *Disconnect) Len() int
Len returns the byte length of the encoded packet.
func (*Disconnect) String ¶
func (d *Disconnect) String() string
String returns a string representation of the packet.
type Error ¶
type Error struct { Type Type // contains filtered or unexported fields }
Error represents decoding and encoding errors.
type Generic ¶
type Generic interface { // Type returns the packets type. Type() Type // Len returns the byte length of the encoded packet. Len() int // Decode reads from the byte slice argument. It returns the total number of // bytes decoded, and whether there have been any errors during the process. Decode(src []byte) (int, error) // Encode writes the packet bytes into the byte slice from the argument. It // returns the number of bytes encoded and whether there's any errors along // the way. If there is an error, the byte slice should be considered invalid. Encode(dst []byte) (int, error) // String returns a string representation of the packet. String() string }
Generic is an MQTT control packet that can be encoded to a buffer or decoded from a buffer.
type ID ¶
type ID uint16
ID is the type used to store packet ids.
type Message ¶
type Message struct { // The Topic of the message. Topic string // The Payload of the message. Payload []byte // The QOS indicates the level of assurance for delivery. QOS QOS // If the Retain flag is set to true, the server must store the message, // so that it can be delivered to future subscribers whose subscriptions // match its topic name. Retain bool }
A Message bundles data that is published between brokers and clients.
type Pingreq ¶
type Pingreq struct{}
A Pingreq packet is sent from a client to the server.
func (*Pingreq) Decode ¶
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Pingreq) Encode ¶
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
type Pingresp ¶
type Pingresp struct{}
A Pingresp packet is sent by the server to the client in response to a Pingreq. It indicates that the server is alive.
func (*Pingresp) Decode ¶
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Pingresp) Encode ¶
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
type Puback ¶
type Puback struct { // The packet identifier. ID ID }
A Puback packet is the response to a Publish packet with QOS level 1.
func (*Puback) Decode ¶
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Puback) Encode ¶
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
type Pubcomp ¶
type Pubcomp struct { // The packet identifier. ID ID }
A Pubcomp packet is the response to a Pubrel. It is the fourth and final packet of the QOS 2 protocol exchange.
func (*Pubcomp) Decode ¶
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Pubcomp) Encode ¶
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
type Publish ¶
type Publish struct { // The message to publish. Message Message // If the Dup flag is set to false, it indicates that this is the first // occasion that the client or server has attempted to send this // Publish packet. If the dup flag is set to true, it indicates that this // might be re-delivery of an earlier attempt to send the packet. Dup bool // The packet identifier. ID ID }
A Publish packet is sent from a client to a server or from server to a client to transport an application message.
func (*Publish) Decode ¶
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Publish) Encode ¶
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
type Pubrec ¶
type Pubrec struct { // Shared packet identifier. ID ID }
A Pubrec packet is the response to a Publish packet with QOS 2. It is the second packet of the QOS 2 protocol exchange.
func (*Pubrec) Decode ¶
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Pubrec) Encode ¶
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
type Pubrel ¶
type Pubrel struct { // Shared packet identifier. ID ID }
A Pubrel packet is the response to a Pubrec packet. It is the third packet of the QOS 2 protocol exchange.
func (*Pubrel) Decode ¶
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Pubrel) Encode ¶
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
type QOS ¶
type QOS byte
QOS is the type used to store quality of service levels.
const ( // QOSAtMostOnce defines that the message is delivered at most once, or it // may not be delivered at all. QOSAtMostOnce QOS = iota // QOSAtLeastOnce defines that the message is always delivered at least once. QOSAtLeastOnce QOS = iota // QOSExactlyOnce defines that the message is always delivered exactly once. QOSExactlyOnce QOS = iota // QOSFailure indicates that there has been an error while subscribing // to a specific topic. QOSFailure QOS = 0x80 )
func (QOS) Successful ¶
Successful returns if the provided quality of service level represents a successful value.
type Suback ¶
type Suback struct { // The granted QOS levels for the requested subscriptions. ReturnCodes []QOS // The packet identifier. ID ID }
A Suback packet is sent by the server to the client to confirm receipt and processing of a Subscribe packet. The Suback packet contains a list of return codes, that specify the maximum QOS levels that have been granted.
func (*Suback) Decode ¶
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Suback) Encode ¶
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
type Subscribe ¶
type Subscribe struct { // The subscriptions. Subscriptions []Subscription // The packet identifier. ID ID }
A Subscribe packet is sent from the client to the server to create one or more Subscriptions. The server will forward application messages that match these subscriptions using PublishPackets.
func (*Subscribe) Decode ¶
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Subscribe) Encode ¶
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
type Subscription ¶
type Subscription struct { // The topic to subscribe. Topic string // The requested maximum QOS level. QOS QOS }
A Subscription is a single subscription in a Subscribe packet.
func (*Subscription) String ¶
func (s *Subscription) String() string
type Type ¶
type Type byte
Type represents the MQTT packet types.
const ( CONNECT Type CONNACK PUBLISH PUBACK PUBREC PUBREL PUBCOMP SUBSCRIBE SUBACK UNSUBSCRIBE UNSUBACK PINGREQ PINGRESP DISCONNECT )
All packet types.
func DetectPacket ¶
DetectPacket tries to detect the next packet in a buffer. It returns a length greater than zero if the packet has been detected as well as its Type.
type Unsuback ¶
type Unsuback struct { // Shared packet identifier. ID ID }
An Unsuback packet is sent by the server to the client to confirm receipt of an Unsubscribe packet.
func (*Unsuback) Decode ¶
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Unsuback) Encode ¶
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
type Unsubscribe ¶
type Unsubscribe struct { // The topics to unsubscribe from. Topics []string // The packet identifier. ID ID }
An Unsubscribe packet is sent by the client to the server.
func NewUnsubscribe ¶
func NewUnsubscribe() *Unsubscribe
NewUnsubscribe creates a new Unsubscribe packet.
func (*Unsubscribe) Decode ¶
func (u *Unsubscribe) Decode(src []byte) (int, error)
Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.
func (*Unsubscribe) Encode ¶
func (u *Unsubscribe) Encode(dst []byte) (int, error)
Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.
func (*Unsubscribe) Len ¶
func (u *Unsubscribe) Len() int
Len returns the byte length of the encoded packet.
func (*Unsubscribe) String ¶
func (u *Unsubscribe) String() string
String returns a string representation of the packet.