-
Notifications
You must be signed in to change notification settings - Fork 76
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
Add TCP keepalive option #143
Conversation
if status == PendingStatus::Ready { | ||
if let Some(keepalive) = &self.keepalive { | ||
#[cfg(target_os = "windows")] | ||
let socket = unsafe { Socket::from_raw_socket(self.stream.as_raw_socket()) }; | ||
#[cfg(not(target_os = "windows"))] | ||
let socket = unsafe { Socket::from_raw_fd(self.stream.as_raw_fd()) }; | ||
|
||
if let Err(e) = socket.set_tcp_keepalive(keepalive) { | ||
log::warn!("TCP set keepalive error: {}", e); | ||
} | ||
|
||
// Don't drop so the underlying socket is not closed. | ||
forget(socket); | ||
} | ||
} | ||
|
||
status |
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.
I'm wondering why do this here instead of in connect()
/listen()
methods.
Could we get rid of the unsafe
code building the socket with socket2
, and once it's built, get the stream
? instead of building the stream and getting the socket from it?
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.
We cannot do that in listen()
because it concerns the accepted socket, not the listener socket. We could do it in accept()
but mio
does quite a bit of platform dependent stuff in the UNIX path which contains unsafe as well. We would loose that or have to copy and maintain it ourselves. I'd rather avoid that and live with the unsafe code.
(The Windows path just calls std::net::TcpListener::accept()
, though.)
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.
Ok, thanks for your thoughts!
The reason the actual work is done when pending() returns PendingStatus::Ready is that on Windows the option cannot be set during connect (when connect() was called but the socket is not yet connected). Also this way it applies to both outgoing and incoming connections. Signed-off-by: Konrad Gräfe <[email protected]>
Signed-off-by: Konrad Gräfe <[email protected]>
No description provided.