Skip to content

Commit 42281bc

Browse files
committed
more
1 parent 9757368 commit 42281bc

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,10 +1348,12 @@ public void SetHandleNonBlocking()
13481348
//
13491349
if (!_isHandleNonBlocking)
13501350
{
1351+
#if !TARGET_WASI // WASI-libc doesn't have emulate this for tcp handles, see https://github.com/WebAssembly/wasi-libc/issues/536
13511352
if (Interop.Sys.Fcntl.SetIsNonBlocking(_socket, 1) != 0)
13521353
{
13531354
throw new SocketException((int)SocketPal.GetSocketErrorForErrorCode(Interop.Sys.GetLastError()));
13541355
}
1356+
#endif
13551357

13561358
_isHandleNonBlocking = true;
13571359
}

src/mono/sample/wasi/Directory.Build.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040

4141
<WasiCommand Condition="'$(_WasiNeedsHttp)' == 'true'" >$(WasiCommand) --wasi http</WasiCommand>
42+
<WasiCommand Condition="'$(_WasiNeedsSocket)' == 'true'" >$(WasiCommand) --wasi inherit-network --wasi tcp --wasi udp --wasi allow-ip-name-lookup</WasiCommand>
4243
<WasiCommand Condition="'$(WasmSingleFileBundle)' != 'true'" >$(WasiCommand) --dir .</WasiCommand>
4344
<WasiCommand >$(WasiCommand) $(_DotnetWasmName)</WasiCommand>
4445
<WasiCommand Condition="'$(WasmSingleFileBundle)' != 'true'" >$(WasiCommand) $(_SampleProjectName)</WasiCommand>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Net.Http.Headers;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
using System.Threading;
9+
using System.Runtime.CompilerServices;
10+
using System.Net;
11+
using System.Net.Sockets;
12+
using System.Text;
13+
14+
public static class WasiMainWrapper
15+
{
16+
public static async Task<int> MainAsync(string[] args)
17+
{
18+
IPHostEntry ipHostInfo = await Dns.GetHostEntryAsync("example.com");
19+
IPAddress ipAddress = ipHostInfo.AddressList[0];
20+
Console.WriteLine($"IP Address: {ipAddress}");
21+
22+
IPEndPoint ipEndPoint = new(ipAddress, 80);
23+
using Socket client = new(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
24+
25+
await client.ConnectAsync(ipEndPoint);
26+
27+
// Send message.
28+
var message = @"GET / HTTP/1.1
29+
Host: example.com
30+
Accept: */*
31+
32+
";
33+
var messageBytes = Encoding.UTF8.GetBytes(message);
34+
var start = 0;
35+
while (start < messageBytes.Length)
36+
{
37+
start += await client.SendAsync(messageBytes.AsMemory(start), SocketFlags.None);
38+
Console.WriteLine("TODO poll here");
39+
}
40+
Console.WriteLine("GET sent");
41+
42+
// Receive ack.
43+
var buffer = new byte[2048];
44+
var received = await client.ReceiveAsync(buffer, SocketFlags.None);
45+
var response = Encoding.UTF8.GetString(buffer, 0, received);
46+
Console.WriteLine(response);
47+
48+
client.Shutdown(SocketShutdown.Both);
49+
50+
return 0;
51+
}
52+
53+
public static int Main(string[] args)
54+
{
55+
return PollWasiEventLoopUntilResolved((Thread)null!, MainAsync(args));
56+
57+
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "PollWasiEventLoopUntilResolved")]
58+
static extern T PollWasiEventLoopUntilResolved<T>(Thread t, Task<T> mainTask);
59+
}
60+
61+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
4+
<_WasiNeedsHttp>true</_WasiNeedsHttp>
5+
<_WasiNeedsSocket>true</_WasiNeedsSocket>
6+
<!--
7+
<WasmSingleFileBundle>true</WasmSingleFileBundle>
8+
<InvariantGlobalization>true</InvariantGlobalization>
9+
-->
10+
</PropertyGroup>
11+
12+
<Target Name="RunSample" DependsOnTargets="RunSampleWithWasmtime" />
13+
</Project>

src/native/libs/System.Native/pal_networking.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,9 @@ int32_t SystemNative_Socket(int32_t addressFamily, int32_t socketType, int32_t p
27832783

27842784
#ifdef SOCK_CLOEXEC
27852785
platformSocketType |= SOCK_CLOEXEC;
2786+
#endif
2787+
#if defined(TARGET_WASI)
2788+
platformSocketType |= SOCK_NONBLOCK; // WASI sockets are always non-blocking, because in ST we don't have another thread which could be blocked
27862789
#endif
27872790
*createdSocket = socket(platformAddressFamily, platformSocketType, platformProtocolType);
27882791
if (*createdSocket == -1)

0 commit comments

Comments
 (0)