2015-05-20 20:23:50 +08:00
|
|
|
package dht
|
|
|
|
|
2016-02-23 23:02:33 +08:00
|
|
|
import "net"
|
2015-05-20 20:23:50 +08:00
|
|
|
|
2016-05-17 14:40:08 +08:00
|
|
|
// 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 {
|
2015-05-20 20:23:50 +08:00
|
|
|
UDPAddr() *net.UDPAddr
|
2016-02-23 23:02:33 +08:00
|
|
|
String() string
|
2015-05-20 20:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Speeds up some of the commonly called Addr methods.
|
|
|
|
type cachedAddr struct {
|
2016-02-23 23:02:33 +08:00
|
|
|
ua net.UDPAddr
|
2015-05-20 20:23:50 +08:00
|
|
|
s string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ca cachedAddr) String() string {
|
|
|
|
return ca.s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ca cachedAddr) UDPAddr() *net.UDPAddr {
|
2016-02-23 23:02:33 +08:00
|
|
|
return &ca.ua
|
2015-05-20 20:23:50 +08:00
|
|
|
}
|
|
|
|
|
2016-02-23 23:02:33 +08:00
|
|
|
func NewAddr(ua *net.UDPAddr) Addr {
|
|
|
|
return cachedAddr{
|
|
|
|
ua: *ua,
|
|
|
|
s: ua.String(),
|
|
|
|
}
|
2015-05-20 20:23:50 +08:00
|
|
|
}
|