torrent/dht/addr.go

42 lines
666 B
Go

package dht
import (
"net"
"github.com/anacrolix/missinggo"
)
// Used internally to refer to node network addresses.
type Addr 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) Addr {
return cachedAddr{addr, addr.String(), missinggo.AddrIP(addr)}
}