packets

package
v0.0.0-...-3133fea Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// ConnAck status codes
	ConnAckAccepted       uint8 = 0x00
	ConnAckBadVersion     uint8 = 0x01
	ConnAckIDNotAllowed   uint8 = 0x02
	ConnAckServerUnavail  uint8 = 0x03
	ConnAckBadCredentials uint8 = 0x04
	ConnAckUnauthorized   uint8 = 0x05
)
View Source
const (

	// Flags
	PublishFlagDuplicate uint8 = 0x08
	PublishFlagRetain    uint8 = 0x01
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnAck

type ConnAck struct {
	SessionPresent bool
	ReturnCode     uint8
	Version        mqtt.Version
}

func (*ConnAck) MarshalBinary

func (c *ConnAck) MarshalBinary() (b []byte, err error)

func (*ConnAck) ReadFrom

func (c *ConnAck) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads and unmarshals the ConnAck request from stream. NOTE: it is assumed that the command byte is already consumed from the reader.

func (*ConnAck) WriteTo

func (c *ConnAck) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes the marshaled ConnAck packet to the stream w.

type Connect

type Connect struct {
	// Version holds the protocol version of this packet (see mqtt package).
	Version mqtt.Version
	// CleanSession stores the clean session flag (MQTT 3.1.1) or clean
	// start flag (MQTT 5.0).
	//
	// For MQTT 3.1.1 the clean session flag forces the server to discard
	// any previous session state and start a new one which last until the
	// client disconnects.
	//
	// For MQTT 5.0 however, the clean start flag only notifies the server
	// to discard session state on connect. What happens after disconnect
	// is determined by session expiry interval.
	CleanSession bool
	// KeepAlive contains the duration in seconds for the client to remain
	// inactive before getting disconnected.
	KeepAlive uint16

	// WillTopic is an optional topic to publish on a successful connect.
	WillTopic mqtt.Topic
	// WillMessage is the payload message for the WillTopic (max 64KiB).
	// NOTE: WillMessage requires WillTopic to be set, otherwise the
	// parameter is ignored.
	WillMessage []byte
	// WillRetain holds the retain flag for the published WillTopic. If
	// set to true, the server retains the message for future subscribers
	// on the WillTopic.
	WillRetain bool

	// ClientID stores the client identifier presented to the server. If
	// left empty a random UUID (v4) is automatically generated.
	ClientID string
	// Username holds the Username credential if the server has access
	// control enabled (cannot be an empty string).
	Username string
	// Password stores the password credential (cannot be an empty string).
	// NOTE: (MQTTv311) Password requires username to be assigned,
	// otherwise the parameter is ignored.
	Password string

	// SessionExpiryInterval holds the duration in seconds the server is
	// required to store the session state. A value of 0xFFFFFFFF
	// (max(uint32)) makes the session does not expire. If the value 0 is
	// used, the session expire when the network connection is closed
	// (defaults to 0).
	SessionExpiryInterval uint32
	// MaxPacketSize tells the server the maximum packet size the client
	// is willing to accept (defaults to 0: no limit).
	MaxPacketSize uint32
	// ReceiveMax notifies the server about the number of QOS1 and QOS2
	// publish packets the client is willing to process simultaneously
	// (defaults to 65535).
	ReceiveMax uint16
	// TopicAliasMax sets the limit on the highest number of topic aliases
	// the client is willing to accept from the server (defaults to 0).
	TopicAliasMax uint16
	// RequestResponseInfo requests the server to return response
	// information in the ConnAck packet. (defaults to false).
	RequestResponseInfo bool
	// DisableProblemInfo requests the server NOT to return a reason string
	// or user properties on Publish, ConnAck and Disconnect packet
	// (defaults to false: enabled).
	DisableProblemInfo bool
	// ConnUserProperties contains user specified (connection related)
	// key-value pairs. The meaning of these properties is not defined by
	// the MQTT 5.0 specification
	// (ref. https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.pdf :871).
	ConnUserProperties map[string]string
	// AuthMethod specifies (and enables) the name of the authentication
	// method used for extensive authentication. If specified, the client
	// must not follow up with any other packets than Auth or Disconnect
	// packets until a ConnAck is received. (defaults to none/disabled).
	AuthMethod string
	// AuthData holds binary data associated with the specified AuthMethod.
	// If an AuthMethod is not specified this parameter is ignored.
	AuthData []byte

	// WillDelayInterval requests the server to delay publishing the
	// WillMessage after the set amount of seconds (defaults to 0).
	WillDelayInterval uint32
	// WillMessageExpiry sets the expiry interval for the published
	// WillMessage (defaults to 0: unset).
	WillMessageExpiry uint32
	// WillFormatUTF8 notifies the server that the WillMessage is encoded
	// using UTF8, otherwise the payload is treated as a stream of bytes
	// (default).
	WillFormatUTF8 bool
	// WillContentType provides a string content-type descriptor of the
	// published WillMessage (unset by default).
	WillContentType string
	// WillResponseTopic provides a response topic the recipients should
	// use to respond to the WillMessage. Setting the WillResponseTopic
	// enables request/response interaction between MQTT clients. (defaults
	// to none).
	WillResponseTopic string
	// WillCorrelationData is used by the sender of the request message to
	// identify which request the response message is for. The value is
	// ignored if WillResponseTopic is not set (defaults to unset).
	WillCorrelationData []byte
	// WillUserProperties provides user-specified key:value pairs of data
	// to the WillMessage. The interpretation of these parameters are
	// completely up to the user's application (think of it as custom HTTP
	// headers).
	WillUserProperties map[string]string
}

Connect contains a structural representation of a connect packet. Some of the parameters are dependent, for instance: Password requires Username to be set. Other parameters are version dependent, these are highlighted in the parameter description.

func (*Connect) MarshalBinary

func (c *Connect) MarshalBinary() (b []byte, err error)

func (*Connect) ReadFrom

func (c *Connect) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads and unmarshals a connect request from the stream. NOTE: it is assumed that the command byte has already been consumed.

func (*Connect) WriteTo

func (c *Connect) WriteTo(w io.Writer) (n int64, err error)

WriteTo marshals and writes the connect request to the stream w.

type Disconnect

type Disconnect struct {
	Version mqtt.Version
}

func (*Disconnect) MarshalBinary

func (d *Disconnect) MarshalBinary() (b []byte, err error)

func (*Disconnect) ReadFrom

func (d *Disconnect) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads the final length byte from stream, verifying that the packet is indeed a disconnect request.

func (*Disconnect) WriteTo

func (d *Disconnect) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes the marshaled Disconnect request to stream.

type Packet

type Packet interface {
	// WriteTo serializes the packet and writes it to the given writer
	// returning the number of bytes written.
	WriteTo(w io.Writer) (n int64, err error)
	// ReadFrom reads and unmarshals the packet from the given stream
	// returning the number of bytes read.
	ReadFrom(r io.Reader) (n int64, err error)
	// MarshalBinary serializes the packet to a binary buffer.
	MarshalBinary() (b []byte, err error)
}

Packet contains a generic packet interface conforming with the standard io WriteTo/ReadFrom definitions.

type PacketIO

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

PacketIO provides an interface for communicating packets between client and server.

func NewPacketIO

func NewPacketIO(
	conn net.Conn,
	version mqtt.Version,
	timeout time.Duration,
) *PacketIO

NewPacketIO initializes a new PacketIO struct.

func (*PacketIO) Close

func (p *PacketIO) Close() error

Close closes the underlying connection.

func (*PacketIO) Recv

func (p *PacketIO) Recv() (pkg Packet, err error)

Recv reads and encodes a packet from stream. The Recv operation is protected by a mutex, but should only be handled by a single goroutine.

func (*PacketIO) Send

func (p *PacketIO) Send(pkt Packet) (err error)

Send writes the packet p to stream w, ensuring mutual exclusive access.

type PingReq

type PingReq struct {
	Version mqtt.Version
}

func (*PingReq) MarshalBinary

func (p *PingReq) MarshalBinary() (b []byte, err error)

func (*PingReq) ReadFrom

func (p *PingReq) ReadFrom(r io.Reader) (n int64, err error)

func (*PingReq) WriteTo

func (p *PingReq) WriteTo(w io.Writer) (n int64, err error)

type PingResp

type PingResp struct {
	Version mqtt.Version
}

func (*PingResp) MarshalBinary

func (p *PingResp) MarshalBinary() (b []byte, err error)

func (*PingResp) ReadFrom

func (p *PingResp) ReadFrom(r io.Reader) (n int64, err error)

func (*PingResp) WriteTo

func (p *PingResp) WriteTo(w io.Writer) (n int64, err error)

type PubAck

type PubAck struct {
	Version mqtt.Version

	// Variable header
	PacketIdentifier uint16
}

func (*PubAck) MarshalBinary

func (p *PubAck) MarshalBinary() (b []byte, err error)

func (*PubAck) ReadFrom

func (p *PubAck) ReadFrom(r io.Reader) (n int64, err error)

func (*PubAck) WriteTo

func (p *PubAck) WriteTo(w io.Writer) (n int64, err error)

type PubComp

type PubComp struct {
	Version mqtt.Version

	// Variable header
	PacketIdentifier uint16
}

func (*PubComp) MarshalBinary

func (p *PubComp) MarshalBinary() (b []byte, err error)

func (*PubComp) ReadFrom

func (p *PubComp) ReadFrom(r io.Reader) (n int64, err error)

func (*PubComp) WriteTo

func (p *PubComp) WriteTo(w io.Writer) (n int64, err error)

type PubRec

type PubRec struct {
	Version mqtt.Version

	// Variable header
	PacketIdentifier uint16
}

func (*PubRec) MarshalBinary

func (p *PubRec) MarshalBinary() (b []byte, err error)

func (*PubRec) ReadFrom

func (p *PubRec) ReadFrom(r io.Reader) (n int64, err error)

func (*PubRec) WriteTo

func (p *PubRec) WriteTo(w io.Writer) (n int64, err error)

type PubRel

type PubRel struct {
	Version mqtt.Version

	// Variable header
	PacketIdentifier uint16
}

func (*PubRel) MarshalBinary

func (p *PubRel) MarshalBinary() (b []byte, err error)

func (*PubRel) ReadFrom

func (p *PubRel) ReadFrom(r io.Reader) (n int64, err error)

func (*PubRel) WriteTo

func (p *PubRel) WriteTo(w io.Writer) (n int64, err error)

type Publish

type Publish struct {
	mqtt.Topic
	Version mqtt.Version

	// Flags
	Duplicate bool
	Retain    bool

	// Variable header
	PacketIdentifier uint16

	Payload []byte
}

func (*Publish) MarshalBinary

func (p *Publish) MarshalBinary() (b []byte, err error)

func (*Publish) ReadFrom

func (p *Publish) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads a publish packet (minus command byte) from the stream. CAUTION: The least significant nibble from the command will not be parsed and must be set outside the scope of this function.

func (*Publish) WriteTo

func (p *Publish) WriteTo(w io.Writer) (n int64, err error)

type SubAck

type SubAck struct {
	Version mqtt.Version

	PacketIdentifier uint16

	ReturnCodes []uint8
}

func (*SubAck) MarshalBinary

func (s *SubAck) MarshalBinary() (b []byte, err error)

func (*SubAck) ReadFrom

func (s *SubAck) ReadFrom(r io.Reader) (n int64, err error)

func (*SubAck) WriteTo

func (s *SubAck) WriteTo(w io.Writer) (n int64, err error)

type Subscribe

type Subscribe struct {
	Version mqtt.Version

	PacketIdentifier uint16

	// Payload
	Topics []mqtt.Topic
}

func (*Subscribe) MarshalBinary

func (s *Subscribe) MarshalBinary() (b []byte, err error)

func (*Subscribe) ReadFrom

func (s *Subscribe) ReadFrom(r io.Reader) (n int64, err error)

func (*Subscribe) WriteTo

func (s *Subscribe) WriteTo(w io.Writer) (n int64, err error)

type UnsubAck

type UnsubAck struct {
	Version mqtt.Version

	PacketIdentifier uint16
}

func (*UnsubAck) MarshalBinary

func (u *UnsubAck) MarshalBinary() (b []byte, err error)

func (*UnsubAck) ReadFrom

func (u *UnsubAck) ReadFrom(r io.Reader) (n int64, err error)

func (*UnsubAck) WriteTo

func (u *UnsubAck) WriteTo(w io.Writer) (n int64, err error)

type Unsubscribe

type Unsubscribe struct {
	Version mqtt.Version

	PacketIdentifier uint16

	Topics []string
}

func (*Unsubscribe) MarshalBinary

func (u *Unsubscribe) MarshalBinary() (b []byte, err error)

func (*Unsubscribe) ReadFrom

func (u *Unsubscribe) ReadFrom(r io.Reader) (n int64, err error)

func (*Unsubscribe) WriteTo

func (u *Unsubscribe) WriteTo(w io.Writer) (n int64, err error)

Jump to

Keyboard shortcuts

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