Skip to main content

Module net

Module net 

Source
Expand description

Sockets: TCP, UDP and Unix domain, as Handles.

Each type keeps std’s own socket inside; the handle adds the async operations (accept, connect, send, recv, peek, send_to, recv_from, peek_from) and, for the streams, AsyncRead and AsyncWrite on the handle and on a shared reference to it, so two tasks can read and write one stream at once. A stream’s read_owned and write_owned take the caller’s buffer (any IoBuf or IoBufMut) and give it back, so the driver reads into it and writes from it with no copy, and read_vectored_owned and write_vectored_owned do the same with several buffers in one call; a TCP stream’s write_zero_copy asks the kernel not to copy it either, and gives it back through a Released once the kernel let go of it. send_file sends a range of a file to a stream.

A stream ends with shut, std’s shutdown made once the bytes written behind reached the kernel, or for TCP with abort, which resets the connection now. TcpSocket and UdpSocketBuilder set a socket up before it listens, connects or binds, and TcpKeepalive times a TCP connection’s keepalive probes.

An address is anything ToSocketAddrs takes: numbers are used at once; a name ("localhost:80") is looked up on the blocking pool, so no shard waits on the resolver. lookup_host looks one up alone.

use nuclei::net::{TcpListener, TcpStream};

let listener = TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.get_ref().local_addr()?;
let client = TcpStream::connect(addr).await?;
let (server, _) = listener.accept().await?;
client.send(b"hi").await?;
let mut buf = [0u8; 2];
assert_eq!(server.recv(&mut buf).await?, 2);

Modules§

unix
Unix domain sockets: listeners, streams and datagrams, and UCred, the credentials of a stream’s peer; and Unix pipes (pipe).

Structs§

DeviceName
A network interface’s name, as bind_device takes it: up to 16 bytes, held in place.
OwnedReadHalf
The read half of a stream, owning it with the write half.
OwnedWriteHalf
The write half of a stream, owning it with the read half.
ReadHalf
The read half of a stream, borrowed from it.
Released
The buffer of a TcpStream::write_zero_copy, as a future that resolves to it once the kernel let go of every byte. For a send the kernel did not copy, that is once the peer acknowledged the bytes, which may be long after the write ended.
ReuniteError
Two halves of different streams, given back by OwnedReadHalf::reunite.
TcpKeepalive
When an idle connection’s keepalive probes start, how far apart they go, and how many unanswered ones drop the connection. A field left None keeps the system’s value.
TcpSocket
A TCP socket to set up before it connects or listens.
TcpStreamGenerator
The connections of a listener, as Handle::accept_multi makes them.
UdpSocketBuilder
A UDP socket to set up before it binds: what only a socket not bound yet takes (SO_REUSEPORT among sockets that share a port, IPV6_V6ONLY, a device), and the rest before the first datagram.
UnixSocket
A Unix domain socket, of streams or of datagrams, to bind before it connects, listens or sends.
WriteHalf
The write half of a stream, borrowed from it.

Traits§

ToSocketAddrs
What an operation that takes an address accepts: a SocketAddr, an (IpAddr, u16) pair, a slice or array of addresses, or a string, "host:port" or ("host", port). Numbers are used as they are; a name is looked up on the blocking pool (getaddrinfo), never on the calling thread.

Functions§

lookup_host
Every address host names: numbers at once, a name looked up on the blocking pool. The iterator owns what it walks, so a slice given here is copied into it; the operations that take an address walk it in place.

Type Aliases§

TcpListener
A TCP listener: std’s, with an async accept.
TcpStream
A TCP stream: std’s, with async reads and writes. A few of std’s calls reached through Deref go around the handle (shutdown among them): see Handle.
UdpSocket
A UDP socket: std’s, with async sends and receives. Its connect makes a socket; socket.connect(addr) on one is std’s own (see there).
UnixDatagram
A Unix datagram socket: std’s, with async sends and receives.
UnixListener
A Unix stream listener: std’s, with an async accept.
UnixStream
A Unix stream: std’s, with async reads and writes. A few of std’s calls reached through Deref go around the handle (shutdown among them): see Handle.