Browse the docs

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#

silk
pub struct SocketError

A 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#

silk
pub code: u32

Implementation Debug for SocketError#

silk
impl Debug for SocketError

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut Writer

Writes SocketError { code: <code> }.

SocketAddress#

silk
pub struct SocketAddress

An IP socket address, including IPv6 flow information and interface scope.

Field endpoint#

silk
pub endpoint: Endpoint

The IP address and port.

Field flowInfo#

silk
pub flowInfo: u32

IPv6 flow information. Ignored for IPv4.

Field scopeId#

silk
pub scopeId: u32

IPv6 interface scope. Ignored for IPv4.

Implementation SocketAddress#

silk
impl SocketAddress

Associated function make#

silk
pub fn make(endpoint: Endpoint) -> SocketAddress

Makes an address with zero IPv6 flow information and scope.

Method lanes#

silk
pub fn lanes(self: &Self) -> [u32; 12]

Returns the twelve flattened canonical lanes of an ip-socket-address.

decodeAddress#

silk
pub unsafe fn decodeAddress(result: &ResultArea, offset: usize) -> SocketAddress

Decodes an initialized canonical ip-socket-address at offset in a result area. The record and its tag must be a valid host result.

Network#

silk
pub struct Network

An owned default-network capability. The runtime controls the addresses it grants.

Implementation Network#

silk
impl Network

Associated function make#

silk
pub fn make() -> Network

Acquires the host's default network capability. This does not grant additional access.

Method handle#

silk
pub fn handle(self: &Self) -> u32

Borrows the network handle for unsafe WASI operations. Ownership stays with this value.

Method connect#

silk
pub effect fn connect(self: &Self, address: &SocketAddress, deadline: Option<Instant>) -> Connection
      ! SocketError ? &mut MonotonicClock

Opens a TCP connection to address, or fails with the host error or timeout code 5.

Method listen#

silk
pub effect fn listen(self: &Self, address: &SocketAddress, deadline: Option<Instant>) -> Listener
      ! SocketError ? &mut MonotonicClock

Binds 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#

silk
impl Drop for Network

Operation drop#

silk
fn drop(self: &mut Self) -> ()

Listener#

silk
pub struct Listener

An owned TCP listener. Drop closes it. Accepted connections own their sockets independently.

Implementation Listener#

silk
impl Listener

Method address#

silk
pub effect fn address(self: &Self) -> SocketAddress ! SocketError

Returns the bound local address, including the port the host chose for a port-zero bind.

Method accept#

silk
pub effect fn accept(self: &mut Self, deadline: Option<Instant>) -> Connection
      ! SocketError ? &mut MonotonicClock

Accepts a connection, polling until one arrives or the deadline expires.

Connection#

silk
pub struct Connection

An owned TCP connection providing portable ByteDuplex operations. Close and drop release input and output streams before releasing their parent socket.

Implementation Connection#

silk
impl Connection

Method close#

silk
pub fn close(self: &mut Self) -> ()

Closes the connection. Repeated calls do nothing.

Method peer#

silk
pub effect fn peer(self: &Self) -> SocketAddress ! SocketError

Returns the remote peer address. Calling it on a closed connection fails with code 9.

Method address#

silk
pub effect fn address(self: &Self) -> SocketAddress ! SocketError

Returns the local address. Calling it on a closed connection fails with code 9.

Implementation Drop for Connection#

silk
impl Drop for Connection

Operation drop#

silk
fn drop(self: &mut Self) -> ()

Implementation ByteDuplex for Connection#

silk
impl ByteDuplex for Connection

Operation readSomeRaw#

silk
unsafe effect fn readSomeRaw(self: &mut Self, output: &mut [u8], deadline: Option<Instant>) -> ReadTransfer
      ! ByteIoError ? &mut MonotonicClock

Operation writeSomeRaw#

silk
unsafe effect fn writeSomeRaw(self: &mut Self, input: &[u8], deadline: Option<Instant>) -> usize
      ! ByteIoError ? &mut MonotonicClock

Operation writeSomeVectoredRaw#

silk
unsafe effect fn writeSomeVectoredRaw(self: &mut Self, inputs: &[&[u8]], deadline: Option<Instant>) -> usize
      ! ByteIoError ? &mut MonotonicClock

Operation flushRaw#

silk
unsafe effect fn flushRaw(self: &mut Self, deadline: Option<Instant>) -> ()
      ! ByteIoError ? &mut MonotonicClock

Operation shutdownWriteRaw#

silk
unsafe effect fn shutdownWriteRaw(self: &mut Self, deadline: Option<Instant>) -> ()
      ! ByteIoError ? &mut MonotonicClock

Operation closeRaw#

silk
unsafe effect fn closeRaw(self: &mut Self) -> () ! ByteIoError

Category: Networking.

Profiles: wasm32-wasip2.

View the source of this page