Skip to content

Commit

Permalink
adding support if connect failed
Browse files Browse the repository at this point in the history
  • Loading branch information
irvingoujAtDevolution committed Jan 25, 2024
1 parent 2089ce2 commit 808428d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 5 deletions.
11 changes: 7 additions & 4 deletions examples/tcp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() -> io::Result<()> {
unsafe {
poller.add(&socket, Event::new(0, true, true))?;
}
let addr = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 8080);
let addr = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 12345); // some address with no server listening
socket.set_nonblocking(true)?;
let _ = socket.connect(&addr.into());

Expand All @@ -19,9 +19,12 @@ fn main() -> io::Result<()> {
poller.wait(&mut events, None)?;

let event = events.iter().next();
let Some(event) = event else {
println!("no event");
return Ok(());
let event = match event {
Some(event) => event,
None => {
println!("no event");
return Ok(());
}
};

println!("event: {:?}", event);
Expand Down
5 changes: 5 additions & 0 deletions src/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ impl Poller {
true
}

/// Whether this poller supports checking for the tcp connection failure.
pub fn supports_is_connect_failed(&self) -> bool {
true
}

/// Adds a new file descriptor.
///
/// # Safety
Expand Down
5 changes: 5 additions & 0 deletions src/iocp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ impl Poller {
false
}

/// Whether this poller supports checking for the tcp connection failure.
pub fn supports_is_connect_failed(&self) -> bool {
true
}

/// Add a new source to the poller.
///
/// # Safety
Expand Down
8 changes: 7 additions & 1 deletion src/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ impl Poller {
true
}

/// Whether this poller supports checking for the tcp connection failure.
pub fn supports_is_connect_failed(&self) -> bool {
false
}

/// Adds a new file descriptor.
///
/// # Safety
Expand Down Expand Up @@ -372,9 +377,10 @@ impl EventExtra {
false
}

/// is the tcp connection failed?
#[inline]
pub fn is_connect_failed(&self) -> bool {
unimplemented!("is connect failed is not supported on kqueue");
false
}
}

Expand Down
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,45 @@ impl Event {
/// 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.
///
/// # Examples
///
/// ```
/// use std::{io, net};
/// 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(), 12345); // some address with no server listening
/// 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 event = match event {
/// Some(event) => event,
/// None => {
/// println!("no event");
/// return Ok(());
/// }
/// };
/// println!("event: {:?}", event);
/// if event.is_connect_failed() {
/// println!("connect failed");
/// }
/// Ok(())
/// }
/// ```
#[inline]
pub fn is_connect_failed(&self) -> bool {
self.extra.is_connect_failed()
Expand Down Expand Up @@ -395,6 +433,11 @@ impl Poller {
self.poller.supports_edge()
}

/// Whether this poller supports checking for the tcp connection failure.
pub fn supports_is_connect_failed(&self) -> bool {
self.poller.supports_is_connect_failed()
}

/// Adds a file descriptor or socket to the poller.
///
/// A file descriptor or socket is considered readable or writable when a read or write
Expand Down
5 changes: 5 additions & 0 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ impl Poller {
false
}

/// Whether this poller supports checking for the tcp connection failure.
pub fn supports_is_connect_failed(&self) -> bool {
true
}

/// Adds a new file descriptor.
pub fn add(&self, fd: RawFd, ev: Event, mode: PollMode) -> io::Result<()> {
if self.notify.has_fd(fd) {
Expand Down
5 changes: 5 additions & 0 deletions src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ impl Poller {
false
}

/// Whether this poller supports checking for the tcp connection failure.
pub fn supports_is_connect_failed(&self) -> bool {
true
}

/// Adds a file descriptor.
///
/// # Safety
Expand Down

0 comments on commit 808428d

Please sign in to comment.