Merge pull request #213 from tokio-rs/tcp-sock-opts

Expose TCP socket options
This commit is contained in:
Alex Crichton
2017-05-27 12:19:08 -05:00
committed by GitHub
2 changed files with 93 additions and 12 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ appveyor = { repository = "alexcrichton/tokio-core" }
[dependencies]
bytes = "0.4"
log = "0.3"
mio = "0.6.7"
mio = { git = "https://github.com/carllerche/mio" }
scoped-tls = "0.1.0"
slab = "0.3"
iovec = "0.1"
+92 -11
View File
@@ -2,6 +2,7 @@ use std::fmt;
use std::io::{self, Read, Write};
use std::mem;
use std::net::{self, SocketAddr, Shutdown};
use std::time::Duration;
use bytes::{Buf, BufMut};
use futures::stream::Stream;
@@ -369,6 +370,41 @@ impl TcpStream {
self.io.get_ref().nodelay()
}
/// Sets the value of the `SO_RCVBUF` option on this socket.
///
/// Changes the size of the operating system's receive buffer associated
/// with the socket.
pub fn set_recv_buffer_size(&self, size: usize) -> io::Result<()> {
self.io.get_ref().set_recv_buffer_size(size)
}
/// Gets the value of the `SO_RCVBUF` option on this socket.
///
/// For more information about this option, see
/// [`set_recv_buffer_size`][link].
///
/// [link]: #tymethod.set_recv_buffer_size
pub fn recv_buffer_size(&self) -> io::Result<usize> {
self.io.get_ref().recv_buffer_size()
}
/// Sets the value of the `SO_SNDBUF` option on this socket.
///
/// Changes the size of the operating system's send buffer associated with
/// the socket.
pub fn set_send_buffer_size(&self, size: usize) -> io::Result<()> {
self.io.get_ref().set_send_buffer_size(size)
}
/// Gets the value of the `SO_SNDBUF` option on this socket.
///
/// For more information about this option, see [`set_send_buffer`][link].
///
/// [link]: #tymethod.set_send_buffer
pub fn send_buffer_size(&self) -> io::Result<usize> {
self.io.get_ref().send_buffer_size()
}
/// Sets whether keepalive messages are enabled to be sent on this socket.
///
/// On Unix, this option will set the `SO_KEEPALIVE` as well as the
@@ -376,23 +412,23 @@ impl TcpStream {
/// On Windows, this will set the `SIO_KEEPALIVE_VALS` option.
///
/// If `None` is specified then keepalive messages are disabled, otherwise
/// the number of milliseconds specified will be the time to remain idle
/// before sending a TCP keepalive probe.
/// the duration specified will be the time to remain idle before sending a
/// TCP keepalive probe.
///
/// Some platforms specify this value in seconds, so sub-second millisecond
/// Some platforms specify this value in seconds, so sub-second
/// specifications may be omitted.
pub fn set_keepalive_ms(&self, keepalive: Option<u32>) -> io::Result<()> {
self.io.get_ref().set_keepalive_ms(keepalive)
pub fn set_keepalive(&self, keepalive: Option<Duration>) -> io::Result<()> {
self.io.get_ref().set_keepalive(keepalive)
}
/// Returns whether keepalive messages are enabled on this socket, and if so
/// the amount of milliseconds between them.
/// the duration of time between them.
///
/// For more information about this option, see [`set_keepalive_ms`][link].
/// For more information about this option, see [`set_keepalive`][link].
///
/// [link]: #method.set_keepalive_ms
pub fn keepalive_ms(&self) -> io::Result<Option<u32>> {
self.io.get_ref().keepalive_ms()
/// [link]: #tymethod.set_keepalive
pub fn keepalive(&self) -> io::Result<Option<Duration>> {
self.io.get_ref().keepalive()
}
/// Sets the value for the `IP_TTL` option on this socket.
@@ -407,10 +443,55 @@ impl TcpStream {
///
/// For more information about this option, see [`set_ttl`][link].
///
/// [link]: #method.set_ttl
/// [link]: #tymethod.set_ttl
pub fn ttl(&self) -> io::Result<u32> {
self.io.get_ref().ttl()
}
/// Sets the value for the `IPV6_V6ONLY` option on this socket.
///
/// If this is set to `true` then the socket is restricted to sending and
/// receiving IPv6 packets only. In this case two IPv4 and IPv6 applications
/// can bind the same port at the same time.
///
/// If this is set to `false` then the socket can be used to send and
/// receive packets from an IPv4-mapped IPv6 address.
pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
self.io.get_ref().set_only_v6(only_v6)
}
/// Gets the value of the `IPV6_V6ONLY` option for this socket.
///
/// For more information about this option, see [`set_only_v6`][link].
///
/// [link]: #tymethod.set_only_v6
pub fn only_v6(&self) -> io::Result<bool> {
self.io.get_ref().only_v6()
}
/// Sets the linger duration of this socket by setting the SO_LINGER option
pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
self.io.get_ref().set_linger(dur)
}
/// reads the linger duration for this socket by getting the SO_LINGER option
pub fn linger(&self) -> io::Result<Option<Duration>> {
self.io.get_ref().linger()
}
#[deprecated(since = "0.1.8", note = "use set_keepalive")]
#[doc(hidden)]
pub fn set_keepalive_ms(&self, keepalive: Option<u32>) -> io::Result<()> {
#[allow(deprecated)]
self.io.get_ref().set_keepalive_ms(keepalive)
}
#[deprecated(since = "0.1.8", note = "use keepalive")]
#[doc(hidden)]
pub fn keepalive_ms(&self) -> io::Result<Option<u32>> {
#[allow(deprecated)]
self.io.get_ref().keepalive_ms()
}
}
impl Read for TcpStream {