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 : WasiNetworkRemarks
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
WasiNetworkWasiInMemoryNetwork
Constructors
| Name | Description |
|---|---|
| WasiInMemoryNetwork() |
WasiInMemoryNetwork()
WasiInMemoryNetwork.cspublic WasiInMemoryNetwork()No documentation is available.
Properties
| Name | Description |
|---|---|
| HostNames | Gets the addresses of the host names (compared without case). The default is localhost, whose addresses are 127.0.0.1 and ::1. |
| LocalAddresses | Gets the addresses of the network, in addition to the loopback addresses. The default is no address. |
HostNames
WasiInMemoryNetwork.csGets 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; }Property value
LocalAddresses
WasiInMemoryNetwork.csGets the addresses of the network, in addition to the loopback addresses. The default is no address.
Property value
Methods
| Name | Description |
|---|---|
| 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.csConnects 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
remoteEndPointIPEndPoint- The address of the listening socket.
cancellationTokenCancellationToken- 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.csCreates a TCP socket.
public override WasiTcpSocket CreateTcpSocket(AddressFamily addressFamily)Parameters
addressFamilyAddressFamily- The address family:
AddressFamily.InterNetworkorAddressFamily.InterNetworkV6(only IPv6, not the IPv4-mapped addresses).
Returns
Exceptions
SocketException- The socket cannot be created.
CreateUdpSocket(AddressFamily)
WasiInMemoryNetwork.csCreates a UDP socket.
public override WasiUdpSocket CreateUdpSocket(AddressFamily addressFamily)Parameters
addressFamilyAddressFamily- The address family:
AddressFamily.InterNetworkorAddressFamily.InterNetworkV6(only IPv6, not the IPv4-mapped addresses).
Returns
Exceptions
SocketException- The socket cannot be created.
Listen(IPEndPoint)
WasiInMemoryNetwork.csListens 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
localEndPointIPEndPoint- 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.csReturns the IP addresses of a host name, in the order of preference.
public override ValueTask<IReadOnlyList<IPAddress>> ResolveAddressesAsync(string name, CancellationToken cancellationToken)Parameters
namestring- The host name: WASI returns the IP addresses itself, without calling this method.
cancellationTokenCancellationToken- 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, ...