clients

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrConnectionClosed is returned when operating on a closed
	// connection and/or when no error cause has been given.
	ErrConnectionClosed = errors.New("Connection not open")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	State        State           // the operational state of the client.
	LWT          LWT             // the last will and testament for the client.
	Inflight     Inflight        // a map of in-flight qos messages.
	sync.RWMutex                 // mutex
	Username     []byte          // the username the client authenticated with.
	AC           auth.Controller // an auth controller inherited from the listener.
	Listener     string          // the id of the listener the client is connected to.
	ID           string          // the client id.

	R             *circ.Reader                  // a reader for reading incoming bytes.
	W             *circ.Writer                  // a writer for writing outgoing bytes.
	Subscriptions map[string]packets.SubOptions // a map of the subscription filters a client maintains.

	CleanSession    bool              // indicates if the client expects a clean-session.
	ProtocolVersion byte              // mqtt protocol version, optional value: v3.1 = 3、v3.1.1 = 4、v5.0 = 5
	TopicAlias      map[uint16]string // key is alias, value is topic, for v5
	// contains filtered or unexported fields
}

Client contains information about a client known by the broker.

func NewClient

func NewClient(c net.Conn, r *circ.Reader, w *circ.Writer, s *system.Info, rm int) *Client

NewClient returns a new instance of Client.

func NewClientStub

func NewClientStub(s *system.Info, rm int) *Client

NewClientStub returns an instance of Client with basic initializations. This method is typically called by the persistence restoration system.

func (*Client) ClearBuffers

func (cl *Client) ClearBuffers()

ClearBuffers sets the read/write buffers to nil so they can be deallocated automatically when no longer in use.

func (*Client) ForgetSubscription

func (cl *Client) ForgetSubscription(filter string)

ForgetSubscription forgests a subscription note for the client.

func (*Client) Identify

func (cl *Client) Identify(lid string, pk packets.Packet, ac auth.Controller)

Identify sets the identification values of a client instance.

func (*Client) Info

func (cl *Client) Info() events.Client

Info returns an event-version of a client, containing minimal information.

func (*Client) NextPacketID

func (cl *Client) NextPacketID() uint32

NextPacketID returns the next packet id for a client, looping back to 0 if the maximum ID has been reached.

func (*Client) NoteSubscription

func (cl *Client) NoteSubscription(filter string, so packets.SubOptions)

NoteSubscription makes a note of a subscription for the client.

func (*Client) Read

func (cl *Client) Read(packetHandler func(*Client, packets.Packet) error) error

Read loops forever reading new packets from a client connection until an error is encountered (or the connection is closed).

func (*Client) ReadConnectPacket

func (cl *Client) ReadConnectPacket(fh *packets.FixedHeader) (pk packets.Packet, err error)

ReadConnectPacket reads the remaining buffer into an MQTT connect packet.

func (*Client) ReadFixedHeader

func (cl *Client) ReadFixedHeader(fh *packets.FixedHeader) error

ReadFixedHeader reads in the values of the next packet's fixed header.

func (*Client) ReadPacket

func (cl *Client) ReadPacket(fh *packets.FixedHeader) (pk packets.Packet, err error)

ReadPacket reads the remaining buffer into an MQTT packet,not connect packet.

func (*Client) SendInflight

func (cl *Client) SendInflight(handler func(cl *Client, im *InflightMessage, force bool) error)

SendInflight resend all inflight message

func (*Client) Start

func (cl *Client) Start()

Start begins the client goroutines reading and writing packets.

func (*Client) Stop

func (cl *Client) Stop(err error)

Stop instructs the client to shut down all processing goroutines and disconnect. A cause error may be passed to identfy the reason for stopping.

func (*Client) StopCause

func (cl *Client) StopCause() error

StopCause returns the reason the client connection was stopped, if any.

func (*Client) WritePacket

func (cl *Client) WritePacket(pk packets.Packet) (n int, err error)

WritePacket encodes and writes a packet to the client.

type Clients

type Clients struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Clients contains a map of the clients known by the broker.

func New

func New() *Clients

New returns an instance of Clients.

func (*Clients) Add

func (cl *Clients) Add(val *Client)

Add adds a new client to the clients map, keyed on client id.

func (*Clients) Delete

func (cl *Clients) Delete(id string)

Delete removes a client from the internal map.

func (*Clients) Get

func (cl *Clients) Get(id string) (*Client, bool)

Get returns the value of a client if it exists.

func (*Clients) GetAll

func (cl *Clients) GetAll() map[string]*Client

GetAll returns all clients

func (*Clients) GetByListener

func (cl *Clients) GetByListener(id string) []*Client

GetByListener returns clients matching a listener id.

func (*Clients) Len

func (cl *Clients) Len() int

Len returns the length of the clients map.

type Inflight

type Inflight interface {
	Set(key uint16, in *InflightMessage) bool
	Get(key uint16) (*InflightMessage, bool)
	GetAll() map[uint16]*InflightMessage
	Walk(cl *Client, handler func(cl *Client, im *InflightMessage, force bool) error)
	Len() int
	Delete(key uint16) bool
}

Inflight is an interface of for storing and manipulating inflight messages

type InflightCache

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

InflightCache is a cache of InflightMessage keyed on packet id.

func NewCache

func NewCache(cap uint16) *InflightCache

func (*InflightCache) Delete

func (i *InflightCache) Delete(key uint16) bool

Delete removes an in-flight message from the map. Returns true if the message existed.

func (*InflightCache) Get

func (i *InflightCache) Get(key uint16) (*InflightMessage, bool)

Get returns the value of an in-flight message if it exists.

func (*InflightCache) GetAll

func (i *InflightCache) GetAll() map[uint16]*InflightMessage

GetAll returns all the in-flight messages.

func (*InflightCache) Len

func (i *InflightCache) Len() int

Len returns the size of the in-flight messages map.

func (*InflightCache) Set

func (i *InflightCache) Set(key uint16, in *InflightMessage) bool

Set stores the packet of an Inflight message, keyed on message id. Returns true if the inflight message was new.

func (*InflightCache) Walk

func (i *InflightCache) Walk(cl *Client, handler func(cl *Client, im *InflightMessage, force bool) error)

Walk

type InflightMap

type InflightMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

InflightMap is a map of InflightMessage keyed on packet id.

func NewMap

func NewMap(cp int) *InflightMap

func (*InflightMap) Delete

func (i *InflightMap) Delete(key uint16) bool

Delete removes an in-flight message from the map. Returns true if the message existed.

func (*InflightMap) Get

func (i *InflightMap) Get(key uint16) (*InflightMessage, bool)

Get returns the value of an in-flight message if it exists.

func (*InflightMap) GetAll

func (i *InflightMap) GetAll() map[uint16]*InflightMessage

GetAll returns all the in-flight messages.

func (*InflightMap) Len

func (i *InflightMap) Len() int

Len returns the size of the in-flight messages map.

func (*InflightMap) Set

func (i *InflightMap) Set(key uint16, in *InflightMessage) bool

func (*InflightMap) Walk

func (i *InflightMap) Walk(cl *Client, handler func(cl *Client, im *InflightMessage, force bool) error)

Walk

type InflightMessage

type InflightMessage struct {
	Packet  packets.Packet // the packet currently in-flight.
	Sent    int64          // the last time the message was sent (for retries) in unixtime.
	Resends int            // the number of times the message was attempted to be sent.
	Expiry  int64          // the message expiration time in unixtime.
}

InflightMessage contains data about a packet which is currently in-flight.

type LWT

type LWT struct {
	Message []byte // the message that shall be sent when the client disconnects.
	Topic   string // the topic the will message shall be sent to.
	Qos     byte   // the quality of service desired.
	Retain  bool   // indicates whether the will message should be retained
}

LWT contains the last will and testament details for a client connection.

type State

type State struct {
	Done uint32 // atomic counter which indicates that the client has closed.
	// contains filtered or unexported fields
}

State tracks the state of the client.

Jump to

Keyboard shortcuts

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