mirror of
https://github.com/EasyTier/kcp-sys.git
synced 2026-10-19 16:16:36 +00:00
990e4f1e2aaf62b1a897b35dd1453535af4c403d
Keep acknowledged KCP data flowing to the application after peer FIN. Delay connection cleanup until the receive task finishes, including when both write directions close before the application reads. Track graceful receive completion separately from resets so AsyncRead reports EOF for FIN and keeps errors for RST or endpoint destruction. Answer keepalive pings while the local write direction is closed. Cover a 200 KiB request followed by a response after EOF, simultaneous buffered shutdown across a cleanup interval, half-close keepalives, and pending reads interrupted by reset or endpoint destruction. Validation: all 7 library tests and both example targets pass; cargo fmt --check and clippy --all-targets -- -D warnings pass.
kcp-sys
Safe bindings to the kcp transport protocol library.
Also including a high level API for connection state management and data stream handling.
Usage
-
Create the endpoint and run it.
let mut endpoint = KcpEndpoint::new(); endpoint.run().await; -
forward the input and output to your transport layer, udp for example.
let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap()); let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54320").await.unwrap()); udp_socket.connect("127.0.0.1:54321").await.unwrap(); let udp = udp_socket.clone(); tokio::spawn(async move { while let Some(data) = output.recv().await { udp.send(&data.inner()).await.unwrap(); } }); let udp = udp_socket.clone(); tokio::spawn(async move { loop { let mut buf = vec![0; 1024]; let (size, _) = udp.recv_from(&mut buf).await.unwrap(); input .send(BytesMut::from(&buf[..size]).into()) .await .unwrap(); } }); -
Create a connection and send / recv data.
let conn_id = endpoint .connect(Duration::from_secs(1), 0, 0, Bytes::new()) .await .unwrap(); let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap(); kcp_stream.write_all(b"hello world").await.unwrap(); let mut buf = vec![0; 64 * 1024]; let size = kcp_stream.read(&mut buf).await.unwrap(); println!("{}", String::from_utf8_lossy(&buf[..size]));
Tune the kcp parameters
You can tune the kcp parameters by set a config factory to the endpoint.
let mut endpoint = KcpEndpoint::new();
endpoint.set_kcp_config_factory(|conv| {
KcpConfig::new_turbo(conv)
});
Languages
Rust
100%