Skip to content

Commit

Permalink
refractor based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
irvingoujAtDevolution committed Jan 26, 2024
1 parent 2089ce2 commit 3144404
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
5 changes: 4 additions & 1 deletion examples/tcp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ fn main() -> io::Result<()> {
};

println!("event: {:?}", event);
if event.is_connect_failed() {
if event
.is_connect_failed()
.expect("is connect failed does not support on this platform")
{
println!("connect failed");
}

Expand Down
4 changes: 2 additions & 2 deletions src/iocp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,8 @@ impl EventExtra {

/// Check if TCP connect failed.
#[inline]
pub fn is_connect_failed(&self) -> bool {
self.flags.intersects(AfdPollMask::CONNECT_FAIL)
pub fn is_connect_failed(&self) -> Option<bool> {
Some(self.flags.intersects(AfdPollMask::CONNECT_FAIL))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ impl EventExtra {
}

#[inline]
pub fn is_connect_failed(&self) -> bool {
unimplemented!("is connect failed is not supported on kqueue");
pub fn is_connect_failed(&self) -> Option<bool> {
None
}
}

Expand Down
54 changes: 49 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,58 @@ impl Event {
self.extra.is_pri()
}

/// Tell if this event is the result of a connect failure.
/// Tells if this event is the result of a connection failure.
///
/// 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.
/// This function checks if a TCP connection has failed. It corresponds to the `EPOLLERR` or `EPOLLHUP` event in Linux
/// and `CONNECT_FAILED` event in Windows IOCP.
///

/// # Examples
///
/// ```
/// use std::{io, net};
/// // Assuming polling and socket2 are included as dependencies in Cargo.toml
/// use polling::Event;
/// use socket2::Type;
///
/// fn main() -> io::Result<()> {
/// let socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?;
/// let poller = polling::Poller::new()?;
/// unsafe {
/// poller.add(&socket, Event::new(0, true, true))?;
/// }
/// let addr = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 8080);
/// socket.set_nonblocking(true)?;
/// let _ = socket.connect(&addr.into());
///
/// let mut events = polling::Events::new();
///
/// events.clear();
/// poller.wait(&mut events, None)?;
///
/// let event = events.iter().next();
/// let Some(event) = event else {
/// println!("no event");
/// return Ok(());
/// };
///
/// println!("event: {:?}", event);
/// if event
/// .is_connect_failed()
/// .expect("is connect failed does not support on this platform")
/// {
/// println!("connect failed");
/// }
///
/// Ok(())
/// }
/// ```
///
/// # Returns
///
/// Returns `Some(true)` if the connection has failed, `Some(false)` if the connection has not failed,
/// or `None` if the platform does not support detecting this condition.
#[inline]
pub fn is_connect_failed(&self) -> bool {
pub fn is_connect_failed(&self) -> Option<bool> {
self.extra.is_connect_failed()
}

Expand Down
6 changes: 2 additions & 4 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,8 @@ impl EventExtra {
}

#[inline]
pub fn is_connect_failed(&self) -> bool {
// need reviewer's special attention, as I do not have access to a system that supports this
// this is a guess based on the documentation of `poll()`
self.flags.contains(PollFlags::ERR) || self.flags.contains(PollFlags::HUP)
pub fn is_connect_failed(&self) -> Option<bool> {
Some(self.flags.contains(PollFlags::ERR) || self.flags.contains(PollFlags::HUP))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,6 @@ impl EventExtra {

#[inline]
pub fn is_connect_failed(&self) -> bool {
self.flags.contains(PollFlags::ERR) || self.flags.contains(PollFlags::HUP)
Some(self.flags.contains(PollFlags::ERR) || self.flags.contains(PollFlags::HUP))
}
}

0 comments on commit 3144404

Please sign in to comment.