-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
707 lines (626 loc) · 15.8 KB
/
client.go
File metadata and controls
707 lines (626 loc) · 15.8 KB
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
package fastwire
import (
"net"
"sync"
"time"
fwcrypto "github.com/marcomoesman/fastwire/crypto"
)
// Client connects to a FastWire server.
type Client struct {
config ClientConfig
conn *net.UDPConn
handler Handler
server *Connection // single connection to the server
clientKP fwcrypto.KeyPair // stored for handshake processing
incoming chan incomingPacket
closeCh chan struct{}
closeOnce sync.Once
wg sync.WaitGroup
mu sync.Mutex
connectDone chan struct{}
connectDoneOnce sync.Once
connectErr error
connected bool
connectAborted bool
// Read buffer pool.
readPool *sync.Pool
// Computed client features for handshake.
clientFeatures byte
}
// NewClient creates a new Client. Call Connect() to connect to a server.
func NewClient(config ClientConfig, handler Handler) (*Client, error) {
// Apply defaults for zero-value fields.
if config.MTU == 0 {
config.MTU = DefaultMTU
}
if config.TickRate == 0 {
config.TickRate = 100
}
if config.HeartbeatInterval == 0 {
config.HeartbeatInterval = 1 * time.Second
}
if config.ConnTimeout == 0 {
config.ConnTimeout = 10 * time.Second
}
if config.ConnectTimeout == 0 {
config.ConnectTimeout = 5 * time.Second
}
if config.ChannelLayout.Len() == 0 {
config.ChannelLayout = DefaultChannelLayout()
}
if config.MaxRetransmits == 0 {
config.MaxRetransmits = maxRetransmits
}
if config.FragmentTimeout == 0 {
config.FragmentTimeout = DefaultFragmentTimeout
}
if config.InitialCwnd == 0 {
config.InitialCwnd = DefaultInitialCwnd
}
if config.MaxReorderWindow == 0 {
config.MaxReorderWindow = DefaultMaxReorderWindow
}
if config.MaxReassemblyBuffers == 0 {
config.MaxReassemblyBuffers = DefaultMaxReassemblyBuffers
}
if config.MaxReassemblyBytes == 0 {
config.MaxReassemblyBytes = DefaultMaxReassemblyBytes
}
cf := featuresFromConfig(config.SendBatching, config.ConnectionMigration)
return &Client{
config: config,
handler: handler,
closeCh: make(chan struct{}),
clientFeatures: cf,
}, nil
}
// Connect initiates a connection to the given server address.
// Blocks until the handshake completes or times out.
func (c *Client) Connect(addr string) error {
c.mu.Lock()
if c.connected {
c.mu.Unlock()
return ErrAlreadyConnected
}
c.mu.Unlock()
// Resolve and dial.
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
udpConn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
return err
}
c.conn = udpConn
// Generate key pair.
kp, err := fwcrypto.GenerateKeyPair()
if err != nil {
_ = c.conn.Close()
return err
}
c.clientKP = kp
// Build and send CONNECT.
connectPkt := &connectPacket{
ProtocolVersion: ProtocolVersion,
AppVersion: ApplicationVersion,
PublicKey: kp.Public.Bytes(),
CipherPref: c.config.CipherPreference,
Compression: c.config.Compression.Algorithm,
Features: c.clientFeatures,
}
if c.config.Compression.Algorithm == CompressionZstd && c.config.Compression.Dictionary != nil {
hash := DictionaryHash(c.config.Compression.Dictionary)
connectPkt.DictHash = hash[:]
}
buf := make([]byte, 128)
n, err := buildConnectPacket(buf, connectPkt)
if err != nil {
_ = c.conn.Close()
return err
}
if _, err := c.conn.Write(buf[:n]); err != nil {
_ = c.conn.Close()
return err
}
// Set up handshake signaling.
c.incoming = make(chan incomingPacket, 1024)
c.connectDone = make(chan struct{})
c.connectDoneOnce = sync.Once{}
c.connectErr = nil
c.connectAborted = false
c.readPool = newReadPool(c.config.MTU + fwcrypto.WireOverhead + MigrationTokenSize + BatchHeaderSize)
// Start read loop.
c.wg.Add(1)
go c.readLoop()
// Wait for handshake or timeout.
select {
case <-c.connectDone:
if c.connectErr != nil {
c.closeOnce.Do(func() {
close(c.closeCh)
_ = c.conn.Close()
})
c.wg.Wait()
return c.connectErr
}
case <-time.After(c.config.ConnectTimeout):
c.mu.Lock()
c.connectAborted = true
c.mu.Unlock()
c.closeOnce.Do(func() {
close(c.closeCh)
_ = c.conn.Close()
})
c.wg.Wait()
return ErrHandshakeTimeout
}
// Set up connection callbacks.
conn := c.server
c.setupSendFunc(conn)
conn.closeFunc = func() {
c.mu.Lock()
c.connected = false
c.server = nil
c.mu.Unlock()
}
c.mu.Lock()
c.connected = true
c.mu.Unlock()
// Start tick loop if TickAuto.
if c.config.TickMode == TickAuto {
c.wg.Add(1)
go c.tickLoop()
}
c.handler.OnConnect(conn)
return nil
}
// setupSendFunc configures the connection's send function, wrapping with
// migration token prefix if needed.
func (c *Client) setupSendFunc(conn *Connection) {
conn.sendFunc = func(data []byte) error {
var buf []byte
if conn.features&byte(FeatureConnectionMigration) != 0 {
buf = make([]byte, MigrationTokenSize+len(data))
copy(buf, conn.migrationToken[:])
copy(buf[MigrationTokenSize:], data)
} else {
buf = data
}
_, err := c.conn.Write(buf)
if err == nil {
conn.bytesSent.Add(uint64(len(buf)))
conn.sendBW.Record(uint64(len(buf)))
}
return err
}
}
// Close disconnects from the server and releases resources.
func (c *Client) Close() error {
c.mu.Lock()
conn := c.server
c.mu.Unlock()
if conn != nil {
// Use Connection.Close() which sets StateDisconnecting and sends first disconnect.
_ = conn.Close()
// For client teardown, force state to Disconnected and do full cleanup.
conn.releasePendingBuffers()
conn.setState(StateDisconnected)
c.mu.Lock()
c.connected = false
c.server = nil
c.mu.Unlock()
c.handler.OnDisconnect(conn, DisconnectGraceful)
}
c.closeOnce.Do(func() {
close(c.closeCh)
if c.conn != nil {
_ = c.conn.Close()
}
})
c.wg.Wait()
return nil
}
// Tick performs one tick cycle. Only valid in TickDriven mode.
func (c *Client) Tick() error {
if c.config.TickMode == TickAuto {
return ErrTickAutoMode
}
c.tick()
return nil
}
// Connection returns the current server connection, or nil if not connected.
func (c *Client) Connection() *Connection {
c.mu.Lock()
defer c.mu.Unlock()
return c.server
}
// --- internal loops ---
func (c *Client) readLoop() {
defer c.wg.Done()
consecutiveErrors := 0
for {
buf := c.readPool.Get().([]byte)
n, err := c.conn.Read(buf)
if err != nil {
//nolint:staticcheck // SA6002: we intentionally store []byte in sync.Pool
c.readPool.Put(buf)
select {
case <-c.closeCh:
return
default:
}
consecutiveErrors++
backoff := time.Duration(1<<min(consecutiveErrors, 7)) * time.Millisecond
select {
case <-time.After(backoff):
case <-c.closeCh:
return
}
continue
}
consecutiveErrors = 0
c.mu.Lock()
connected := c.connected
c.mu.Unlock()
if !connected {
// Still in handshake phase — process inline, return buffer immediately.
c.processHandshakePacket(buf[:n])
//nolint:staticcheck // SA6002: we intentionally store []byte in sync.Pool
c.readPool.Put(buf)
continue
}
pkt := incomingPacket{
data: buf[:n],
n: n,
buf: buf,
}
select {
case c.incoming <- pkt:
case <-c.closeCh:
//nolint:staticcheck // SA6002: we intentionally store []byte in sync.Pool
c.readPool.Put(buf)
return
}
}
}
func (c *Client) processHandshakePacket(data []byte) {
// Try to parse as unencrypted control packet.
_, ct, _, err := parseControlPacket(data)
if err != nil {
return
}
switch ct {
case ControlChallenge:
c.handleChallenge(data)
case ControlVersionMismatch:
c.connectErr = ErrVersionMismatch
c.connectDoneOnce.Do(func() { close(c.connectDone) })
case ControlReject:
c.connectErr = ErrConnectionClosed
c.connectDoneOnce.Do(func() { close(c.connectDone) })
}
}
func (c *Client) handleChallenge(data []byte) {
sendCipher, recvCipher, suite, encryptedResp, err := clientProcessChallenge(data, c.clientKP)
if err != nil {
c.connectErr = err
c.connectDoneOnce.Do(func() { close(c.connectDone) })
return
}
// Send encrypted RESPONSE.
if _, err := c.conn.Write(encryptedResp); err != nil {
c.connectErr = err
c.connectDoneOnce.Do(func() { close(c.connectDone) })
return
}
// Extract negotiated features from the challenge.
_, _, ctrlPayload, _ := parseControlPacket(data)
challenge, _ := unmarshalChallenge(ctrlPayload)
// Resolve server address.
serverAddr := c.conn.RemoteAddr().(*net.UDPAddr).AddrPort()
// Create connection with negotiated features.
conn := newConnection(connectionInput{
addr: serverAddr,
sendCipher: sendCipher,
recvCipher: recvCipher,
suite: suite,
layout: c.config.ChannelLayout,
compression: c.config.Compression,
congestionMode: c.config.Congestion,
initialCwnd: c.config.InitialCwnd,
features: challenge.Features,
token: challenge.MigrationToken,
settings: connSettings{
maxReorderWindow: c.config.MaxReorderWindow,
maxReassemblyBuffers: c.config.MaxReassemblyBuffers,
maxReassemblyBytes: c.config.MaxReassemblyBytes,
fragmentTimeout: c.config.FragmentTimeout,
},
})
c.mu.Lock()
if c.connectAborted {
c.mu.Unlock()
return
}
c.server = conn
c.mu.Unlock()
c.connectDoneOnce.Do(func() { close(c.connectDone) })
}
func (c *Client) tickLoop() {
defer c.wg.Done()
ticker := time.NewTicker(time.Second / time.Duration(c.config.TickRate))
defer ticker.Stop()
for {
select {
case <-ticker.C:
c.tick()
case <-c.closeCh:
return
}
}
}
func (c *Client) tick() {
now := time.Now()
// Drain incoming packets.
for {
select {
case pkt := <-c.incoming:
c.mu.Lock()
conn := c.server
c.mu.Unlock()
if conn != nil {
c.processConnData(conn, pkt.data)
}
if pkt.buf != nil {
//nolint:staticcheck // SA6002: we intentionally store []byte in sync.Pool
c.readPool.Put(pkt.buf)
}
default:
goto doneIncoming
}
}
doneIncoming:
c.mu.Lock()
conn := c.server
c.mu.Unlock()
if conn != nil {
c.tickConnection(conn, now)
}
}
// processConnData handles a raw datagram for an established connection.
// Server→Client datagrams don't have migration tokens (only client→server does).
func (c *Client) processConnData(conn *Connection, data []byte) {
conn.bytesReceived.Add(uint64(len(data)))
conn.recvBW.Record(uint64(len(data)))
// Handle batching.
if conn.batchEnabled {
packets, err := UnmarshalBatch(data)
if err != nil {
c.handler.OnError(conn, err)
return
}
for _, pkt := range packets {
c.processPacket(conn, pkt)
}
} else {
c.processPacket(conn, data)
}
}
// processPacket is the receive pipeline for a single encrypted packet.
func (c *Client) processPacket(conn *Connection, data []byte) {
dstBuf := getDecryptBuffer()
defer putDecryptBuffer(dstBuf)
decrypted, err := fwcrypto.Decrypt(conn.recvCipher, data, dstBuf)
if err != nil {
c.handler.OnError(conn, err)
return
}
hdr, n, err := UnmarshalHeader(decrypted)
if err != nil {
c.handler.OnError(conn, err)
return
}
conn.touchRecv()
ch := conn.channel(hdr.Channel)
if ch == nil {
return
}
// Process acks.
acked := ch.processAcks(hdr.Ack, hdr.AckField, conn.rttState)
if len(acked) > 0 {
conn.cc.OnAck(len(acked))
for _, seq := range acked {
conn.loss.RecordAck(seq)
}
}
// Control packet.
if hdr.Flags&FlagControl != 0 {
if len(decrypted[n:]) < 1 {
return
}
ct := ControlType(decrypted[n])
switch ct {
case ControlHeartbeat:
return
case ControlMultiAck:
entries, _, err := unmarshalMultiAck(decrypted[n+1:])
if err != nil {
return
}
for _, entry := range entries {
aCh := conn.channel(entry.Channel)
if aCh == nil {
continue
}
acked := aCh.processAcks(entry.Ack, entry.AckField, conn.rttState)
if len(acked) > 0 {
conn.cc.OnAck(len(acked))
for _, seq := range acked {
conn.loss.RecordAck(seq)
}
}
}
return
case ControlDisconnect:
conn.releasePendingBuffers()
conn.setState(StateDisconnected)
c.mu.Lock()
c.connected = false
c.server = nil
c.mu.Unlock()
c.handler.OnDisconnect(conn, DisconnectGraceful)
return
default:
return
}
}
// Duplicate check.
if !ch.recordReceive(hdr.Sequence) {
return
}
payload := decrypted[n:]
// Fragment handling.
if hdr.Flags&FlagFragment != 0 {
fh, fn, err := UnmarshalFragmentHeader(payload)
if err != nil {
c.handler.OnError(conn, err)
return
}
assembled, complete, err := conn.reassembly.addFragment(fh, payload[fn:])
if err != nil {
c.handler.OnError(conn, err)
return
}
if !complete {
// Consume sequence slot for ordered delivery (nil = fragment placeholder).
ch.deliver(hdr.Sequence, nil)
return
}
decompressed, err := conn.compress.decompressPayload(assembled, fh.FragmentFlags)
if err != nil {
c.handler.OnError(conn, err)
return
}
payload = decompressed
}
// Deliver.
msgs := ch.deliver(hdr.Sequence, payload)
for _, msg := range msgs {
if msg != nil {
c.handler.OnMessage(conn, msg, hdr.Channel)
}
}
}
// tickConnection runs per-connection tick logic.
func (c *Client) tickConnection(conn *Connection, now time.Time) {
state := conn.State()
// Handle disconnect retry.
if state == StateDisconnecting {
conn.mu.Lock()
retries := conn.disconnectRetries
nextRetry := conn.nextDisconnectRetry
pkt := conn.disconnectPacket
sf := conn.sendFunc
conn.mu.Unlock()
if retries >= maxDisconnectRetries {
// Max retries reached — force close.
conn.releasePendingBuffers()
conn.setState(StateDisconnected)
c.mu.Lock()
c.connected = false
c.server = nil
c.mu.Unlock()
c.handler.OnDisconnect(conn, DisconnectGraceful)
return
}
if now.After(nextRetry) && pkt != nil && sf != nil {
_ = sf(pkt)
conn.mu.Lock()
conn.disconnectRetries++
conn.nextDisconnectRetry = now.Add(conn.rttState.RTO())
conn.mu.Unlock()
}
return
}
if state != StateConnected {
return
}
// Timeout check.
if conn.isTimedOutAt(now, c.config.ConnTimeout) {
conn.releasePendingBuffers()
conn.setState(StateDisconnected)
c.mu.Lock()
c.connected = false
c.server = nil
c.mu.Unlock()
c.handler.OnDisconnect(conn, DisconnectTimeout)
return
}
// Retransmission check.
rto := conn.rttState.RTO()
if conn.cc.HalvesRTO() {
rto /= 2
}
for _, ch := range conn.channels {
retransmits, kill := ch.checkRetransmissions(now, rto, c.config.MaxRetransmits)
if kill {
conn.releasePendingBuffers()
conn.setState(StateDisconnected)
c.mu.Lock()
c.connected = false
c.server = nil
c.mu.Unlock()
c.handler.OnError(conn, ErrMaxRetransmits)
c.handler.OnDisconnect(conn, DisconnectError)
return
}
for _, p := range retransmits {
encBuf := getSendBuffer(fwcrypto.NonceSize + len(p.raw) + fwcrypto.TagSize)
encrypted, err := fwcrypto.Encrypt(conn.sendCipher, p.raw, encBuf)
if err != nil {
putSendBuffer(encBuf)
continue
}
_ = conn.sendFramed(encrypted)
putSendBuffer(encrypted[:cap(encrypted)])
conn.lastSendNano.Store(now.UnixNano())
conn.cc.OnLoss()
}
}
// Drain send queue.
msgs := conn.drainSendQueue()
for i, msg := range msgs {
if !conn.cc.CanSend(conn.InFlightCount()) {
conn.requeue(msgs[i:])
break
}
if err := conn.sendMessage(msg.data, msg.channel, now); err != nil {
c.handler.OnError(conn, err)
}
}
// Flush batch buffer.
if conn.batchEnabled {
if err := conn.flushBatch(c.config.MTU); err != nil {
c.handler.OnError(conn, err)
}
}
// Flush pending acks — coalesce all channels into a single multi-ack packet.
var ackChannels []byte
for i, ch := range conn.channels {
if ch.clearNeedsAck() {
ackChannels = append(ackChannels, byte(i))
}
}
if len(ackChannels) > 0 {
_ = conn.sendMultiChannelHeartbeat(ackChannels)
}
// Heartbeat.
if conn.needsHeartbeatAt(now, c.config.HeartbeatInterval) {
_ = conn.sendHeartbeat()
}
// Cleanup stale reassembly buffers.
conn.reassembly.cleanup(c.config.FragmentTimeout)
// Update bandwidth estimates.
conn.tickBandwidth()
}