pub struct TcpSocket { /* private fields */ }Expand description
A TCP socket to set up before it connects or listens.
use nuclei::net::TcpSocket;
let socket = TcpSocket::new_v4()?;
socket.set_reuseaddr(true)?;
socket.bind("127.0.0.1:0".parse().unwrap())?;
let addr = socket.local_addr()?;
let listener = socket.listen(128)?;
let client = TcpSocket::new_v4()?;
client.set_nodelay(true)?;
let stream = client.connect(addr).await?;
let (_peer, _) = listener.accept().await?;
assert!(stream.nodelay()?);Implementations§
Source§impl TcpSocket
impl TcpSocket
Sourcepub fn set_reuseaddr(&self, reuseaddr: bool) -> Result<()>
pub fn set_reuseaddr(&self, reuseaddr: bool) -> Result<()>
Sets SO_REUSEADDR: the socket may bind an address still held by a
connection that is closing.
Sourcepub fn set_reuseport(&self, reuseport: bool) -> Result<()>
pub fn set_reuseport(&self, reuseport: bool) -> Result<()>
Sets SO_REUSEPORT: several sockets may bind the same address and
port, and the system shares what comes among them.
Sourcepub fn set_send_buffer_size(&self, size: u32) -> Result<()>
pub fn set_send_buffer_size(&self, size: u32) -> Result<()>
Sets the size of the system’s send buffer (SO_SNDBUF); the system
may round it (Linux doubles it).
Sourcepub fn send_buffer_size(&self) -> Result<u32>
pub fn send_buffer_size(&self) -> Result<u32>
The size of the system’s send buffer (SO_SNDBUF).
Sourcepub fn set_recv_buffer_size(&self, size: u32) -> Result<()>
pub fn set_recv_buffer_size(&self, size: u32) -> Result<()>
Sets the size of the system’s receive buffer (SO_RCVBUF); the
system may round it (Linux doubles it).
Sourcepub fn recv_buffer_size(&self) -> Result<u32>
pub fn recv_buffer_size(&self) -> Result<u32>
The size of the system’s receive buffer (SO_RCVBUF).
Sourcepub fn set_tos(&self, tos: u32) -> Result<()>
pub fn set_tos(&self, tos: u32) -> Result<()>
Sets the type of service of the socket’s IPv4 packets (IP_TOS).
Sourcepub fn set_tclass_v6(&self, tclass: u32) -> Result<()>
pub fn set_tclass_v6(&self, tclass: u32) -> Result<()>
Sets the traffic class of the socket’s IPv6 packets (IPV6_TCLASS).
Sourcepub fn tclass_v6(&self) -> Result<u32>
pub fn tclass_v6(&self) -> Result<u32>
The traffic class of the socket’s IPv6 packets (IPV6_TCLASS).
Sourcepub fn set_only_v6(&self, only_v6: bool) -> Result<()>
pub fn set_only_v6(&self, only_v6: bool) -> Result<()>
Sets IPV6_V6ONLY on an IPv6 socket: with true it takes IPv6 alone,
else IPv4 too, through addresses mapped into IPv6.
§Errors
As setsockopt(2): an IPv4 socket has no such option, and a bound one
may no longer change it.
Sourcepub fn bind_device(&self, interface: Option<&[u8]>) -> Result<()>
pub fn bind_device(&self, interface: Option<&[u8]>) -> Result<()>
Binds the socket to the network interface named interface
(SO_BINDTODEVICE), or, with None, to none.
Sourcepub fn device(&self) -> Result<Option<DeviceName>>
pub fn device(&self) -> Result<Option<DeviceName>>
The network interface the socket is bound to, if any
(SO_BINDTODEVICE).
Source§impl TcpSocket
impl TcpSocket
Sourcepub fn set_keepalive(&self, keepalive: bool) -> Result<()>
pub fn set_keepalive(&self, keepalive: bool) -> Result<()>
Sets SO_KEEPALIVE: the system probes an idle connection.
Sourcepub fn set_linger(&self, linger: Option<Duration>) -> Result<()>
pub fn set_linger(&self, linger: Option<Duration>) -> Result<()>
Sets SO_LINGER: with Some(time) a close waits up to time (whole
seconds) for unsent data; with None it returns at once and the
system sends what is left.
Sourcepub fn set_zero_linger(&self) -> Result<()>
pub fn set_zero_linger(&self) -> Result<()>
Sets SO_LINGER to zero: a close drops unsent data and resets the
connection.
Sourcepub fn linger(&self) -> Result<Option<Duration>>
pub fn linger(&self) -> Result<Option<Duration>>
SO_LINGER: how long a close waits for unsent data, None if it
does not wait.
Sourcepub fn set_nodelay(&self, nodelay: bool) -> Result<()>
pub fn set_nodelay(&self, nodelay: bool) -> Result<()>
Sets TCP_NODELAY: small writes go out at once, not gathered.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
The address the socket is bound to.
Sourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&self) -> Result<Option<Error>>
The socket’s pending error (SO_ERROR), taken.
Sourcepub fn bind(&self, addr: SocketAddr) -> Result<()>
pub fn bind(&self, addr: SocketAddr) -> Result<()>
Sourcepub async fn connect(self, addr: SocketAddr) -> Result<TcpStream>
pub async fn connect(self, addr: SocketAddr) -> Result<TcpStream>
Connects the socket to addr, as a stream.
§Errors
As connect(2): refused, unreachable, or timed out.
Sourcepub fn listen(self, backlog: u32) -> Result<TcpListener>
pub fn listen(self, backlog: u32) -> Result<TcpListener>
Makes the socket a listener, with room for backlog connections not
accepted yet.
§Errors
As listen(2); a socket not bound gets an address of the system’s
choosing on unix, and an error on Windows.
Sourcepub fn from_std_stream(stream: TcpStream) -> TcpSocket
pub fn from_std_stream(stream: TcpStream) -> TcpSocket
Takes std’s stream over as a socket to set up, connected or not.
Source§impl TcpSocket
impl TcpSocket
Sourcepub fn set_keepalive_timing(&self, keepalive: TcpKeepalive) -> Result<()>
pub fn set_keepalive_timing(&self, keepalive: TcpKeepalive) -> Result<()>
Turns keepalive probes on (SO_KEEPALIVE), timed as keepalive says
(TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT; macOS calls the first
TCP_KEEPALIVE).
§Errors
Unsupported where the system has no such options, InvalidInput for a value
past what it takes, and what setsockopt(2) answers (a zero time or count).
Sourcepub fn keepalive_timing(&self) -> Result<TcpKeepalive>
pub fn keepalive_timing(&self) -> Result<TcpKeepalive>
The keepalive’s timing: the system’s values where none was set.
§Errors
Unsupported where the system has no such options.
Sourcepub fn set_user_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub fn set_user_timeout(&self, timeout: Option<Duration>) -> Result<()>
Sets TCP_USER_TIMEOUT (Linux): how long sent bytes may go unacknowledged before
the connection is dropped, in whole milliseconds; None or zero leaves it to the
system.
§Errors
Unsupported off Linux, InvalidInput for a time past what the system takes.
Source§impl TcpSocket
impl TcpSocket
Sourcepub fn set_incoming_cpu(&self, cpu: u32) -> Result<()>
pub fn set_incoming_cpu(&self, cpu: u32) -> Result<()>
Sets SO_INCOMING_CPU (Linux): the CPU whose connections this listener takes among
listeners sharing a port with SO_REUSEPORT, so each core accepts the connections its
own queues received.
§Errors
Unsupported off Linux, InvalidInput for a CPU past what the system takes.
Sourcepub fn incoming_cpu(&self) -> Result<Option<u32>>
pub fn incoming_cpu(&self) -> Result<Option<u32>>
SO_INCOMING_CPU (Linux): the CPU set, or the last to handle the socket’s packets;
None before either.
§Errors
Unsupported off Linux.
Sourcepub fn set_fastopen(&self, queue: u32) -> Result<()>
pub fn set_fastopen(&self, queue: u32) -> Result<()>
Sets TCP_FASTOPEN on a socket that will listen: connections may bring data with their
handshake. Linux keeps up to queue such handshakes waiting; macOS, FreeBSD and Windows
take it as on (above zero) or off.
§Errors
Unsupported where the system has no fast open, InvalidInput for a queue past what
it takes, and what setsockopt(2) answers where the system turned fast open off.
Sourcepub fn fastopen(&self) -> Result<u32>
pub fn fastopen(&self) -> Result<u32>
TCP_FASTOPEN: the queue’s length on Linux, and 1 or 0 where the system takes it as on or
off.
§Errors
Unsupported where the system has no fast open.
Sourcepub fn set_defer_accept(&self, wait: Option<Duration>) -> Result<()>
pub fn set_defer_accept(&self, wait: Option<Duration>) -> Result<()>
Sets TCP_DEFER_ACCEPT (Linux) on a socket that will listen: a connection is handed to
accept only once its first data came, or wait passed (whole seconds, which the
kernel rounds up to its retransmission steps); None hands each over at once.
§Errors
Unsupported off Linux, InvalidInput for a time past what the system takes.