| Crates.io | kcp-tokio |
| lib.rs | kcp-tokio |
| version | 0.3.5 |
| created_at | 2025-08-10 11:29:46.78894+00 |
| updated_at | 2025-12-26 13:59:42.491581+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 | 263,615 |
A high-performance async Rust implementation of KCP - A Fast and Reliable ARQ Protocol built on top of Tokio.
bytes cratecrossbeamKcpStream, KcpListener)Add this to your Cargo.toml:
[dependencies]
kcp-tokio = "0.3.4"
tokio = { version = "1.0", features = ["full"] }
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".parse()?, config).await?;
// Send data
stream.write_all(b"Hello, KCP!").await?;
// Receive response
let mut buffer = [0u8; 1024];
let n = stream.read(&mut buffer).await?;
println!("Received: {}", String::from_utf8_lossy(&buffer[..n]));
Ok(())
}
use kcp_tokio::{KcpConfig, async_kcp::KcpListener};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = KcpConfig::realtime();
let mut listener = KcpListener::bind("127.0.0.1:12345".parse()?, config).await?;
println!("Server listening on 127.0.0.1:12345");
while let Ok((mut stream, addr)) = listener.accept().await {
println!("New connection from {}", addr);
tokio::spawn(async move {
let mut buf = [0u8; 1024];
while let Ok(n) = stream.read(&mut buf).await {
if n == 0 { break; }
let _ = stream.write_all(&buf[..n]).await;
}
});
}
Ok(())
}
┌─────────────────────────────────────────────────────────────┐
│ Application Layer │
│ (User code using KcpStream/KcpListener) │
├─────────────────────────────────────────────────────────────┤
│ High-Level API Layer │
│ KcpStream KcpListener │
│ (AsyncRead/AsyncWrite, TCP-like interface) │
├─────────────────────────────────────────────────────────────┤
│ Protocol Core Layer │
│ KcpEngine │
│ (ARQ logic, congestion control, retransmission) │
├─────────────────────────────────────────────────────────────┤
│ Common Layer │
│ KcpSegment, KcpHeader, BufferPool, Constants │
├─────────────────────────────────────────────────────────────┤
│ Transport Layer │
│ UDP Socket (Tokio) │
└─────────────────────────────────────────────────────────────┘
// Gaming - ultra-low latency (3ms update interval)
let config = KcpConfig::gaming();
// Real-time communication (8ms update interval)
let config = KcpConfig::realtime();
// File transfer - high throughput
let config = KcpConfig::file_transfer();
// Testing with packet loss simulation
let config = KcpConfig::testing(0.1); // 10% packet loss
| Mode | Update Interval | Resend | Congestion Control | Use Case |
|---|---|---|---|---|
| Normal | 40ms | 0 | Yes | General purpose |
| Fast | 8ms | 2 | Yes | Low latency |
| Turbo | 4ms | 1 | No | Maximum speed |
| Gaming | 3ms | 1 | No | Real-time games |
use std::time::Duration;
let config = KcpConfig::new()
.fast_mode()
.window_size(128, 128)
.mtu(1400)
.connect_timeout(Duration::from_secs(10))
.keep_alive(Some(Duration::from_secs(30)))
.stream_mode(true);
# Run performance test server
cargo run --example perf_test_server -- 127.0.0.1:12345 gaming
# Run performance test client
cargo run --example perf_test_client -- 127.0.0.1:12345
# Run simple echo example
cargo run --example simple_echo
# Run all tests
cargo test
# Run with logging
RUST_LOG=debug cargo test -- --nocapture
# Run specific test
cargo test test_echo_debug -- --nocapture
# Run clippy
cargo clippy --all-targets
Detailed documentation is available in the doc/ directory:
| Document | Description |
|---|---|
| ARCHITECTURE.md | System architecture and design |
| MODULES.md | Module reference and APIs |
| USAGE.md | Usage guide and examples |
| TESTING.md | Testing guide |
KCP provides significant latency improvements over TCP:
crossbeam::queue::ArrayQueuebytes crateMIT License - see LICENSE file.
Contributions are welcome! Please feel free to submit a Pull Request.