Simplify echo example
This commit is contained in:
parent
9b9f1b5a3a
commit
ae196a2ecf
|
@ -1,3 +1,4 @@
|
|||
use polldance::io::{Nonblocking, ReadBytes};
|
||||
use polldance::{net::TcpStream, PollManager, Readiness};
|
||||
use std::sync::Arc;
|
||||
|
||||
|
@ -50,28 +51,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
'l2: loop {
|
||||
for (key, readiness) in poller.poll(None)? {
|
||||
if stream_key.is_key(key) {
|
||||
if readiness.is_hangup() {
|
||||
break 'l2;
|
||||
}
|
||||
if readiness.is_read() {
|
||||
loop {
|
||||
let mut buf = [0; 1024];
|
||||
|
||||
match rustix::io::read(&*stream, &mut buf) {
|
||||
Ok(n) => {
|
||||
if n == 0 {
|
||||
break 'l2;
|
||||
}
|
||||
match polldance::io::try_read(stream.as_ref(), &mut buf)? {
|
||||
Nonblocking::Ready(ReadBytes::Read(n)) => {
|
||||
let n: usize = n.into();
|
||||
|
||||
let should_break = n < buf.len();
|
||||
bytes.extend_from_slice(&buf[0..n]);
|
||||
if should_break {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(e)
|
||||
if e == rustix::io::Error::WOULDBLOCK
|
||||
|| e == rustix::io::Error::AGAIN =>
|
||||
{
|
||||
break;
|
||||
}
|
||||
Err(e) => return Err(e.into()),
|
||||
Nonblocking::Ready(ReadBytes::EOF) => break 'l2,
|
||||
Nonblocking::WouldBlock => break,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,27 +78,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.update_interests(&stream, |interests| Readiness::write() | interests);
|
||||
}
|
||||
}
|
||||
if readiness.is_hangup() {
|
||||
break 'l2;
|
||||
}
|
||||
if readiness.is_write() {
|
||||
match rustix::io::write(&*stream, &bytes) {
|
||||
Ok(n) if n == bytes.len() => {
|
||||
match polldance::io::try_write(stream.as_ref(), &bytes)? {
|
||||
Nonblocking::Ready(n) if n == bytes.len() => {
|
||||
bytes = Vec::new();
|
||||
poller.update_interests(&stream, |_| {
|
||||
Readiness::read() | Readiness::hangup()
|
||||
});
|
||||
}
|
||||
Ok(n) => {
|
||||
Nonblocking::Ready(n) => {
|
||||
bytes = Vec::from(&bytes[n..]);
|
||||
}
|
||||
Err(e)
|
||||
if e == rustix::io::Error::WOULDBLOCK
|
||||
|| e == rustix::io::Error::AGAIN =>
|
||||
{
|
||||
Nonblocking::WouldBlock => {
|
||||
continue;
|
||||
}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue