Aye documentation

std.net

std.net: TCP over IPv4 and IPv6. `listen` gives a `Listener`, whose `accept` gives a `Conn`; `connect` gives a `Conn` too. A host is an IPv4 or IPv6 address (`127.0.0.1`, `::1`) or a name, resolved with `getaddrinfo`. `connectTimeout` is `connect` that gives up after a number of milliseconds.

The C constants that differ between macOS and Linux (the layout of `struct addrinfo`, SOL_SOCKET and its options, errno values, MSG_NOSIGNAL or SO_NOSIGPIPE) are chosen by `@targetOs()` when the program is built. Writes never raise SIGPIPE: a closed peer is an error.

ListenError

pub error ListenError { InvalidAddress, AddressInUse, AccessDenied, UnknownHost, ListenFailed }

ConnectError

pub error ConnectError { InvalidAddress, UnknownHost, ConnectionRefused, Unreachable, TimedOut, ConnectFailed }

AcceptError

pub error AcceptError { AcceptFailed }

ReadError

pub error ReadError { TimedOut, ConnectionReset, WouldBlock, ReadFailed }

WriteError

pub error WriteError { ConnectionClosed, WriteFailed }

Listener

pub struct Listener {
    sock: i32
    portNumber: u16
    pub fn accept(self: Self) AcceptError!Conn
    pub fn fd(self: Self) i32
    pub fn port(self: Self) u16
    pub fn again(self: Self) ListenError!Listener
    pub fn close(self: Self)
}

A socket that waits for connections.

pub fn accept(self: Self) AcceptError!Conn

Waits for the next connection.

pub fn fd(self: Self) i32

The socket's file descriptor, for `poll` (O3.9b). It stays owned by the listener: do not close it.

pub fn port(self: Self) u16

The port it listens on: the one the system picked for port 0.

pub fn again(self: Self) ListenError!Listener

Another listener on the same address and port, for a listener from `listenShared` on Linux, where the kernel spreads the new connections over them (`SO_REUSEPORT`). Elsewhere (macOS does not spread them), or for a listener from `listen`, it is ListenFailed or AddressInUse.

pub fn close(self: Self)

Stops listening and releases the port.

Conn

pub struct Conn {
    sock: i32
    pub fn fd(self: Self) i32
    pub fn read(self: Self, buf: []mut u8) ReadError!usize
    pub fn readTimeout(self: Self, buf: []mut u8, ms: u32) ReadError!usize
    pub fn write(self: Self, bytes: []u8) WriteError!void
    pub fn writeSome(self: Self, bytes: []u8) WriteError!usize
    pub fn setNonBlocking(self: Self) bool
    pub fn close(self: Self)
}

A TCP connection.

pub fn fd(self: Self) i32

The socket's file descriptor, for `poll` (O3.9b). It stays owned by the connection: do not close it.

pub fn read(self: Self, buf: []mut u8) ReadError!usize

Waits for data and reads at most `buf.len` bytes: the number read, 0 when the peer has closed its side. On a non-blocking connection (`setNonBlocking`) with no data there, WouldBlock.

pub fn readTimeout(self: Self, buf: []mut u8, ms: u32) ReadError!usize

`read`, waiting at most `ms` milliseconds for data (`poll`): TimedOut when none came.

pub fn write(self: Self, bytes: []u8) WriteError!void

Writes all of `bytes`; ConnectionClosed when the peer is gone. WriteFailed on a non-blocking connection whose buffer is full.

pub fn writeSome(self: Self, bytes: []u8) WriteError!usize

Writes what the socket takes now, at most `bytes.len`: the number written, 0 when the buffer of a non-blocking connection is full (`setNonBlocking`; wait with `poll` for POLLOUT). A blocking connection waits for room, as `write` does. ConnectionClosed when the peer is gone.

pub fn setNonBlocking(self: Self) bool

Makes the connection non-blocking (`O_NONBLOCK`): `writeSome` gives 0 instead of waiting, and `read` gives WouldBlock when no data is there, so wait with `poll` (on `fd()`) first. False when the system refused.

pub fn close(self: Self)

Closes the connection.

Ready

pub enum Ready { Read, Write }

What `wait` waits for: data to read (or the peer closing), or room to write.

wait

pub fn wait(fd: i32, ready: Ready, ms: u32) bool

Waits at most `ms` milliseconds until the socket `fd` (`Conn.fd()`, `Listener.fd()`, or a socket from a C library) is ready: readable for `Ready.Read` (a listener: a connection to accept), writable for `Ready.Write`. True when it is, also when the socket failed or the peer hung up, so that the next read or write reports it; false when `ms` passed first. `ms` 0 only checks.

listen

pub fn listen(host: []u8, port: u16) ListenError!Listener

Listens on `host` at `port`; port 0 lets the system pick one (`Listener.port`). `host` is a dotted IPv4 address, an IPv6 address (`::1`, `::` for every interface), a host name (`getaddrinfo`), or `0.0.0.0` for every IPv4 interface. `localhost` is 127.0.0.1. A name that does not resolve is UnknownHost; an empty or over-long one is InvalidAddress.

listenShared

pub fn listenShared(host: []u8, port: u16) ListenError!Listener

`listen` that lets `Listener.again` open more listeners on the same address (O3.9c). On Linux it sets `SO_REUSEPORT`, so another `listenShared` of the same user on that port succeeds too and the kernel spreads the new connections over all of them; elsewhere it is `listen`.

connect

pub fn connect(host: []u8, port: u16) ConnectError!Conn

Connects to `host` at `port`. `host` is a dotted IPv4 address, an IPv6 address (`::1`) or a host name (`getaddrinfo`); a name with several addresses is tried address by address. A name that does not resolve is UnknownHost; an empty or over-long one is InvalidAddress.

connectTimeout

pub fn connectTimeout(host: []u8, port: u16, ms: u32) ConnectError!Conn

`connect` that gives up after `ms` milliseconds (per address when a name has several): a non-blocking connect, `poll` for POLLOUT, then `SO_ERROR`. TimedOut when the peer did not answer in time; the other errors are `connect`'s. The returned `Conn` is blocking, as from `connect`.

PollFd

pub extern struct PollFd {
    fd: i32
    events: i16
    revents: i16
}

C's `struct pollfd`, for `poll` on `Listener.fd()` and `Conn.fd()` (`std.http.Server` uses it).