diff --git a/examples/tcp_client.rs b/examples/tcp_client.rs index 0a71cf3..aa9d708 100644 --- a/examples/tcp_client.rs +++ b/examples/tcp_client.rs @@ -2,7 +2,6 @@ use std::{io, net}; use polling::Event; use socket2::Type; -use tracing::event; fn main() -> io::Result<()> { let socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?; @@ -12,13 +11,13 @@ fn main() -> io::Result<()> { } let addr = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 8080); socket.set_nonblocking(true)?; - let mut res = socket.connect(&addr.into()); + let res = socket.connect(&addr.into()); let mut events = polling::Events::new(); - while let Err(ref e) = res { - if e.kind() != io::ErrorKind::WouldBlock { - return Err(io::Error::new(e.kind(), e.to_string())); - } + // while let Err(ref e) = res { + // if e.kind() != io::ErrorKind::WouldBlock { + // return Err(io::Error::new(e.kind(), e.to_string())); + // } events.clear(); poller.wait(&mut events, None)?; @@ -26,16 +25,16 @@ fn main() -> io::Result<()> { let event = events.iter().next(); if event.is_none() { println!("no event"); - break; + // break; } let event = event.unwrap(); println!("event: {:?}", event); if event.is_connect_failed() { println!("connect failed"); - break; + // break; } - } + // } Ok(()) } diff --git a/src/epoll.rs b/src/epoll.rs index 606dd1c..66067be 100644 --- a/src/epoll.rs +++ b/src/epoll.rs @@ -365,6 +365,11 @@ impl EventExtra { pub fn is_pri(&self) -> bool { self.flags.contains(epoll::EventFlags::PRI) } + + #[inline] + pub fn is_connect_failed(&self) -> bool { + self.flags.contains(epoll::EventFlags::ERR) && self.flags.contains(epoll::EventFlags::HUP) + } } /// The notifier for Linux. diff --git a/src/lib.rs b/src/lib.rs index 1870e63..e0e78f6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -334,7 +334,10 @@ impl Event { /// Tell if this event is the result of a connect failure. /// - /// This indicates that a non-blocking connect operation has failed. + /// This indicates a tcp connection has failed, it corresponds to the `EPOLLERR` along with `EPOLLHUP` event in linux + /// and `CONNECT_FAILED` event in windows IOCP. + /// + #[inline] pub fn is_connect_failed(&self) -> bool { self.extra.is_connect_failed()