-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
addr.go
61 lines (50 loc) · 1.07 KB
/
addr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package dht
import (
"net"
"github.com/anacrolix/missinggo/v2"
"github.com/anacrolix/dht/v2/krpc"
)
// 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.
type Addr interface {
Raw() net.Addr
Port() int
IP() net.IP
String() string
KRPC() krpc.NodeAddr
}
// Speeds up some of the commonly called Addr methods.
type cachedAddr struct {
raw net.Addr
port int
ip net.IP
s string
}
func (ca cachedAddr) String() string {
return ca.s
}
func (ca cachedAddr) KRPC() krpc.NodeAddr {
return krpc.NodeAddr{
IP: ca.ip,
Port: ca.port,
}
}
func (ca cachedAddr) IP() net.IP {
return ca.ip
}
func (ca cachedAddr) Port() int {
return ca.port
}
func (ca cachedAddr) Raw() net.Addr {
return ca.raw
}
func NewAddr(raw net.Addr) Addr {
return cachedAddr{
raw: raw,
s: raw.String(),
ip: missinggo.AddrIP(raw),
port: missinggo.AddrPort(raw),
}
}