Skip to content

Commit 50f31f1

Browse files
eddi0815Thomasdezeeuw
authored andcommitted
Add support for the socket option TCP_CORK
1 parent 271fd2e commit 50f31f1

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/sys/unix.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,57 @@ impl crate::Socket {
12831283
}
12841284
}
12851285

1286+
/// Get the value of the `TCP_CORK` option on this socket.
1287+
///
1288+
/// For more information about this option, see [`set_cork`].
1289+
///
1290+
/// [`set_cork`]: Socket::set_cork
1291+
#[cfg(all(
1292+
feature = "all",
1293+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1294+
))]
1295+
#[cfg_attr(
1296+
docsrs,
1297+
doc(cfg(all(
1298+
feature = "all",
1299+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1300+
)))
1301+
)]
1302+
pub fn cork(&self) -> io::Result<bool> {
1303+
unsafe {
1304+
getsockopt::<Bool>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_CORK)
1305+
.map(|cork| cork != 0)
1306+
}
1307+
}
1308+
1309+
/// Set the value of the `TCP_CORK` option on this socket.
1310+
///
1311+
/// If set, don't send out partial frames. All queued partial frames are
1312+
/// sent when the option is cleared again. There is a 200 millisecond ceiling on
1313+
/// the time for which output is corked by `TCP_CORK`. If this ceiling is reached,
1314+
/// then queued data is automatically transmitted.
1315+
#[cfg(all(
1316+
feature = "all",
1317+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1318+
))]
1319+
#[cfg_attr(
1320+
docsrs,
1321+
doc(cfg(all(
1322+
feature = "all",
1323+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1324+
)))
1325+
)]
1326+
pub fn set_cork(&self, cork: bool) -> io::Result<()> {
1327+
unsafe {
1328+
setsockopt(
1329+
self.as_raw(),
1330+
libc::IPPROTO_TCP,
1331+
libc::TCP_CORK,
1332+
cork as c_int,
1333+
)
1334+
}
1335+
}
1336+
12861337
/// Gets the value for the `SO_BINDTODEVICE` option on this socket.
12871338
///
12881339
/// This value gets the socket binded device's interface name.

tests/socket.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,11 @@ test!(
11371137
mark,
11381138
set_mark(123)
11391139
);
1140+
#[cfg(all(
1141+
feature = "all",
1142+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1143+
))]
1144+
test!(cork, set_cork(true));
11401145
test!(linger, set_linger(Some(Duration::from_secs(10))));
11411146
test!(
11421147
read_timeout,

0 commit comments

Comments
 (0)