-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat : add the ability to identify if tcp connection has failed #185
Changes from 8 commits
9cd7902
7d587d8
920ed83
dea4289
903db66
13b09ae
6983e64
2089ce2
3144404
a00420b
68fd1df
166a14f
621584b
159ab1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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(), 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() { | ||
println!("connect failed"); | ||
} | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -371,6 +371,11 @@ impl EventExtra { | |
pub fn is_pri(&self) -> bool { | ||
false | ||
} | ||
|
||
#[inline] | ||
pub fn is_connect_failed(&self) -> bool { | ||
unimplemented!("is connect failed is not supported on kqueue"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
} | ||
} | ||
|
||
pub(crate) fn mode_to_flags(mode: PollMode) -> kqueue::EventFlags { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -332,6 +332,17 @@ impl Event { | |
self.extra.is_pri() | ||
} | ||
|
||
/// Tell if this event is the result of a connect 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. | ||
/// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have an example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
|
||
#[inline] | ||
pub fn is_connect_failed(&self) -> bool { | ||
self.extra.is_connect_failed() | ||
} | ||
|
||
/// Remove any extra information from this event. | ||
#[inline] | ||
pub fn clear_extra(&mut self) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -426,6 +426,13 @@ impl EventExtra { | |
pub fn is_pri(&self) -> bool { | ||
self.flags.contains(PollFlags::PRI) | ||
} | ||
|
||
#[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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks right to me |
||
} | ||
} | ||
|
||
fn cvt_mode_as_remove(mode: PollMode) -> io::Result<bool> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this syntax compatible with Rust 1.63?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated