Skip to content

Commit

Permalink
Add support for the socket option TCP_CORK
Browse files Browse the repository at this point in the history
  • Loading branch information
eddi0815 authored and Thomasdezeeuw committed Jan 19, 2022
1 parent 271fd2e commit 50f31f1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,57 @@ impl crate::Socket {
}
}

/// Get the value of the `TCP_CORK` option on this socket.
///
/// For more information about this option, see [`set_cork`].
///
/// [`set_cork`]: Socket::set_cork
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
#[cfg_attr(
docsrs,
doc(cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
)))
)]
pub fn cork(&self) -> io::Result<bool> {
unsafe {
getsockopt::<Bool>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_CORK)
.map(|cork| cork != 0)
}
}

/// Set the value of the `TCP_CORK` option on this socket.
///
/// If set, don't send out partial frames. All queued partial frames are
/// sent when the option is cleared again. There is a 200 millisecond ceiling on
/// the time for which output is corked by `TCP_CORK`. If this ceiling is reached,
/// then queued data is automatically transmitted.
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
#[cfg_attr(
docsrs,
doc(cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
)))
)]
pub fn set_cork(&self, cork: bool) -> io::Result<()> {
unsafe {
setsockopt(
self.as_raw(),
libc::IPPROTO_TCP,
libc::TCP_CORK,
cork as c_int,
)
}
}

/// Gets the value for the `SO_BINDTODEVICE` option on this socket.
///
/// This value gets the socket binded device's interface name.
Expand Down
5 changes: 5 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,11 @@ test!(
mark,
set_mark(123)
);
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
test!(cork, set_cork(true));
test!(linger, set_linger(Some(Duration::from_secs(10))));
test!(
read_timeout,
Expand Down

0 comments on commit 50f31f1

Please sign in to comment.