5
5
"encoding"
6
6
"errors"
7
7
"fmt"
8
+ "github.com/sandertv/go-raknet/internal"
8
9
"github.com/sandertv/go-raknet/internal/message"
9
10
"io"
10
11
"net"
@@ -79,7 +80,7 @@ type Conn struct {
79
80
packetQueue * packetQueue
80
81
// packets is a channel containing content of packets that were fully
81
82
// processed. Calling Conn.Read() consumes a value from this channel.
82
- packets chan * [ ]byte
83
+ packets * internal. ElasticChan [[ ]byte ]
83
84
84
85
// retransmission is a queue filled with packets that were sent with a given
85
86
// datagram sequence number.
@@ -100,7 +101,7 @@ func newConn(conn net.PacketConn, raddr net.Addr, mtu uint16, h connectionHandle
100
101
pk : new (packet ),
101
102
closed : make (chan struct {}),
102
103
connected : make (chan struct {}),
103
- packets : make ( chan * [ ]byte , 512 ),
104
+ packets : internal. Chan [[ ]byte ]( 4 ),
104
105
splits : make (map [uint16 ][][]byte ),
105
106
win : newDatagramWindow (),
106
107
packetQueue : newPacketQueue (),
@@ -273,27 +274,24 @@ func (conn *Conn) write(b []byte) (n int, err error) {
273
274
// Read blocks until a packet is received over the connection, or until the
274
275
// session is closed or the read times out, in which case an error is returned.
275
276
func (conn * Conn ) Read (b []byte ) (n int , err error ) {
276
- select {
277
- case pk := <- conn .packets :
278
- if len (b ) < len (* pk ) {
279
- err = conn .error (ErrBufferTooSmall , "read" )
280
- }
281
- return copy (b , * pk ), err
282
- case <- conn .closed :
277
+ pk , ok := conn .packets .Recv (conn .closed )
278
+ if ! ok {
283
279
return 0 , conn .error (net .ErrClosed , "read" )
280
+ } else if len (b ) < len (pk ) {
281
+ return 0 , conn .error (ErrBufferTooSmall , "read" )
284
282
}
283
+ return copy (b , pk ), err
285
284
}
286
285
287
286
// ReadPacket attempts to read the next packet as a byte slice. ReadPacket
288
287
// blocks until a packet is received over the connection, or until the session
289
288
// is closed or the read times out, in which case an error is returned.
290
289
func (conn * Conn ) ReadPacket () (b []byte , err error ) {
291
- select {
292
- case pk := <- conn .packets :
293
- return * pk , err
294
- case <- conn .closed :
290
+ pk , ok := conn .packets .Recv (conn .closed )
291
+ if ! ok {
295
292
return nil , conn .error (net .ErrClosed , "read" )
296
293
}
294
+ return pk , err
297
295
}
298
296
299
297
// Close closes the connection. All blocking Read or Write actions are
@@ -377,19 +375,19 @@ func (conn *Conn) receiveDatagram(b []byte) error {
377
375
return fmt .Errorf ("read datagram: %w" , io .ErrUnexpectedEOF )
378
376
}
379
377
seq := loadUint24 (b )
380
- conn .ackMu .Lock ()
381
- // Add this sequence number to the received datagrams, so that it is
382
- // included in an ACK.
383
- conn .ackSlice = append (conn .ackSlice , seq )
384
- conn .ackMu .Unlock ()
385
-
386
378
if ! conn .win .add (seq ) {
387
379
// Datagram was already received, this might happen if a packet took a
388
380
// long time to arrive, and we already sent a NACK for it. This is
389
381
// expected to happen sometimes under normal circumstances, so no reason
390
382
// to return an error.
391
383
return nil
392
384
}
385
+ conn .ackMu .Lock ()
386
+ // Add this sequence number to the received datagrams, so that it is
387
+ // included in an ACK.
388
+ conn .ackSlice = append (conn .ackSlice , seq )
389
+ conn .ackMu .Unlock ()
390
+
393
391
if conn .win .shift () == 0 {
394
392
// Datagram window couldn't be shifted up, so we're still missing
395
393
// packets.
@@ -463,13 +461,7 @@ func (conn *Conn) handlePacket(b []byte) error {
463
461
return fmt .Errorf ("handle packet: %w" , err )
464
462
}
465
463
if ! handled {
466
- // Insert the packet contents the packet queue could release in the
467
- // channel so that Conn.Read() can get a hold of them, but always first
468
- // try to escape if the connection was closed.
469
- select {
470
- case <- conn .closed :
471
- case conn .packets <- & b :
472
- }
464
+ conn .packets .Send (b )
473
465
}
474
466
return nil
475
467
}
0 commit comments