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
//! Transport abstraction for KCP packet I/O.
//!
//! This module provides [`KcpTransport`], a trait that decouples the KCP protocol
//! engine from the underlying network transport. Users can implement custom
//! transports to:
//!
//! - Use non-UDP transport layers (e.g., WebSocket, Unix sockets)
//! - Encrypt/decrypt packets before/after transmission
//! - Filter, log, or modify packets in transit
//! - Implement custom congestion or QoS policies
//!
//! The default implementation is [`KcpUdpTransport`], which wraps a
//! `tokio::net::UdpSocket` for standard UDP-based KCP communication.
//!
//! # Example: Custom Transport with XOR Obfuscation
//!
//! ```no_run
//! use kcp_io::tokio_rt::{KcpTransport, KcpUdpTransport};
//! use std::io;
//! use std::net::SocketAddr;
//! use std::sync::Arc;
//! use tokio::net::UdpSocket;
//!
//! struct XorTransport {
//! inner: KcpUdpTransport,
//! key: u8,
//! }
//!
//! impl KcpTransport for XorTransport {
//! fn try_send(&self, data: &[u8], addr: SocketAddr) -> io::Result<usize> {
//! self.inner.try_send(data, addr)
//! }
//!
//! fn process_outgoing(&self, data: &[u8], _addr: SocketAddr) -> Vec<u8> {
//! data.iter().map(|b| b ^ self.key).collect()
//! }
//!
//! fn process_incoming(&self, data: &[u8], _addr: SocketAddr) -> Option<Vec<u8>> {
//! Some(data.iter().map(|b| b ^ self.key).collect())
//! }
//! }
//! ```
use io;
use SocketAddr;
use Arc;
use UdpSocket;
/// Trait for custom KCP transport implementations.
///
/// Implementors provide the actual packet I/O for a KCP session. The trait
/// separates three concerns:
///
/// 1. **Actual transmission** ([`try_send`](KcpTransport::try_send)) — the
/// synchronous send operation called from KCP's output callback.
/// 2. **Outgoing transformation** ([`process_outgoing`](KcpTransport::process_outgoing)) —
/// called before data is sent to the wire. Use for encryption, compression, etc.
/// 3. **Incoming transformation** ([`process_incoming`](KcpTransport::process_incoming)) —
/// called after data is received from the wire, before feeding to KCP.
/// Return `None` to silently drop the packet.
///
/// # Thread Safety
///
/// The trait requires `Send + Sync + 'static`. Implementations are typically
/// wrapped in `Arc<dyn KcpTransport>` and shared between the session, read half,
/// and write half.
///
/// # Performance
///
/// [`try_send`](KcpTransport::try_send) is called from KCP's synchronous output
/// callback context. It must not block — use non-blocking I/O (e.g.,
/// `UdpSocket::try_send_to`).
/// Default UDP-based transport for KCP communication.
///
/// Wraps a `tokio::net::UdpSocket` in an `Arc` and provides non-blocking
/// send via [`UdpSocket::try_send_to`].
///
/// This is the transport used by [`KcpStream::connect`](super::KcpStream::connect)
/// and [`KcpListener::bind`](super::KcpListener::bind) by default.
///
/// # Example
///
/// ```no_run
/// use kcp_io::tokio_rt::KcpUdpTransport;
/// use std::sync::Arc;
/// use tokio::net::UdpSocket;
///
/// # async fn example() -> std::io::Result<()> {
/// let socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
/// let transport = KcpUdpTransport::new(socket);
/// # Ok(())
/// # }
/// ```