-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconn.go
108 lines (87 loc) · 1.96 KB
/
conn.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package tor
import (
"net"
"time"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
type listConn struct {
netConnWithoutAddr
l *listener
raddr ma.Multiaddr
}
func (c *listConn) LocalAddr() net.Addr {
return maddrToNetAddr(c.LocalMultiaddr())
}
func (c *listConn) RemoteAddr() net.Addr {
return maddrToNetAddr(c.RemoteMultiaddr())
}
func (c *listConn) LocalMultiaddr() ma.Multiaddr {
return c.l.Multiaddr()
}
func (c *listConn) RemoteMultiaddr() ma.Multiaddr {
return c.raddr
}
type dialConn struct {
netConnWithoutAddr
laddr *listenStore
raddr ma.Multiaddr
}
func (c *dialConn) LocalAddr() net.Addr {
return maddrToNetAddr(c.LocalMultiaddr())
}
func (c *dialConn) RemoteAddr() net.Addr {
return maddrToNetAddr(c.RemoteMultiaddr())
}
func (c *dialConn) RemoteMultiaddr() ma.Multiaddr {
return c.raddr
}
func (c *dialConn) LocalMultiaddr() ma.Multiaddr {
var laddr ma.Multiaddr
c.laddr.RLock()
cur := c.laddr.cur
c.laddr.RUnlock()
if cur == nil {
laddr = NopMaddr2
} else {
laddr = cur.addr
}
return laddr
}
type dialConnTcp struct {
netConnWithoutAddr
laddr *listenStore
raddr ma.Multiaddr
}
func (c *dialConnTcp) LocalAddr() net.Addr {
return maddrToNetAddr(c.LocalMultiaddr())
}
func (c *dialConnTcp) RemoteAddr() net.Addr {
addr, err := manet.ToNetAddr(c.RemoteMultiaddr())
checkError(err)
return addr
}
func (c *dialConnTcp) RemoteMultiaddr() ma.Multiaddr {
return c.raddr
}
func (c *dialConnTcp) LocalMultiaddr() ma.Multiaddr {
var laddr ma.Multiaddr
c.laddr.RLock()
cur := c.laddr.cur
c.laddr.RUnlock()
if cur == nil {
laddr = NopMaddr2
} else {
laddr = cur.addr
}
return laddr
}
// netConnWithoutAddr is a net.Conn like but without LocalAddr and RemoteAddr.
type netConnWithoutAddr interface {
Read(b []byte) (n int, err error)
Write(b []byte) (n int, err error)
Close() error
SetDeadline(t time.Time) error
SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
}