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: 23 Imported by: 11

Documentation

Index

Constants

This section is empty.

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:                 "tcp",
		ConnectionCacheSize: 2048,
	}
	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("client handler: 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
	Dialer                          *net.Dialer
	TLSCfg                          *tls.Config
	Handler                         HandlerFunc
	ConnectionCacheSize             uint16
	DisablePeerTCPSignalMessageCSMs bool
	CloseSocket                     bool
	DisableTCPSignalMessageCSM      bool
}

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(
	connection *coapNet.Conn,
	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(connection *coapNet.Conn, 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 wait of ends 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) 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) 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 HandlerFunc)

func (*Conn) ReleaseMessage

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

func (*Conn) RemoteAddr

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

RemoteAddr gets remote address.

func (*Conn) Run

func (cc *Conn) Run() (err error)

Run reads and process requests from a connection, until the connection is not 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) WriteMessage

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

WriteMessage sends an coap message.

type ConnOptions added in v3.3.0

type ConnOptions struct {
	CreateBlockWise   func(cc *Conn) *blockwise.BlockWise[*Conn]
	InactivityMonitor InactivityMonitor
	RequestMonitor    RequestMonitorFunc
}

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 Notifier

type Notifier interface {
	Notify()
}

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 Session

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

func NewSession

func NewSession(
	ctx context.Context,
	connection *coapNet.Conn,
	maxMessageSize uint32,
	errors ErrorFunc,
	disableTCPSignalMessageCSM bool,
	closeSocket bool,
	inactivityMonitor InactivityMonitor,
	requestMonitor RequestMonitorFunc,
	connectionCacheSize uint16,
	messagePool *pool.Pool,
) *Session

func (*Session) AcquireMessage

func (s *Session) AcquireMessage(ctx context.Context) *pool.Message

func (*Session) AddOnClose

func (s *Session) AddOnClose(f EventFunc)

func (*Session) CheckExpirations

func (s *Session) CheckExpirations(now time.Time, cc *Conn)

CheckExpirations checks and remove expired items from caches.

func (*Session) Close

func (s *Session) Close() error

func (*Session) Context

func (s *Session) Context() context.Context

func (*Session) Done

func (s *Session) Done() <-chan struct{}

Done signalizes that connection is not more processed.

func (*Session) LocalAddr

func (s *Session) LocalAddr() net.Addr

func (*Session) NetConn

func (s *Session) NetConn() net.Conn

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

func (*Session) ReleaseMessage

func (s *Session) ReleaseMessage(m *pool.Message)

func (*Session) RemoteAddr

func (s *Session) RemoteAddr() net.Addr

RemoteAddr gets remote address.

func (*Session) Run

func (s *Session) Run(cc *Conn) (err error)

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

func (*Session) Sequence

func (s *Session) Sequence() uint64

func (*Session) SetContextValue

func (s *Session) SetContextValue(key interface{}, val interface{})

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

func (*Session) WriteMessage

func (s *Session) WriteMessage(req *pool.Message) error

Jump to

Keyboard shortcuts

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