Change example to use non-blocking io

This commit is contained in:
Aode (lion) 2022-02-16 11:12:58 -05:00
parent e7ce722285
commit 347b9ba83a

View file

@ -1,5 +1,8 @@
use async_join::join_all;
use foxtrot::Async;
use foxtrot::{
io::{Nonblocking, ReadBytes, Readiness},
Async,
};
async fn echo(port: u16) -> Result<(), foxtrot::Error> {
let listener = Async::bind([127, 0, 0, 1].into(), port).await?;
@ -9,21 +12,47 @@ async fn echo(port: u16) -> Result<(), foxtrot::Error> {
let (stream, _addr) = listener.accept().await?;
println!("Accepted connection");
loop {
let mut buf = [0; 1024];
if let Err(e) = stream.read(&mut buf).await {
if e == rustix::io::Error::PIPE {
break;
}
let mut bytes = vec![];
let mut interests = Readiness::read();
let mut buf = [0; 1024];
return Err(e.into());
'l2: loop {
let readiness = stream.ready(interests).await?;
if readiness.is_hangup() {
break;
}
if let Err(e) = stream.write_all(&buf).await {
if e == rustix::io::Error::PIPE {
break;
if readiness.is_read() {
loop {
match stream.read_nonblocking(&mut buf)? {
Nonblocking::Ready(ReadBytes::Read(n)) => {
let n: usize = n.into();
bytes.extend_from_slice(&buf[0..n]);
if n < buf.len() {
break;
}
}
Nonblocking::Ready(ReadBytes::EOF) if bytes.is_empty() => break 'l2,
Nonblocking::Ready(ReadBytes::EOF) | Nonblocking::WouldBlock => break,
}
}
return Err(e.into());
if !bytes.is_empty() {
interests = Readiness::read() | Readiness::write();
}
}
if readiness.is_write() {
match stream.write_nonblocking(&bytes)? {
Nonblocking::Ready(n) => {
bytes = bytes.split_off(n);
if bytes.is_empty() {
interests = Readiness::read();
}
}
Nonblocking::WouldBlock => {}
}
}
}