Documentation ¶
Overview ¶
Package packet implements functionality for encoding and decoding MQTT packets.
Example ¶
/* Packet Encoding */ // Create new packet. pkt1 := NewConnectPacket() pkt1.Username = "gomqtt" pkt1.Password = "amazing!" // Allocate buffer. buf := make([]byte, pkt1.Len()) // Encode the packet. if _, err := pkt1.Encode(buf); err != nil { panic(err) // error while encoding } /* Packet Decoding */ // Detect packet. l, mt := DetectPacket(buf) // Check length if l == 0 { return // buffer not complete yet } // Create packet. pkt2, err := mt.New() if err != nil { panic(err) // packet type is invalid } // Decode packet. _, err = pkt2.Decode(buf) if err != nil { panic(err) // there was an error while decoding } switch pkt2.Type() { case CONNECT: c := pkt2.(*ConnectPacket) fmt.Println(c.Username) fmt.Println(c.Password) }
Output: gomqtt amazing!
Index ¶
- Constants
- Variables
- func Fuzz(data []byte) int
- type ConnackCode
- type ConnackPacket
- type ConnectPacket
- type Decoder
- type DisconnectPacket
- type Encoder
- type GenericPacket
- type ID
- type Message
- type PingreqPacket
- type PingrespPacket
- type PubackPacket
- type PubcompPacket
- type PublishPacket
- type PubrecPacket
- type PubrelPacket
- type Stream
- type SubackPacket
- type SubscribePacket
- type Subscription
- type Type
- type UnsubackPacket
- type UnsubscribePacket
Examples ¶
Constants ¶
const ( Version311 byte = 4 Version31 byte = 3 )
The supported MQTT versions.
const ( // QOSAtMostOnce defines that the message is delivered at most once, or it // may not be delivered at all. QOSAtMostOnce byte = iota // QOSAtLeastOnce defines that the message is always delivered at least once. QOSAtLeastOnce // QOSExactlyOnce defines that the message is always delivered exactly once. QOSExactlyOnce // QOSFailure indicates that there has been an error while subscribing // to a specific topic. QOSFailure = 0x80 )
Variables ¶
var ErrDetectionOverflow = errors.New("detection overflow")
ErrDetectionOverflow is returned by the Decoder if the next packet couldn't be detect from the initial header bytes.
var ErrReadLimitExceeded = errors.New("read limit exceeded")
ErrReadLimitExceeded can be returned during a Receive if the connection exceeded its read limit.
Note: this error is wrapped in an Error with a NetworkError code.
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 ConnackCode ¶
type ConnackCode uint8
The ConnackCode represents the return code in a ConnackPacket.
const ( ConnectionAccepted ConnackCode = iota ErrInvalidProtocolVersion ErrIdentifierRejected ErrBadUsernameOrPassword ErrNotAuthorized )
All available ConnackCodes.
func (ConnackCode) Error ¶
func (cc ConnackCode) Error() string
Error returns the corresponding error string for the ConnackCode.
func (ConnackCode) Valid ¶
func (cc ConnackCode) Valid() bool
Valid checks if the ConnackCode is valid.
type ConnackPacket ¶
type ConnackPacket struct { // The SessionPresent flag enables a client to establish whether the // client and server have a consistent view about whether there is already // stored session state. SessionPresent bool // If a well formed ConnectPacket is received by the server, but the server // is unable to process it for some reason, then the server should attempt // to send a ConnackPacket containing a non-zero ReturnCode. ReturnCode ConnackCode }
A ConnackPacket is sent by the server in response to a ConnectPacket received from a client.
func NewConnackPacket ¶
func NewConnackPacket() *ConnackPacket
NewConnackPacket creates a new ConnackPacket.
func (*ConnackPacket) Decode ¶
func (cp *ConnackPacket) 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 (*ConnackPacket) Encode ¶
func (cp *ConnackPacket) 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 (*ConnackPacket) Len ¶
func (cp *ConnackPacket) Len() int
Len returns the byte length of the encoded packet.
func (*ConnackPacket) String ¶
func (cp *ConnackPacket) String() string
String returns a string representation of the packet.
type ConnectPacket ¶
type ConnectPacket struct { // The clients client id. ClientID string // The keep alive value. KeepAlive uint16 // The authentication username. Username string // The authentication password. Password string // The clean session flag. CleanSession bool // The will message. Will *Message // The MQTT version 3 or 4 (defaults to 4 when 0). Version byte }
A ConnectPacket is sent by a client to the server after a network connection has been established.
func NewConnectPacket ¶
func NewConnectPacket() *ConnectPacket
NewConnectPacket creates a new ConnectPacket.
func (*ConnectPacket) Decode ¶
func (cp *ConnectPacket) 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 (*ConnectPacket) Encode ¶
func (cp *ConnectPacket) 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 (*ConnectPacket) Len ¶
func (cp *ConnectPacket) Len() int
Len returns the byte length of the encoded packet.
func (*ConnectPacket) String ¶
func (cp *ConnectPacket) String() string
String returns a string representation of the packet.
type Decoder ¶
type Decoder struct { Limit int64 // contains filtered or unexported fields }
A Decoder wraps a Reader and continuously decodes packets.
func (*Decoder) Read ¶
func (d *Decoder) Read() (GenericPacket, error)
Read reads the next packet from the buffered reader.
type DisconnectPacket ¶
type DisconnectPacket struct{}
A DisconnectPacket is sent from the client to the server. It indicates that the client is disconnecting cleanly.
func NewDisconnectPacket ¶
func NewDisconnectPacket() *DisconnectPacket
NewDisconnectPacket creates a new DisconnectPacket.
func (*DisconnectPacket) Decode ¶
func (dp *DisconnectPacket) 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 (*DisconnectPacket) Encode ¶
func (dp *DisconnectPacket) 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 (*DisconnectPacket) Len ¶
func (dp *DisconnectPacket) Len() int
Len returns the byte length of the encoded packet.
func (*DisconnectPacket) String ¶
func (dp *DisconnectPacket) String() string
String returns a string representation of the packet.
func (*DisconnectPacket) Type ¶
func (dp *DisconnectPacket) Type() Type
Type returns the packets type.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder wraps a Writer and continuously encodes packets.
func (*Encoder) Write ¶
func (e *Encoder) Write(pkt GenericPacket) error
Write encodes and writes the passed packet to the write buffer.
type GenericPacket ¶ added in v0.2.0
type GenericPacket 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 }
A GenericPacket is an MQTT control packet that can be encoded to a buffer or decoded from a buffer.
type ID ¶ added in v0.2.0
type ID uint16
ID is the type used to store packet ids.
func GetID ¶ added in v0.2.0
func GetID(packet GenericPacket) (ID, bool)
GetID checks the packets type and returns its ID and true, or if it does not have a ID, zero and false.
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 byte // 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 PingreqPacket ¶
type PingreqPacket struct{}
A PingreqPacket is sent from a client to the server.
func NewPingreqPacket ¶
func NewPingreqPacket() *PingreqPacket
NewPingreqPacket creates a new PingreqPacket.
func (*PingreqPacket) Decode ¶
func (pp *PingreqPacket) 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 (*PingreqPacket) Encode ¶
func (pp *PingreqPacket) 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 (*PingreqPacket) Len ¶
func (pp *PingreqPacket) Len() int
Len returns the byte length of the encoded packet.
func (*PingreqPacket) String ¶
func (pp *PingreqPacket) String() string
String returns a string representation of the packet.
type PingrespPacket ¶
type PingrespPacket struct{}
A PingrespPacket is sent by the server to the client in response to a PingreqPacket. It indicates that the server is alive.
func NewPingrespPacket ¶
func NewPingrespPacket() *PingrespPacket
NewPingrespPacket creates a new PingrespPacket.
func (*PingrespPacket) Decode ¶
func (pp *PingrespPacket) 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 (*PingrespPacket) Encode ¶
func (pp *PingrespPacket) 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 (*PingrespPacket) Len ¶
func (pp *PingrespPacket) Len() int
Len returns the byte length of the encoded packet.
func (*PingrespPacket) String ¶
func (pp *PingrespPacket) String() string
String returns a string representation of the packet.
type PubackPacket ¶
type PubackPacket struct { // The packet identifier. ID ID }
A PubackPacket is the response to a PublishPacket with QOS level 1.
func NewPubackPacket ¶
func NewPubackPacket() *PubackPacket
NewPubackPacket creates a new PubackPacket.
func (*PubackPacket) Decode ¶
func (pp *PubackPacket) 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 (*PubackPacket) Encode ¶
func (pp *PubackPacket) 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 (*PubackPacket) Len ¶
func (pp *PubackPacket) Len() int
Len returns the byte length of the encoded packet.
func (*PubackPacket) String ¶
func (pp *PubackPacket) String() string
String returns a string representation of the packet.
type PubcompPacket ¶
type PubcompPacket struct { // The packet identifier. ID ID }
A PubcompPacket is the response to a PubrelPacket. It is the fourth and final packet of the QOS 2 protocol exchange.
func NewPubcompPacket ¶
func NewPubcompPacket() *PubcompPacket
NewPubcompPacket creates a new PubcompPacket.
func (*PubcompPacket) Decode ¶
func (pp *PubcompPacket) 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 (*PubcompPacket) Encode ¶
func (pp *PubcompPacket) 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 (*PubcompPacket) Len ¶
func (pp *PubcompPacket) Len() int
Len returns the byte length of the encoded packet.
func (*PubcompPacket) String ¶
func (pp *PubcompPacket) String() string
String returns a string representation of the packet.
type PublishPacket ¶
type PublishPacket 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 // PublishPacket. 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 PublishPacket is sent from a client to a server or from server to a client to transport an application message.
func NewPublishPacket ¶
func NewPublishPacket() *PublishPacket
NewPublishPacket creates a new PublishPacket.
func (*PublishPacket) Decode ¶
func (pp *PublishPacket) 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 (*PublishPacket) Encode ¶
func (pp *PublishPacket) 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 (*PublishPacket) Len ¶
func (pp *PublishPacket) Len() int
Len returns the byte length of the encoded packet.
func (*PublishPacket) String ¶
func (pp *PublishPacket) String() string
String returns a string representation of the packet.
type PubrecPacket ¶
type PubrecPacket struct { // Shared packet identifier. ID ID }
A PubrecPacket is the response to a PublishPacket with QOS 2. It is the second packet of the QOS 2 protocol exchange.
func NewPubrecPacket ¶
func NewPubrecPacket() *PubrecPacket
NewPubrecPacket creates a new PubrecPacket.
func (*PubrecPacket) Decode ¶
func (pp *PubrecPacket) 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 (*PubrecPacket) Encode ¶
func (pp *PubrecPacket) 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 (*PubrecPacket) Len ¶
func (pp *PubrecPacket) Len() int
Len returns the byte length of the encoded packet.
func (*PubrecPacket) String ¶
func (pp *PubrecPacket) String() string
String returns a string representation of the packet.
type PubrelPacket ¶
type PubrelPacket struct { // Shared packet identifier. ID ID }
A PubrelPacket is the response to a PubrecPacket. It is the third packet of the QOS 2 protocol exchange.
func NewPubrelPacket ¶
func NewPubrelPacket() *PubrelPacket
NewPubrelPacket creates a new PubrelPacket.
func (*PubrelPacket) Decode ¶
func (pp *PubrelPacket) 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 (*PubrelPacket) Encode ¶
func (pp *PubrelPacket) 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 (*PubrelPacket) Len ¶
func (pp *PubrelPacket) Len() int
Len returns the byte length of the encoded packet.
func (*PubrelPacket) String ¶
func (pp *PubrelPacket) String() string
String returns a string representation of the packet.
type SubackPacket ¶
type SubackPacket struct { // The granted QOS levels for the requested subscriptions. ReturnCodes []uint8 // The packet identifier. ID ID }
A SubackPacket is sent by the server to the client to confirm receipt and processing of a SubscribePacket. The SubackPacket contains a list of return codes, that specify the maximum QOS levels that have been granted.
func NewSubackPacket ¶
func NewSubackPacket() *SubackPacket
NewSubackPacket creates a new SubackPacket.
func (*SubackPacket) Decode ¶
func (sp *SubackPacket) 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 (*SubackPacket) Encode ¶
func (sp *SubackPacket) 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 (*SubackPacket) Len ¶
func (sp *SubackPacket) Len() int
Len returns the byte length of the encoded packet.
func (*SubackPacket) String ¶
func (sp *SubackPacket) String() string
String returns a string representation of the packet.
type SubscribePacket ¶
type SubscribePacket struct { // The subscriptions. Subscriptions []Subscription // The packet identifier. ID ID }
A SubscribePacket 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 NewSubscribePacket ¶
func NewSubscribePacket() *SubscribePacket
NewSubscribePacket creates a new SUBSCRIBE packet.
func (*SubscribePacket) Decode ¶
func (sp *SubscribePacket) 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 (*SubscribePacket) Encode ¶
func (sp *SubscribePacket) 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 (*SubscribePacket) Len ¶
func (sp *SubscribePacket) Len() int
Len returns the byte length of the encoded packet.
func (*SubscribePacket) String ¶
func (sp *SubscribePacket) String() string
String returns a string representation of the packet.
func (*SubscribePacket) Type ¶
func (sp *SubscribePacket) Type() Type
Type returns the packets type.
type Subscription ¶
type Subscription struct { // The topic to subscribe. Topic string // The requested maximum QOS level. QOS uint8 }
A Subscription is a single subscription in a SubscribePacket.
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.
func (Type) New ¶
func (t Type) New() (GenericPacket, error)
New creates a new packet based on the type. It is a shortcut to call one of the New*Packet functions. An error is returned if the type is invalid.
type UnsubackPacket ¶
type UnsubackPacket struct { // Shared packet identifier. ID ID }
An UnsubackPacket is sent by the server to the client to confirm receipt of an UnsubscribePacket.
func NewUnsubackPacket ¶
func NewUnsubackPacket() *UnsubackPacket
NewUnsubackPacket creates a new UnsubackPacket.
func (*UnsubackPacket) Decode ¶
func (up *UnsubackPacket) 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 (*UnsubackPacket) Encode ¶
func (up *UnsubackPacket) 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 (*UnsubackPacket) Len ¶
func (up *UnsubackPacket) Len() int
Len returns the byte length of the encoded packet.
func (*UnsubackPacket) String ¶
func (up *UnsubackPacket) String() string
String returns a string representation of the packet.
type UnsubscribePacket ¶
type UnsubscribePacket struct { // The topics to unsubscribe from. Topics []string // The packet identifier. ID ID }
An UnsubscribePacket is sent by the client to the server.
func NewUnsubscribePacket ¶
func NewUnsubscribePacket() *UnsubscribePacket
NewUnsubscribePacket creates a new UnsubscribePacket.
func (*UnsubscribePacket) Decode ¶
func (up *UnsubscribePacket) 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 (*UnsubscribePacket) Encode ¶
func (up *UnsubscribePacket) 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 (*UnsubscribePacket) Len ¶
func (up *UnsubscribePacket) Len() int
Len returns the byte length of the encoded packet.
func (*UnsubscribePacket) String ¶
func (up *UnsubscribePacket) String() string
String returns a string representation of the packet.
func (*UnsubscribePacket) Type ¶
func (up *UnsubscribePacket) Type() Type
Type returns the packets type.