Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/huoyijie/rust-udpack
udpack protocol implements in rust
https://github.com/huoyijie/rust-udpack
tcp udp udpack
Last synced: 4 days ago
JSON representation
udpack protocol implements in rust
- Host: GitHub
- URL: https://github.com/huoyijie/rust-udpack
- Owner: huoyijie
- License: mit
- Created: 2022-04-26T07:10:11.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-12-02T06:52:13.000Z (almost 2 years ago)
- Last Synced: 2024-08-01T22:37:43.457Z (3 months ago)
- Topics: tcp, udp, udpack
- Language: Rust
- Homepage: https://huoyijie.cn/article/399d47e0717311ed8812b9a35e7c2efa/
- Size: 2.52 MB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rust-udpack
## Examples
### server.rs
```rust
use rust_udpack::Transport;
use rust_udpack::Udpack;
use std::io;
use tokio::signal;
use tokio::task::JoinHandle;#[tokio::main]
async fn main() -> io::Result<()> {
let mut udpack: Udpack = Udpack::new("0.0.0.0:8080").await?;loop {
tokio::select! {
res = udpack.accept() => {
let _handle: JoinHandle> = tokio::spawn(async move {
let mut transport: Transport = res.unwrap();while let Some(bytes) = transport.read().await {
println!("{:?}", bytes);
transport.write(bytes).await?;
}
Ok(())
});
}
_ = signal::ctrl_c() => {
println!("ctrl-c received!");
udpack.shutdown().await?;
break;
}
}
}
Ok(())
}
```### client.rs
```rust
use bytes::Bytes;
use rust_udpack::Transport;
use rust_udpack::Udpack;
use std::io;
use tokio::signal;
use tokio::time;
use tokio::time::Duration;#[tokio::main]
async fn main() -> io::Result<()> {
let udpack: Udpack = Udpack::new("0.0.0.0:0").await?;
let mut transport: Transport = udpack.connect("127.0.0.1:8080").await?;
let mut interval = time::interval(Duration::from_secs(3));loop {
tokio::select! {
res = transport.read() => {
if let Some(bytes) = res {
println!("{:?}", bytes);
}
}
_ = interval.tick() => {
transport.write(Bytes::copy_from_slice(&[1u8; 2048])).await?;
}
_ = signal::ctrl_c() => {
println!("ctrl-c received!");
udpack.shutdown().await?;
return Ok(());
}
};
}
}
```