Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ConnectAsync with buffer on Windows #79669

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ internal unsafe SocketError DoOperationConnectEx(Socket socket, SafeSocketHandle
handle,
PtrSocketAddressBuffer,
_socketAddress!.Size,
(IntPtr)((byte*)_singleBufferHandle.Pointer + _offset),
(IntPtr)(bufferPtr + _offset),
_count,
out int bytesTransferred,
overlapped);
Expand Down
53 changes: 53 additions & 0 deletions src/libraries/System.Net.Sockets/tests/FunctionalTests/Connect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
using System.Linq;

namespace System.Net.Sockets.Tests
{
Expand Down Expand Up @@ -241,6 +242,58 @@ public static void Connect_ThrowSocketException_Success()
public sealed class ConnectEap : Connect<SocketHelperEap>
{
public ConnectEap(ITestOutputHelper output) : base(output) {}

[Theory]
[PlatformSpecific(TestPlatforms.Windows)]
[InlineData(true)]
[InlineData(false)]
public async Task ConnectAsync_WithData_DataReceived(bool useArrayApi)
{
using Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
IPEndPoint serverEp = (IPEndPoint)listener.LocalEndPoint!;
listener.Listen();

var serverTask = Task.Run(async () =>
{
using Socket handler = await listener.AcceptAsync();
using var cts = new CancellationTokenSource(TestSettings.PassingTestTimeout);
byte[] recvBuffer = new byte[6];
int received = await handler.ReceiveAsync(recvBuffer, SocketFlags.None, cts.Token);
Assert.True(received == 4);

recvBuffer.AsSpan(0, 4).SequenceEqual(new byte[] { 2, 3, 4, 5 });
});

using var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

byte[] buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };

var mre = new ManualResetEventSlim(false);
var saea = new SocketAsyncEventArgs();
saea.RemoteEndPoint = serverEp;

// Slice the buffer to test the offset management:
if (useArrayApi)
{
saea.SetBuffer(buffer, 2, 4);
}
else
{
saea.SetBuffer(buffer.AsMemory(2, 4));
}

saea.Completed += (_, __) => mre.Set();

if (client.ConnectAsync(saea))
{
Assert.True(mre.Wait(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
}

Assert.Equal(SocketError.Success, saea.SocketError);

await serverTask;
}
}

public sealed class ConnectCancellableTask : Connect<SocketHelperCancellableTask>
Expand Down