28 releases (11 stable)
1.1.5 | Jul 1, 2024 |
---|---|
1.1.3 | Jan 18, 2024 |
1.1.2 | Mar 6, 2023 |
0.3.2 | May 25, 2022 |
0.2.7 | Oct 30, 2020 |
#847 in Network programming
74KB
1.5K
SLoC
kcpserver
最好用的RUST KCP 服务器框架 (The best rust kcp server framework)
Examples Echo
use kcpserver::prelude::{
kcp_module::{KcpConfig, KcpNoDelayConfig},
*,
};
use tokio::io::AsyncReadExt;
use log::LevelFilter;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::Builder::new()
.filter_level(LevelFilter::Debug)
.init();
let mut config = KcpConfig::default();
config.nodelay = Some(KcpNoDelayConfig::fastest());
let kcp_server = KcpListener::new("0.0.0.0:5555", config, 1, |peer| async move {
log::debug!("create kcp peer:{}", peer);
let mut buf = [0; 1024];
let mut reader = peer.get_reader();
let mut writer = peer.get_writer();
while let Ok(size) = reader.read(&mut buf).await {
log::debug!("read peer:{} buff:{}", peer, size);
writer.write_all(&buf[..size]).await?;
writer.flush().await?;
}
writer.shutdown().await?;
log::debug!("kcp peer:{} closed", peer.to_string());
Ok(())
})?;
kcp_server.start().await?;
Ok(())
}
Dependencies
~5–12MB
~131K SLoC