Skip to content
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

net: add SO_LINGER option for TcpStream #1402

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::net::{self, Shutdown, SocketAddr};
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
use std::time::Duration;

use crate::io_source::IoSource;
use crate::net::TcpSocket;
Expand Down Expand Up @@ -123,6 +124,40 @@ impl TcpStream {
self.inner.nodelay()
}

/// Sets the value for the `SO_LINGER` option on this socket.
///
/// When set, the closing/shutting down of the socket will not return
/// until all queued messages for the socket have been successfully
/// sent or the linger timeout has been reached.
pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should stick to the same API as std lib.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So for tokio::net TcpStream @carllerche mentioned to me the plan was to follow std lib's api but it was abandoned.
I assumed we want mio's TcpStream to be similar to tokio's.

If we only add From traits here then we'd still have to have deal with ManuallyDrop at tokio TcpStream layer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Thomasdezeeuw what is the std lib API? I don't see one? Could you clarify what API you mean?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carllerche that is my point. Not having set_linger on TcpStream.

let sock = std::mem::ManuallyDrop::new(self.to_sock());

sock.set_linger(dur)
}

/// Gets the value of the `SO_LINGER` option on this socket.
///
/// For more information about this option, see [`set_linger`][link].
///
/// [link]: #method.set_linger
pub fn linger(&self) -> io::Result<Option<Duration>> {
let sock = std::mem::ManuallyDrop::new(self.to_sock());

sock.get_linger()
}

fn to_sock(&self) -> TcpSocket {
zekisherif marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(windows)]
{
unsafe { TcpSocket::from_raw_socket(self.as_raw_socket()) }
}

#[cfg(unix)]
{
unsafe { TcpSocket::from_raw_fd(self.as_raw_fd()) }
}
}

/// Sets the value for the `IP_TTL` option on this socket.
///
/// This value sets the time-to-live field that is used in every packet sent
Expand Down
13 changes: 13 additions & 0 deletions tests/tcp_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,3 +806,16 @@ fn set_linger_zero(socket: &TcpStream) {
s.set_linger(Some(Duration::from_millis(0))).unwrap();
std::mem::drop(s);
}

#[test]
fn set_linger() {
let listener = net::TcpListener::bind(any_local_address()).unwrap();

let stream = TcpStream::connect(listener.local_addr().unwrap()).unwrap();

stream.set_linger(Some(Duration::from_secs(1))).unwrap();
assert_eq!(stream.linger().unwrap().unwrap().as_secs(), 1);

stream.set_linger(None).unwrap();
assert!(stream.linger().unwrap().is_none());
}