silk.net.socket_wasi.
TCP connections and listeners through WASI Preview 2 network capabilities.
When to use#
Use Network.make to acquire the host-granted network, then connect or listen. Connections
provide ByteDuplex for portable HTTP, TLS, and other stream protocols.
Details#
The host decides which addresses may be used. Socket operations retain WASI network error codes. Polling honors deadlines on the supplied monotonic clock. Closing a connection drops its streams before its parent socket; drop also closes every remaining resource.
Import as wasi_socket with import silk.net.socket_wasi as wasi_socket.
Public declarations: 6.
SocketError#
pub struct SocketErrorA WASI network error. code is the ordinal of wasi:sockets/network.error-code.
In particular 1 is access denied, 5 timeout, 8 would block, and 14 connection refused.
Field code#
pub code: u32Implementation Debug for SocketError#
impl Debug for SocketErrorOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites SocketError { code: <code> }.
SocketAddress#
pub struct SocketAddressAn IP socket address, including IPv6 flow information and interface scope.
Field endpoint#
pub endpoint: EndpointThe IP address and port.
Field flowInfo#
pub flowInfo: u32IPv6 flow information. Ignored for IPv4.
Field scopeId#
pub scopeId: u32IPv6 interface scope. Ignored for IPv4.
Implementation SocketAddress#
impl SocketAddressAssociated function make#
pub fn make(endpoint: Endpoint) -> SocketAddressMakes an address with zero IPv6 flow information and scope.
Method lanes#
pub fn lanes(self: &Self) -> [u32; 12]Returns the twelve flattened canonical lanes of an ip-socket-address.
decodeAddress#
pub unsafe fn decodeAddress(result: &ResultArea, offset: usize) -> SocketAddressDecodes an initialized canonical ip-socket-address at offset in a result area.
The record and its tag must be a valid host result.
Network#
pub struct NetworkAn owned default-network capability. The runtime controls the addresses it grants.
Implementation Network#
impl NetworkAssociated function make#
pub fn make() -> NetworkAcquires the host's default network capability. This does not grant additional access.
Method handle#
pub fn handle(self: &Self) -> u32Borrows the network handle for unsafe WASI operations. Ownership stays with this value.
Method connect#
pub effect fn connect(self: &Self, address: &SocketAddress, deadline: Option<Instant>) -> Connection
! SocketError ? &mut MonotonicClockOpens a TCP connection to address, or fails with the host error or timeout code 5.
Method listen#
pub effect fn listen(self: &Self, address: &SocketAddress, deadline: Option<Instant>) -> Listener
! SocketError ? &mut MonotonicClockBinds and starts a TCP listener, using the host's default backlog.
Port zero asks the host to choose a port; Listener.address returns it after binding.
Implementation Drop for Network#
impl Drop for NetworkOperation drop#
fn drop(self: &mut Self) -> ()Listener#
pub struct ListenerAn owned TCP listener. Drop closes it. Accepted connections own their sockets independently.
Implementation Listener#
impl ListenerMethod address#
pub effect fn address(self: &Self) -> SocketAddress ! SocketErrorReturns the bound local address, including the port the host chose for a port-zero bind.
Method accept#
pub effect fn accept(self: &mut Self, deadline: Option<Instant>) -> Connection
! SocketError ? &mut MonotonicClockAccepts a connection, polling until one arrives or the deadline expires.
Connection#
pub struct ConnectionAn owned TCP connection providing portable ByteDuplex operations.
Close and drop release input and output streams before releasing their parent socket.
Implementation Connection#
impl ConnectionMethod close#
pub fn close(self: &mut Self) -> ()Closes the connection. Repeated calls do nothing.
Method peer#
pub effect fn peer(self: &Self) -> SocketAddress ! SocketErrorReturns the remote peer address. Calling it on a closed connection fails with code 9.
Method address#
pub effect fn address(self: &Self) -> SocketAddress ! SocketErrorReturns the local address. Calling it on a closed connection fails with code 9.
Implementation Drop for Connection#
impl Drop for ConnectionOperation drop#
fn drop(self: &mut Self) -> ()Implementation ByteDuplex for Connection#
impl ByteDuplex for ConnectionOperation readSomeRaw#
unsafe effect fn readSomeRaw(self: &mut Self, output: &mut [u8], deadline: Option<Instant>) -> ReadTransfer
! ByteIoError ? &mut MonotonicClockOperation writeSomeRaw#
unsafe effect fn writeSomeRaw(self: &mut Self, input: &[u8], deadline: Option<Instant>) -> usize
! ByteIoError ? &mut MonotonicClockOperation writeSomeVectoredRaw#
unsafe effect fn writeSomeVectoredRaw(self: &mut Self, inputs: &[&[u8]], deadline: Option<Instant>) -> usize
! ByteIoError ? &mut MonotonicClockOperation flushRaw#
unsafe effect fn flushRaw(self: &mut Self, deadline: Option<Instant>) -> ()
! ByteIoError ? &mut MonotonicClockOperation shutdownWriteRaw#
unsafe effect fn shutdownWriteRaw(self: &mut Self, deadline: Option<Instant>) -> ()
! ByteIoError ? &mut MonotonicClockOperation closeRaw#
unsafe effect fn closeRaw(self: &mut Self) -> () ! ByteIoErrorCategory: Networking.
Profiles: wasm32-wasip2.