-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
1017 lines (805 loc) · 25.1 KB
/
client.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package GRING
import (
"bufio"
"context"
"crypto/aes"
"crypto/cipher"
"encoding/binary"
//"encoding/hex"
"errors"
"fmt"
"go.uber.org/zap"
"io"
"net"
"sync"
"time"
"math"
)
type clientSide bool
var starttime time.Time
var duration time.Duration
var datasize int
const (
clientSideInbound clientSide = false
clientSideOutbound clientSide = true
NO_OP = 0xFF
)
func (c clientSide) String() string {
if c {
return "outbound"
}
return "inbound"
}
// Client represents an pooled inbound/outbound connection under some node. Should a client successfully undergo
// GRING's protocol handshake, information about the peer representative of this client, such as its ID is available.
//
// A clients connection may be closed through (*Client).Close, through the result of a failed handshake, through
// exceeding the max inbound/outbound connection count configured on the clients associated node, through a node
// gracefully being stopped, through a Handler configured on the node returning an error upon recipient of some data,
// or through receiving unexpected/suspicious data.
//
// The lifecycle of a client may be controlled through (*Client).WaitUntilReady, and (*Client).WaitUntilClosed. It
// provably has been useful in writing unit tests where a client instance is used under high concurrency scenarios.
//
// A client in total has two goroutines associated to it: a goroutine responsible for handling writing messages, and a
// goroutine responsible for handling the recipient of messages.
type Client struct {
node *Node
id ID
addr string
side clientSide
suite cipher.AEAD
logger struct {
sync.RWMutex
*zap.Logger
}
conn net.Conn
reader *bufio.Reader
writer *bufio.Writer
readerBuf []byte
writerBuf []message
writerCond sync.Cond
writerClosed bool
requests *requestMap
requests_recv *requestMap
ready chan struct{}
readerDone chan struct{}
writerDone chan struct{}
clientDone chan struct{}
err struct {
sync.Mutex
error
}
closeOnce sync.Once
}
func newClient(node *Node) *Client {
//fmt.Printf("new client\n")
c := &Client{
node: node,
requests: newRequestMap(),
requests_recv: newRequestMap(),
readerBuf: make([]byte, 4+node.maxRecvMessageSize),
ready: make(chan struct{}),
readerDone: make(chan struct{}),
writerDone: make(chan struct{}),
clientDone: make(chan struct{}),
}
c.writerCond.L = &sync.Mutex{}
c.SetLogger(node.logger)
return c
}
// ID returns an immutable copy of the ID of this client, which is established once the client has successfully
// completed the handshake protocol configured from this clients associated node.
//
// ID may be called concurrently.
func (c *Client) ID() ID {
return c.id
}
// Logger returns the underlying logger associated to this client. It may optionally be set via (*Client).SetLogger.
//
// Logger may be called concurrently.
func (c *Client) Logger() *zap.Logger {
c.logger.RLock()
defer c.logger.RUnlock()
return c.logger.Logger
}
// SetLogger updates the logger instance of this client.
//
// SetLogger may be called concurrently.
func (c *Client) SetLogger(logger *zap.Logger) {
c.logger.Lock()
defer c.logger.Unlock()
c.logger.Logger = logger
}
// Close asynchronously kills the underlying connection and signals all goroutines to stop underlying this client.
//
// Close may be called concurrently.
func (c *Client) Close() {
c.close()
}
// WaitUntilReady pauses the goroutine to which it was called within until/unless the client has successfully
// completed/failed the handshake protocol configured under the node instance to which this peer was derived from.
//
// It pauses the goroutine by reading from a channel that is closed when the client has successfully completed/failed
// the aforementioned handshake protocol.
//
// WaitUntilReady may be called concurrently.
func (c *Client) WaitUntilReady() {
c.waitUntilReady()
}
// WaitUntilClosed pauses the goroutine to which it was called within until all goroutines associated to this client
// has been closed. The goroutines associated to this client would only close should:
//
// 1) handshaking failed/succeeded,
// 2) the connection was dropped, or
// 3) (*Client).Close was called.
//
// WaitUntilReady may be called concurrently.
func (c *Client) WaitUntilClosed() {
c.waitUntilClosed()
}
// Error returns the very first error that has caused this clients connection to have dropped.
//
// Error may be called concurrently.
func (c *Client) Error() error {
c.err.Lock()
defer c.err.Unlock()
return c.err.error
}
func (c *Client) reportError(err error) {
c.err.Lock()
defer c.err.Unlock()
if c.err.error == nil {
c.err.error = err
}
}
func (c *Client) close() {
c.closeOnce.Do(func() {
c.writerCond.L.Lock()
c.writerClosed = true
c.writerCond.Signal()
c.writerCond.L.Unlock()
if c.conn != nil {
c.conn.Close()
}
})
}
func (c *Client) waitUntilReady() {
<-c.ready
}
func (c *Client) waitUntilClosed() {
<-c.clientDone
}
func (c *Client) outbound(ctx context.Context, addr string, opcode ...byte) {
c.addr = addr
c.side = clientSideInbound
defer func() {
c.node.outbound.remove(addr)
close(c.clientDone)
}()
var dialer net.Dialer
conn, err := dialer.DialContext(ctx, "tcp", addr)
if err != nil {
c.reportError(err)
close(c.ready)
close(c.writerDone)
close(c.readerDone)
return
}
c.reader = bufio.NewReader(conn)
c.writer = bufio.NewWriter(conn)
c.conn = conn
c.handshakeOutbound(opcode...)
go c.writeLoop()
c.recvLoop()
c.close()
//fmt.Printf("JHJH %s outbound Peer connection closed.\n",c.id.Address)
//c.Logger().Debug("Peer connection closed.")
for _, protocol := range c.node.protocols {
if protocol.OnPeerDisconnected == nil {
continue
}
protocol.OnPeerDisconnected(c)
}
}
func (c *Client) inbound(conn net.Conn, addr string) {
c.addr = addr
c.side = clientSideOutbound
defer func() {
c.node.inbound.remove(addr)
close(c.clientDone)
}()
c.reader = bufio.NewReader(conn)
c.writer = bufio.NewWriter(conn)
c.conn = conn
c.handshakeInbound()
if c.Error() != nil {
//fmt.Printf("JHJH inbound bread1 Err: %v\n", c.Error())
c.close()
return
}
go c.writeLoop()
c.recvLoop()
c.close()
//fmt.Printf("JHJH %s inbound Peer connection closed. %s\n",c.id.Address, c.addr)
for _, protocol := range c.node.protocols {
if protocol.OnPeerDisconnected == nil {
continue
}
protocol.OnPeerDisconnected(c)
}
}
func (c *Client) read() ([]byte, error) {
if c.node.idleTimeout > 0 {
if err := c.conn.SetReadDeadline(time.Now().Add(c.node.idleTimeout)); err != nil {
return nil, err
}
}
if _, err := io.ReadFull(c.reader, c.readerBuf[:4]); err != nil {
return nil, err
}
size := binary.BigEndian.Uint32(c.readerBuf[:4])
if c.node.maxRecvMessageSize > 0 && size > c.node.maxRecvMessageSize {
//fmt.Printf("JHJH got %d bytes, limit is set to %d", size, c.node.maxRecvMessageSize)
return nil, fmt.Errorf("got %d bytes, but limit is set to %d: %w", size, c.node.maxRecvMessageSize, ErrMessageTooLarge)
}
if _, err := io.ReadFull(c.reader, c.readerBuf[4:size+4]); err != nil {
return nil, err
}
if c.suite == nil {
return c.readerBuf[4 : size+4], nil
}
buf, err := decryptAEAD(c.suite, c.readerBuf[4:size+4])
if err != nil {
return nil, err
}
return buf, nil
}
func (c *Client) write(data []byte) error {
if c.node.idleTimeout > 0 {
if err := c.conn.SetWriteDeadline(time.Now().Add(c.node.idleTimeout)); err != nil {
return err
}
}
if c.suite != nil {
var err error
if data, err = encryptAEAD(c.suite, data); err != nil {
return err
}
}
data = append(make([]byte, 4), data...)
binary.BigEndian.PutUint32(data[:4], uint32(len(data)-4))
if _, err := c.writer.Write(data); err != nil {
return err
}
return c.writer.Flush()
}
func (c *Client) send(nonce uint64, recv_nonce uint64, data []byte, length uint32) error {
c.writerCond.L.Lock()
//c.writeSingle(message{nonce: nonce, length: length, data: data})
c.writerBuf = append(c.writerBuf, message{length: length, nonce: nonce, recv_nonce: recv_nonce, data: data})
//fmt.Printf("writeBuf message len=%d\n",len(c.writerBuf))
c.writerCond.Signal()
c.writerCond.L.Unlock()
return nil
}
func (c *Client) request(ctx context.Context, data []byte, length uint32) (message, error) {
// Figure out an available request nonce.
ch, nonce, err := c.requests.nextNonce()
if err != nil {
fmt.Printf("request nonce error\n")
return message{}, err
}
// Send request.
if err := c.send(nonce, math.MaxUint64, data, length); err != nil {
fmt.Printf("request send error\n")
c.requests.markRequestFailed(nonce)
return message{}, err
}
//fmt.Printf("wait for ACK\n")
var msg message
select {
case msg = <-ch:
if msg.nonce == 0 {
fmt.Printf("request: receive ACK nonce %d\n",msg.nonce)
return message{}, io.EOF
}
case <-ctx.Done():
//fmt.Printf("ctx done\n")
return message{}, ctx.Err()
}
return msg, nil
}
// inbound doesn't have opcode in function argument
func (c *Client) handshakeInbound() {
defer close(c.ready)
var opcode []byte
// Generate Ed25519 ephemeral keypair to perform a Diffie-Hellman handshake.
pub, sec, err := GenerateKeys(nil)
if err != nil {
c.reportError(err)
return
}
// Send our Ed25519 ephemeral public key and signature of the message '.__GRING_handshake'.
signature := sec.Sign([]byte(".__GRING_handshake"))
if err := c.write(append(pub[:], signature[:]...)); err != nil {
c.reportError(fmt.Errorf("failed to send session handshake: %w", err))
return
}
// Read from our peer their Ed25519 ephemeral public key and signature of the message '.__GRING_handshake'.
data, err := c.read()
if err != nil {
c.reportError(err)
return
}
if len(data) != SizePublicKey+SizeSignature {
c.reportError(fmt.Errorf("received invalid number of bytes opening a session: expected %d byte(s), but got %d byte(s)",
SizePublicKey+SizeSignature,
len(data),
))
return
}
var peerPublicKey PublicKey
copy(peerPublicKey[:], data[:SizePublicKey])
// Verify ownership of our peers Ed25519 public key by verifying the signature they sent.
if !peerPublicKey.Verify([]byte(".__GRING_handshake"), UnmarshalSignature(data[SizePublicKey:SizePublicKey+SizeSignature])) {
c.reportError(errors.New("could not verify session handshake"))
return
}
// Transform all Ed25519 points to Curve25519 points and perform a Diffie-Hellman handshake
// to derive a shared key.
shared, err := ECDH(sec, peerPublicKey)
if err != nil {
c.reportError(err)
return
}
// Use the derived shared key from Diffie-Hellman to encrypt/decrypt all future communications
// with AES-256 Galois Counter Mode (GCM).
core, err := aes.NewCipher(shared[:])
if err != nil {
c.reportError(fmt.Errorf("could not instantiate aes: %w", err))
return
}
suite, err := cipher.NewGCM(core)
if err != nil {
c.reportError(fmt.Errorf("could not instantiate aes-gcm: %w", err))
return
}
c.suite = suite
// Send to our peer our overlay ID.
buf := c.node.id.Marshal()
signature = c.node.Sign(append(buf, shared...))
buf = append(buf, signature[:]...)
if err := c.write(buf); err != nil {
c.reportError(fmt.Errorf("failed to send session handshake: %w", err))
return
}
// Read and parse from our peer their overlay ID.
data, err = c.read()
if err != nil {
c.reportError(fmt.Errorf("failed to read overlay handshake: %w", err))
return
}
id, err := UnmarshalID(data)
if err != nil {
c.reportError(fmt.Errorf("failed to parse peer id while handling overlay handshake: %w", err))
return
}
// Validate the peers ownership of the overlay ID.
buf = make([]byte, id.Size())
copy(buf, data)
if len(data) != len(buf)+SizeSignature {
c.reportError(fmt.Errorf("received invalid number of bytes handshaking: expected %d byte(s), got %d byte(s)",
len(buf)+SizeSignature,
len(data),
))
return
}
if !id.ID.Verify(append(buf, shared...), UnmarshalSignature(data[len(buf):len(buf)+SizeSignature])) {
c.reportError(errors.New("overlay handshake signature is malformed"))
return
}
c.id = id
/*
c.SetLogger(c.Logger().With(
zap.String("peer_id", id.ID.String()),
zap.String("peer_addr", id.Address),
zap.String("remote_addr", c.conn.RemoteAddr().String()),
zap.String("session_key", hex.EncodeToString(shared[:])),
))
*/
//fmt.Printf("JHJH handshakeinbound peer_addr %s\n remote_addr %s\n",id.Address,c.conn.RemoteAddr().String())
// Read from our peer their opcode
data, err = c.read()
if err != nil {
fmt.Printf("handshake : recv opcode error\n")
// if nothing received, we do nothing here
// if this is outbound, simply we use opcode in the argument
// if this is inbound and doesn't have opcode received, counterpart outbound didn't send the opcode
}else{
// if there is an opcode we received, we pass this opcode
opcode = data
//fmt.Printf("handshake : receive protocol opcode : %d\n",opcode)
var op uint16
op = binary.BigEndian.Uint16(data[:2])
//fmt.Printf("handshake : receive protocol opcode : %d\n",op)
if op == uint16(NO_OP) {
//fmt.Printf("handshake : receive protocol opcode : NO_OP %d\n",int(op))
opcode = nil
}else{
opcode = data[:2]
}
}
c.Logger().Debug("Peer connection opened.")
for _, protocol := range c.node.protocols {
if protocol.OnPeerConnected == nil {
continue
}
protocol.OnPeerConnected(true, c, opcode...)
}
}
// outbound could have opcode in function arguement
func (c *Client) handshakeOutbound(opcode ...byte) {
defer close(c.ready)
// Generate Ed25519 ephemeral keypair to perform a Diffie-Hellman handshake.
pub, sec, err := GenerateKeys(nil)
if err != nil {
//fmt.Printf("JHJH handshakeoutbound fail to gen key: %w\n", err)
c.reportError(err)
return
}
// Send our Ed25519 ephemeral public key and signature of the message '.__GRING_handshake'.
signature := sec.Sign([]byte(".__GRING_handshake"))
if err := c.write(append(pub[:], signature[:]...)); err != nil {
//fmt.Printf("JHJH handshakeoutbound failed to send session handshake: %w", err)
c.reportError(fmt.Errorf("failed to send session handshake: %w", err))
return
}
// Read from our peer their Ed25519 ephemeral public key and signature of the message '.__GRING_handshake'.
data, err := c.read()
if err != nil {
//fmt.Printf("JHJH handshakeoutbound failed to read pub key: %w", err)
c.reportError(err)
return
}
if len(data) != SizePublicKey+SizeSignature {
c.reportError(fmt.Errorf("received invalid number of bytes opening a session: expected %d byte(s), but got %d byte(s)",
SizePublicKey+SizeSignature,
len(data),
))
return
}
var peerPublicKey PublicKey
copy(peerPublicKey[:], data[:SizePublicKey])
// Verify ownership of our peers Ed25519 public key by verifying the signature they sent.
if !peerPublicKey.Verify([]byte(".__GRING_handshake"), UnmarshalSignature(data[SizePublicKey:SizePublicKey+SizeSignature])) {
c.reportError(errors.New("could not verify session handshake"))
return
}
// Transform all Ed25519 points to Curve25519 points and perform a Diffie-Hellman handshake
// to derive a shared key.
shared, err := ECDH(sec, peerPublicKey)
if err != nil {
c.reportError(err)
return
}
// Use the derived shared key from Diffie-Hellman to encrypt/decrypt all future communications
// with AES-256 Galois Counter Mode (GCM).
core, err := aes.NewCipher(shared[:])
if err != nil {
c.reportError(fmt.Errorf("could not instantiate aes: %w", err))
return
}
suite, err := cipher.NewGCM(core)
if err != nil {
c.reportError(fmt.Errorf("could not instantiate aes-gcm: %w", err))
return
}
c.suite = suite
// Send to our peer our overlay ID.
buf := c.node.id.Marshal()
signature = c.node.Sign(append(buf, shared...))
buf = append(buf, signature[:]...)
if err := c.write(buf); err != nil {
c.reportError(fmt.Errorf("failed to send session handshake: %w", err))
return
}
// Read and parse from our peer their overlay ID.
data, err = c.read()
if err != nil {
c.reportError(fmt.Errorf("failed to read overlay handshake: %w", err))
return
}
id, err := UnmarshalID(data)
if err != nil {
c.reportError(fmt.Errorf("failed to parse peer id while handling overlay handshake: %w", err))
return
}
// Validate the peers ownership of the overlay ID.
buf = make([]byte, id.Size())
copy(buf, data)
if len(data) != len(buf)+SizeSignature {
c.reportError(fmt.Errorf("received invalid number of bytes handshaking: expected %d byte(s), got %d byte(s)",
len(buf)+SizeSignature,
len(data),
))
return
}
if !id.ID.Verify(append(buf, shared...), UnmarshalSignature(data[len(buf):len(buf)+SizeSignature])) {
c.reportError(errors.New("overlay handshake signature is malformed"))
return
}
c.id = id
/*
c.SetLogger(c.Logger().With(
zap.String("peer_id", id.ID.String()),
zap.String("peer_addr", id.Address),
zap.String("remote_addr", c.conn.RemoteAddr().String()),
zap.String("session_key", hex.EncodeToString(shared[:])),
))
*/
//fmt.Printf("JHJH handshakeoutbound peer_addr %s remote_addr %s\n",id.Address,c.conn.RemoteAddr().String())
if len(opcode) != 0 {
// send opcode to peer
if err := c.write(opcode); err != nil {
c.reportError(fmt.Errorf("failed to send session handshake: %w", err))
return
}
//fmt.Printf("handshake : send protocol opcode : %d\n",opcode)
}else{
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf[:2], NO_OP)
if err := c.write(buf); err != nil {
c.reportError(fmt.Errorf("failed to send session handshake: %w", err))
return
}
}
c.Logger().Debug("Peer connection opened.")
for _, protocol := range c.node.protocols {
if protocol.OnPeerConnected == nil {
continue
}
protocol.OnPeerConnected(false, c, opcode...)
}
}
func (c *Client) recvBulkHandleLoop(recv_nonce uint64, first_msg message, ch chan message) {
var msg message
dummy := make([]byte, 1)
nonce := first_msg.nonce
first_msg.data = append([]byte{}, first_msg.data...)
//fmt.Printf("start bulk loop. receive first partial. nonce:%d recv_nonce:%d legnth:%d\n",nonce,recv_nonce, first_msg.length)
if err := c.send(nonce, recv_nonce, dummy, 0); err != nil {
// TODO : error handle
fmt.Printf("Error send ACK nonce:%d recv_nonce:%d length:%d dummy size:%d\n",nonce,recv_nonce,first_msg.length,len(dummy))
return
}
//fmt.Printf("sent ACK nonce:%d recv_nonce:%d length:%d\n",nonce, recv_nonce, first_msg.length)
for {
//fmt.Printf("wait for data nonce:%d recv_nonce:%d\n",nonce, recv_nonce)
select {
case msg = <-ch:
first_msg.data = append(first_msg.data, msg.data...)
nonce = msg.nonce
if msg.length == 1 {
//fmt.Printf("receive last partial bulk nonce:%d recv_nonce:%d legnth:%d\n",nonce, recv_nonce, msg.length)
// send ACK
if err := c.send(nonce, recv_nonce, dummy,0); err != nil {
// TODO : error handle
fmt.Printf("error on sent ACK nonce:%d recv_nonce:%d length:%d\n",nonce, recv_nonce, msg.length)
continue
}
//fmt.Printf("sent ACK nonce:%d recv_nonce:%d length:%d\n",nonce, recv_nonce, msg.length)
break
}
//fmt.Printf("receive partial bulk nonce:%d recv_nonce:%d legnth:%d\n",nonce, recv_nonce, msg.length)
// send ACK
if err := c.send(nonce, recv_nonce, dummy,0); err != nil {
// TODO : error handle
fmt.Printf("error on sent ACK nonce:%d recv_nonce:%d length:%d\n",nonce, recv_nonce, msg.length)
continue
}
//fmt.Printf("sent ACK nonce:%d recv_nonce:%d length:%d\n",nonce, recv_nonce, msg.length)
continue
}
//fmt.Printf("handle message nonce:%d recv_nonce:%d size:%d\n",nonce, recv_nonce, len(first_msg.data))
c.node.work <- HandlerContext{client: c, msg: first_msg}
for _, protocol := range c.node.protocols {
if protocol.OnMessageRecv == nil {
continue
}
protocol.OnMessageRecv(c)
}
close(ch)
dummy = nil
c.requests_recv.deleteRequest(recv_nonce)
return
}
}
func (c *Client) recvLoop() {
defer close(c.readerDone)
for {
buf, err := c.read()
if err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error while sending messages.", zap.Error(err))
}
c.reportError(err)
break
}
msg, err := unmarshalMessage(buf)
if err != nil {
c.Logger().Warn("Got an error while reading incoming messages.", zap.Error(err))
c.reportError(err)
break
}
if msg.length > uint32(0) { // if not a single message or request message
// check if it is existing bulk request
if ch,_ := c.requests_recv.findRequest(msg.recv_nonce); ch != nil {
//fmt.Printf("bulk request exists. nonce:%d recv_nonce:%d length:%d\n",msg.nonce,msg.recv_nonce, msg.length)
// if existing, add to channel
ch <- msg
continue
}else{
// if new request and bulk, create new channel for this nonce
//fmt.Printf("bulk request doesnt exist. nonce:%d recv_nonce:%d length:%d\n",msg.nonce,msg.recv_nonce, msg.length)
ch, recv_nonce, err := c.requests_recv.nextNonce()
if err != nil {
fmt.Printf("error to get next nonce\n")
// TODO : error ACK return and continue
continue
}
//fmt.Printf("this is first bulk request nonce:%d register recv_nonce:%d length:%d\n",msg.nonce, recv_nonce, msg.length)
go c.recvBulkHandleLoop(recv_nonce, msg, ch)
continue
}
}
msg.data = append([]byte{}, msg.data...)
if ch, exists := c.requests.findRequest(msg.nonce); ch != nil {
ch <- msg
close(ch)
if exists {
c.requests.deleteRequest(msg.nonce)
}
continue
}
c.node.work <- HandlerContext{client: c, msg: msg}
for _, protocol := range c.node.protocols {
if protocol.OnMessageRecv == nil {
continue
}
protocol.OnMessageRecv(c)
}
}
}
func (c *Client) writeSingle(msg message) {
header := make([]byte, 4)
buf := make([]byte, 0, 1024)
buf = buf[:0]
buf = msg.marshal(buf)
if c.suite != nil {
var err error
if buf, err = encryptAEAD(c.suite, buf); err != nil {
c.Logger().Warn("Got an error encrypting a message.", zap.Error(err))
c.reportError(err)
return
}
}
binary.BigEndian.PutUint32(header, uint32(len(buf)))
if _, err := c.writer.Write(header); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error writing header.", zap.Error(err))
}
c.reportError(err)
return
}
if _, err := c.writer.Write(buf); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error writing a message.", zap.Error(err))
}
c.reportError(err)
return
}
if err := c.writer.Flush(); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error flushing.", zap.Error(err))
}
c.reportError(err)
return
}
for _, protocol := range c.node.protocols {
if protocol.OnMessageSent == nil {
continue
}
protocol.OnMessageSent(c)
}
}
func (c *Client) writeLoop() {
defer close(c.writerDone)
header := make([]byte, 4)
buf := make([]byte, 0, 1024)
Write:
for {
select {
case <-c.readerDone:
return
case <-c.clientDone:
return
default:
}
if c.node.idleTimeout > 0 {
if err := c.conn.SetWriteDeadline(time.Now().Add(c.node.idleTimeout)); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error setting write deadline.", zap.Error(err))
}
c.reportError(err)
break Write
}
}
c.writerCond.L.Lock()
for len(c.writerBuf) == 0 && !c.writerClosed {
c.writerCond.Wait()
}
writerClosed := c.writerClosed
if writerClosed {
break Write
}
starttime = time.Now()
for _, msg := range c.writerBuf {
buf = buf[:0]
buf = msg.marshal(buf)
datasize = len(buf)
if c.suite != nil {
var err error
if buf, err = encryptAEAD(c.suite, buf); err != nil {
c.Logger().Warn("Got an error encrypting a message.", zap.Error(err))
c.reportError(err)
break Write
}
}
binary.BigEndian.PutUint32(header, uint32(len(buf)))
if _, err := c.writer.Write(header); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error writing header.", zap.Error(err))
}
c.reportError(err)
break Write
}
if _, err := c.writer.Write(buf); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error writing a message.", zap.Error(err))
}
c.reportError(err)
break Write
}
}
if err := c.writer.Flush(); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error flushing.", zap.Error(err))
}
c.reportError(err)
break Write
}
duration = time.Since(starttime)
//fmt.Printf("msg:%d I/O time: %d ns\n",datasize, duration.Nanoseconds())
for _, protocol := range c.node.protocols {
if protocol.OnMessageSent == nil {
continue
}
protocol.OnMessageSent(c)
}
c.writerBuf = nil
c.writerCond.L.Unlock()
}