package server
import "github.com/plgd-dev/go-coap/v3/udp/server"
Index ¶
- Variables
- type Config
- type ErrorFunc
- type EventFunc
- type GetMIDFunc
- type HandlerFunc
- type OnNewConnFunc
- type Option
- type Server
- func New(opt ...Option) *Server
- func (s *Server) Discover(ctx context.Context, address, path string, receiverFunc func(cc *client.Conn, resp *pool.Message), opts ...coapNet.MulticastOption) error
- func (s *Server) DiscoveryRequest(req *pool.Message, address string, receiverFunc func(cc *client.Conn, resp *pool.Message), opts ...coapNet.MulticastOption) error
- func (s *Server) NewConn(addr *net.UDPAddr) (*client.Conn, error)
- func (s *Server) Serve(l *coapNet.UDPConn) error
- func (s *Server) Stop()
- type Session
- func NewSession( ctx context.Context, doneCtx context.Context, connection *coapNet.UDPConn, raddr *net.UDPAddr, maxMessageSize uint32, mtu uint16, closeSocket bool, ) *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
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 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, TransmissionMaxRetransmit: 4, GetMID: message.GetMID, MTU: udpClient.DefaultMTU, } opts.Handler = func(w *responsewriter.ResponseWriter[*udpClient.Conn], _ *pool.Message) { if err := w.SetResponse(codes.NotFound, message.TextPlain, nil); err != nil { opts.Errors(fmt.Errorf("udp server: cannot set response: %w", err)) } } return opts }()
Types ¶
type Config ¶
type Config struct { config.Common[*udpClient.Conn] CreateInactivityMonitor udpClient.CreateInactivityMonitorFunc GetMID GetMIDFunc Handler HandlerFunc OnNewConn OnNewConnFunc RequestMonitor udpClient.RequestMonitorFunc TransmissionNStart uint32 TransmissionAcknowledgeTimeout time.Duration TransmissionMaxRetransmit uint32 MTU uint16 }
type ErrorFunc ¶
type ErrorFunc = func(error)
type EventFunc ¶
type EventFunc = func()
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 New ¶
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 ¶
func (*Server) Serve ¶
func (*Server) Stop ¶
func (s *Server) Stop()
Stop stops server without wait of ends Serve function.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
func NewSession ¶
func NewSession( ctx context.Context, doneCtx context.Context, connection *coapNet.UDPConn, raddr *net.UDPAddr, maxMessageSize uint32, mtu uint16, closeSocket bool, ) *Session
func (*Session) AddOnClose ¶
func (*Session) Close ¶
func (*Session) Context ¶
func (*Session) Done ¶
func (s *Session) Done() <-chan struct{}
Done signalizes that connection is not more processed.
func (*Session) LocalAddr ¶
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) Run ¶
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 (*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.
Source Files ¶
config.go discover.go server.go session.go
- Version
- v3.3.6 (latest)
- Published
- Sep 18, 2024
- Platform
- linux/amd64
- Imports
- 20 packages
- Last checked
- 1 week ago –
Tools for package owners.