-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8ae841
commit c7280f7
Showing
13 changed files
with
322 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Wasi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Concurrent; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Net.Sockets | ||
{ | ||
internal sealed partial class SocketAsyncEngine : IThreadPoolWorkItem | ||
{ | ||
private SocketAsyncEngine() | ||
{ | ||
#pragma warning disable CS4014 | ||
WasiSocketsEventLoop(); | ||
#pragma warning restore CS4014 | ||
} | ||
|
||
private async Task WasiSocketsEventLoop() | ||
{ | ||
try | ||
{ | ||
SocketEventHandler handler = new SocketEventHandler(this); | ||
while (true) | ||
{ | ||
int numEvents = EventBufferCount; | ||
await WaitForAnySocketPollable().ConfigureAwait(false); | ||
|
||
// The native shim is responsible for ensuring this condition. | ||
Debug.Assert(numEvents > 0, $"Unexpected numEvents: {numEvents}"); | ||
|
||
// Only enqueue a work item if the stage is NotScheduled. | ||
// Otherwise there must be a work item already queued or another thread already handling parallelization. | ||
if (handler.HandleSocketEvents(numEvents) && | ||
Interlocked.Exchange( | ||
ref _eventQueueProcessingStage, | ||
EventQueueProcessingStage.Scheduled) == EventQueueProcessingStage.NotScheduled) | ||
{ | ||
ThreadPool.UnsafeQueueUserWorkItem(this, preferLocal: false); | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Environment.FailFast("Exception thrown from SocketAsyncEngine event loop: " + e.ToString(), e); | ||
} | ||
} | ||
|
||
#pragma warning disable CA1822 // TODO remove | ||
private Task WaitForAnySocketPollable() | ||
{ | ||
TaskCompletionSource todo = new TaskCompletionSource(); | ||
/* TODO something like this, but with handle->pollable conversion | ||
Interop.Error err = Interop.Sys.WaitForSocketEvents(_port, handler.Buffer, &numEvents); | ||
if (err != Interop.Error.SUCCESS) | ||
{ | ||
throw new InternalException(err); | ||
}*/ | ||
return todo.Task; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Net.Http.Headers; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System.Runtime.CompilerServices; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
|
||
public static class WasiMainWrapper | ||
{ | ||
public static async Task<int> MainAsync(string[] args) | ||
{ | ||
IPHostEntry ipHostInfo = await Dns.GetHostEntryAsync("example.com"); | ||
IPAddress ipAddress = ipHostInfo.AddressList[0]; | ||
Console.WriteLine($"IP Address: {ipAddress}"); | ||
|
||
IPEndPoint ipEndPoint = new(ipAddress, 80); | ||
using Socket client = new(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); | ||
|
||
await client.ConnectAsync(ipEndPoint); | ||
|
||
// Send message. | ||
var message = @"GET / HTTP/1.1 | ||
Host: example.com | ||
Accept: */* | ||
"; | ||
var messageBytes = Encoding.UTF8.GetBytes(message); | ||
var start = 0; | ||
while (start < messageBytes.Length) | ||
{ | ||
start += await client.SendAsync(messageBytes.AsMemory(start), SocketFlags.None); | ||
Console.WriteLine("TODO poll here"); | ||
} | ||
Console.WriteLine("GET sent"); | ||
|
||
// Receive ack. | ||
var buffer = new byte[2048]; | ||
var received = await client.ReceiveAsync(buffer, SocketFlags.None); | ||
var response = Encoding.UTF8.GetString(buffer, 0, received); | ||
Console.WriteLine(response); | ||
|
||
client.Shutdown(SocketShutdown.Both); | ||
|
||
return 0; | ||
} | ||
|
||
public static int Main(string[] args) | ||
{ | ||
return PollWasiEventLoopUntilResolved((Thread)null!, MainAsync(args)); | ||
|
||
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "PollWasiEventLoopUntilResolved")] | ||
static extern T PollWasiEventLoopUntilResolved<T>(Thread t, Task<T> mainTask); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/mono/sample/wasi/sockets-p2/Wasip2.Sockets.Console.Sample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework> | ||
<_WasiNeedsHttp>true</_WasiNeedsHttp> | ||
<_WasiNeedsSocket>true</_WasiNeedsSocket> | ||
<!-- | ||
<WasmSingleFileBundle>true</WasmSingleFileBundle> | ||
<InvariantGlobalization>true</InvariantGlobalization> | ||
--> | ||
</PropertyGroup> | ||
|
||
<Target Name="RunSample" DependsOnTargets="RunSampleWithWasmtime" /> | ||
</Project> |
Oops, something went wrong.