-
Notifications
You must be signed in to change notification settings - Fork 3
/
peer.go
282 lines (255 loc) · 5.92 KB
/
peer.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2018 The uranus Authors
// This file is part of the uranus library.
//
// The uranus library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The uranus library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the uranus library. If not, see <http://www.gnu.org/licenses/>.
package p2p
import (
"errors"
"fmt"
"io"
"net"
"sync"
"time"
"github.com/UranusBlockStack/uranus/common/log"
"github.com/UranusBlockStack/uranus/p2p/discover"
)
const (
keepAliveInterval = 15 * time.Second
)
// message code
const (
handshakeMsg = iota
pingMsg
pongMsg
quitMsg
)
// Peer represent a connected remote node
type Peer struct {
rw *conn
running map[string]*protoRW //protocols
runningErr chan error
quit chan string
closed chan struct{}
wg sync.WaitGroup
}
// ID return the node id
func (p *Peer) ID() discover.NodeID {
return p.rw.id
}
// Name return the node name
func (p *Peer) Name() string {
return p.String()
}
func (p *Peer) String() string {
return fmt.Sprintf("Peer %x(%v)", p.rw.id[:8], p.RemoteAddr())
}
// Protocols return supported subprotocols of the remote peer.
func (p *Peer) Protocols() []*ProtocolKey {
return p.rw.protocols
}
// RemoteAddr returns the remote address of connection.
func (p *Peer) RemoteAddr() net.Addr {
return p.rw.fd.RemoteAddr()
}
// LocalAddr returns the local address of connection.
func (p *Peer) LocalAddr() net.Addr {
return p.rw.fd.LocalAddr()
}
// Disconnect terminates the peer connection with the given reason.
func (p *Peer) Disconnect(reason string) {
select {
case p.quit <- reason:
case <-p.closed:
}
}
// NewPeer returns a peer
func NewPeer(conn *conn, protocols []*Protocol) *Peer {
running := make(map[string]*protoRW)
for _, protocolKey := range conn.protocols {
for _, protocol := range protocols {
if protocol.Name == protocolKey.Name && protocol.Version == protocolKey.Version {
running[protocol.Name] = &protoRW{
Protocol: *protocol,
in: make(chan *Message),
w: conn,
}
}
}
}
p := &Peer{
rw: conn,
running: running,
runningErr: make(chan error, len(running)+1),
quit: make(chan string),
closed: make(chan struct{}),
}
return p
}
func (p *Peer) pingloop() {
pingTimer := time.NewTimer(keepAliveInterval)
defer p.wg.Done()
defer pingTimer.Stop()
for {
select {
case <-pingTimer.C:
if err := SendMessage(p.rw, pingMsg, nil); err != nil {
p.runningErr <- err
return
}
pingTimer.Reset(keepAliveInterval)
case <-p.closed:
return
}
}
}
func (p *Peer) loop() {
defer p.wg.Done()
for {
msg, err := p.rw.ReadMsg()
if opErr, ok := err.(*net.OpError); ok && (opErr.Timeout() || opErr.Temporary()) {
continue
}
if err != nil {
p.runningErr <- err
return
}
msg.ReceivedAt = time.Now()
if err = p.handle(msg); err != nil {
p.runningErr <- err
return
}
}
}
func (p *Peer) startProtocols() {
wstart := make(chan struct{}, 1)
wstart <- struct{}{}
for _, proto := range p.running {
proto := proto
proto.closed = p.closed
proto.wstart = wstart
proto.werr = p.runningErr
go func() {
defer p.wg.Done()
err := proto.Run(p, proto)
p.runningErr <- err
}()
}
}
func (p *Peer) run() (err error) {
p.wg.Add(2)
go p.pingloop()
go p.loop()
p.wg.Add(len(p.running))
p.startProtocols()
running:
for {
select {
case err = <-p.runningErr:
log.Warnf("peer %v close --- %v", p.ID().String(), err)
break running
case str := <-p.quit:
log.Infof("peer %v close --- %v", p.ID().String(), str)
err = errors.New(str)
break running
}
}
close(p.closed)
p.rw.close(err)
p.wg.Wait()
return err
}
func (p *Peer) handle(msg *Message) error {
switch {
case msg.Code == pingMsg:
if err := SendMessage(p.rw, pongMsg, nil); err != nil {
return err
}
case msg.Code == pongMsg:
case msg.Code == quitMsg:
var reason string
msg.EncodePayload(&reason)
return errors.New(reason)
default:
proto, err := p.getProto(msg.Code)
if err != nil {
return fmt.Errorf("msg code out of range: %v", msg.Code)
}
select {
case proto.in <- msg:
return nil
case <-p.closed:
return io.EOF
}
}
return nil
}
func (p *Peer) getProto(code uint64) (*protoRW, error) {
for _, proto := range p.running {
if code >= proto.Offset && code < proto.Offset+proto.Size {
return proto, nil
}
}
return nil, fmt.Errorf("invalid message code")
}
func (p *Peer) PeerInfo() *PeerInfo {
info := &PeerInfo{
ID: p.ID().String(),
Name: p.Name(),
Protocols: make(map[string]interface{}),
}
info.Network.LocalAddress = p.LocalAddr().String()
info.Network.RemoteAddress = p.RemoteAddr().String()
for _, proto := range p.running {
protoInfo := interface{}("unknown")
if query := proto.Protocol.PeerInfo; query != nil {
if metadata := query(p.ID()); metadata != nil {
protoInfo = metadata
} else {
protoInfo = "handshake"
}
}
info.Protocols[proto.Name] = protoInfo
}
return info
}
type protoRW struct {
Protocol
in chan *Message
w MsgReadWriter
closed <-chan struct{}
wstart chan struct{}
werr chan error
}
func (rw *protoRW) WriteMsg(msg *Message) (err error) {
select {
case <-rw.wstart:
err = rw.w.WriteMsg(msg)
if err == nil {
rw.wstart <- struct{}{}
} else {
rw.werr <- err
}
case <-rw.closed:
err = fmt.Errorf("shutting down")
}
return err
}
func (rw *protoRW) ReadMsg() (*Message, error) {
select {
case msg := <-rw.in:
return msg, nil
case <-rw.closed:
return nil, io.EOF
}
}