client

package
v3.3.3 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: Apache-2.0 Imports: 25 Imported by: 10

Documentation

Index

Constants

View Source
const DefaultMTU = 1472
View Source
const ExchangeLifetime = 247 * time.Second

https://datatracker.ietf.org/doc/html/rfc7252#section-4.8.2

Variables

View Source
var DefaultConfig = func() Config {
	opts := Config{
		Common: config.NewCommon[*Conn](),
		CreateInactivityMonitor: func() InactivityMonitor {
			return inactivity.NewNilMonitor[*Conn]()
		},
		RequestMonitor: func(*Conn, *pool.Message) (bool, error) {
			return false, nil
		},
		Dialer:                         &net.Dialer{Timeout: time.Second * 3},
		Net:                            "udp",
		TransmissionNStart:             1,
		TransmissionAcknowledgeTimeout: time.Second * 2,
		TransmissionMaxRetransmit:      4,
		GetMID:                         message.GetMID,
		MTU:                            DefaultMTU,
	}
	opts.Handler = func(w *responsewriter.ResponseWriter[*Conn], r *pool.Message) {
		switch r.Code() {
		case codes.POST, codes.PUT, codes.GET, codes.DELETE:
			if err := w.SetResponse(codes.NotFound, message.TextPlain, nil); err != nil {
				opts.Errors(fmt.Errorf("udp client: cannot set response: %w", err))
			}
		}
	}
	return opts
}()

Functions

This section is empty.

Types

type Config

type Config struct {
	config.Common[*Conn]
	CreateInactivityMonitor        CreateInactivityMonitorFunc
	RequestMonitor                 RequestMonitorFunc
	Net                            string
	GetMID                         GetMIDFunc
	Handler                        HandlerFunc
	Dialer                         *net.Dialer
	TransmissionNStart             uint32
	TransmissionAcknowledgeTimeout time.Duration
	TransmissionMaxRetransmit      uint32
	CloseSocket                    bool
	MTU                            uint16
}

type Conn

type Conn struct {
	*client.Client[*Conn]
	// contains filtered or unexported fields
}

Conn represents a virtual connection to a conceptual endpoint, to perform COAPs commands.

func NewConn

func NewConn(
	session Session,
	createBlockWise func(cc *Conn) *blockwise.BlockWise[*Conn],
	inactivityMonitor InactivityMonitor,
	cfg *Config,
) *Conn

NewConn creates connection over session and observation.

func NewConnWithOpts added in v3.3.0

func NewConnWithOpts(session Session, cfg *Config, opts ...Option) *Conn

func (*Conn) AcquireMessage

func (cc *Conn) AcquireMessage(ctx context.Context) *pool.Message

func (*Conn) AddOnClose

func (cc *Conn) AddOnClose(f EventFunc)

AddOnClose calls function on close connection event.

func (*Conn) AsyncPing

func (cc *Conn) AsyncPing(receivedPong func()) (func(), error)

AsyncPing sends ping and receivedPong will be called when pong arrives. It returns cancellation of ping operation.

func (*Conn) CheckExpirations

func (cc *Conn) CheckExpirations(now time.Time)

CheckExpirations checks and remove expired items from caches.

func (*Conn) Close

func (cc *Conn) Close() error

Close closes connection without waiting for the end of the Run function.

func (*Conn) Context

func (cc *Conn) Context() context.Context

Context returns the client's context.

If connections was closed context is cancelled.

func (*Conn) Done

func (cc *Conn) Done() <-chan struct{}

Done signalizes that connection is not more processed.

func (*Conn) GetMessageID

func (cc *Conn) GetMessageID() int32

func (*Conn) InactivityMonitor

func (cc *Conn) InactivityMonitor() InactivityMonitor

func (*Conn) LocalAddr

func (cc *Conn) LocalAddr() net.Addr

func (*Conn) NetConn

func (cc *Conn) NetConn() net.Conn

NetConn returns the underlying connection that is wrapped by cc. The Conn returned is shared by all invocations of NetConn, so do not modify it.

func (*Conn) Process

func (cc *Conn) Process(cm *coapNet.ControlMessage, datagram []byte) error

func (*Conn) ProcessReceivedMessage added in v3.1.0

func (cc *Conn) ProcessReceivedMessage(req *pool.Message)

func (*Conn) ProcessReceivedMessageWithHandler added in v3.1.0

func (cc *Conn) ProcessReceivedMessageWithHandler(req *pool.Message, handler config.HandlerFunc[*Conn])

func (*Conn) ReleaseMessage

func (cc *Conn) ReleaseMessage(m *pool.Message)

func (*Conn) RemoteAddr

func (cc *Conn) RemoteAddr() net.Addr

func (*Conn) Run

func (cc *Conn) Run() error

Run reads and process requests from a connection, until the connection is closed.

func (*Conn) Sequence

func (cc *Conn) Sequence() uint64

Sequence acquires sequence number.

func (*Conn) Session

func (cc *Conn) Session() Session

func (*Conn) SetContextValue

func (cc *Conn) SetContextValue(key interface{}, val interface{})

SetContextValue stores the value associated with key to context of connection.

func (*Conn) Transmission

func (cc *Conn) Transmission() *Transmission

func (*Conn) WriteMessage

func (cc *Conn) WriteMessage(req *pool.Message) error

WriteMessage sends an coap message.

func (*Conn) WriteMulticastMessage

func (cc *Conn) WriteMulticastMessage(req *pool.Message, address *net.UDPAddr, options ...coapNet.MulticastOption) error

WriteMulticastMessage sends multicast to the remote multicast address. By default it is sent over all network interfaces and all compatible source IP addresses with hop limit 1. Via opts you can specify the network interface, source IP address, and hop limit.

type ConnOptions added in v3.3.0

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

type CreateInactivityMonitorFunc

type CreateInactivityMonitorFunc = func() InactivityMonitor

type ErrorFunc

type ErrorFunc = func(error)

type EventFunc

type EventFunc = func()

type GetMIDFunc

type GetMIDFunc = func() int32

type HandlerFunc

type HandlerFunc = func(*responsewriter.ResponseWriter[*Conn], *pool.Message)

type InactivityMonitor

type InactivityMonitor interface {
	Notify()
	CheckInactivity(now time.Time, cc *Conn)
}

type MutexMap

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

MutexMap wraps a map of mutexes. Each key locks separately.

func NewMutexMap

func NewMutexMap() *MutexMap

NewMutexMap returns an initialized MutexMap.

func (*MutexMap) Lock

func (m *MutexMap) Lock(key interface{}) Unlocker

Lock acquires a lock corresponding to this key. This method will never return nil and Unlock() must be called to release the lock when done.

type Option added in v3.3.0

type Option = func(opts *ConnOptions)

func WithBlockWise added in v3.3.0

func WithBlockWise(createBlockWise func(cc *Conn) *blockwise.BlockWise[*Conn]) Option

WithBlockWise enables block-wise transfer for the connection.

func WithInactivityMonitor added in v3.3.0

func WithInactivityMonitor(inactivityMonitor InactivityMonitor) Option

WithInactivityMonitor enables inactivity monitor for the connection.

func WithRequestMonitor added in v3.3.0

func WithRequestMonitor(requestMonitor RequestMonitorFunc) Option

WithRequestMonitor enables request monitoring for the connection. It is called for each CoAP message received from the peer before it is processed. If it returns an error, the connection is closed. If it returns true, the message is dropped.

type RequestMonitorFunc added in v3.3.0

type RequestMonitorFunc = func(cc *Conn, req *pool.Message) (drop bool, err error)

type RequestsMap

type RequestsMap = coapSync.Map[uint64, *pool.Message]

type Session

type Session interface {
	Context() context.Context
	Close() error
	MaxMessageSize() uint32
	RemoteAddr() net.Addr
	LocalAddr() net.Addr
	// NetConn returns the underlying connection that is wrapped by Session. The Conn returned is shared by all invocations of NetConn, so do not modify it.
	NetConn() net.Conn
	WriteMessage(req *pool.Message) error
	// WriteMulticast sends multicast to the remote multicast address.
	// By default it is sent over all network interfaces and all compatible source IP addresses with hop limit 1.
	// Via opts you can specify the network interface, source IP address, and hop limit.
	WriteMulticastMessage(req *pool.Message, address *net.UDPAddr, opts ...coapNet.MulticastOption) error
	Run(cc *Conn) error
	AddOnClose(f EventFunc)
	SetContextValue(key interface{}, val interface{})
	Done() <-chan struct{}
}

type Transmission

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

Transmission is a threadsafe container for transmission related parameters

func (*Transmission) SetTransmissionAcknowledgeTimeout

func (t *Transmission) SetTransmissionAcknowledgeTimeout(d time.Duration)

func (*Transmission) SetTransmissionMaxRetransmit

func (t *Transmission) SetTransmissionMaxRetransmit(d int32)

func (*Transmission) SetTransmissionNStart

func (t *Transmission) SetTransmissionNStart(d uint32)

SetTransmissionNStart changing the nStart value will only effect requests queued after the change. The requests waiting here already before the change will get unblocked when enough weight has been released.

type Unlocker

type Unlocker interface {
	Unlock()
}

Unlocker provides an Unlock method to release the lock.

Jump to

Keyboard shortcuts

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