WasiInMemoryNetwork Class

A network inside the process: the sockets of the components connect to each other and to the host (WasiInMemoryNetwork.ConnectAsync, WasiInMemoryNetwork.Listen), without the network of the machine. It works on every platform, including the browser.

public sealed class WasiInMemoryNetwork : WasiNetwork

Remarks

The addresses of the network are the loopback addresses (127.0.0.0/8 and ::1) and the WasiInMemoryNetwork.LocalAddresses: a socket binds to one of them, or to the unspecified address (all of them). The other addresses are not reachable. The ports are allocated as on a machine: a port 0 is a free port from 49152, and two sockets cannot bind to the same port of the same address.

The names are the WasiInMemoryNetwork.HostNames: localhost is the loopback addresses.

Example

// A network inside the process: the host listens and connects next to the components, without the network of the machine
var network = new WasiInMemoryNetwork();
using var listener = network.Listen(new IPEndPoint(IPAddress.Loopback, 8080));

await using var client = await network.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 8080));
await using var server = await listener.AcceptAsync();
await client.WriteAsync("ping"u8.ToArray());

var buffer = new byte[4];
await server.ReadExactlyAsync(buffer);
Assert.Equal("ping", Encoding.ASCII.GetString(buffer));

var wasi = new WasiComponentContext(new WasiOptions { Network = network });
Inheritance
WasiNetwork WasiInMemoryNetwork

Constructors

NameDescription
WasiInMemoryNetwork()

WasiInMemoryNetwork()

WasiInMemoryNetwork.cs
public WasiInMemoryNetwork()

No documentation is available.

Properties

NameDescription
HostNamesGets the addresses of the host names (compared without case). The default is localhost, whose addresses are 127.0.0.1 and ::1.
LocalAddressesGets the addresses of the network, in addition to the loopback addresses. The default is no address.

Gets the addresses of the host names (compared without case). The default is localhost, whose addresses are 127.0.0.1 and ::1.

public IDictionary<string, IReadOnlyList<IPAddress>> HostNames { get; }

Gets the addresses of the network, in addition to the loopback addresses. The default is no address.

public IList<IPAddress> LocalAddresses { get; }

Property value

IList<IPAddress>

Methods

NameDescription
ConnectAsync(IPEndPoint, CancellationToken)Connects the host to a listening socket of the network, such as a server of a component.
CreateTcpSocket(AddressFamily)Creates a TCP socket.
CreateUdpSocket(AddressFamily)Creates a UDP socket.
Listen(IPEndPoint)Listens on an address of the network for the connections of the components, such as a server of the host.
ResolveAddressesAsync(string, CancellationToken)Returns the IP addresses of a host name, in the order of preference.

ConnectAsync(IPEndPoint, CancellationToken)

WasiInMemoryNetwork.cs

Connects the host to a listening socket of the network, such as a server of a component.

net10.0

public ValueTask<Stream> ConnectAsync(IPEndPoint remoteEndPoint, CancellationToken cancellationToken = default)

net11.0

public ValueTask<Stream> ConnectAsync(IPEndPoint remoteEndPoint, CancellationToken cancellationToken = default)

Parameters

remoteEndPoint IPEndPoint
The address of the listening socket.
cancellationToken CancellationToken
The token to cancel the connection.

Returns

ValueTask<Stream> The stream of the connection: dispose it to close the connection.

Exceptions

SocketException
No socket listens on the address (SocketError.ConnectionRefused).

CreateTcpSocket(AddressFamily)

WasiInMemoryNetwork.cs

Creates a TCP socket.

public override WasiTcpSocket CreateTcpSocket(AddressFamily addressFamily)

Parameters

addressFamily AddressFamily
The address family: AddressFamily.InterNetwork or AddressFamily.InterNetworkV6 (only IPv6, not the IPv4-mapped addresses).

Returns

WasiTcpSocket

Exceptions

SocketException
The socket cannot be created.

CreateUdpSocket(AddressFamily)

WasiInMemoryNetwork.cs

Creates a UDP socket.

public override WasiUdpSocket CreateUdpSocket(AddressFamily addressFamily)

Parameters

addressFamily AddressFamily
The address family: AddressFamily.InterNetwork or AddressFamily.InterNetworkV6 (only IPv6, not the IPv4-mapped addresses).

Returns

WasiUdpSocket

Exceptions

SocketException
The socket cannot be created.

Listen(IPEndPoint)

WasiInMemoryNetwork.cs

Listens on an address of the network for the connections of the components, such as a server of the host.

public WasiInMemoryTcpListener Listen(IPEndPoint localEndPoint)

Parameters

localEndPoint IPEndPoint
The address. The port 0 is a free port.

Returns

WasiInMemoryTcpListener The listener: dispose it to stop listening.

Exceptions

SocketException
The address is not an address of the network, or is in use.

ResolveAddressesAsync(string, CancellationToken)

WasiInMemoryNetwork.cs

Returns the IP addresses of a host name, in the order of preference.

public override ValueTask<IReadOnlyList<IPAddress>> ResolveAddressesAsync(string name, CancellationToken cancellationToken)

Parameters

name string
The host name: WASI returns the IP addresses itself, without calling this method.
cancellationToken CancellationToken
The token that is cancelled when the component cancels the lookup.

Returns

ValueTask<IReadOnlyList<IPAddress>>

Exceptions

SocketException
The name cannot be resolved: SocketError.HostNotFound, SocketError.TryAgain, SocketError.AccessDenied, ...