forked from libp2p/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_conn.go
187 lines (150 loc) · 3.56 KB
/
mock_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package mocknet
import (
"container/list"
"context"
"strconv"
"sync"
"sync/atomic"
ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
var connCounter atomic.Int64
// conn represents one side's perspective of a
// live connection between two peers.
// it goes over a particular link.
type conn struct {
notifLk sync.Mutex
id int64
local peer.ID
remote peer.ID
localAddr ma.Multiaddr
remoteAddr ma.Multiaddr
localPrivKey ic.PrivKey
remotePubKey ic.PubKey
net *peernet
link *link
rconn *conn // counterpart
streams list.List
stat network.ConnStats
closeOnce sync.Once
isClosed atomic.Bool
sync.RWMutex
}
func newConn(ln, rn *peernet, l *link, dir network.Direction) *conn {
c := &conn{net: ln, link: l}
c.local = ln.peer
c.remote = rn.peer
c.stat.Direction = dir
c.id = connCounter.Add(1)
c.localAddr = ln.ps.Addrs(ln.peer)[0]
for _, a := range rn.ps.Addrs(rn.peer) {
if !manet.IsIPUnspecified(a) {
c.remoteAddr = a
break
}
}
if c.remoteAddr == nil {
c.remoteAddr = rn.ps.Addrs(rn.peer)[0]
}
c.localPrivKey = ln.ps.PrivKey(ln.peer)
c.remotePubKey = rn.ps.PubKey(rn.peer)
return c
}
func (c *conn) IsClosed() bool {
return c.isClosed.Load()
}
func (c *conn) ID() string {
return strconv.FormatInt(c.id, 10)
}
func (c *conn) Close() error {
c.closeOnce.Do(func() {
c.isClosed.Store(true)
go c.rconn.Close()
c.teardown()
})
return nil
}
func (c *conn) teardown() {
for _, s := range c.allStreams() {
s.Reset()
}
c.net.removeConn(c)
}
func (c *conn) addStream(s *stream) {
c.Lock()
defer c.Unlock()
s.conn = c
c.streams.PushBack(s)
}
func (c *conn) removeStream(s *stream) {
c.Lock()
defer c.Unlock()
for e := c.streams.Front(); e != nil; e = e.Next() {
if s == e.Value {
c.streams.Remove(e)
return
}
}
}
func (c *conn) allStreams() []network.Stream {
c.RLock()
defer c.RUnlock()
strs := make([]network.Stream, 0, c.streams.Len())
for e := c.streams.Front(); e != nil; e = e.Next() {
s := e.Value.(*stream)
strs = append(strs, s)
}
return strs
}
func (c *conn) remoteOpenedStream(s *stream) {
c.addStream(s)
c.net.handleNewStream(s)
}
func (c *conn) openStream() *stream {
sl, sr := newStreamPair()
go c.rconn.remoteOpenedStream(sr)
c.addStream(sl)
return sl
}
func (c *conn) NewStream(context.Context) (network.Stream, error) {
log.Debugf("Conn.NewStreamWithProtocol: %s --> %s", c.local, c.remote)
s := c.openStream()
return s, nil
}
func (c *conn) GetStreams() []network.Stream {
return c.allStreams()
}
// LocalMultiaddr is the Multiaddr on this side
func (c *conn) LocalMultiaddr() ma.Multiaddr {
return c.localAddr
}
// LocalPeer is the Peer on our side of the connection
func (c *conn) LocalPeer() peer.ID {
return c.local
}
// RemoteMultiaddr is the Multiaddr on the remote side
func (c *conn) RemoteMultiaddr() ma.Multiaddr {
return c.remoteAddr
}
// RemotePeer is the Peer on the remote side
func (c *conn) RemotePeer() peer.ID {
return c.remote
}
// RemotePublicKey is the private key of the peer on our side.
func (c *conn) RemotePublicKey() ic.PubKey {
return c.remotePubKey
}
// ConnState of security connection. Empty if not supported.
func (c *conn) ConnState() network.ConnectionState {
return network.ConnectionState{}
}
// Stat returns metadata about the connection
func (c *conn) Stat() network.ConnStats {
return c.stat
}
func (c *conn) Scope() network.ConnScope {
return &network.NullScope{}
}