status-go/vendor/github.com/koron/go-ssdp/udp.go

66 lines
1.3 KiB
Go
Raw Normal View History

2019-06-09 07:24:20 +00:00
package ssdp
import (
"net"
2021-10-19 13:43:41 +00:00
"sync"
2019-06-09 07:24:20 +00:00
)
2021-10-19 13:43:41 +00:00
type packetHandler func(net.Addr, []byte) error
2019-06-09 07:24:20 +00:00
2021-10-19 13:43:41 +00:00
type udpAddrResolver struct {
addr string
2019-06-09 07:24:20 +00:00
2021-10-19 13:43:41 +00:00
mu sync.RWMutex
udp *net.UDPAddr
err error
}
2019-06-09 07:24:20 +00:00
2021-10-19 13:43:41 +00:00
func (r *udpAddrResolver) setAddress(addr string) {
r.mu.Lock()
r.addr = addr
r.udp = nil
r.err = nil
r.mu.Unlock()
2019-06-09 07:24:20 +00:00
}
2021-10-19 13:43:41 +00:00
func (r *udpAddrResolver) resolve() (*net.UDPAddr, error) {
r.mu.RLock()
if err := r.err; err != nil {
r.mu.RUnlock()
return nil, err
2019-06-09 07:24:20 +00:00
}
2021-10-19 13:43:41 +00:00
if udp := r.udp; udp != nil {
r.mu.RUnlock()
return udp, nil
2019-06-09 07:24:20 +00:00
}
2021-10-19 13:43:41 +00:00
r.mu.RUnlock()
r.mu.Lock()
defer r.mu.Unlock()
r.udp, r.err = net.ResolveUDPAddr("udp4", r.addr)
return r.udp, r.err
2019-06-09 07:24:20 +00:00
}
2021-10-19 13:43:41 +00:00
var recvAddrResolver = &udpAddrResolver{addr: "224.0.0.0:1900"}
// SetMulticastRecvAddrIPv4 updates multicast address where to receive packets.
// This never fail now.
func SetMulticastRecvAddrIPv4(addr string) error {
recvAddrResolver.setAddress(addr)
2019-06-09 07:24:20 +00:00
return nil
}
2021-10-19 13:43:41 +00:00
var sendAddrResolver = &udpAddrResolver{addr: "239.255.255.250:1900"}
// multicastSendAddr returns an address to send multicast UDP package.
func multicastSendAddr() (*net.UDPAddr, error) {
return sendAddrResolver.resolve()
}
// SetMulticastSendAddrIPv4 updates a UDP address to send multicast packets.
// This never fail now.
func SetMulticastSendAddrIPv4(addr string) error {
sendAddrResolver.setAddress(addr)
2019-06-09 07:24:20 +00:00
return nil
}