Documentation ¶
Index ¶
- Constants
- Variables
- func ReadFixedHeader2(r io.ByteReader) (pktType, flags byte, payloadSize uint32, err error)
- type ConnackPacket
- type ConnectPacket
- type ConnectPacketConfig
- type ConnectReturnCode
- type ControlPacketType
- type DisconnectPacket
- type FixedHeader
- type Packet
- type PingreqPacket
- type PingrespPacket
- type PublishPacket
- type SubackPacket
- type SubscribePacket
- type TopicQoS
- type UnsubackPacket
- type UnsubscribePacket
Constants ¶
const ( Connect uint8 = iota + 1 Connack Publish Puback Pubrec Pubrel Pubcomp Subscribe Suback Unsubscribe Unsuback Pingreq Pingresp Disconnect )
Connect well, connect packet type, the rest are also self-explanatory
Variables ¶
var ErrInvalidPacket = errors.New("Packet content is invalid")
ErrInvalidPacket ...
var ErrShortBuffer = errors.New("Buffer is too short")
ErrShortBuffer ...
Functions ¶
func ReadFixedHeader2 ¶
func ReadFixedHeader2(r io.ByteReader) (pktType, flags byte, payloadSize uint32, err error)
ReadFixedHeader2 retrieves both the ctrl byte(type + flags) and payloadSize from an io.ByteReader such as net.Conn
Types ¶
type ConnackPacket ¶
type ConnackPacket struct { Code ConnectReturnCode SessionPresent bool }
ConnackPacket holds the in-memory representation of a connack packet
func DeserializeConnackPktPayload ¶
func DeserializeConnackPktPayload(f FixedHeader, p []byte) (*ConnackPacket, error)
DeserializeConnackPktPayload parses the contents of a bytes slice and returns a ConnackPacket as required.
func (*ConnackPacket) ConnectionAccepted ¶
func (p *ConnackPacket) ConnectionAccepted() (ok bool, description string)
ConnectionAccepted is a convenience method that allows the user, ie a client to check whether their connection was accepted and if not, the reason why
func (*ConnackPacket) Len ¶
func (p *ConnackPacket) Len() int
Len returns the total length in terms of bytes that the Connack packet takes
func (*ConnackPacket) Serialize ¶
func (p *ConnackPacket) Serialize(b []byte) ([]byte, error)
Serialize serializes the contents of a connack packet into a []byte buffer. Buffer should be of appropriate length otherwise a ErrShortBuffer error is returned. If nil buffer is provided, Serialize instantiates a buffer of required length and returns it
type ConnectPacket ¶
type ConnectPacket struct { WillRetain bool WillQoS byte WillFlag bool CleanSession bool // ok KeepAlive uint16 // ok ClientIdentifier []byte // ok WillTopic []byte WillMessage []byte Username []byte // ok Password []byte // ok // contains filtered or unexported fields }
ConnectPacket holds the in-application deserialization of a ConnectPacket
func DeserializeConnectPktPayload ¶
func DeserializeConnectPktPayload(f FixedHeader, p []byte) (*ConnectPacket, error)
DeserializeConnectPktPayload parses the contents of a bytes slice and returns a ConnectPacket as required.
func NewConnectPacket ¶
func NewConnectPacket(cfg *ConnectPacketConfig) (*ConnectPacket, error)
NewConnectPacket instantiates a ConnectPacket based on the config object passed. To be used by the client rather than the server. A nil or zero length cfg.ClientIdentifier indicates that the client intends for the broker to assign a unique client identifier for it. If the cfg.Username is nil or of len 0, the username and password flags will not be set, plus the respective strings will be of 0 length. The Will Flag is set iff both the cfg.WillTopic and cfg.WillMessage are of nonzero length. If the WillQoS is invalid, ie not equal to 0x0, 0x1, 0x2 then an error is returned.
func (*ConnectPacket) Len ¶
func (p *ConnectPacket) Len() int
Len returns the total number of bytes the ConnectPacket will take up
func (*ConnectPacket) Serialize ¶
func (p *ConnectPacket) Serialize(b []byte) ([]byte, error)
Serialize serializes the contents of a connect packet into a []byte buffer. Buffer should be of appropriate length otherwise a ErrShortBuffer error is returned. If nil buffer is provided, Serialize instantiates a buffer of required length and returns it
type ConnectPacketConfig ¶
type ConnectPacketConfig struct {
ClientIdentifier, Username, Password []byte
KeepAliveSeconds uint16
ShouldCleanSession bool
WillTopic, WillMessage []byte
WillQoS byte
WillRetain bool
}
ConnectPacketConfig is more of a necessary evil, it is used to configure the connect packet during instantiation. The alternative was either to have a constructor with lots and lots of parameters or to make fields in ConnectPacket public which I tried to avoid since the flags too must be made public and that places more burden on the end user to make sure the flags set are consistent with the fields present. The non-primitive ConnectPacketConfig fields should not be modified any further after a ConnectPacket is instantiated from the config since the constructor shallow copies the fields. As a sidenote as to why this is a necessary evil, check out the article below, config objects are kind of an antipattern https://middlemost.com/object-lifecycle/
type ConnectReturnCode ¶
type ConnectReturnCode byte
ConnectReturnCode holds the return code for when a broker responds to a client's connect packet
const ( ConnAccepted ConnectReturnCode = iota ConnRefusedUnacceptableProtocol ConnRefusedIdentifierRejected ConnRefusedBadUsernamePass ConnRefusedNotAuthorized )
ConnAccepted etc self-explanatory
func (ConnectReturnCode) String ¶
func (code ConnectReturnCode) String() string
type ControlPacketType ¶
type ControlPacketType uint8
ControlPacketType represent the packet type of an mqtt packet
func (ControlPacketType) String ¶
func (c ControlPacketType) String() string
type DisconnectPacket ¶
type DisconnectPacket struct{}
DisconnectPacket holds the in-memory representation of a disconnect packet
func (*DisconnectPacket) Len ¶
func (p *DisconnectPacket) Len() int
Len returns the total length in terms of bytes that the disconnect packet takes
func (*DisconnectPacket) Serialize ¶
func (p *DisconnectPacket) Serialize(b []byte) ([]byte, error)
Serialize serializes the contents of a disconnect packet into a []byte buffer. Buffer should be of appropriate length otherwise a ErrShortBuffer error is returned. If nil buffer is provided, Serialize instantiates a buffer of required length and returns it
type FixedHeader ¶
FixedHeader represents the content of a fixed header for a given mqtt packet
func ReadFixedHeader ¶
func ReadFixedHeader(r io.ByteReader) (f FixedHeader, err error)
ReadFixedHeader retrieves both the ctrl byte(type + flags) and payloadSize from an io.ByteReader such as net.Conn
func (FixedHeader) IsValidFlagsSet ¶
func (f FixedHeader) IsValidFlagsSet() bool
IsValidFlagsSet checks if the correct flags are set
type PingreqPacket ¶
type PingreqPacket struct{}
PingreqPacket holds the in-memory representation of a ping request packet
func (*PingreqPacket) Len ¶
func (p *PingreqPacket) Len() int
Len returns the total length in terms of bytes that a ping request packet takes
func (*PingreqPacket) Serialize ¶
func (p *PingreqPacket) Serialize(b []byte) ([]byte, error)
Serialize serializes the contents of a ping request packet into a []byte buffer. Buffer should be of appropriate length otherwise an error is returned. If nil buffer is provided, Serialize instantiates a buffer of required length and returns it
type PingrespPacket ¶
type PingrespPacket struct{}
PingrespPacket holds the in-memory representation of a ping request packet
func (*PingrespPacket) Len ¶
func (p *PingrespPacket) Len() int
Len returns the total length in terms of bytes that a ping response packet takes
func (*PingrespPacket) Serialize ¶
func (p *PingrespPacket) Serialize(b []byte) ([]byte, error)
Serialize serializes the contents of a ping response packet into a []byte buffer. Buffer should be of appropriate length otherwise an error is returned. If nil buffer is provided, Serialize instantiates a buffer of required length and returns it
type PublishPacket ¶
type PublishPacket struct { Dup bool QoS byte Retain bool TopicName []byte PacketIdentifier uint16 Payload []byte }
PublishPacket is an in-mem representation of a Publish packet
func DeserializePublishPktPayload ¶
func DeserializePublishPktPayload(f FixedHeader, p []byte) (*PublishPacket, error)
DeserializePublishPktPayload parses the contents of a bytes slice and returns a PublishPacket as required.
func (*PublishPacket) Len ¶
func (p *PublishPacket) Len() int
Len returns number of bytes publish packet will take when serialized
func (*PublishPacket) Serialize ¶
func (p *PublishPacket) Serialize(b []byte) ([]byte, error)
Serialize serializes the contents of a publish packet into a []byte buffer. Buffer should be of appropriate length otherwise a ErrShortBuffer error is returned. If nil buffer is provided, Serialize instantiates a buffer of required length and returns it
type SubackPacket ¶
SubackPacket is an in-mem representation of a sub packet
func DeserializeSubackPktPayload ¶
func DeserializeSubackPktPayload(f FixedHeader, p []byte) (*SubackPacket, error)
DeserializeSubackPktPayload parses the contents of a bytes slice and returns a PublishPacket as required.
func (*SubackPacket) AddCode ¶
func (p *SubackPacket) AddCode(c byte) error
AddCode might be failure or QoS
func (*SubackPacket) AddFailure ¶
func (p *SubackPacket) AddFailure()
AddFailure adds a failure code
func (*SubackPacket) AddQoSGranted ¶
func (p *SubackPacket) AddQoSGranted(qos byte) error
AddQoSGranted adds the QoS granted
func (*SubackPacket) Len ¶
func (p *SubackPacket) Len() int
Len returns number of bytes suback packet will take when serialized
type SubscribePacket ¶
SubscribePacket is an in-mem representation of a sub packet
func DeserializeSubscribePktPayload ¶
func DeserializeSubscribePktPayload(f FixedHeader, p []byte) (*SubscribePacket, error)
DeserializeSubscribePktPayload parses the contents of a bytes slice and returns a PublishPacket as required.
func (*SubscribePacket) AddTopic ¶
func (p *SubscribePacket) AddTopic(topic []byte, QoS byte) error
AddTopic adds a given topic plus QoS level to the Subscribe Packet, It checks if the topic and qos levels are valid, if not, returns an error
func (*SubscribePacket) Len ¶
func (p *SubscribePacket) Len() int
Len returns number of bytes subscribe packet will take when serialized
type UnsubackPacket ¶
type UnsubackPacket struct {
PacketIdentifier uint16
}
UnsubackPacket is an in-mem representation of a unsuback packet
func DeserializeUnsubackPktPayload ¶
func DeserializeUnsubackPktPayload(f FixedHeader, p []byte) (*UnsubackPacket, error)
DeserializeUnsubackPktPayload parses the contents of a bytes slice and returns a Unsuback packet as required.
func (*UnsubackPacket) Len ¶
func (p *UnsubackPacket) Len() int
Len returns number of bytes packet will take when serialized
type UnsubscribePacket ¶
UnsubscribePacket is an in-mem representation of a Unsubscribe packet
func DeserializeUnsubscribePktPayload ¶
func DeserializeUnsubscribePktPayload(f FixedHeader, p []byte) (*UnsubscribePacket, error)
DeserializeUnsubscribePktPayload parses the contents of a bytes slice and returns a Unsubscribe as required.
func (*UnsubscribePacket) AddTopic ¶
func (p *UnsubscribePacket) AddTopic(topic []byte) error
AddTopic adds a given topic that a client wishes to unsubscribe from.
func (*UnsubscribePacket) Len ¶
func (p *UnsubscribePacket) Len() int
Len returns number of bytes publish packet will take when serialized