FedP2P/dht/addr.go

34 lines
687 B
Go
Raw Normal View History

package dht
import "net"
// Used internally to refer to node network addresses. String() is called a
// lot, and so can be optimized. Network() is not exposed, so that the
// interface does not satisfy net.Addr, as the underlying type must be passed
// to any OS-level function that take net.Addr.
2016-02-23 22:50:15 +08:00
type Addr interface {
UDPAddr() *net.UDPAddr
String() string
}
// Speeds up some of the commonly called Addr methods.
type cachedAddr struct {
ua net.UDPAddr
s string
}
func (ca cachedAddr) String() string {
return ca.s
}
func (ca cachedAddr) UDPAddr() *net.UDPAddr {
return &ca.ua
}
func NewAddr(ua *net.UDPAddr) Addr {
return cachedAddr{
ua: *ua,
s: ua.String(),
}
}