Documentation
¶
Index ¶
- Constants
- Variables
- type Config
- type EndpointMessageIDCacheConfig
- type EndpointMessageIDOverflowMode
- type ErrorFunc
- type EventFunc
- type GetMIDFunc
- type HandlerFunc
- type OnNewConnFunc
- type Option
- type Server
- func (s *Server) Discover(ctx context.Context, address, path string, ...) error
- func (s *Server) DiscoveryRequest(req *pool.Message, address string, ...) error
- func (s *Server) NewConn(addr *net.UDPAddr, laddr ...*net.UDPAddr) (*client.Conn, error)
- func (s *Server) Serve(l *coapNet.UDPConn) error
- func (s *Server) Stop()
- type Session
- func (s *Session) AddOnClose(f EventFunc)
- func (s *Session) Close() error
- func (s *Session) Context() context.Context
- func (s *Session) Done() <-chan struct{}
- func (s *Session) LocalAddr() net.Addr
- func (s *Session) MaxMessageSize() uint32
- func (s *Session) NetConn() net.Conn
- func (s *Session) RemoteAddr() net.Addr
- func (s *Session) Run(cc *client.Conn) (err error)
- func (s *Session) SetContextValue(key interface{}, val interface{})
- func (s *Session) WriteMessage(req *pool.Message) error
- func (s *Session) WriteMulticastMessage(req *pool.Message, address *net.UDPAddr, opts ...coapNet.MulticastOption) error
Constants ¶
const DefaultMaxEndpointMessageIDs uint32 = 4096
DefaultMaxEndpointMessageIDs is the default number of retained peer sequences.
Variables ¶
var DefaultConfig = func() Config { opts := Config{ Common: config.NewCommon[*udpClient.Conn](), CreateInactivityMonitor: func() udpClient.InactivityMonitor { timeout := time.Second * 16 onInactive := func(cc *udpClient.Conn) { _ = cc.Close() } return udpClient.DeferInactivityWhileConfirmableMessagesPending(inactivity.New(timeout, onInactive)) }, OnNewConn: func(*udpClient.Conn) { }, RequestMonitor: func(*udpClient.Conn, *pool.Message) (bool, error) { return false, nil }, TransmissionNStart: 1, TransmissionAcknowledgeTimeout: time.Second * 2, TransmissionAcknowledgeRandomFactor: 1.5, TransmissionMaxRetransmit: 4, GetMID: message.GetMID, MTU: udpClient.DefaultMTU, MaxPendingMessages: udpClient.DefaultMaxPendingMessages, EndpointMessageIDCache: DefaultEndpointMessageIDCacheConfig(), WriteTimeout: 5 * time.Second, } opts.Handler = func(w *responsewriter.ResponseWriter[*udpClient.Conn], r *pool.Message) { if r.Type() == message.Confirmable && r.Code() >= 64 && r.Code() < 192 { w.Message().SetType(message.Reset) return } if err := w.SetResponse(codes.NotFound, message.TextPlain, nil); err != nil { opts.Errors(fmt.Errorf("udp server: cannot set response: %w", err)) } } return opts }()
var ErrEndpointMessageIDsFull = errors.New("endpoint message ID cache is full")
ErrEndpointMessageIDsFull means that no endpoint sequence can be safely evicted.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
config.Common[*udpClient.Conn]
CreateInactivityMonitor udpClient.CreateInactivityMonitorFunc
// GetMID contributes to the initial message ID for a newly tracked peer.
// A random offset is added; subsequent connections reuse the peer's counter
// while it remains in the server's endpoint cache.
// It is called while the server's connection lock is held and must not block
// or call back into the server. Its result does not determine the initial MID.
GetMID GetMIDFunc
Handler HandlerFunc
OnNewConn OnNewConnFunc
RequestMonitor udpClient.RequestMonitorFunc
TransmissionNStart uint32
TransmissionAcknowledgeTimeout time.Duration
TransmissionAcknowledgeRandomFactor float64
TransmissionMaxRetransmit uint32
MTU uint16
// MaxPendingMessages limits in-flight duplicate suppression per connection.
// Zero disables in-flight tracking.
MaxPendingMessages uint32
// WriteTimeout bounds a UDP write, including time waiting for the shared
// socket. Nonpositive values disable the timeout; the default is five seconds.
WriteTimeout time.Duration
// EndpointMessageIDCache configures retained per-peer outgoing MID sequences.
// Active peers and peers whose last connection closed less than
// client.ExchangeLifetime ago cannot be evicted.
// MID wraparound, caller-assigned IDs and process restarts are not tracked.
EndpointMessageIDCache EndpointMessageIDCacheConfig
// QBlockwiseMode is Disabled by default and supports plain UDP.
QBlockwiseMode blockwise.QBlockwiseMode
QBlockwiseConfig blockwise.QBlockwiseConfig
}
type EndpointMessageIDCacheConfig ¶ added in v3.9.2
type EndpointMessageIDCacheConfig struct {
// MaxEntries limits retained peers. Zero disables retention when Unlimited is false.
MaxEntries uint32
// Unlimited removes the limit and requires MaxEntries to be zero.
// It may allow unbounded memory growth.
Unlimited bool
// OverflowMode selects behavior when no entry can be evicted.
// A zero limit without Unlimited requires EndpointMessageIDFailOpen.
OverflowMode EndpointMessageIDOverflowMode
}
EndpointMessageIDCacheConfig controls retention of outgoing peer sequences.
func DefaultEndpointMessageIDCacheConfig ¶ added in v3.9.2
func DefaultEndpointMessageIDCacheConfig() EndpointMessageIDCacheConfig
DefaultEndpointMessageIDCacheConfig returns the default bounded, fail-open configuration.
type EndpointMessageIDOverflowMode ¶ added in v3.9.2
type EndpointMessageIDOverflowMode uint8
EndpointMessageIDOverflowMode controls admission when the MID cache is full.
const ( // EndpointMessageIDFailOpen admits peers with an untracked random sequence. EndpointMessageIDFailOpen EndpointMessageIDOverflowMode = iota // EndpointMessageIDFailClosed rejects peers until a cache entry can be evicted. EndpointMessageIDFailClosed )
type GetMIDFunc ¶
type GetMIDFunc = func() int32
type HandlerFunc ¶
type HandlerFunc = func(*responsewriter.ResponseWriter[*udpClient.Conn], *pool.Message)
The HandlerFunc type is an adapter to allow the use of ordinary functions as COAP handlers.
type OnNewConnFunc ¶
OnNewConnFunc is the callback for new connections.
type Option ¶
type Option interface {
UDPServerApply(cfg *Config)
}
A Option sets options such as credentials, codec and keepalive parameters, etc.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func (*Server) Discover ¶
func (s *Server) Discover(ctx context.Context, address, path string, receiverFunc func(cc *client.Conn, resp *pool.Message), opts ...coapNet.MulticastOption) error
Discover sends GET to multicast or unicast address and waits for responses until context timeouts or server shutdown. For unicast there is a difference against the Dial. The Dial is connection-oriented and it means that, if you send a request to an address, the peer must send the response from the same address where was request sent. For Discover it allows the client to send a response from another address where was request send. 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.
func (*Server) DiscoveryRequest ¶
func (s *Server) DiscoveryRequest(req *pool.Message, address string, receiverFunc func(cc *client.Conn, resp *pool.Message), opts ...coapNet.MulticastOption) error
DiscoveryRequest sends request to multicast/unicast address and wait for responses until request timeouts or server shutdown. For unicast there is a difference against the Dial. The Dial is connection-oriented and it means that, if you send a request to an address, the peer must send the response from the same address where was request sent. For Discover it allows the client to send a response from another address where was request send. 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.
func (*Server) NewConn ¶
NewConn creates or gets a connection for the provided remote address.
Optional laddr may be used to pin a concrete local address when the listener is bound to a wildcard address. If laddr is omitted or nil, listener's local address is used. For wildcard listeners, incoming packets addressed to concrete local IPs are matched to this conn.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
func NewSession ¶
func NewSession( ctx, doneCtx context.Context, connection *coapNet.UDPConn, raddr *net.UDPAddr, maxMessageSize uint32, mtu uint16, closeSocket bool, errorHandlers ...client.ErrorFunc, ) *Session
NewSession creates a UDP session. Optional error handlers receive recoverable datagram errors; otherwise the default client error handler is used.
func (*Session) AddOnClose ¶
func (*Session) Close ¶
Close cancels the session and blocks until in-flight writes have drained. It must not be called while holding the session write lock.
func (*Session) Done ¶
func (s *Session) Done() <-chan struct{}
Done signalizes that connection is not more processed.
func (*Session) MaxMessageSize ¶
func (*Session) NetConn ¶
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) RemoteAddr ¶
func (*Session) SetContextValue ¶
func (s *Session) SetContextValue(key interface{}, val interface{})
SetContextValue stores the value associated with key to context of connection.
func (*Session) WriteMulticastMessage ¶
func (s *Session) WriteMulticastMessage(req *pool.Message, address *net.UDPAddr, opts ...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.