| Crates.io | kcp-tokio |
| lib.rs | kcp-tokio |
| version | 0.3.4 |
| created_at | 2025-08-10 11:29:46.78894+00 |
| updated_at | 2025-09-01 13:38:40.228292+00 |
| description | A high-performance async Rust implementation of KCP - A Fast and Reliable ARQ Protocol |
| homepage | https://github.com/leihuxi/rust-kcp |
| repository | https://github.com/leihuxi/rust-kcp |
| max_upload_size | |
| id | 1788801 |
| size | 265,681 |
A high-performance async Rust implementation of KCP - A Fast and Reliable ARQ Protocol built on top of Tokio.
bytes crateKcpStream, KcpListener)Add this to your Cargo.toml:
[dependencies]
kcp-tokio = "0.3.3"
use kcp_tokio::{KcpConfig, async_kcp::{KcpListener, KcpStream}};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Server
let config = KcpConfig::realtime();
let mut listener = KcpListener::bind("127.0.0.1:12345", config).await?;
while let Ok((mut stream, addr)) = listener.accept().await {
tokio::spawn(async move {
let mut buf = [0u8; 1024];
while let Ok(n) = stream.read(&mut buf).await {
if n == 0 { break; }
stream.write_all(&buf[..n]).await.unwrap();
}
});
}
Ok(())
}
use kcp_tokio::{KcpConfig, async_kcp::KcpStream};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = KcpConfig::new().fast_mode();
let mut stream = KcpStream::connect("127.0.0.1:12345", config).await?;
// Send data
stream.write_all(b"Hello, KCP!").await?;
// Receive echo
let mut buffer = [0u8; 1024];
let n = stream.read(&mut buffer).await?;
println!("Received: {}", String::from_utf8_lossy(&buffer[..n]));
Ok(())
}
This implementation features a layered architecture designed for performance and maintainability:
โโโโโโโโโโโโโโโโโโโโโโโ
โ High-Level API โ KcpStream, KcpListener
โโโโโโโโโโโโโโโโโโโโโโโค
โ Connection Layer โ KcpConnection, Session Management
โโโโโโโโโโโโโโโโโโโโโโโค
โ Protocol Core โ Async KCP Engine
โโโโโโโโโโโโโโโโโโโโโโโค
โ Transport Layer โ UDP Socket, Packet I/O
โโโโโโโโโโโโโโโโโโโโโโโ
KCP Rust provides flexible configuration options for different use cases:
// For gaming applications - ultra-low latency
let config = KcpConfig::gaming();
// For file transfers - high throughput
let config = KcpConfig::file_transfer();
// For real-time communication - balanced
let config = KcpConfig::realtime();
// For testing with packet loss simulation
let config = KcpConfig::testing(0.1); // 10% packet loss
let config = KcpConfig::new()
.fast_mode() // Low latency mode
.window_size(128, 128) // Send/Receive windows
.mtu(1400) // Maximum transmission unit
.connect_timeout(Duration::from_secs(10))
.keep_alive(Some(Duration::from_secs(30)))
.stream_mode(true); // Enable streaming mode
let config = KcpConfig::new()
.turbo_mode() // Maximum performance (5ms updates)
.window_size(256, 256) // Larger windows
.mtu(1200); // Optimized MTU
The examples/ directory contains several complete examples:
# Terminal 1 - Start server
cargo run --example simple_echo server 127.0.0.1:12345
# Terminal 2 - Run client
cargo run --example simple_echo client 127.0.0.1:12345
Run the test suite:
# Run all tests
cargo test
# Run integration tests
cargo test --test integration_test
# Run with output
cargo test -- --nocapture
let config = KcpConfig::new().stream_mode(true);
Enables TCP-like streaming without message boundaries.
let config = KcpConfig::new().simulate_packet_loss(0.05); // 5% loss
Useful for testing network resilience.
let config = KcpConfig::new().socket_buffer_size(64 * 1024); // 64KB buffers
KCP Rust is optimized for:
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)