aria2-protocol 0.2.3

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
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
//! uTP socket implementation
//!
//! Implements the UDP-based socket for uTP protocol as specified in BEP 29.
//! Manages multiple uTP connections over a single UDP socket.

use std::collections::HashMap;
use std::net::{SocketAddr, UdpSocket};
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::Mutex;

use crate::bittorrent::utp::connection::{ConnectionError, ConnectionState, UtpConnection};
use crate::bittorrent::utp::packet::{PacketType, UtpPacket, UtpPacketError};
use crate::bittorrent::utp::timer::{TimerManager, TimerType};

/// Maximum number of concurrent uTP connections per socket
const MAX_CONNECTIONS: usize = 100;

/// Default timeout for connection establishment
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);

/// Default timeout for idle connections
const DEFAULT_IDLE_TIMEOUT: Duration = Duration::from_secs(30);

/// Default keepalive interval
const DEFAULT_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(15);

/// Maximum receive buffer size for UDP
const MAX_UDP_RECV_BUFFER: usize = 65535;

/// Errors that can occur during socket operations
#[derive(Debug, thiserror::Error)]
pub enum UtpSocketError {
    #[error("Failed to bind UDP socket: {0}")]
    BindFailed(std::io::Error),

    #[error("Failed to send packet: {0}")]
    SendFailed(std::io::Error),

    #[error("Failed to receive packet: {0}")]
    RecvFailed(std::io::Error),

    #[error("Connection error: {0}")]
    ConnectionError(#[from] ConnectionError),

    #[error("Packet error: {0}")]
    PacketError(#[from] UtpPacketError),

    #[error("Maximum connections reached")]
    MaxConnectionsReached,

    #[error("Connection not found: {0}")]
    ConnectionNotFound(u16),

    #[error("Address not found for connection: {0}")]
    AddressNotFound(u16),

    #[error("Socket closed")]
    SocketClosed,

    #[error("Invalid packet from {addr}: {reason}")]
    InvalidPacket { addr: SocketAddr, reason: String },

    #[error("Timeout: {0}")]
    Timeout(String),
}

/// Connection identifier (combination of connection_id and remote address)
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct ConnectionId {
    /// Connection ID from uTP packet
    pub conn_id: u16,
    /// Remote socket address
    pub remote_addr: SocketAddr,
}

impl ConnectionId {
    /// Create a new connection identifier
    pub fn new(conn_id: u16, remote_addr: SocketAddr) -> Self {
        Self {
            conn_id,
            remote_addr,
        }
    }
}

/// uTP socket that manages multiple connections over UDP
///
/// This is the main interface for uTP communication. It wraps a UDP socket
/// and provides connection-oriented semantics with congestion control.
pub struct UtpSocket {
    /// Underlying UDP socket
    socket: UdpSocket,
    /// Active connections indexed by connection ID
    connections: HashMap<u16, UtpConnection>,
    /// Mapping from remote address to connection ID (for incoming packets)
    addr_to_conn: HashMap<SocketAddr, u16>,
    /// Timer management for all connections
    timers: TimerManager,
    /// Local socket address
    local_addr: SocketAddr,
    /// Connection timeout
    connect_timeout: Duration,
    /// Idle timeout for connections
    idle_timeout: Duration,
    /// Keepalive interval
    keepalive_interval: Duration,
    /// Whether the socket is closed
    is_closed: bool,
    /// Receive buffer
    recv_buffer: Vec<u8>,
}

impl UtpSocket {
    /// Create a new uTP socket bound to the specified address
    pub fn bind(addr: &str) -> Result<Self, UtpSocketError> {
        let socket = UdpSocket::bind(addr).map_err(UtpSocketError::BindFailed)?;
        let local_addr = socket.local_addr().map_err(UtpSocketError::BindFailed)?;
        socket
            .set_nonblocking(true)
            .map_err(UtpSocketError::BindFailed)?;

        Ok(Self {
            socket,
            connections: HashMap::new(),
            addr_to_conn: HashMap::new(),
            timers: TimerManager::new(),
            local_addr,
            connect_timeout: DEFAULT_CONNECT_TIMEOUT,
            idle_timeout: DEFAULT_IDLE_TIMEOUT,
            keepalive_interval: DEFAULT_KEEPALIVE_INTERVAL,
            is_closed: false,
            recv_buffer: vec![0u8; MAX_UDP_RECV_BUFFER],
        })
    }

    /// Bind to any available port
    pub fn bind_any() -> Result<Self, UtpSocketError> {
        Self::bind("0.0.0.0:0")
    }

    /// Bind to a specific port
    pub fn bind_port(port: u16) -> Result<Self, UtpSocketError> {
        Self::bind(&format!("0.0.0.0:{}", port))
    }

    /// Get the local address this socket is bound to
    pub fn local_addr(&self) -> SocketAddr {
        self.local_addr
    }

    /// Set connection timeout
    pub fn set_connect_timeout(&mut self, timeout: Duration) {
        self.connect_timeout = timeout;
    }

    /// Set idle timeout for connections
    pub fn set_idle_timeout(&mut self, timeout: Duration) {
        self.idle_timeout = timeout;
    }

    /// Set keepalive interval
    pub fn set_keepalive_interval(&mut self, interval: Duration) {
        self.keepalive_interval = interval;
    }

    /// Check if socket is closed
    pub fn is_closed(&self) -> bool {
        self.is_closed
    }

    /// Get number of active connections
    pub fn connection_count(&self) -> usize {
        self.connections.len()
    }

    /// Initiate a uTP connection to a remote peer
    pub fn connect(&mut self, remote_addr: SocketAddr) -> Result<u16, UtpSocketError> {
        if self.is_closed {
            return Err(UtpSocketError::SocketClosed);
        }

        if self.connections.len() >= MAX_CONNECTIONS {
            return Err(UtpSocketError::MaxConnectionsReached);
        }

        let mut conn = UtpConnection::new();
        let syn_packet = conn.connect(remote_addr)?;
        let conn_id = conn.local_connection_id();

        self.send_packet(&syn_packet, remote_addr)?;

        self.connections.insert(conn_id, conn);
        self.addr_to_conn.insert(remote_addr, conn_id);

        self.timers
            .set_timer(conn_id, TimerType::ConnectTimeout, self.connect_timeout);

        Ok(conn_id)
    }

    /// Send a packet to a remote address
    fn send_packet(&self, packet: &UtpPacket, addr: SocketAddr) -> Result<(), UtpSocketError> {
        let data = packet.to_bytes();
        self.socket
            .send_to(&data, addr)
            .map_err(UtpSocketError::SendFailed)?;
        Ok(())
    }

    /// Send data on an established connection
    pub fn send(&mut self, conn_id: u16, data: &[u8]) -> Result<usize, UtpSocketError> {
        if self.is_closed {
            return Err(UtpSocketError::SocketClosed);
        }

        // Get connection info first
        let (remote_addr, rto) = {
            let conn = self
                .connections
                .get(&conn_id)
                .ok_or(UtpSocketError::ConnectionNotFound(conn_id))?;

            if !conn.is_established() {
                return Err(UtpSocketError::ConnectionError(
                    ConnectionError::NotConnected,
                ));
            }

            let remote_addr = conn
                .remote_addr()
                .ok_or(UtpSocketError::AddressNotFound(conn_id))?;
            (remote_addr, conn.rto())
        };

        // Get packets to send
        let packets = {
            let conn = self
                .connections
                .get_mut(&conn_id)
                .ok_or(UtpSocketError::ConnectionNotFound(conn_id))?;
            conn.send_data(data)?
        };

        // Send all packets
        let mut bytes_sent = 0;
        for packet in &packets {
            bytes_sent += packet.payload.len();
            self.send_packet(packet, remote_addr)?;
            self.timers
                .set_timer(conn_id, TimerType::Retransmit(packet.seq_nr), rto);
        }

        Ok(bytes_sent)
    }

    /// Receive data from a connection
    pub fn recv(&mut self, conn_id: u16, buf: &mut [u8]) -> Result<usize, UtpSocketError> {
        if self.is_closed {
            return Err(UtpSocketError::SocketClosed);
        }

        // Process incoming packets first to potentially receive new data
        self.process_incoming_packets()?;

        // Get data from connection
        let conn = self
            .connections
            .get_mut(&conn_id)
            .ok_or(UtpSocketError::ConnectionNotFound(conn_id))?;

        let data = conn.recv_data();
        let len = std::cmp::min(data.len(), buf.len());
        buf[..len].copy_from_slice(&data[..len]);
        Ok(len)
    }

    /// Process incoming UDP packets
    fn process_incoming_packets(&mut self) -> Result<(), UtpSocketError> {
        match self.socket.recv_from(&mut self.recv_buffer) {
            Ok((len, addr)) => {
                let packet = UtpPacket::from_bytes(&self.recv_buffer[..len])?;
                self.handle_packet(&packet, addr)?;
            }
            Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {}
            Err(e) => return Err(UtpSocketError::RecvFailed(e)),
        }
        Ok(())
    }

    /// Handle an incoming packet
    fn handle_packet(
        &mut self,
        packet: &UtpPacket,
        addr: SocketAddr,
    ) -> Result<(), UtpSocketError> {
        let packet_type = packet.packet_type()?;

        match packet_type {
            PacketType::StSyn => self.handle_syn(packet, addr)?,
            PacketType::StData | PacketType::StAck | PacketType::StFin => {
                let conn_id = self.find_connection_for_packet(packet, addr)?;
                if let Some(conn_id) = conn_id {
                    let (response_packets, keepalive_interval) = {
                        let conn = self.connections.get_mut(&conn_id);
                        if let Some(conn) = conn {
                            let response_packets = conn.on_packet_received(packet)?;
                            let keepalive_interval = self.keepalive_interval;
                            (response_packets, keepalive_interval)
                        } else {
                            return Ok(());
                        }
                    };

                    for resp_packet in response_packets {
                        self.send_packet(&resp_packet, addr)?;
                    }

                    self.timers.cancel_timer(conn_id, TimerType::ConnectTimeout);
                    self.timers
                        .set_timer(conn_id, TimerType::Keepalive, keepalive_interval);
                }
            }
            PacketType::StReset => {
                let conn_id = self.find_connection_for_packet(packet, addr)?;
                if let Some(conn_id) = conn_id {
                    self.close_connection_internal(conn_id)?;
                }
            }
        }
        Ok(())
    }

    /// Handle incoming SYN packet
    fn handle_syn(&mut self, packet: &UtpPacket, addr: SocketAddr) -> Result<(), UtpSocketError> {
        if self.connections.len() >= MAX_CONNECTIONS {
            let reset = UtpPacket::reset(packet.connection_id);
            self.send_packet(&reset, addr)?;
            return Err(UtpSocketError::MaxConnectionsReached);
        }

        let mut conn = UtpConnection::new();
        let syn_ack = conn.accept(packet, addr)?;
        let conn_id = conn.local_connection_id();

        self.send_packet(&syn_ack, addr)?;

        self.connections.insert(conn_id, conn);
        self.addr_to_conn.insert(addr, conn_id);

        self.timers
            .set_timer(conn_id, TimerType::Keepalive, self.keepalive_interval);

        Ok(())
    }

    /// Find the connection ID for an incoming packet
    fn find_connection_for_packet(
        &self,
        packet: &UtpPacket,
        addr: SocketAddr,
    ) -> Result<Option<u16>, UtpSocketError> {
        if let Some(conn_id) = self.addr_to_conn.get(&addr) {
            return Ok(Some(*conn_id));
        }

        for (conn_id, conn) in &self.connections {
            if conn.local_connection_id() == packet.connection_id
                || conn.remote_connection_id() == packet.connection_id
            {
                return Ok(Some(*conn_id));
            }
        }

        Ok(None)
    }

    /// Close a connection gracefully
    pub fn close_connection(&mut self, conn_id: u16) -> Result<(), UtpSocketError> {
        if self.is_closed {
            return Err(UtpSocketError::SocketClosed);
        }
        self.close_connection_internal(conn_id)?;
        Ok(())
    }

    /// Internal connection close logic
    fn close_connection_internal(&mut self, conn_id: u16) -> Result<(), UtpSocketError> {
        let (should_send_fin, remote_addr) = {
            let conn = self.connections.get(&conn_id);
            if let Some(conn) = conn {
                (conn.is_established(), conn.remote_addr())
            } else {
                return Ok(());
            }
        };

        if should_send_fin {
            let fin = {
                let conn = self.connections.get_mut(&conn_id);
                if let Some(conn) = conn {
                    conn.close()?
                } else {
                    return Ok(());
                }
            };

            if let Some(addr) = remote_addr {
                self.send_packet(&fin, addr)?;
            }
        }

        if let Some(addr) = remote_addr {
            self.addr_to_conn.remove(&addr);
        }

        self.timers.cancel_all_timers(conn_id);
        self.connections.remove(&conn_id);

        Ok(())
    }

    /// Close the entire socket and all connections
    pub fn close(&mut self) {
        if self.is_closed {
            return;
        }

        self.is_closed = true;

        let conn_ids: Vec<u16> = self.connections.keys().copied().collect();
        for conn_id in conn_ids {
            let _ = self.close_connection_internal(conn_id);
        }

        self.connections.clear();
        self.addr_to_conn.clear();
        self.timers.clear();
    }

    /// Process timers and handle expired ones
    pub fn process_timers(&mut self) -> Result<(), UtpSocketError> {
        let expired = self.timers.get_expired_timers();

        for (conn_id, timer_type) in expired {
            self.handle_timer_expired(conn_id, timer_type)?;
        }

        Ok(())
    }

    /// Handle an expired timer
    fn handle_timer_expired(
        &mut self,
        conn_id: u16,
        timer_type: TimerType,
    ) -> Result<(), UtpSocketError> {
        match timer_type {
            TimerType::ConnectTimeout => {
                let should_close = {
                    let conn = self.connections.get(&conn_id);
                    conn.is_some_and(|c| c.state() == ConnectionState::SynSent)
                };

                if should_close {
                    self.close_connection_internal(conn_id)?;
                }
            }
            TimerType::Retransmit(_) => {
                let (is_timeout, remote_addr, packets_to_send, rto) = {
                    let conn = self.connections.get_mut(&conn_id);
                    if let Some(conn) = conn {
                        let is_timeout = conn.check_timeout(self.idle_timeout);
                        let remote_addr = conn.remote_addr();
                        let packets = conn.get_sendable_packets();
                        let rto = conn.rto();
                        (is_timeout, remote_addr, packets, rto)
                    } else {
                        return Ok(());
                    }
                };

                if is_timeout {
                    self.close_connection_internal(conn_id)?;
                } else {
                    for packet in packets_to_send {
                        if let Some(addr) = remote_addr {
                            self.send_packet(&packet, addr)?;
                            self.timers.set_timer(
                                conn_id,
                                TimerType::Retransmit(packet.seq_nr),
                                rto,
                            );
                        }
                    }
                }
            }
            TimerType::Keepalive => {
                let (
                    is_established,
                    remote_conn_id,
                    ack_nr,
                    seq_nr,
                    recv_window,
                    remote_addr,
                    keepalive_interval,
                ) = {
                    let conn = self.connections.get(&conn_id);
                    if let Some(conn) = conn {
                        let is_established = conn.is_established();
                        let remote_conn_id = conn.remote_connection_id();
                        let ack_nr = conn.current_ack_nr();
                        let seq_nr = conn.current_seq_nr();
                        let recv_window = conn.receive_window();
                        let remote_addr = conn.remote_addr();
                        let keepalive_interval = self.keepalive_interval;
                        (
                            is_established,
                            remote_conn_id,
                            ack_nr,
                            seq_nr,
                            recv_window,
                            remote_addr,
                            keepalive_interval,
                        )
                    } else {
                        return Ok(());
                    }
                };

                if is_established {
                    let ack = UtpPacket::ack(remote_conn_id, ack_nr, seq_nr, recv_window);
                    if let Some(addr) = remote_addr {
                        self.send_packet(&ack, addr)?;
                    }
                    self.timers
                        .set_timer(conn_id, TimerType::Keepalive, keepalive_interval);
                }
            }
            TimerType::IdleTimeout => {
                self.close_connection_internal(conn_id)?;
            }
        }

        Ok(())
    }

    /// Check connection state
    pub fn connection_state(&self, conn_id: u16) -> Result<ConnectionState, UtpSocketError> {
        let conn = self
            .connections
            .get(&conn_id)
            .ok_or(UtpSocketError::ConnectionNotFound(conn_id))?;
        Ok(conn.state())
    }

    /// Get connection statistics
    pub fn connection_stats(&self, conn_id: u16) -> Result<ConnectionStats, UtpSocketError> {
        let conn = self
            .connections
            .get(&conn_id)
            .ok_or(UtpSocketError::ConnectionNotFound(conn_id))?;

        Ok(ConnectionStats {
            state: conn.state(),
            local_connection_id: conn.local_connection_id(),
            remote_connection_id: conn.remote_connection_id(),
            remote_addr: conn.remote_addr(),
            rtt: conn.rtt(),
            rto: conn.rto(),
            congestion_window: conn.congestion_window(),
            receive_window: conn.receive_window(),
            bytes_in_flight: conn.bytes_in_flight(),
            idle_time: conn.idle_time(),
        })
    }

    /// Poll for incoming data (non-blocking)
    pub fn poll_recv(&mut self) -> Result<Vec<(u16, Vec<u8>)>, UtpSocketError> {
        self.process_incoming_packets()?;

        let mut results = Vec::new();
        let conn_ids: Vec<u16> = self.connections.keys().copied().collect();

        for conn_id in conn_ids {
            if let Some(conn) = self.connections.get_mut(&conn_id) {
                let data = conn.recv_data();
                if !data.is_empty() {
                    results.push((conn_id, data));
                }
            }
        }

        Ok(results)
    }

    /// Get list of active connection IDs
    pub fn connection_ids(&self) -> Vec<u16> {
        self.connections.keys().copied().collect()
    }
}

impl Drop for UtpSocket {
    fn drop(&mut self) {
        self.close();
    }
}

/// Connection statistics
#[derive(Debug, Clone)]
pub struct ConnectionStats {
    /// Current connection state
    pub state: ConnectionState,
    /// Local connection ID
    pub local_connection_id: u16,
    /// Remote connection ID
    pub remote_connection_id: u16,
    /// Remote socket address
    pub remote_addr: Option<SocketAddr>,
    /// Estimated RTT
    pub rtt: Duration,
    /// Retransmission timeout
    pub rto: Duration,
    /// Congestion window size
    pub congestion_window: u32,
    /// Receive window size
    pub receive_window: u32,
    /// Bytes in flight
    pub bytes_in_flight: u32,
    /// Time since last activity
    pub idle_time: Duration,
}

/// Async wrapper for UtpSocket
pub struct AsyncUtpSocket {
    inner: Arc<Mutex<UtpSocket>>,
}

impl AsyncUtpSocket {
    /// Create a new async uTP socket
    pub fn bind(addr: &str) -> Result<Self, UtpSocketError> {
        let socket = UtpSocket::bind(addr)?;
        Ok(Self {
            inner: Arc::new(Mutex::new(socket)),
        })
    }

    /// Bind to any available port
    pub fn bind_any() -> Result<Self, UtpSocketError> {
        Self::bind("0.0.0.0:0")
    }

    /// Get local address
    pub async fn local_addr(&self) -> SocketAddr {
        let socket = self.inner.lock().await;
        socket.local_addr()
    }

    /// Connect to a remote peer
    pub async fn connect(&self, remote_addr: SocketAddr) -> Result<u16, UtpSocketError> {
        let mut socket = self.inner.lock().await;
        socket.connect(remote_addr)
    }

    /// Send data on a connection
    pub async fn send(&self, conn_id: u16, data: &[u8]) -> Result<usize, UtpSocketError> {
        let mut socket = self.inner.lock().await;
        socket.send(conn_id, data)
    }

    /// Receive data from a connection
    pub async fn recv(&self, conn_id: u16, buf: &mut [u8]) -> Result<usize, UtpSocketError> {
        let mut socket = self.inner.lock().await;
        socket.recv(conn_id, buf)
    }

    /// Close a connection
    pub async fn close_connection(&self, conn_id: u16) -> Result<(), UtpSocketError> {
        let mut socket = self.inner.lock().await;
        socket.close_connection(conn_id)
    }

    /// Close the socket
    pub async fn close(&self) {
        let mut socket = self.inner.lock().await;
        socket.close();
    }

    /// Get connection state
    pub async fn connection_state(&self, conn_id: u16) -> Result<ConnectionState, UtpSocketError> {
        let socket = self.inner.lock().await;
        socket.connection_state(conn_id)
    }

    /// Get connection stats
    pub async fn connection_stats(&self, conn_id: u16) -> Result<ConnectionStats, UtpSocketError> {
        let socket = self.inner.lock().await;
        socket.connection_stats(conn_id)
    }

    /// Process timers
    pub async fn process_timers(&self) -> Result<(), UtpSocketError> {
        let mut socket = self.inner.lock().await;
        socket.process_timers()
    }

    /// Poll for incoming data
    pub async fn poll_recv(&self) -> Result<Vec<(u16, Vec<u8>)>, UtpSocketError> {
        let mut socket = self.inner.lock().await;
        socket.poll_recv()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{IpAddr, Ipv4Addr};

    fn test_addr() -> SocketAddr {
        SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 12345)
    }

    #[test]
    fn test_socket_bind() {
        let socket = UtpSocket::bind_any();
        assert!(socket.is_ok());

        let socket = socket.unwrap();
        assert!(socket.local_addr().port() > 0);
        assert!(!socket.is_closed());
        assert_eq!(socket.connection_count(), 0);
    }

    #[test]
    fn test_socket_close() {
        let mut socket = UtpSocket::bind_any().unwrap();
        assert!(!socket.is_closed());

        socket.close();
        assert!(socket.is_closed());
    }

    #[test]
    fn test_socket_connect_closed() {
        let mut socket = UtpSocket::bind_any().unwrap();
        socket.close();

        let result = socket.connect(test_addr());
        assert!(matches!(result, Err(UtpSocketError::SocketClosed)));
    }

    #[test]
    fn test_socket_connection_not_found() {
        let mut socket = UtpSocket::bind_any().unwrap();

        let result = socket.send(60000, &[1, 2, 3]);
        assert!(matches!(
            result,
            Err(UtpSocketError::ConnectionNotFound(60000))
        ));

        let mut buf = [0u8; 100];
        let result = socket.recv(60000, &mut buf);
        assert!(matches!(
            result,
            Err(UtpSocketError::ConnectionNotFound(60000))
        ));
    }

    #[test]
    fn test_connection_id_creation() {
        let conn_id = ConnectionId::new(12345, test_addr());
        assert_eq!(conn_id.conn_id, 12345);
        assert_eq!(conn_id.remote_addr, test_addr());
    }

    #[test]
    fn test_async_socket_bind() {
        let socket = AsyncUtpSocket::bind_any();
        assert!(socket.is_ok());
    }

    #[test]
    fn test_socket_process_timers_no_connections() {
        let mut socket = UtpSocket::bind_any().unwrap();
        let result = socket.process_timers();
        assert!(result.is_ok());
    }

    #[test]
    fn test_socket_poll_recv_no_data() {
        let mut socket = UtpSocket::bind_any().unwrap();
        let result = socket.poll_recv();
        assert!(result.is_ok());
        let data = result.unwrap();
        assert!(data.is_empty());
    }
}