Skip to main content

Module net

Module net 

Source
Expand description

Portable async networking primitives.

This module provides TCP and UDP sockets that integrate with runite’s event-loop-per-thread runtime. The public surface follows the general shape of std::net, but uses async methods for operations that would otherwise block the caller. Unix domain sockets are available from the unix submodule on Unix platforms and are re-exported from this module.

§Network model

Networking operations are tied to the current runite event loop. TcpStream, split halves, UDP sockets, and their pending I/O futures are effectively current-thread values; drive them on the runtime thread that owns the operation, and move work to another thread by sending a macrotask to that thread rather than by migrating the socket future. Linux uses completion-based io_uring operations, macOS aarch64 waits for readiness with kqueue and then performs nonblocking socket calls, and Windows drives overlapped Winsock operations (ConnectEx/AcceptEx, WSASend/WSARecv) through the thread’s I/O completion port.

This differs from Tokio’s default scheduler and async-std: runite has no work-stealing socket executor, and split TCP halves are intended for separate runite tasks on the same runtime thread, not cross-thread ownership.

§Examples

A TCP listener and client can run on the same local runtime loop:

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

runite::spawn(async {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let address = listener.local_addr().unwrap();

    let server = runite::spawn(async move {
        let (mut stream, _) = listener.accept().await.unwrap();
        let mut byte = [0];
        stream.read_exact(&mut byte).await.unwrap();
        stream.write_all(&byte).await.unwrap();
    });

    let mut client = TcpStream::connect(address).await.unwrap();
    client.write_all(b"x").await.unwrap();
    let mut echoed = [0];
    client.read_exact(&mut echoed).await.unwrap();
    assert_eq!(echoed, *b"x");
    server.await.unwrap();
});

runite::run();

Re-exports§

pub use unix::UnixDatagram;Unix
pub use unix::UnixListener;Unix
pub use unix::UnixStream;Unix

Modules§

unixUnix
Async Unix domain socket primitives.

Structs§

Incoming
Stream of inbound TCP connections.
OwnedReadHalf
Owned read half of a TcpStream.
OwnedWriteHalf
Owned write half of a TcpStream.
ReuniteError
Error returned by TcpStream::reunite when the two halves did not originate from the same TcpStream. Returns ownership of both halves.
TcpListener
Async TCP listening socket.
TcpSocket
Configurable TCP socket builder.
TcpStream
Async TCP stream connected to a peer.
UdpSocket
Async UDP socket.