packet

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2018 License: Apache-2.0 Imports: 7 Imported by: 51

Documentation

Overview

Package packet implements functionality for encoding and decoding MQTT packets.

Example
/* Packet Encoding */

// Create new packet.
pkt1 := NewConnect()
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.(*Connect)
	fmt.Println(c.Username)
	fmt.Println(c.Password)
}
Output:

gomqtt
amazing!

Index

Examples

Constants

View Source
const (
	Version311 byte = 4
	Version31  byte = 3
)

The supported MQTT versions.

View Source
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

View Source
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.

View Source
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

func Fuzz(data []byte) int

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 added in v0.7.1

type Connack 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 Connect packet is received by the server, but the server
	// is unable to process it for some reason, then the server should attempt
	// to send a Connack containing a non-zero ReturnCode.
	ReturnCode ConnackCode
}

A Connack packet is sent by the server in response to a Connect packet received from a client.

func NewConnack added in v0.7.1

func NewConnack() *Connack

NewConnack creates a new Connack packet.

func (*Connack) Decode added in v0.7.1

func (cp *Connack) 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 (*Connack) Encode added in v0.7.1

func (cp *Connack) 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 (*Connack) Len added in v0.7.1

func (cp *Connack) Len() int

Len returns the byte length of the encoded packet.

func (*Connack) String added in v0.7.1

func (cp *Connack) String() string

String returns a string representation of the packet.

func (*Connack) Type added in v0.7.1

func (cp *Connack) Type() Type

Type returns the packets type.

type ConnackCode

type ConnackCode uint8

The ConnackCode represents the return code in a Connack packet.

const (
	ConnectionAccepted ConnackCode = iota
	ErrInvalidProtocolVersion
	ErrIdentifierRejected
	ErrServerUnavailable
	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 Connect added in v0.7.1

type Connect 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 Connect packet is sent by a client to the server after a network connection has been established.

func NewConnect added in v0.7.1

func NewConnect() *Connect

NewConnect creates a new Connect packet.

func (*Connect) Decode added in v0.7.1

func (cp *Connect) 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 (*Connect) Encode added in v0.7.1

func (cp *Connect) 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 (*Connect) Len added in v0.7.1

func (cp *Connect) Len() int

Len returns the byte length of the encoded packet.

func (*Connect) String added in v0.7.1

func (cp *Connect) String() string

String returns a string representation of the packet.

func (*Connect) Type added in v0.7.1

func (cp *Connect) Type() Type

Type returns the packets type.

type Decoder

type Decoder struct {
	Limit int64
	// contains filtered or unexported fields
}

A Decoder wraps a Reader and continuously decodes packets.

func NewDecoder

func NewDecoder(reader io.Reader) *Decoder

NewDecoder returns a new Decoder.

func (*Decoder) Read

func (d *Decoder) Read() (Generic, error)

Read reads the next packet from the buffered reader.

type Disconnect added in v0.7.1

type Disconnect struct{}

A Disconnect packet is sent from the client to the server. It indicates that the client is disconnecting cleanly.

func NewDisconnect added in v0.7.1

func NewDisconnect() *Disconnect

NewDisconnect creates a new Disconnect packet.

func (*Disconnect) Decode added in v0.7.1

func (dp *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 added in v0.7.1

func (dp *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 added in v0.7.1

func (dp *Disconnect) Len() int

Len returns the byte length of the encoded packet.

func (*Disconnect) String added in v0.7.1

func (dp *Disconnect) String() string

String returns a string representation of the packet.

func (*Disconnect) Type added in v0.7.1

func (dp *Disconnect) 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 NewEncoder

func NewEncoder(writer io.Writer) *Encoder

NewEncoder creates a new Encoder.

func (*Encoder) Flush

func (e *Encoder) Flush() error

Flush flushes the writer buffer.

func (*Encoder) Write

func (e *Encoder) Write(pkt Generic) error

Write encodes and writes the passed packet to the write buffer.

type Generic added in v0.7.1

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 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(pkt Generic) (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.

func (Message) Copy

func (m Message) Copy() *Message

Copy returns a copy of the message.

func (*Message) String

func (m *Message) String() string

String returns a string representation of the message.

type Pingreq added in v0.7.1

type Pingreq struct{}

A Pingreq packet is sent from a client to the server.

func NewPingreq added in v0.7.1

func NewPingreq() *Pingreq

NewPingreq creates a new Pingreq packet.

func (*Pingreq) Decode added in v0.7.1

func (pp *Pingreq) 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 (*Pingreq) Encode added in v0.7.1

func (pp *Pingreq) 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 (*Pingreq) Len added in v0.7.1

func (pp *Pingreq) Len() int

Len returns the byte length of the encoded packet.

func (*Pingreq) String added in v0.7.1

func (pp *Pingreq) String() string

String returns a string representation of the packet.

func (*Pingreq) Type added in v0.7.1

func (pp *Pingreq) Type() Type

Type returns the packets type.

type Pingresp added in v0.7.1

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 NewPingresp added in v0.7.1

func NewPingresp() *Pingresp

NewPingresp creates a new Pingresp packet.

func (*Pingresp) Decode added in v0.7.1

func (pp *Pingresp) 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 (*Pingresp) Encode added in v0.7.1

func (pp *Pingresp) 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 (*Pingresp) Len added in v0.7.1

func (pp *Pingresp) Len() int

Len returns the byte length of the encoded packet.

func (*Pingresp) String added in v0.7.1

func (pp *Pingresp) String() string

String returns a string representation of the packet.

func (*Pingresp) Type added in v0.7.1

func (pp *Pingresp) Type() Type

Type returns the packets type.

type Puback added in v0.7.1

type Puback struct {
	// The packet identifier.
	ID ID
}

A Puback packet is the response to a Publish packet with QOS level 1.

func NewPuback added in v0.7.1

func NewPuback() *Puback

NewPuback creates a new Puback packet.

func (*Puback) Decode added in v0.7.1

func (pp *Puback) 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 (*Puback) Encode added in v0.7.1

func (pp *Puback) 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 (*Puback) Len added in v0.7.1

func (pp *Puback) Len() int

Len returns the byte length of the encoded packet.

func (*Puback) String added in v0.7.1

func (pp *Puback) String() string

String returns a string representation of the packet.

func (*Puback) Type added in v0.7.1

func (pp *Puback) Type() Type

Type returns the packets type.

type Pubcomp added in v0.7.1

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 NewPubcomp added in v0.7.1

func NewPubcomp() *Pubcomp

NewPubcomp creates a new Pubcomp packet.

func (*Pubcomp) Decode added in v0.7.1

func (pp *Pubcomp) 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 (*Pubcomp) Encode added in v0.7.1

func (pp *Pubcomp) 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 (*Pubcomp) Len added in v0.7.1

func (pp *Pubcomp) Len() int

Len returns the byte length of the encoded packet.

func (*Pubcomp) String added in v0.7.1

func (pp *Pubcomp) String() string

String returns a string representation of the packet.

func (*Pubcomp) Type added in v0.7.1

func (pp *Pubcomp) Type() Type

Type returns the packets type.

type Publish added in v0.7.1

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 NewPublish added in v0.7.1

func NewPublish() *Publish

NewPublish creates a new Publish packet.

func (*Publish) Decode added in v0.7.1

func (pp *Publish) 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 (*Publish) Encode added in v0.7.1

func (pp *Publish) 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 (*Publish) Len added in v0.7.1

func (pp *Publish) Len() int

Len returns the byte length of the encoded packet.

func (*Publish) String added in v0.7.1

func (pp *Publish) String() string

String returns a string representation of the packet.

func (*Publish) Type added in v0.7.1

func (pp *Publish) Type() Type

Type returns the packets type.

type Pubrec added in v0.7.1

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 NewPubrec added in v0.7.1

func NewPubrec() *Pubrec

NewPubrec creates a new Pubrec packet.

func (*Pubrec) Decode added in v0.7.1

func (pp *Pubrec) 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 (*Pubrec) Encode added in v0.7.1

func (pp *Pubrec) 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 (*Pubrec) Len added in v0.7.1

func (pp *Pubrec) Len() int

Len returns the byte length of the encoded packet.

func (*Pubrec) String added in v0.7.1

func (pp *Pubrec) String() string

String returns a string representation of the packet.

func (*Pubrec) Type added in v0.7.1

func (pp *Pubrec) Type() Type

Type returns the packets type.

type Pubrel added in v0.7.1

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 NewPubrel added in v0.7.1

func NewPubrel() *Pubrel

NewPubrel creates a new Pubrel packet.

func (*Pubrel) Decode added in v0.7.1

func (pp *Pubrel) 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 (*Pubrel) Encode added in v0.7.1

func (pp *Pubrel) 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 (*Pubrel) Len added in v0.7.1

func (pp *Pubrel) Len() int

Len returns the byte length of the encoded packet.

func (*Pubrel) String added in v0.7.1

func (pp *Pubrel) String() string

String returns a string representation of the packet.

func (*Pubrel) Type added in v0.7.1

func (pp *Pubrel) Type() Type

Type returns the packets type.

type Stream

type Stream struct {
	Decoder
	Encoder
}

A Stream combines an Encoder and Decoder

func NewStream

func NewStream(reader io.Reader, writer io.Writer) *Stream

NewStream creates a new Stream.

type Suback added in v0.7.1

type Suback struct {
	// The granted QOS levels for the requested subscriptions.
	ReturnCodes []uint8

	// 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 NewSuback added in v0.7.1

func NewSuback() *Suback

NewSuback creates a new Suback packet.

func (*Suback) Decode added in v0.7.1

func (sp *Suback) 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 (*Suback) Encode added in v0.7.1

func (sp *Suback) 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 (*Suback) Len added in v0.7.1

func (sp *Suback) Len() int

Len returns the byte length of the encoded packet.

func (*Suback) String added in v0.7.1

func (sp *Suback) String() string

String returns a string representation of the packet.

func (*Suback) Type added in v0.7.1

func (sp *Suback) Type() Type

Type returns the packets type.

type Subscribe added in v0.7.1

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 NewSubscribe added in v0.7.1

func NewSubscribe() *Subscribe

NewSubscribe creates a new Subscribe packet.

func (*Subscribe) Decode added in v0.7.1

func (sp *Subscribe) 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 (*Subscribe) Encode added in v0.7.1

func (sp *Subscribe) 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 (*Subscribe) Len added in v0.7.1

func (sp *Subscribe) Len() int

Len returns the byte length of the encoded packet.

func (*Subscribe) String added in v0.7.1

func (sp *Subscribe) String() string

String returns a string representation of the packet.

func (*Subscribe) Type added in v0.7.1

func (sp *Subscribe) 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 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

func DetectPacket(src []byte) (int, Type)

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() (Generic, 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.

func (Type) String

func (t Type) String() string

String returns the type as a string.

func (Type) Valid

func (t Type) Valid() bool

Valid returns a boolean indicating whether the type is valid or not.

type Unsuback added in v0.7.1

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 NewUnsuback added in v0.7.1

func NewUnsuback() *Unsuback

NewUnsuback creates a new Unsuback packet.

func (*Unsuback) Decode added in v0.7.1

func (up *Unsuback) 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 (*Unsuback) Encode added in v0.7.1

func (up *Unsuback) 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 (*Unsuback) Len added in v0.7.1

func (up *Unsuback) Len() int

Len returns the byte length of the encoded packet.

func (*Unsuback) String added in v0.7.1

func (up *Unsuback) String() string

String returns a string representation of the packet.

func (*Unsuback) Type added in v0.7.1

func (up *Unsuback) Type() Type

Type returns the packets type.

type Unsubscribe added in v0.7.1

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 added in v0.7.1

func NewUnsubscribe() *Unsubscribe

NewUnsubscribe creates a new Unsubscribe packet.

func (*Unsubscribe) Decode added in v0.7.1

func (up *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 added in v0.7.1

func (up *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 added in v0.7.1

func (up *Unsubscribe) Len() int

Len returns the byte length of the encoded packet.

func (*Unsubscribe) String added in v0.7.1

func (up *Unsubscribe) String() string

String returns a string representation of the packet.

func (*Unsubscribe) Type added in v0.7.1

func (up *Unsubscribe) Type() Type

Type returns the packets type.

Jump to

Keyboard shortcuts

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