1 个不稳定版本
0.1.0 | 2022年5月7日 |
---|
#128 在 #udp
用于 kaminari
23KB
471 代码行
UDPflow
tokio-udp 的 Stream API。
类似 TCP 的 UDP 流
use tokio::net::UdpSocket;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use udpflow::{UdpListener, UdpStreamLocal, UdpStreamRemote};
async fn server() {
let socket = UdpSocket::bind("127.0.0.1:5000").await.unwrap();
let listener = UdpListener::new(socket);
let mut buf = vec![0u8; 0x2000];
// listener must be continuously polled to recv packets or accept new streams
while let Ok((stream, addr)) = listener.accept(&mut buf).await {
tokio::spawn(handle(stream));
}
}
async fn handle(mut stream1: UdpStreamLocal) {
let socket = UdpSocket::bind("127.0.0.1:0").await.unwrap();
let mut stream2 = UdpStreamRemote::new(socket, "127.0.0.1:10000".parse().unwrap());
let mut buf = vec![0u8; 256];
stream1.read(&mut buf).await; stream2.write(&buf).await;
stream2.read(&mut buf).await; stream1.write(&buf).await;
}
发送/接收帧数据
use tokio::net::TcpStream;
use udpflow::UotStream;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
async {
let stream = TcpStream::connect("127.0.0.1:8080").await.unwrap();
let mut stream = UotStream::new(stream);
let mut buf = vec![0u8; 256];
// read a frame
let n = stream.read(&mut buf).await.unwrap();
// write a frame
stream.write_all(&buf[..n]).await;
};
UoT 规范
+------+----------+
| LEN | DATA |
+------+----------+
| 2 | Variable |
+------+----------+
*LEN 是大端字节顺序的 16 位无符号整数。
依赖项
~2–10MB
~86K SLoC