From f47c1b500e4cb9895ae5b34c7050c577aa9fb187 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 2 Feb 2021 13:48:41 +0100 Subject: [PATCH] Respect offset in Socket.ReceiveMessageFrom(saea) on Windows (#47642) Fixes #47637, and adds ReceiveSent_UDP_Success test to both ReceiveFrom and ReceiveMessageFrom --- .../Net/Sockets/SocketAsyncEventArgs.Windows.cs | 2 +- .../tests/FunctionalTests/ReceiveFrom.cs | 42 ++++++++++++++++++++++ .../tests/FunctionalTests/ReceiveMessageFrom.cs | 6 ++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs index def3414..8e99813 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs @@ -563,7 +563,7 @@ namespace System.Net.Sockets Debug.Assert(_singleBufferHandleState == SingleBufferHandleState.None); _singleBufferHandleState = SingleBufferHandleState.InProcess; - _wsaRecvMsgWSABufferArrayPinned[0].Pointer = (IntPtr)bufferPtr; + _wsaRecvMsgWSABufferArrayPinned[0].Pointer = (IntPtr)bufferPtr + _offset; _wsaRecvMsgWSABufferArrayPinned[0].Length = _count; wsaRecvMsgWSABufferArray = _wsaRecvMsgWSABufferArrayPinned; wsaRecvMsgWSABufferCount = 1; diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/ReceiveFrom.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/ReceiveFrom.cs index ce8e3f9..e686dd4 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/ReceiveFrom.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/ReceiveFrom.cs @@ -84,6 +84,48 @@ namespace System.Net.Sockets.Tests } [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task ReceiveSent_UDP_Success(bool ipv4) + { + const int Offset = 10; + const int DatagramSize = 256; + const int DatagramsToSend = 16; + + IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback; + using Socket receiver = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); + using Socket sender = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); + + ConfigureNonBlocking(sender); + ConfigureNonBlocking(receiver); + + receiver.BindToAnonymousPort(address); + sender.BindToAnonymousPort(address); + + byte[] sendBuffer = new byte[DatagramSize]; + var receiveInternalBuffer = new byte[DatagramSize + Offset]; + var emptyBuffer = new byte[Offset]; + ArraySegment receiveBuffer = new ArraySegment(receiveInternalBuffer, Offset, DatagramSize); + + Random rnd = new Random(0); + + IPEndPoint remoteEp = new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0); + + for (int i = 0; i < DatagramsToSend; i++) + { + rnd.NextBytes(sendBuffer); + sender.SendTo(sendBuffer, receiver.LocalEndPoint); + + SocketReceiveFromResult result = await ReceiveFromAsync(receiver, receiveBuffer, remoteEp); + + Assert.Equal(DatagramSize, result.ReceivedBytes); + AssertExtensions.SequenceEqual(emptyBuffer, new ReadOnlySpan(receiveInternalBuffer, 0, Offset)); + AssertExtensions.SequenceEqual(sendBuffer, new ReadOnlySpan(receiveInternalBuffer, Offset, DatagramSize)); + Assert.Equal(sender.LocalEndPoint, result.RemoteEndPoint); + } + } + + [Theory] [InlineData(true)] [InlineData(false)] public async Task ClosedBeforeOperation_Throws_ObjectDisposedException(bool closeOrDispose) diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/ReceiveMessageFrom.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/ReceiveMessageFrom.cs index 49d95c3..a944380 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/ReceiveMessageFrom.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/ReceiveMessageFrom.cs @@ -52,10 +52,9 @@ namespace System.Net.Sockets.Tests [Theory] [InlineData(false)] [InlineData(true)] - public async Task ReceiveSentMessages_UDP_Success(bool ipv4) + public async Task ReceiveSent_UDP_Success(bool ipv4) { - // [ActiveIssue("https://github.com/dotnet/runtime/issues/47637")] - int Offset = UsesSync || !PlatformDetection.IsWindows ? 10 : 0; + const int Offset = 10; const int DatagramSize = 256; const int DatagramsToSend = 16; @@ -74,6 +73,7 @@ namespace System.Net.Sockets.Tests var receiveInternalBuffer = new byte[DatagramSize + Offset]; var emptyBuffer = new byte[Offset]; ArraySegment receiveBuffer = new ArraySegment(receiveInternalBuffer, Offset, DatagramSize); + Random rnd = new Random(0); IPEndPoint remoteEp = new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0); -- 2.7.4