Skip to content

Commit

Permalink
Respect offset in Socket.ReceiveMessageFrom(saea) on Windows (dotnet#…
Browse files Browse the repository at this point in the history
…47642)

Fixes dotnet#47637, and adds ReceiveSent_UDP_Success test to both ReceiveFrom and ReceiveMessageFrom
  • Loading branch information
antonfirsov authored Feb 2, 2021
1 parent 9104f3c commit f47c1b5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ internal unsafe SocketError DoOperationReceiveMessageFrom(Socket socket, SafeSoc
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,48 @@ public async Task ReceiveSent_TCP_Success(bool ipv6)
}
}

[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<byte> receiveBuffer = new ArraySegment<byte>(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<byte>(receiveInternalBuffer, 0, Offset));
AssertExtensions.SequenceEqual(sendBuffer, new ReadOnlySpan<byte>(receiveInternalBuffer, Offset, DatagramSize));
Assert.Equal(sender.LocalEndPoint, result.RemoteEndPoint);
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ public async Task ReceiveSent_TCP_Success(bool ipv6)
[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;

Expand All @@ -74,6 +73,7 @@ public async Task ReceiveSentMessages_UDP_Success(bool ipv4)
var receiveInternalBuffer = new byte[DatagramSize + Offset];
var emptyBuffer = new byte[Offset];
ArraySegment<byte> receiveBuffer = new ArraySegment<byte>(receiveInternalBuffer, Offset, DatagramSize);

Random rnd = new Random(0);

IPEndPoint remoteEp = new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0);
Expand Down

0 comments on commit f47c1b5

Please sign in to comment.