2
0
mirror of synced 2025-02-24 14:48:27 +00:00
torrent/dht/addr.go
2015-05-20 22:23:50 +10:00

42 lines
670 B
Go

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)}
}