48 lines
1.1 KiB
Rust
48 lines
1.1 KiB
Rust
use futures_lite::{future::zip, StreamExt};
|
|
use wove::{
|
|
futures::Nexus,
|
|
io::{AsyncReadLoan, AsyncWriteLoan, Transpose},
|
|
io_impl::io_uring::IoUring,
|
|
};
|
|
|
|
pub async fn go(uring: &IoUring) -> std::io::Result<()> {
|
|
let mut listener = wove::net::TcpListener::bind(uring, "127.0.0.1:0").await?;
|
|
let addr = listener.local_addr().await?;
|
|
println!("{addr}");
|
|
let mut incoming = listener.incoming().await?;
|
|
|
|
let handlers = Nexus::new();
|
|
|
|
let accept = async {
|
|
while let Some(conn) = incoming.next().await {
|
|
let mut conn = conn?;
|
|
|
|
handlers.push(Box::pin(async move {
|
|
loop {
|
|
let (buf, read_amt) = conn.read(vec![0; 4096]).await.transpose()?;
|
|
if read_amt == 0 {
|
|
break;
|
|
}
|
|
conn.write(buf).await.transpose()?;
|
|
}
|
|
|
|
std::io::Result::Ok(())
|
|
}))
|
|
}
|
|
|
|
std::io::Result::Ok(())
|
|
};
|
|
|
|
let handle_connections = handlers.stream().fuse().last();
|
|
let (accept_result, handle_result) = zip(accept, handle_connections).await;
|
|
accept_result?;
|
|
handle_result.transpose()?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn main() {
|
|
let uring = IoUring::new().unwrap();
|
|
uring.block_on(go(&uring)).unwrap();
|
|
}
|