Skip to content

Commit

Permalink
update linux
Browse files Browse the repository at this point in the history
  • Loading branch information
irvingoujAtDevolution committed Jan 25, 2024
1 parent 9cd7902 commit 7d587d8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
17 changes: 8 additions & 9 deletions examples/tcp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -12,30 +11,30 @@ 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)?;

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(())
}
5 changes: 5 additions & 0 deletions src/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7d587d8

Please sign in to comment.