module std::netsource
TCP connections: a listener accepts connections on IPv4, and a connection reaches a host by its name, IPv6 or IPv4; each is read and written as bytes, through the compiler's al_sys_* functions, one C file a platform (alloyc/src/support/system.c). Natively only: the interpreter has no sockets, and WASI preview 1 opens none.
ListenerConnectionNetErrorConnection::connectListener::openportnext_connectiondisposereadwriteset_timeoutset_timeoutshutdownhandledispose
type Listenersource
exp type Listener : Disposable = struct
A socket listening for connections, closed by disposing it.
Functions
type Connectionsource
exp type Connection : Disposable = struct
One end of a connection, closed by disposing it.
Functions
exp fn Connection::connect( host: &[u8], port: u16, timeout: Duration ) -> Result<Connection, NetError>exp fn read(self connection: &var Connection, buffer: &var [u8]) -> u64exp fn write(self connection: &var Connection, bytes: &[u8]) -> boolexp fn set_timeout(self connection: &var Connection, seconds: u64)exp fn set_timeout(self connection: &var Connection, limit: Duration)exp fn shutdown(self connection: &Connection)exp fn handle(self connection: &Connection) -> i64exp fn dispose(self connection: &var Connection)
type NetErrorsource
exp type NetError = enum
Why a connection was not made.
Variants
Resolvethe host has no address
Connectno address took the connection
Timeoutthe time ran out first
Unsupportedno sockets here: WASI, the interpreter
fn Connection::connectsource
exp fn Connection::connect(
host: &[u8],
port: u16,
timeout: Duration
) -> Result<Connection, NetError>
Looks the host up (IPv6 and IPv4, in the order the resolver gives) and
connects to the first address taking the connection, each tried for at
most 3 seconds within the timeout, the last for what is left. A statically linked Linux program
resolves through /etc/hosts and /etc/resolv.conf alone, so mDNS's
.local names do not resolve there.
fn Listener::opensource
exp fn Listener::open(address: &[u8], port: u16) -> Option<Listener>
Listens on the IPv4 address (127.0.0.1, or 0.0.0.0 for every
interface) and port, 0 for any free one; None when it cannot.
fn portsource
exp fn port(self listener: &Listener) -> u16
The port it listens on.
fn next_connectionsource
exp fn next_connection(self listener: &Listener) -> Option<Connection>
Waits for the next connection; None when accepting fails. Several tasks may wait on one listener, each taking its own connections.
fn disposesource
exp fn dispose(self listener: &var Listener)
fn readsource
exp fn read(self connection: &var Connection, buffer: &var [u8]) -> u64
Reads what has arrived into the buffer, waiting for some; how many bytes, 0 when the other end closed or after an error or a timeout.
fn writesource
exp fn write(self connection: &var Connection, bytes: &[u8]) -> bool
Writes all the bytes; false when the connection failed.
fn set_timeoutsource
exp fn set_timeout(self connection: &var Connection, seconds: u64)
Makes reads and writes waiting longer than the seconds fail.
fn set_timeoutsource
exp fn set_timeout(self connection: &var Connection, limit: Duration)
Makes reads and writes waiting longer than the limit, to the millisecond, fail; none waits for ever.
fn shutdownsource
exp fn shutdown(self connection: &Connection)
Ends the connection both ways, from any task: a read or write waiting on it returns at once, failed. How another task cancels an exchange.
fn handlesource
exp fn handle(self connection: &Connection) -> i64
The operating system's handle, for code layering a protocol over it.
fn disposesource
exp fn dispose(self connection: &var Connection)