2
0
mirror of synced 2025-02-24 22:58:28 +00:00
torrent/dht/addr.go

42 lines
670 B
Go
Raw Normal View History

package dht
import (
"net"
"github.com/anacrolix/torrent/util"
)
// Used internally to refer to node network addresses.
type dHTAddr interface {
net.Addr
UDPAddr() *net.UDPAddr
IP() net.IP
}
// Speeds up some of the commonly called Addr methods.
type cachedAddr struct {
a net.Addr
s string
ip net.IP
}
func (ca cachedAddr) Network() string {
return ca.a.Network()
}
func (ca cachedAddr) String() string {
return ca.s
}
func (ca cachedAddr) UDPAddr() *net.UDPAddr {
return ca.a.(*net.UDPAddr)
}
func (ca cachedAddr) IP() net.IP {
return ca.ip
}
func newDHTAddr(addr net.Addr) dHTAddr {
return cachedAddr{addr, addr.String(), util.AddrIP(addr)}
}