-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathquic.go
867 lines (782 loc) · 21.3 KB
/
quic.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
// Package quic provides client and server functions for QUIC connections.
//
// The process of creating a basic client and server looks like this:
//
// config := transport.NewConfig()
//
// client := quic.NewClient(config)
// client.SetHandler(handler)
// err := client.ListenAndServe(address)
// err = client.Connect(serverAddress)
// // wait
// client.Close()
//
// server := quic.NewServer(config)
// server.SetHandler(handler)
// err := server.ListenAndServe(address)
//
// The handler is where applications interact with QUIC connections:
//
// func (handler) Serve(conn *quic.Conn, events []transport.Event) {
// for _, e := range events {
// switch e.Type {
// case transport.EventConnOpen:
// case transport.EventConnClosed:
// }
// }
// }
package quic
import (
"crypto/rand"
"io"
"net"
"sync"
"time"
"github.com/goburrow/quic/transport"
)
const (
cidLength = 16 // must be less than transport.MaxCIDLength
// bufferSize is the size of the global buffers for sending and receiving UDP datagrams.
bufferSize = 1500
keyUnavailableTrigger = "key_unavailable"
)
// Handler defines interface to handle QUIC connection states.
// Multiple goroutines may invoke methods on a Handler simultaneously.
type Handler interface {
// Serve handles connection events.
// Applications should keep execution time in the callback to a minimum.
// If extra works needed, applications should consider using asynchronous APIs,
// i.e Stream functions instead of Conn.Stream* functions.
// Currently, each connection has its own goroutine for this callback.
Serve(conn *Conn, events []transport.Event)
}
type noopHandler struct{}
func (s noopHandler) Serve(*Conn, []transport.Event) {}
// Conn is a QUIC connection presenting a peer connected to this client or server.
// Conn is not safe for concurrent use.
// Its methods would only be called inside the Handler callback.
type Conn struct {
conn *transport.Conn
addr net.Addr
udpAddr net.UDPAddr
scid []byte
streams map[uint64]*Stream // async streams
datagram *Datagram // async datagram
events []transport.Event
// Channels for communicating with the connection.
recvCh chan *packet
cmdCh chan connCommand
// Timers
timeoutTimer *time.Timer // Read timeout timer, set when polling.
// Initial attempt key genereted for server connection.
attemptKey []byte
// Stream IDs
nextStreamIDBidi uint64
nextStreamIDUni uint64
// queuedPackets is for undecryptable packets which are queued for later processing.
// Currently, only last one is needed.
queuedPackets *packet
userData interface{}
}
func newRemoteConn(addr net.Addr, scid []byte, conn *transport.Conn, isClient bool) *Conn {
c := &Conn{
scid: scid,
conn: conn,
streams: make(map[uint64]*Stream),
recvCh: make(chan *packet, 16),
cmdCh: make(chan connCommand, 8),
nextStreamIDBidi: 0, // client by default
nextStreamIDUni: 2, // client by default
}
if udpAddr, ok := addr.(*net.UDPAddr); ok {
c.udpAddr = *udpAddr
c.addr = &c.udpAddr
} else {
c.addr = addr
}
if !isClient {
c.nextStreamIDBidi++
c.nextStreamIDUni++
}
return c
}
// ConnectionState returns details about the connection.
func (s *Conn) ConnectionState() transport.ConnectionState {
return s.conn.ConnectionState()
}
// StreamWrite adds data to the stream buffer for sending.
func (s *Conn) StreamWrite(streamID uint64, b []byte) (int, error) {
st, err := s.conn.Stream(streamID)
if err != nil {
return 0, err
}
return st.Write(b)
}
// StreamRead gets data received from the stream buffer.
func (s *Conn) StreamRead(streamID uint64, b []byte) (int, error) {
st, err := s.conn.Stream(streamID)
if err != nil {
return 0, err
}
return st.Read(b)
}
// StreamWriteTo writes stream data to w until there is no more data or when an error occurs.
func (s *Conn) StreamWriteTo(streamID uint64, w io.Writer) (int64, error) {
st, err := s.conn.Stream(streamID)
if err != nil {
return 0, err
}
return st.WriteTo(w)
}
// StreamCloseWrite terminates sending part of the stream.
func (s *Conn) StreamCloseWrite(streamID uint64, errorCode uint64) error {
st, err := s.conn.Stream(streamID)
if err != nil {
return err
}
return st.CloseWrite(errorCode)
}
// StreamCloseRead terminates reading part of the stream.
func (s *Conn) StreamCloseRead(streamID uint64, errorCode uint64) error {
st, err := s.conn.Stream(streamID)
if err != nil {
return err
}
return st.CloseRead(errorCode)
}
// StreamClose gracefully closes sending part of the stream.
func (s *Conn) StreamClose(streamID uint64) error {
st, err := s.conn.Stream(streamID)
if err != nil {
return err
}
return st.Close()
}
// Stream creates or returns an existing QUIC stream given the ID.
// NOTE: The returned stream would only be used in a different goroutine.
// See Stream struct for details.
func (s *Conn) Stream(streamID uint64) (*Stream, error) {
if st := s.streams[streamID]; st != nil {
return st, nil
}
_, err := s.conn.Stream(streamID)
if err != nil {
return nil, err
}
st := newStream(s, streamID)
s.streams[streamID] = st
return st, nil
}
// NewStream creates and returns a new local stream id.
// If number of streams exceeds peer limits, the function will return false.
func (s *Conn) NewStream(bidi bool) (uint64, bool) {
var id uint64
if bidi {
id = s.nextStreamIDBidi
} else {
id = s.nextStreamIDUni
}
_, err := s.conn.Stream(id)
if err != nil {
return 0, false
}
if bidi {
s.nextStreamIDBidi += 4
} else {
s.nextStreamIDUni += 4
}
return id, true
}
// DatagramWrite queues data to the connection buffer for sending via datagram.
func (s *Conn) DatagramWrite(b []byte) (int, error) {
return s.conn.Datagram().Write(b)
}
// DatagramRead pulls received datagram directly from the connection buffer.
// It returns nil when there is no data to read.
func (s *Conn) DatagramRead(b []byte) (int, error) {
return s.conn.Datagram().Read(b)
}
// Datagram returns a Datagram associated with the connection.
// NOTE: Unlike other Conn.Datagram* functions, the returned Datagram must only be used
// in a different goroutine (i.e. not in the connection handler).
// See Datagram struct for details.
func (s *Conn) Datagram() *Datagram {
if s.datagram == nil {
s.datagram = newDatagram(s)
}
return s.datagram
}
// LocalAddr returns the local network address.
func (s *Conn) LocalAddr() net.Addr {
return s.addr // TODO: get from socket
}
// RemoteAddr returns the remote network address.
func (s *Conn) RemoteAddr() net.Addr {
return s.addr
}
// SetUserData attaches (or removes) user data to the connection.
func (s *Conn) SetUserData(data interface{}) {
s.userData = data
}
// UserData returns attached data.
func (s *Conn) UserData() interface{} {
return s.userData
}
// Close sets the connection status to closing state.
// If peer has already initiated closing with an error, this function will return
// that error, which is either transport.Error or transport.AppError
func (s *Conn) Close() error {
return s.conn.Close(transport.NoError, "bye", false)
}
// CloseWithError sets the connection to closing state with an
// application code code and reason sending to peer.
// The function returns error if peer has already initiated closing.
func (s *Conn) CloseWithError(code uint64, reason string) error {
return s.conn.Close(code, reason, true)
}
func (s *Conn) setClosing(errCode uint64, reason string) {
s.conn.Close(errCode, reason, false)
}
func (s *Conn) onClosed(err error) {
// When connection closed and there are still data not read by Datagram or Stream
// because of async
if s.datagram != nil {
s.datagram.setClosed(err, s.conn.Datagram())
}
for id, st := range s.streams {
t, _ := s.conn.Stream(id)
st.setClosed(err, t)
}
}
func (s *Conn) readEvents() {
s.events = s.conn.Events(s.events)
}
// handleEvents handles transport connection events.
func (s *Conn) handleEvents() {
for _, e := range s.events {
switch e.Type {
case transport.EventStreamWritable:
s.cmdStreamWrite(e.Data)
case transport.EventStreamReadable:
s.cmdStreamRead(e.Data)
case transport.EventStreamClosed:
s.eventStreamClosed(e.Data)
case transport.EventDatagramWritable:
s.cmdDatagramWrite()
case transport.EventDatagramReadable:
s.cmdDatagramRead()
}
}
s.events = s.events[:0]
}
// handleCommand handles commands sent by Stream or Datagram.
func (s *Conn) handleCommand(p *connCommand) {
switch p.cmd {
case cmdStreamWrite:
s.cmdStreamWrite(p.id)
case cmdStreamRead:
s.cmdStreamRead(p.id)
case cmdStreamClose:
s.cmdStreamClose(p.id)
case cmdStreamCloseWrite:
s.cmdStreamCloseWrite(p.id, p.n)
case cmdStreamCloseRead:
s.cmdStreamCloseRead(p.id, p.n)
case cmdDatagramWrite:
s.cmdDatagramWrite()
case cmdDatagramRead:
s.cmdDatagramRead()
}
}
// cmdStreamWrite handles command to write data to a stream for sending.
func (s *Conn) cmdStreamWrite(streamID uint64) {
ss := s.streams[streamID]
if ss == nil {
return
}
st, _ := s.conn.Stream(streamID)
if st != nil {
ss.sendWriter(st)
}
}
// cmdStreamRead handles command to read data from a stream and send back to the Stream caller.
func (s *Conn) cmdStreamRead(streamID uint64) {
ss := s.streams[streamID]
if ss == nil {
return
}
st, _ := s.conn.Stream(streamID)
if st != nil {
ss.sendReader(st)
}
}
// cmdStreamClose handles command to gracefully close a connection stream.
// It sends closing result to the Stream closing goroutine.
func (s *Conn) cmdStreamClose(streamID uint64) {
ss := s.streams[streamID]
if ss == nil {
return
}
st, err := s.conn.Stream(streamID)
if err != nil {
ss.sendCloseResult(err)
return
}
err = st.Close()
ss.sendCloseResult(err)
}
// cmdStreamCloseWrite handles command to close sending part of the stream.
func (s *Conn) cmdStreamCloseWrite(streamID uint64, errorCode uint64) {
ss := s.streams[streamID]
if ss == nil {
return
}
st, err := s.conn.Stream(streamID)
if err != nil {
ss.sendCloseResult(err)
return
}
err = st.CloseWrite(errorCode)
ss.sendCloseResult(err)
}
// cmdStreamCloseRead handles command to close receiving part of the stream.
func (s *Conn) cmdStreamCloseRead(streamID uint64, errorCode uint64) {
ss := s.streams[streamID]
if ss == nil {
return
}
st, err := s.conn.Stream(streamID)
if err != nil {
ss.sendCloseResult(err)
return
}
err = st.CloseRead(errorCode)
ss.sendCloseResult(err)
}
// cmdDatagramWrite handles command to send a datagram.
func (s *Conn) cmdDatagramWrite() {
if s.datagram == nil {
return
}
s.datagram.sendWriter(s.conn.Datagram())
}
// cmdDatagramRead handles command to receive a datagram if available.
func (s *Conn) cmdDatagramRead() {
if s.datagram == nil {
return
}
s.datagram.sendReader(s.conn.Datagram())
}
func (s *Conn) eventStreamClosed(streamID uint64) {
// Stream has been closed gracefully.
ss := s.streams[streamID]
if ss != nil {
// We do not expect application to read this error as stream is only closed
// when data is fully read by the application.
ss.setClosed(io.ErrClosedPipe, nil)
delete(s.streams, streamID)
}
}
// setTimeoutTimer set timer with the duration from connection receive timeout.
func (s *Conn) setTimeoutTimer() {
timeout := s.conn.Timeout()
if timeout < 0 {
// TODO
timeout = 10 * time.Second
}
if s.timeoutTimer == nil {
s.timeoutTimer = time.NewTimer(timeout)
} else {
s.timeoutTimer.Reset(timeout)
}
}
func (s *Conn) stopTimeoutTimer() {
s.timeoutTimer.Stop()
}
func (s *Conn) queuePacket(p *packet) {
if s.queuedPackets != nil {
freePacket(s.queuedPackets)
}
s.queuedPackets = p
}
func (s *Conn) dequeuePacket() *packet {
if s.queuedPackets == nil {
return nil
}
p := s.queuedPackets
s.queuedPackets = nil
return p
}
// localConn is a local QUIC connection, either Client or Server.
type localConn struct {
config *transport.Config
socket net.PacketConn
// peers include all active connections.
peersMu sync.RWMutex
peers map[string]*Conn // by cid and attempt key
closing bool // locked by peersMu.
closeCond sync.Cond // locked by peersMu. Closing a connection will broadcast when connections is empty
closeCh chan struct{}
handler Handler
cidIss CIDIssuer
logger logger
}
func (s *localConn) init(config *transport.Config) {
s.config = config
s.peers = make(map[string]*Conn)
s.closeCh = make(chan struct{})
s.closeCond.L = &s.peersMu
s.handler = noopHandler{}
s.cidIss = newCIDIssuer(config)
}
// SetHandler sets QUIC connection callbacks.
func (s *localConn) SetHandler(v Handler) {
s.handler = v
}
// SetLogger sets transaction logger.
// It is safe to change connection logger at any time.
func (s *localConn) SetLogger(level int, w io.Writer) {
s.logger.setLevel(logLevel(level))
s.logger.setWriter(w)
}
// SetListener sets listening socket connection.
func (s *localConn) SetListener(conn net.PacketConn) {
s.socket = conn
}
// SetCIDIssuer sets generator for connection ids.
// By default, it generates random IDs from Reader in crypto/rand.
// If transport.Config.TLS.Rand is available, it will use that source instead.
func (s *localConn) SetCIDIssuer(cidIss CIDIssuer) {
s.cidIss = cidIss
}
// LocalAddr returns the local network address.
func (s *localConn) LocalAddr() net.Addr {
if s.socket == nil {
return nil
}
return s.socket.LocalAddr()
}
// handleConn handles data sending to the connection c.
// Each connection is run in its own goroutine.
func (s *localConn) handleConn(c *Conn) {
defer s.connClosed(c)
established := false
for !c.conn.IsClosed() {
s.connPoll(c)
if established {
s.connServe(c)
} else {
// First time state switched to active
if c.conn.HandshakeComplete() {
// Handshake done, remove the attempt key
if c.attemptKey != nil {
s.peersMu.Lock()
delete(s.peers, string(c.attemptKey))
c.attemptKey = nil
s.peersMu.Unlock()
}
established = true
s.connServe(c)
}
}
err := s.connSend(c)
if err != nil && established {
s.connServe(c)
}
}
}
func (s *localConn) connPoll(c *Conn) {
c.setTimeoutTimer()
defer c.stopTimeoutTimer()
select {
case p := <-c.recvCh:
// Got packet
err := s.connRecv(c, p)
if err != nil {
return
}
case p := <-c.cmdCh:
c.handleCommand(&p)
case <-c.timeoutTimer.C:
// Read timeout
s.logger.log(levelTrace, zs("", "generic:verbose"),
zx("cid", c.scid), zs("message", "read_timed_out"))
c.conn.Write(nil)
return
case <-s.closeCh:
// Server is closing (see s.close)
c.Close()
return
}
// Maybe another packets arrived too while we processed the first one.
s.connPollNoDelay(c, 3)
}
func (s *localConn) connPollNoDelay(c *Conn, count int) int {
packets := 0
for ; count > 0; count-- {
select {
case p := <-c.recvCh:
err := s.connRecv(c, p)
if err != nil {
return -1
}
packets++
case p := <-c.cmdCh:
c.handleCommand(&p)
case <-s.closeCh:
c.Close()
return -1
default:
}
}
return packets
}
func (s *localConn) connRecv(c *Conn, p *packet) error {
_, err := c.conn.Write(p.data)
if err != nil {
if trigger := transport.IsPacketDropped(err); trigger != "" {
// Queue packet for later processing.
if trigger == keyUnavailableTrigger {
c.queuePacket(p)
s.logger.log(levelDebug, zs("", "transport:packet_buffered"),
zx("cid", c.scid), zv("", &p.header))
} else {
freePacket(p)
}
return nil
}
// Close connection when receive failed
s.logger.log(levelError, zs("", "generic:error"),
zx("cid", c.scid), zs("message", "receive_failed"), ze("", err))
if err, ok := err.(*transport.Error); ok {
c.setClosing(err.Code, err.Reason)
} else {
c.setClosing(transport.InternalError, "")
}
freePacket(p)
return err
}
freePacket(p)
// Re process previous undecryptable packets.
s.connDeq(c)
return nil
}
func (s *localConn) connDeq(c *Conn) {
p := c.dequeuePacket()
if p == nil {
return
}
s.logger.log(levelDebug, zs("", "transport:packet_restored"),
zx("cid", c.scid), zv("", &p.header))
_, err := c.conn.Write(p.data)
if err != nil {
if trigger := transport.IsPacketDropped(err); trigger != "" {
if trigger == keyUnavailableTrigger {
c.queuePacket(p)
s.logger.log(levelDebug, zs("", "transport:packet_buffered"),
zx("cid", c.scid), zv("", &p.header))
} else {
freePacket(p)
}
return
}
// Close connection when receive failed
s.logger.log(levelError, zs("", "generic:error"),
zx("cid", c.scid), zs("message", "receive_failed"), ze("", err))
if err, ok := err.(*transport.Error); ok {
c.setClosing(err.Code, err.Reason)
} else {
c.setClosing(transport.InternalError, "")
}
}
freePacket(p)
}
// connSend returns additional received packets when waiting.
func (s *localConn) connSend(c *Conn) error {
p := newPacket()
defer freePacket(p)
for {
n, err := c.conn.Read(p.buf[:])
if err != nil {
// Close connection when send failed
s.logger.log(levelError, zs("", "generic:error"),
zx("cid", c.scid), zs("message", "send_failed"), ze("", err))
if err, ok := err.(*transport.Error); ok {
c.setClosing(err.Code, err.Reason)
} else {
c.setClosing(transport.InternalError, "")
}
return err
}
if n == 0 {
s.logger.log(levelTrace, zs("", "generic:verbose"),
zx("cid", c.scid), zs("message", "send_done"))
return nil
}
delay := c.conn.Delay()
if delay > 0 {
// Process incoming packets or commands while waiting until the time for sending this packet.
s.connPollNoDelay(c, 1+int(delay/time.Millisecond))
}
p.data = p.buf[:n]
n, err = s.socket.WriteTo(p.data, c.addr)
if err != nil {
s.logger.log(levelError, zs("", "generic:error"),
zx("cid", c.scid), zv("addr", c.addr), zs("message", "send_failed"), ze("", err))
c.setClosing(transport.InternalError, "")
return err
}
s.logger.log(levelTrace, zs("", "transport:datagrams_sent"),
zx("cid", c.scid), zv("addr", c.addr), zx("raw", p.data))
}
}
func (s *localConn) connServe(c *Conn) {
c.readEvents()
if len(c.events) > 0 {
s.logger.log(levelDebug, zs("", "generic:debug"),
zx("cid", c.scid), zs("message", "events"), zi("", len(c.events)))
s.handler.Serve(c, c.events)
c.handleEvents()
}
}
func (s *localConn) connClosed(c *Conn) {
// Use peer error if presents
state := c.ConnectionState()
err := state.PeerError
if err == nil {
err = state.LocalError
}
s.connServe(c)
if err == nil {
s.logger.log(levelInfo, zs("", "connectivity:connection_closed"),
zx("cid", c.scid), zv("addr", c.addr))
err = net.ErrClosed // Async streams will get this error.
} else {
s.logger.log(levelError, zs("", "connectivity:connection_closed"),
zx("cid", c.scid), zv("addr", c.addr), ze("message", err))
}
c.onClosed(err) // This is called after callback to cleanup any resources the application created.
s.peersMu.Lock()
delete(s.peers, string(c.scid))
if c.attemptKey != nil {
delete(s.peers, string(c.attemptKey))
c.attemptKey = nil
}
// If server is closing and this is the last one, tell others
if s.closing && len(s.peers) == 0 {
s.closeCond.Broadcast()
}
s.peersMu.Unlock()
}
// close closes receiving packet channel of all connections to signal terminating handleConn goroutines.
func (s *localConn) close(timeout time.Duration) {
s.peersMu.Lock()
if s.closing {
// Already closing
s.peersMu.Unlock()
return
}
s.closing = true
close(s.closeCh) // This should ask all connections to close
s.peersMu.Unlock()
if timeout > 0 {
// Can not use WaitGroup since we want to use closing timeout (and possible context.Context)
timer := time.AfterFunc(timeout, func() {
s.peersMu.Lock()
s.closeCond.Broadcast()
s.peersMu.Unlock()
})
defer timer.Stop()
s.peersMu.Lock()
if len(s.peers) > 0 {
s.closeCond.Wait()
}
s.peersMu.Unlock()
}
}
type command uint8
const (
cmdStreamWrite command = iota
cmdStreamRead
cmdStreamClose
cmdStreamCloseWrite
cmdStreamCloseRead
cmdDatagramWrite
cmdDatagramRead
)
type connCommand struct {
id uint64 // stream id
n uint64
cmd command
}
type packet struct {
data []byte // Always points to buf
addr net.Addr
udpAddr net.UDPAddr
header transport.Header
buf [bufferSize]byte
}
var packetPool = sync.Pool{
New: func() interface{} {
return &packet{}
},
}
func newPacket() *packet {
return packetPool.Get().(*packet)
}
func freePacket(p *packet) {
p.data = nil
p.addr = nil
p.udpAddr = net.UDPAddr{}
p.header = transport.Header{}
packetPool.Put(p)
}
func readPacket(p *packet, conn net.PacketConn) error {
if udpConn, ok := conn.(*net.UDPConn); ok {
// Use UDP directly to reduce memory allocations
n, addr, err := udpConn.ReadFromUDP(p.buf[:])
if err != nil {
return err
}
p.data = p.buf[:n]
p.udpAddr = *addr
p.addr = &p.udpAddr
} else {
n, addr, err := conn.ReadFrom(p.buf[:])
if err != nil {
return err
}
p.data = p.buf[:n]
p.addr = addr
}
return nil
}
// CIDIssuer generates connection ID.
type CIDIssuer interface {
// NewCID generates a new connection ID.
NewCID() ([]byte, error)
// CIDLength returns the length of generated connection id which is needed
// to decode short-header packets.
// Currently, only constant length is supported.
CIDLength() int
}
type cidIssuer struct {
reader io.Reader
}
func newCIDIssuer(config *transport.Config) *cidIssuer {
reader := rand.Reader
if config.TLS != nil && config.TLS.Rand != nil {
reader = config.TLS.Rand
}
return &cidIssuer{
reader: reader,
}
}
func (s *cidIssuer) NewCID() ([]byte, error) {
cid := make([]byte, cidLength)
_, err := io.ReadFull(s.reader, cid)
return cid, err
}
func (s *cidIssuer) CIDLength() int {
return cidLength
}