forked from perlin-network/noise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer.go
640 lines (500 loc) · 16.3 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package noise
import (
"bufio"
"bytes"
"encoding/binary"
"github.com/perlin-network/noise/callbacks"
"github.com/perlin-network/noise/log"
"github.com/perlin-network/noise/payload"
"github.com/pkg/errors"
"io"
"net"
"strings"
"sync"
"sync/atomic"
"time"
)
type receiveHandle struct {
hub chan Message
lock chan struct{}
}
func (r *receiveHandle) Unlock() {
<-r.lock
}
type sendHandle struct {
payload []byte
result chan error
}
type Peer struct {
node *Node
conn net.Conn
onConnErrorCallbacks *callbacks.SequentialCallbackManager
onDisconnectCallbacks *callbacks.SequentialCallbackManager
onEncodeHeaderCallbacks *callbacks.ReduceCallbackManager
onEncodeFooterCallbacks *callbacks.ReduceCallbackManager
onDecodeHeaderCallbacks *callbacks.SequentialCallbackManager
onDecodeFooterCallbacks *callbacks.SequentialCallbackManager
beforeMessageSentCallbacks *callbacks.ReduceCallbackManager
beforeMessageReceivedCallbacks *callbacks.ReduceCallbackManager
afterMessageSentCallbacks *callbacks.SequentialCallbackManager
afterMessageReceivedCallbacks *callbacks.SequentialCallbackManager
sendQueue chan sendHandle
receiveQueues sync.Map // map[Opcode]chan receiveHandle
kill chan *sync.WaitGroup
killOnce uint32
metadata sync.Map
}
func newPeer(node *Node, conn net.Conn) *Peer {
return &Peer{
node: node,
conn: conn,
onConnErrorCallbacks: callbacks.NewSequentialCallbackManager(),
onDisconnectCallbacks: callbacks.NewSequentialCallbackManager(),
onEncodeHeaderCallbacks: callbacks.NewReduceCallbackManager(),
onEncodeFooterCallbacks: callbacks.NewReduceCallbackManager(),
onDecodeHeaderCallbacks: callbacks.NewSequentialCallbackManager(),
onDecodeFooterCallbacks: callbacks.NewSequentialCallbackManager(),
beforeMessageReceivedCallbacks: callbacks.NewReduceCallbackManager().UnsafelySetReverse(),
beforeMessageSentCallbacks: callbacks.NewReduceCallbackManager(),
afterMessageReceivedCallbacks: callbacks.NewSequentialCallbackManager(),
afterMessageSentCallbacks: callbacks.NewSequentialCallbackManager(),
kill: make(chan *sync.WaitGroup, 2),
sendQueue: make(chan sendHandle, 128),
}
}
func (p *Peer) init() {
go p.spawnSendWorker()
go p.spawnReceiveWorker()
}
func (p *Peer) spawnSendWorker() {
for {
var cmd sendHandle
select {
case wg := <-p.kill:
wg.Done()
return
case cmd = <-p.sendQueue:
}
payload := cmd.payload
pp, errs := p.beforeMessageSentCallbacks.RunCallbacks(payload, p.node)
if len(errs) > 0 {
if cmd.result != nil {
var err = errs[0]
if len(errs) > 1 {
for _, e := range errs[1:] {
err = errors.Wrap(err, e.Error())
}
}
cmd.result <- errors.Wrap(err, "got errors running BeforeMessageSent callbacks")
close(cmd.result)
}
}
payload = pp.([]byte)
size := len(payload)
// Prepend message length to packet.
buf := make([]byte, binary.MaxVarintLen64)
prepended := binary.PutUvarint(buf[:], uint64(size))
buf = append(buf[:prepended], payload[:]...)
copied, err := io.Copy(p.conn, bytes.NewReader(buf))
if copied != int64(size+prepended) {
if cmd.result != nil {
cmd.result <- errors.Errorf("only written %d bytes when expected to write %d bytes to peer", copied, size+prepended)
close(cmd.result)
}
continue
}
if err != nil {
if cmd.result != nil {
cmd.result <- errors.Wrap(err, "failed to send message to peer")
close(cmd.result)
}
continue
}
if errs := p.afterMessageSentCallbacks.RunCallbacks(p.node); len(errs) > 0 {
if cmd.result != nil {
var err = errs[0]
if len(errs) > 1 {
for _, e := range errs[1:] {
err = errors.Wrap(err, e.Error())
}
}
cmd.result <- errors.Wrap(err, "got errors running AfterMessageSent callbacks")
close(cmd.result)
}
continue
}
if cmd.result != nil {
cmd.result <- nil
close(cmd.result)
}
}
}
func (p *Peer) spawnReceiveWorker() {
reader := bufio.NewReader(p.conn)
for {
select {
case wg := <-p.kill:
wg.Done()
return
default:
}
size, err := binary.ReadUvarint(reader)
if err != nil {
if errors.Cause(err) != io.EOF && !strings.Contains(errors.Cause(err).Error(), "use of closed network connection") && !strings.Contains(errors.Cause(err).Error(), "read: connection reset by peer") {
p.onConnErrorCallbacks.RunCallbacks(p.node, errors.Wrap(err, "failed to read message size"))
}
p.DisconnectAsync()
continue
}
if size > p.node.maxMessageSize {
p.onConnErrorCallbacks.RunCallbacks(p.node, errors.Errorf("exceeded max message size; got size %d", size))
p.DisconnectAsync()
continue
}
buf := make([]byte, int(size))
seen, err := io.ReadFull(reader, buf)
if err != nil {
p.onConnErrorCallbacks.RunCallbacks(p.node, errors.Wrap(err, "failed to read remaining message contents"))
p.DisconnectAsync()
continue
}
if seen < int(size) {
p.onConnErrorCallbacks.RunCallbacks(p.node, errors.Errorf("only read %d bytes when expected to read %d from peer", seen, size))
p.DisconnectAsync()
continue
}
b, errs := p.beforeMessageReceivedCallbacks.RunCallbacks(buf, p.node)
if len(errs) > 0 {
log.Warn().Errs("errors", errs).Msg("Got errors running BeforeMessageReceived callbacks.")
p.DisconnectAsync()
continue
}
buf = b.([]byte)
opcode, msg, err := p.DecodeMessage(buf)
if opcode == OpcodeNil || err != nil {
p.onConnErrorCallbacks.RunCallbacks(p.node, errors.Wrap(err, "failed to decode message"))
p.DisconnectAsync()
continue
}
c, _ := p.receiveQueues.LoadOrStore(opcode, receiveHandle{hub: make(chan Message), lock: make(chan struct{}, 1)})
recv := c.(receiveHandle)
select {
case recv.hub <- msg:
recv.lock <- struct{}{}
<-recv.lock
case <-time.After(p.node.receiveMessageTimeout):
p.DisconnectAsync()
continue
}
if errs := p.afterMessageReceivedCallbacks.RunCallbacks(p.node); len(errs) > 0 {
log.Warn().Errs("errors", errs).Msg("Got errors running AfterMessageReceived callbacks.")
p.DisconnectAsync()
continue
}
}
}
// SendMessage sends a message whose type is registered with Noise to a specified peer. Calling
// this function will block the current goroutine until the message is successfully sent. In
// order to not block, refer to `SendMessageAsync(message Message) <-chan error`.
//
// It is guaranteed that all messages are sent in a linearized order.
//
// It returns an error should it take too long to send a message, the message is not registered
// with Noise, or there are message that are blocking the peers send worker.
func (p *Peer) SendMessage(message Message) error {
payload, err := p.EncodeMessage(message)
if err != nil {
return errors.Wrap(err, "failed to serialize message contents to be sent to a peer")
}
cmd := sendHandle{payload: payload, result: make(chan error, 1)}
select {
case <-time.After(p.node.sendWorkerBusyTimeout):
close(cmd.result)
return errors.New("send message queue is full and not being processed")
case p.sendQueue <- cmd:
}
select {
case <-time.After(p.node.sendMessageTimeout):
return errors.New("timed out attempting to send a message")
case err = <-cmd.result:
return err
}
}
// SendMessageAsync sends a message whose type is registered with Noise to a specified peer. Calling
// this function will not block the current goroutine until the message is successfully sent. In
// order to block, refer to `SendMessage(message Message) error`.
//
// It is guaranteed that all messages are sent in a linearized order.
//
// It returns an error should the message not be registered with Noise, or there are message that are
// blocking the peers send worker.
func (p *Peer) SendMessageAsync(message Message) <-chan error {
result := make(chan error, 1)
payload, err := p.EncodeMessage(message)
if err != nil {
result <- errors.Wrap(err, "failed to serialize message contents to be sent to a peer")
return result
}
cmd := sendHandle{payload: payload, result: result}
select {
case <-time.After(p.node.sendWorkerBusyTimeout):
result <- errors.New("send message queue is full and not being processed")
return result
case p.sendQueue <- cmd:
}
return result
}
// BeforeMessageSent registers a callback to be called before a message
// is sent to a specified peer.
func (p *Peer) BeforeMessageSent(c BeforeMessageSentCallback) {
p.beforeMessageSentCallbacks.RegisterCallback(func(in interface{}, params ...interface{}) (i interface{}, e error) {
if len(params) != 1 {
panic(errors.Errorf("noise: BeforeMessageSent received unexpected args %v", params))
}
node, ok := params[0].(*Node)
if !ok {
return in.([]byte), nil
}
return c(node, p, in.([]byte))
})
}
// BeforeMessageReceived registers a callback to be called before a message
// is to be received from a specified peer.
func (p *Peer) BeforeMessageReceived(c BeforeMessageReceivedCallback) {
p.beforeMessageReceivedCallbacks.RegisterCallback(func(in interface{}, params ...interface{}) (i interface{}, e error) {
if len(params) != 1 {
panic(errors.Errorf("noise: BeforeMessageReceived received unexpected args %v", params))
}
node, ok := params[0].(*Node)
if !ok {
return in.([]byte), nil
}
return c(node, p, in.([]byte))
})
}
// AfterMessageSent registers a callback to be called after a message
// is sent to a specified peer.
func (p *Peer) AfterMessageSent(c AfterMessageSentCallback) {
p.afterMessageSentCallbacks.RegisterCallback(func(params ...interface{}) error {
if len(params) != 1 {
panic(errors.Errorf("noise: AfterMessageSent received unexpected args %v", params))
}
node, ok := params[0].(*Node)
if !ok {
return nil
}
return c(node, p)
})
}
// AfterMessageReceived registers a callback to be called after a message
// is to be received from a specified peer.
func (p *Peer) AfterMessageReceived(c AfterMessageReceivedCallback) {
p.afterMessageReceivedCallbacks.RegisterCallback(func(params ...interface{}) error {
if len(params) != 1 {
panic(errors.Errorf("noise: AfterMessageReceived received unexpected args %v", params))
}
node, ok := params[0].(*Node)
if !ok {
return nil
}
return c(node, p)
})
}
// OnDecodeHeader registers a callback that is fed in the contents of the
// header portion of an incoming message from a specified peer.
func (p *Peer) OnDecodeHeader(c OnPeerDecodeHeaderCallback) {
p.onDecodeHeaderCallbacks.RegisterCallback(func(params ...interface{}) error {
if len(params) != 2 {
panic(errors.Errorf("noise: OnDecodeHeader received unexpected args %v", params))
}
node, ok := params[0].(*Node)
if !ok {
return nil
}
reader, ok := params[1].(payload.Reader)
if !ok {
return nil
}
return c(node, p, reader)
})
}
// OnDecodeFooter registers a callback that is fed in the contents of the
// footer portion of an incoming message from a specified peer.
func (p *Peer) OnDecodeFooter(c OnPeerDecodeFooterCallback) {
p.onDecodeFooterCallbacks.RegisterCallback(func(params ...interface{}) error {
if len(params) != 3 {
panic(errors.Errorf("noise: OnDecodeFooter received unexpected args %v", params))
}
node, ok := params[0].(*Node)
if !ok {
return nil
}
msg, ok := params[1].([]byte)
if !ok {
return nil
}
reader, ok := params[2].(payload.Reader)
if !ok {
return nil
}
return c(node, p, msg, reader)
})
}
// OnEncodeHeader registers a callback that is fed in the raw contents of
// a message to be sent, which then outputs bytes that are to be appended
// to the header of an outgoing message.
func (p *Peer) OnEncodeHeader(c AfterMessageEncodedCallback) {
p.onEncodeHeaderCallbacks.RegisterCallback(func(header interface{}, params ...interface{}) (i interface{}, e error) {
if len(params) != 2 {
panic(errors.Errorf("noise: OnEncodeHeader received unexpected args %v", params))
}
node, ok := params[0].(*Node)
if !ok {
return header.([]byte), errors.New("noise: OnEncodeHeader did not receive 1st param (node *noise.Node)")
}
msg, ok := params[1].([]byte)
if !ok {
return header.([]byte), errors.New("noise: OnEncodeHeader did not receive 2nd param (msg []byte)")
}
return c(node, p, header.([]byte), msg)
})
}
// OnEncodeFooter registers a callback that is fed in the raw contents of
// a message to be sent, which then outputs bytes that are to be appended
// to the footer of an outgoing message.
func (p *Peer) OnEncodeFooter(c AfterMessageEncodedCallback) {
p.onEncodeFooterCallbacks.RegisterCallback(func(footer interface{}, params ...interface{}) (i interface{}, e error) {
if len(params) != 2 {
panic(errors.Errorf("noise: OnEncodeFooter received unexpected args %v", params))
}
node, ok := params[0].(*Node)
if !ok {
return footer.([]byte), errors.New("noise: OnEncodeHeader did not receive 1st param (node *noise.Node)")
}
msg, ok := params[1].([]byte)
if !ok {
return footer.([]byte), errors.New("noise: OnEncodeHeader did not receive (msg []byte)")
}
return c(node, p, footer.([]byte), msg)
})
}
// OnConnError registers a callback for whenever something goes wrong with the
// connection to our peer.
func (p *Peer) OnConnError(c OnPeerErrorCallback) {
p.onConnErrorCallbacks.RegisterCallback(func(params ...interface{}) error {
if len(params) != 2 {
panic(errors.Errorf("noise: OnConnError received unexpected args %v", params))
}
node, ok := params[0].(*Node)
if !ok {
return nil
}
err, ok := params[1].(error)
if !ok {
return nil
}
return c(node, p, errors.Wrap(err, "peer conn reported error"))
})
}
// OnDisconnect registers a callback for whenever the peer disconnects.
func (p *Peer) OnDisconnect(srcCallbacks ...OnPeerDisconnectCallback) {
targetCallbacks := make([]callbacks.Callback, 0, len(srcCallbacks))
for _, c := range srcCallbacks {
c := c
targetCallbacks = append(targetCallbacks, func(params ...interface{}) error {
node, ok := params[0].(*Node)
if !ok {
panic("params[0] is not a Node")
}
return c(node, p)
})
}
p.onDisconnectCallbacks.RegisterCallback(targetCallbacks...)
}
func (p *Peer) Receive(o Opcode) <-chan Message {
c, _ := p.receiveQueues.LoadOrStore(o, receiveHandle{hub: make(chan Message), lock: make(chan struct{}, 1)})
return c.(receiveHandle).hub
}
func (p *Peer) Disconnect() {
if !atomic.CompareAndSwapUint32(&p.killOnce, 0, 1) {
return
}
var wg sync.WaitGroup
wg.Add(2)
for i := 0; i < 2; i++ {
p.kill <- &wg
}
if err := p.conn.Close(); err != nil {
p.onConnErrorCallbacks.RunCallbacks(p.node, errors.Wrapf(err, "got errors closing peer connection"))
}
wg.Wait()
close(p.kill)
p.onDisconnectCallbacks.RunCallbacks(p.node)
}
func (p *Peer) DisconnectAsync() <-chan struct{} {
signal := make(chan struct{})
if !atomic.CompareAndSwapUint32(&p.killOnce, 0, 1) {
close(signal)
return signal
}
var wg sync.WaitGroup
wg.Add(2)
for i := 0; i < 2; i++ {
p.kill <- &wg
}
if err := p.conn.Close(); err != nil {
p.onConnErrorCallbacks.RunCallbacks(p.node, errors.Wrapf(err, "got errors closing peer connection"))
}
go func() {
wg.Wait()
close(p.kill)
p.onDisconnectCallbacks.RunCallbacks(p.node)
close(signal)
}()
return signal
}
func (p *Peer) LocalIP() net.IP {
return p.node.transport.IP(p.conn.LocalAddr())
}
func (p *Peer) LocalPort() uint16 {
return p.node.transport.Port(p.conn.LocalAddr())
}
func (p *Peer) RemoteIP() net.IP {
return p.node.transport.IP(p.conn.RemoteAddr())
}
func (p *Peer) RemotePort() uint16 {
return p.node.transport.Port(p.conn.RemoteAddr())
}
// Set sets a metadata entry given a key-value pair on our node.
func (p *Peer) Set(key string, val interface{}) {
p.metadata.Store(key, val)
}
// Get returns the value to a metadata key from our node, or otherwise returns nil should
// there be no corresponding value to a provided key.
func (p *Peer) Get(key string) interface{} {
val, _ := p.metadata.Load(key)
return val
}
func (p *Peer) LoadOrStore(key string, val interface{}) interface{} {
val, _ = p.metadata.LoadOrStore(key, val)
return val
}
func (p *Peer) Has(key string) bool {
_, exists := p.metadata.Load(key)
return exists
}
func (p *Peer) Delete(key string) {
p.metadata.Delete(key)
}
func (p *Peer) Node() *Node {
return p.node
}
func (p *Peer) LockOnReceive(opcode Opcode) receiveHandle {
c, _ := p.receiveQueues.LoadOrStore(opcode, receiveHandle{hub: make(chan Message), lock: make(chan struct{}, 1)})
recv := c.(receiveHandle)
recv.lock <- struct{}{}
return recv
}
func (p *Peer) SetNode(node *Node) {
p.node = node
}