gosh-dl 0.4.0

A fast, embeddable download engine for Rust. HTTP/HTTPS with multi-connection acceleration and full BitTorrent protocol support.
Documentation
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
//! uTP Socket Implementation
//!
//! This module implements a single uTP connection with reliable,
//! ordered delivery over UDP.

use std::collections::{BTreeMap, VecDeque};
use std::io;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};

use tokio::sync::{mpsc, Mutex};
use tokio::time::timeout;

use super::congestion::LedbatController;
use super::packet::{timestamp_us, Packet, PacketType, SelectiveAck, MAX_PAYLOAD_SIZE};
use super::state::{ConnectionState, ConnectionStats, PendingPacket};

/// Maximum number of retransmissions before giving up
const MAX_RETRANSMITS: u32 = 10;

/// Receive buffer size
const RECV_BUFFER_SIZE: usize = 1024 * 1024; // 1MB

/// Maximum out-of-order packets to buffer
const MAX_OOO_PACKETS: usize = 256;

/// uTP socket configuration
#[derive(Debug, Clone)]
pub struct UtpConfig {
    /// Enable selective ACK extension
    pub enable_sack: bool,

    /// Maximum window size (bytes)
    pub max_window_size: u32,

    /// Initial receive window (bytes)
    pub recv_window: u32,
}

impl Default for UtpConfig {
    fn default() -> Self {
        Self {
            enable_sack: true,
            max_window_size: 1024 * 1024,
            recv_window: 1024 * 1024,
        }
    }
}

/// Channel for sending packets to the multiplexer
pub type PacketSender = mpsc::Sender<(Vec<u8>, SocketAddr)>;

/// Channel for receiving packets from the multiplexer
pub type PacketReceiver = mpsc::Receiver<Packet>;

/// Internal state for a uTP socket
pub struct UtpSocketInner {
    /// Remote peer address
    pub remote_addr: SocketAddr,

    /// Connection ID for sending (remote expects this)
    pub send_conn_id: u16,

    /// Connection ID for receiving (we expect this)
    pub recv_conn_id: u16,

    /// Current connection state
    pub state: ConnectionState,

    /// Our sequence number (next to send)
    pub seq_nr: u16,

    /// Their sequence number (next expected)
    pub ack_nr: u16,

    /// Last ACK we sent
    pub last_ack_sent: u16,

    /// Congestion controller
    pub congestion: LedbatController,

    /// Packets awaiting acknowledgment
    pub pending_packets: BTreeMap<u16, PendingPacket>,

    /// Out-of-order received packets
    pub ooo_packets: BTreeMap<u16, Vec<u8>>,

    /// Receive buffer (ordered data ready for reading)
    pub recv_buffer: VecDeque<u8>,

    /// Receive window size we advertise
    pub recv_window: u32,

    /// Remote's advertised window size
    pub remote_window: u32,

    /// Time of last received packet
    pub last_recv_time: Instant,

    /// Time of last sent packet
    pub last_send_time: Instant,

    /// Connection statistics
    pub stats: ConnectionStats,

    /// Configuration
    pub config: UtpConfig,

    /// Channel to send packets
    pub packet_tx: PacketSender,

    /// FIN received from peer
    pub fin_received: bool,

    /// FIN sent to peer
    pub fin_sent: bool,
}

impl UtpSocketInner {
    /// Create a new socket for an outgoing connection
    pub fn new_outgoing(
        remote_addr: SocketAddr,
        conn_id: u16,
        packet_tx: PacketSender,
        config: UtpConfig,
    ) -> Self {
        let now = Instant::now();
        Self {
            remote_addr,
            send_conn_id: conn_id,
            recv_conn_id: conn_id.wrapping_add(1),
            state: ConnectionState::Idle,
            seq_nr: 1,
            ack_nr: 0,
            last_ack_sent: 0,
            congestion: LedbatController::new(),
            pending_packets: BTreeMap::new(),
            ooo_packets: BTreeMap::new(),
            recv_buffer: VecDeque::with_capacity(RECV_BUFFER_SIZE),
            recv_window: config.recv_window,
            remote_window: 0,
            last_recv_time: now,
            last_send_time: now,
            stats: ConnectionStats::new(),
            config,
            packet_tx,
            fin_received: false,
            fin_sent: false,
        }
    }

    /// Create a new socket for an incoming connection
    pub fn new_incoming(
        remote_addr: SocketAddr,
        conn_id: u16,
        peer_seq_nr: u16,
        packet_tx: PacketSender,
        config: UtpConfig,
    ) -> Self {
        let now = Instant::now();
        Self {
            remote_addr,
            send_conn_id: conn_id,
            recv_conn_id: conn_id.wrapping_add(1),
            state: ConnectionState::SynRecv,
            seq_nr: 1,
            ack_nr: peer_seq_nr,
            last_ack_sent: 0,
            congestion: LedbatController::new(),
            pending_packets: BTreeMap::new(),
            ooo_packets: BTreeMap::new(),
            recv_buffer: VecDeque::with_capacity(RECV_BUFFER_SIZE),
            recv_window: config.recv_window,
            remote_window: 0,
            last_recv_time: now,
            last_send_time: now,
            stats: ConnectionStats::new(),
            config,
            packet_tx,
            fin_received: false,
            fin_sent: false,
        }
    }

    /// Start the connection (send SYN)
    pub async fn connect(&mut self) -> io::Result<()> {
        if self.state != ConnectionState::Idle {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "Connection already started",
            ));
        }

        self.state = ConnectionState::SynSent;
        let pkt = self.build_packet(PacketType::Syn, Vec::new());
        self.send_packet(pkt).await
    }

    /// Accept an incoming connection (send SYN-ACK)
    pub async fn accept(&mut self) -> io::Result<()> {
        if self.state != ConnectionState::SynRecv {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "No incoming connection to accept",
            ));
        }

        let pkt = self.build_packet(PacketType::State, Vec::new());
        self.send_packet(pkt).await?;
        self.state = ConnectionState::Connected;
        Ok(())
    }

    /// Process a received packet
    pub async fn process_packet(&mut self, pkt: Packet) -> io::Result<()> {
        self.last_recv_time = Instant::now();
        self.remote_window = pkt.wnd_size;
        self.stats.packets_received += 1;

        // Update congestion control with delay sample
        if pkt.timestamp_diff_us > 0 {
            // Calculate RTT if we can correlate
            let _rtt_sample = if !self.pending_packets.is_empty() {
                Some(pkt.timestamp_diff_us)
            } else {
                None
            };

            // Any packet with timestamp_diff gives us delay info
            // (This is their measurement of our delay)
        }

        match self.state {
            ConnectionState::SynSent => {
                // Expecting SYN-ACK (STATE packet with our seq_nr acked)
                if pkt.is_state() {
                    self.ack_nr = pkt.seq_nr;
                    self.process_acks(pkt.ack_nr, pkt.selective_ack.as_ref());
                    self.state = ConnectionState::Connected;
                } else if pkt.is_reset() {
                    self.state = ConnectionState::Reset;
                }
            }

            ConnectionState::Connected | ConnectionState::FinSent => {
                if pkt.is_reset() {
                    self.state = ConnectionState::Reset;
                    return Ok(());
                }

                if pkt.is_fin() {
                    self.fin_received = true;
                    self.ack_nr = pkt.seq_nr;
                    // Send ACK for FIN
                    let ack = self.build_packet(PacketType::State, Vec::new());
                    self.send_packet_direct(ack).await?;

                    if self.fin_sent {
                        self.state = ConnectionState::Closed;
                    } else {
                        self.state = ConnectionState::Closing;
                    }
                }

                // Process ACKs
                self.process_acks(pkt.ack_nr, pkt.selective_ack.as_ref());

                // Process data
                if pkt.is_data() && !pkt.payload.is_empty() {
                    self.receive_data(pkt.seq_nr, pkt.payload)?;
                }
            }

            ConnectionState::Closing => {
                if pkt.is_state() {
                    self.process_acks(pkt.ack_nr, pkt.selective_ack.as_ref());
                    if self.pending_packets.is_empty() {
                        self.state = ConnectionState::Closed;
                    }
                }
            }

            _ => {}
        }

        Ok(())
    }

    /// Process acknowledgments
    fn process_acks(&mut self, ack_nr: u16, sack: Option<&SelectiveAck>) {
        // Remove all packets up to and including ack_nr
        let to_remove: Vec<u16> = self
            .pending_packets
            .keys()
            .copied()
            .filter(|&seq| self.seq_before_eq(seq, ack_nr))
            .collect();

        for seq in to_remove {
            if let Some(pkt) = self.pending_packets.remove(&seq) {
                let rtt = pkt.first_sent.elapsed().as_micros() as u32;
                self.congestion.on_ack(pkt.size, 0, Some(rtt));
            }
        }

        // Process selective ACKs
        if let Some(sack) = sack {
            // SACK bitmap starts at ack_nr + 2
            for i in 0..sack.bitmask.len() * 8 {
                if sack.is_acked(i as u16) {
                    let seq = ack_nr.wrapping_add(2).wrapping_add(i as u16);
                    if let Some(pkt) = self.pending_packets.remove(&seq) {
                        self.congestion.on_ack(pkt.size, 0, None);
                    }
                }
            }
        }
    }

    /// Receive data into buffer, handling out-of-order
    fn receive_data(&mut self, seq_nr: u16, payload: Vec<u8>) -> io::Result<()> {
        let expected = self.ack_nr.wrapping_add(1);

        if seq_nr == expected {
            // In-order packet
            self.recv_buffer.extend(&payload);
            self.ack_nr = seq_nr;
            self.stats.bytes_received += payload.len() as u64;

            // Deliver any buffered out-of-order packets
            loop {
                let next = self.ack_nr.wrapping_add(1);
                if let Some(data) = self.ooo_packets.remove(&next) {
                    self.recv_buffer.extend(&data);
                    self.ack_nr = next;
                    self.stats.bytes_received += data.len() as u64;
                } else {
                    break;
                }
            }
        } else if self.seq_after(seq_nr, expected) && self.ooo_packets.len() < MAX_OOO_PACKETS {
            // Out-of-order packet - buffer it
            self.ooo_packets.insert(seq_nr, payload);
        }
        // Else: duplicate or too old, ignore

        Ok(())
    }

    /// Send data (returns amount actually queued)
    pub async fn send_data(&mut self, data: &[u8]) -> io::Result<usize> {
        if !self.state.can_send_data() {
            return Err(io::Error::new(
                io::ErrorKind::NotConnected,
                "Cannot send in current state",
            ));
        }

        let mut sent = 0;

        // Check if we can send based on congestion window
        while sent < data.len() && self.congestion.can_send() {
            let end = (sent + MAX_PAYLOAD_SIZE).min(data.len());
            let chunk = data[sent..end].to_vec();

            let pkt = self.build_packet(PacketType::Data, chunk);
            self.send_packet(pkt).await?;
            sent = end;
        }

        Ok(sent)
    }

    /// Read data from receive buffer
    pub fn read_data(&mut self, buf: &mut [u8]) -> usize {
        let len = buf.len().min(self.recv_buffer.len());
        for (i, byte) in self.recv_buffer.drain(..len).enumerate() {
            buf[i] = byte;
        }
        len
    }

    /// Send a FIN to close the connection
    pub async fn close(&mut self) -> io::Result<()> {
        if self.state == ConnectionState::Connected {
            self.fin_sent = true;
            let pkt = self.build_packet(PacketType::Fin, Vec::new());
            self.send_packet(pkt).await?;
            self.state = ConnectionState::FinSent;
        }
        Ok(())
    }

    /// Build a packet with current state
    fn build_packet(&mut self, pkt_type: PacketType, payload: Vec<u8>) -> Packet {
        let conn_id = if pkt_type == PacketType::Syn {
            self.recv_conn_id
        } else {
            self.send_conn_id
        };

        let seq_nr = self.seq_nr;
        if pkt_type == PacketType::Data
            || pkt_type == PacketType::Syn
            || pkt_type == PacketType::Fin
        {
            self.seq_nr = self.seq_nr.wrapping_add(1);
        }

        let mut pkt = Packet::new(pkt_type, conn_id, seq_nr, self.ack_nr)
            .with_timestamps(timestamp_us(), 0)
            .with_window(self.available_recv_window());

        // Add selective ACK if we have out-of-order packets
        if self.config.enable_sack && !self.ooo_packets.is_empty() && pkt_type == PacketType::State
        {
            let mut sack = SelectiveAck::default();
            for &seq in self.ooo_packets.keys() {
                let offset = seq.wrapping_sub(self.ack_nr).wrapping_sub(2);
                if offset < 256 {
                    sack.set_acked(offset);
                }
            }
            pkt = pkt.with_selective_ack(sack);
        }

        pkt.payload = payload;
        pkt
    }

    /// Send a packet and track it for retransmission
    async fn send_packet(&mut self, pkt: Packet) -> io::Result<()> {
        let data = pkt.encode();
        let payload = pkt.payload.clone();
        let seq_nr = pkt.seq_nr;
        let is_data = pkt.is_data() || pkt.is_syn() || pkt.is_fin();

        self.send_packet_direct(pkt).await?;

        // Track for retransmission if it's a data-bearing packet
        if is_data {
            let pending = PendingPacket::new(seq_nr, data.clone(), payload);
            self.congestion.on_send(pending.size);
            self.pending_packets.insert(seq_nr, pending);
        }

        Ok(())
    }

    /// Send a packet without tracking
    async fn send_packet_direct(&mut self, pkt: Packet) -> io::Result<()> {
        let data = pkt.encode();

        self.packet_tx
            .send((data, self.remote_addr))
            .await
            .map_err(|_| io::Error::new(io::ErrorKind::ConnectionReset, "Send channel closed"))?;

        self.stats.packets_sent += 1;
        if !pkt.payload.is_empty() {
            self.stats.bytes_sent += pkt.payload.len() as u64;
        }
        self.last_send_time = Instant::now();
        self.last_ack_sent = self.ack_nr;

        Ok(())
    }

    /// Check and perform retransmissions
    pub async fn check_retransmits(&mut self) -> io::Result<()> {
        let rto = self.congestion.rto();
        let now = Instant::now();

        let to_retransmit: Vec<u16> = self
            .pending_packets
            .iter()
            .filter(|(_, p)| now.duration_since(p.last_sent) > rto)
            .map(|(seq, _)| *seq)
            .collect();

        for seq in to_retransmit {
            // Extract data we need from pending packet
            let (pkt_seq_nr, payload, _retransmits, max_exceeded) = {
                let pending = match self.pending_packets.get(&seq) {
                    Some(p) => p,
                    None => continue,
                };
                (
                    pending.seq_nr,
                    pending.payload.clone(),
                    pending.retransmits,
                    pending.retransmits >= MAX_RETRANSMITS,
                )
            };

            if max_exceeded {
                self.state = ConnectionState::TimedOut;
                return Err(io::Error::new(
                    io::ErrorKind::TimedOut,
                    "Max retransmits exceeded",
                ));
            }

            // Rebuild packet with current timestamps
            let pkt_type = if pkt_seq_nr == 1 && self.state == ConnectionState::SynSent {
                PacketType::Syn
            } else {
                PacketType::Data
            };

            let recv_window = self.available_recv_window();
            let mut pkt = Packet::new(pkt_type, self.send_conn_id, pkt_seq_nr, self.ack_nr)
                .with_timestamps(timestamp_us(), 0)
                .with_window(recv_window);

            pkt.payload = payload;

            self.send_packet_direct(pkt).await?;

            // Now update the pending packet
            if let Some(pending) = self.pending_packets.get_mut(&seq) {
                pending.mark_retransmit();
            }
            self.stats.retransmits += 1;
            self.congestion.on_loss();
        }

        Ok(())
    }

    /// Send an ACK if needed
    pub async fn maybe_send_ack(&mut self) -> io::Result<()> {
        if self.ack_nr != self.last_ack_sent {
            let pkt = self.build_packet(PacketType::State, Vec::new());
            self.send_packet_direct(pkt).await?;
        }
        Ok(())
    }

    /// Calculate available receive window
    fn available_recv_window(&self) -> u32 {
        let used = self.recv_buffer.len() as u32;
        self.recv_window.saturating_sub(used)
    }

    /// Check if seq_a comes before seq_b (handles wrapping)
    fn seq_before_eq(&self, seq_a: u16, seq_b: u16) -> bool {
        let diff = seq_b.wrapping_sub(seq_a);
        diff == 0 || diff < 32768
    }

    /// Check if seq_a comes after seq_b (handles wrapping)
    fn seq_after(&self, seq_a: u16, seq_b: u16) -> bool {
        let diff = seq_a.wrapping_sub(seq_b);
        diff > 0 && diff < 32768
    }

    /// Get connection state
    pub fn state(&self) -> ConnectionState {
        self.state
    }

    /// Get statistics
    pub fn stats(&self) -> &ConnectionStats {
        &self.stats
    }

    /// Check if there's data available to read
    pub fn has_data(&self) -> bool {
        !self.recv_buffer.is_empty()
    }

    /// Get amount of data available to read
    pub fn available_data(&self) -> usize {
        self.recv_buffer.len()
    }
}

/// High-level uTP socket wrapper
pub struct UtpSocket {
    inner: Arc<Mutex<UtpSocketInner>>,
    packet_rx: Mutex<PacketReceiver>,
}

impl UtpSocket {
    /// Create a new outgoing socket
    pub fn new_outgoing(
        remote_addr: SocketAddr,
        conn_id: u16,
        packet_tx: PacketSender,
        packet_rx: PacketReceiver,
        config: UtpConfig,
    ) -> Self {
        Self {
            inner: Arc::new(Mutex::new(UtpSocketInner::new_outgoing(
                remote_addr,
                conn_id,
                packet_tx,
                config,
            ))),
            packet_rx: Mutex::new(packet_rx),
        }
    }

    /// Create a new incoming socket
    pub fn new_incoming(
        remote_addr: SocketAddr,
        conn_id: u16,
        peer_seq_nr: u16,
        packet_tx: PacketSender,
        packet_rx: PacketReceiver,
        config: UtpConfig,
    ) -> Self {
        Self {
            inner: Arc::new(Mutex::new(UtpSocketInner::new_incoming(
                remote_addr,
                conn_id,
                peer_seq_nr,
                packet_tx,
                config,
            ))),
            packet_rx: Mutex::new(packet_rx),
        }
    }

    /// Connect to remote peer
    pub async fn connect(&self) -> io::Result<()> {
        {
            let mut inner = self.inner.lock().await;
            inner.connect().await?;
        }

        // Wait for SYN-ACK with timeout
        let result = timeout(Duration::from_secs(30), self.wait_connected()).await;
        match result {
            Ok(Ok(())) => Ok(()),
            Ok(Err(e)) => Err(e),
            Err(_) => {
                let mut inner = self.inner.lock().await;
                inner.state = ConnectionState::TimedOut;
                Err(io::Error::new(
                    io::ErrorKind::TimedOut,
                    "Connection timeout",
                ))
            }
        }
    }

    /// Wait for connection to be established
    async fn wait_connected(&self) -> io::Result<()> {
        loop {
            // Process incoming packets
            let pkt = {
                let mut rx = self.packet_rx.lock().await;
                rx.recv().await
            };

            if let Some(pkt) = pkt {
                let mut inner = self.inner.lock().await;
                inner.process_packet(pkt).await?;

                if inner.state == ConnectionState::Connected {
                    return Ok(());
                }
                if inner.state.is_closed() {
                    return Err(io::Error::new(
                        io::ErrorKind::ConnectionRefused,
                        "Connection failed",
                    ));
                }
            } else {
                return Err(io::Error::new(
                    io::ErrorKind::ConnectionReset,
                    "Packet channel closed",
                ));
            }
        }
    }

    /// Accept an incoming connection
    pub async fn accept(&self) -> io::Result<()> {
        let mut inner = self.inner.lock().await;
        inner.accept().await
    }

    /// Read data from the socket
    pub async fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
        loop {
            // Check if we have data
            {
                let mut inner = self.inner.lock().await;
                if inner.has_data() {
                    return Ok(inner.read_data(buf));
                }
                if inner.fin_received && inner.recv_buffer.is_empty() {
                    return Ok(0); // EOF
                }
                if inner.state.is_closed() {
                    return Err(io::Error::new(
                        io::ErrorKind::ConnectionReset,
                        format!("Connection closed: {}", inner.state),
                    ));
                }
            }

            // Wait for more data
            let pkt = {
                let mut rx = self.packet_rx.lock().await;
                rx.recv().await
            };

            if let Some(pkt) = pkt {
                let mut inner = self.inner.lock().await;
                inner.process_packet(pkt).await?;
            } else {
                return Err(io::Error::new(
                    io::ErrorKind::ConnectionReset,
                    "Connection lost",
                ));
            }
        }
    }

    /// Read exactly len bytes
    pub async fn read_exact(&self, buf: &mut [u8]) -> io::Result<()> {
        let mut total = 0;
        while total < buf.len() {
            let n = self.read(&mut buf[total..]).await?;
            if n == 0 {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "Unexpected EOF",
                ));
            }
            total += n;
        }
        Ok(())
    }

    /// Write data to the socket
    pub async fn write_all(&self, data: &[u8]) -> io::Result<()> {
        let mut offset = 0;
        while offset < data.len() {
            let sent = {
                let mut inner = self.inner.lock().await;
                inner.send_data(&data[offset..]).await?
            };
            if sent == 0 {
                // Congestion window full, wait a bit
                tokio::time::sleep(Duration::from_millis(10)).await;
                continue;
            }
            offset += sent;
        }
        Ok(())
    }

    /// Flush (no-op for uTP, data is sent immediately)
    pub async fn flush(&self) -> io::Result<()> {
        Ok(())
    }

    /// Shutdown the socket
    pub async fn shutdown(&self) -> io::Result<()> {
        let mut inner = self.inner.lock().await;
        inner.close().await
    }

    /// Get peer address
    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
        // This is sync-safe since remote_addr doesn't change
        Ok(self
            .inner
            .try_lock()
            .map(|i| i.remote_addr)
            .unwrap_or_else(|_| {
                // Fallback - shouldn't happen in practice
                "0.0.0.0:0".parse().unwrap()
            }))
    }

    /// Get connection state
    pub async fn state(&self) -> ConnectionState {
        self.inner.lock().await.state
    }

    /// Get inner for direct access (used by multiplexer)
    pub fn inner(&self) -> Arc<Mutex<UtpSocketInner>> {
        self.inner.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_seq_comparison() {
        let inner = UtpSocketInner::new_outgoing(
            "127.0.0.1:8080".parse().unwrap(),
            1000,
            mpsc::channel(1).0,
            UtpConfig::default(),
        );

        // Normal case
        assert!(inner.seq_before_eq(10, 20));
        assert!(inner.seq_before_eq(10, 10));
        assert!(!inner.seq_before_eq(20, 10));

        // Wrap around
        assert!(inner.seq_before_eq(65530, 5));
        assert!(!inner.seq_before_eq(5, 65530));
    }
}