From 7d7760190f9b547e3cc6f38ffec97bead719926f Mon Sep 17 00:00:00 2001 From: skyoxZ Date: Thu, 4 Apr 2024 06:51:14 +0800 Subject: [PATCH 1/6] Remove received bytes length check --- .../src/System/Net/Sockets/UDPClient.cs | 32 +++---------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs index 7646d0ae458d3..c71bbaa82eaa3 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs @@ -379,14 +379,7 @@ public byte[] EndReceive(IAsyncResult asyncResult, ref IPEndPoint? remoteEP) // Because we don't return the actual length, we need to ensure the returned buffer // has the appropriate length. - if (received < MaxUDPSize) - { - byte[] newBuffer = new byte[received]; - Buffer.BlockCopy(_buffer, 0, newBuffer, 0, received); - return newBuffer; - } - - return _buffer; + return _buffer.AsSpan(0, reeceived).ToArray(); } // Joins a multicast address group. @@ -623,11 +616,7 @@ public Task ReceiveAsync() async Task WaitAndWrap(Task task) { SocketReceiveFromResult result = await task.ConfigureAwait(false); - - byte[] buffer = result.ReceivedBytes < MaxUDPSize ? - _buffer.AsSpan(0, result.ReceivedBytes).ToArray() : - _buffer; - + byte[] buffer = _buffer.AsSpan(0, result.ReceivedBytes).ToArray(); return new UdpReceiveResult(buffer, (IPEndPoint)result.RemoteEndPoint); } } @@ -653,11 +642,7 @@ public ValueTask ReceiveAsync(CancellationToken cancellationTo async ValueTask WaitAndWrap(ValueTask task) { SocketReceiveFromResult result = await task.ConfigureAwait(false); - - byte[] buffer = result.ReceivedBytes < MaxUDPSize ? - _buffer.AsSpan(0, result.ReceivedBytes).ToArray() : - _buffer; - + byte[] buffer = _buffer.AsSpan(0, result.ReceivedBytes).ToArray(); return new UdpReceiveResult(buffer, (IPEndPoint)result.RemoteEndPoint); } } @@ -842,17 +827,10 @@ public byte[] Receive([NotNull] ref IPEndPoint? remoteEP) int received = Client.ReceiveFrom(_buffer, MaxUDPSize, 0, ref tempRemoteEP); remoteEP = (IPEndPoint)tempRemoteEP; - + // because we don't return the actual length, we need to ensure the returned buffer // has the appropriate length. - - if (received < MaxUDPSize) - { - byte[] newBuffer = new byte[received]; - Buffer.BlockCopy(_buffer, 0, newBuffer, 0, received); - return newBuffer; - } - return _buffer; + return _buffer.AsSpan(0, received).ToArray(); } From 3d696f17bd4ce87ec3f1ce0575e2195d89b1d10e Mon Sep 17 00:00:00 2001 From: skyoxZ Date: Thu, 4 Apr 2024 06:57:28 +0800 Subject: [PATCH 2/6] delete whitespace --- .../System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs index c71bbaa82eaa3..5aea30ef1a3ec 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs @@ -827,7 +827,7 @@ public byte[] Receive([NotNull] ref IPEndPoint? remoteEP) int received = Client.ReceiveFrom(_buffer, MaxUDPSize, 0, ref tempRemoteEP); remoteEP = (IPEndPoint)tempRemoteEP; - + // because we don't return the actual length, we need to ensure the returned buffer // has the appropriate length. return _buffer.AsSpan(0, received).ToArray(); From 86216c5f80e54ea08799ac3d713a04ea9ad20483 Mon Sep 17 00:00:00 2001 From: skyoxZ Date: Thu, 4 Apr 2024 07:16:37 +0800 Subject: [PATCH 3/6] fix typo --- .../System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs index 5aea30ef1a3ec..8d628720d7e43 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs @@ -379,7 +379,7 @@ public byte[] EndReceive(IAsyncResult asyncResult, ref IPEndPoint? remoteEP) // Because we don't return the actual length, we need to ensure the returned buffer // has the appropriate length. - return _buffer.AsSpan(0, reeceived).ToArray(); + return _buffer.AsSpan(0, received).ToArray(); } // Joins a multicast address group. From 66e51aca8d0f511c1bcff4bff30c95af43716a84 Mon Sep 17 00:00:00 2001 From: skyoxZ Date: Thu, 11 Apr 2024 21:26:43 +0800 Subject: [PATCH 4/6] Improve tests --- .../tests/FunctionalTests/UdpClientTest.cs | 85 +++++++++++-------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs index 1b90ef0d688d4..8d4d43bdf2167 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; @@ -517,11 +518,12 @@ public void Send_Receive_Success(bool ipv4) using (var receiver = new UdpClient(new IPEndPoint(address, 0))) using (var sender = new UdpClient(new IPEndPoint(address, 0))) { - sender.Send(new byte[1], 1, new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port)); - AssertReceive(receiver); + byte[] data = [1, 2, 3]; + sender.Send(data, 2, new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port)); + AssertReceive(receiver, [1, 2]); - sender.Send(new ReadOnlySpan(new byte[1]), new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port)); - AssertReceive(receiver); + sender.Send(new ReadOnlySpan(data), new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port)); + AssertReceive(receiver, data); } } @@ -536,11 +538,12 @@ public void Send_Receive_With_HostName_Success(bool ipv4) using (var receiver = new UdpClient(new IPEndPoint(address, 0))) using (var sender = new UdpClient(new IPEndPoint(address, 0))) { - sender.Send(new byte[1], 1, "localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port); - AssertReceive(receiver); + byte[] data = [1, 2, 3]; + sender.Send(data, 2, "localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port); + AssertReceive(receiver, [1, 2]); - sender.Send(new ReadOnlySpan(new byte[1]), "localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port); - AssertReceive(receiver); + sender.Send(new ReadOnlySpan(data), "localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port); + AssertReceive(receiver, data); } } @@ -551,20 +554,22 @@ public void Send_Receive_Connected_Success() using (var receiver = new UdpClient("localhost", 0)) using (var sender = new UdpClient("localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port)) { - sender.Send(new byte[1], 1); - AssertReceive(receiver); + byte[] data = [1, 2, 3]; + + sender.Send(data, 2); + AssertReceive(receiver, [1, 2]); - sender.Send(new ReadOnlySpan(new byte[1])); - AssertReceive(receiver); + sender.Send(new ReadOnlySpan(data)); + AssertReceive(receiver, data); } } - private static void AssertReceive(UdpClient receiver) + private static void AssertReceive(UdpClient receiver, byte[] sentData) { IPEndPoint remoteEP = null; byte[] data = receiver.Receive(ref remoteEP); Assert.NotNull(remoteEP); - Assert.InRange(data.Length, 1, int.MaxValue); + Assert.True(Enumerable.SequenceEqual(sentData, data)); } [Theory] @@ -589,16 +594,17 @@ public void Send_Available_Success(bool ipv4) public void BeginEndSend_BeginEndReceive_Success(bool ipv4) { IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback; + byte[] sentData = [1, 2, 3]; using (var receiver = new UdpClient(new IPEndPoint(address, 0))) using (var sender = new UdpClient(new IPEndPoint(address, 0))) { - sender.EndSend(sender.BeginSend(new byte[1], 1, new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port), null, null)); + sender.EndSend(sender.BeginSend(sentData, 2, new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port), null, null)); IPEndPoint remoteEP = null; byte[] data = receiver.EndReceive(receiver.BeginReceive(null, null), ref remoteEP); Assert.NotNull(remoteEP); - Assert.InRange(data.Length, 1, int.MaxValue); + Assert.True(Enumerable.SequenceEqual(data, new byte[] {1, 2})); } } @@ -606,15 +612,17 @@ public void BeginEndSend_BeginEndReceive_Success(bool ipv4) [PlatformSpecific(TestPlatforms.Windows)] // "localhost" resolves to IPv4 & IPV6 on Windows, but may resolve to only one of those on Unix public void BeginEndSend_BeginEndReceive_Connected_Success() { + byte[] sentData = [1, 2, 3]; + using (var receiver = new UdpClient("localhost", 0)) using (var sender = new UdpClient("localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port)) { - sender.EndSend(sender.BeginSend(new byte[1], 1, null, null)); + sender.EndSend(sender.BeginSend(sentData, 2, null, null)); IPEndPoint remoteEP = null; byte[] data = receiver.EndReceive(receiver.BeginReceive(null, null), ref remoteEP); Assert.NotNull(remoteEP); - Assert.InRange(data.Length, 1, int.MaxValue); + Assert.True(Enumerable.SequenceEqual(data, new byte[] {1, 2})); } } @@ -624,15 +632,16 @@ public void BeginEndSend_BeginEndReceive_Connected_Success() public async Task SendAsync_ReceiveAsync_Success(bool ipv4) { IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback; + byte[] data = [1, 2, 3]; using (var receiver = new UdpClient(new IPEndPoint(address, 0))) using (var sender = new UdpClient(new IPEndPoint(address, 0))) { - await sender.SendAsync(new byte[1], 1, new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port)); - await AssertReceiveAsync(receiver); + await sender.SendAsync(data, 2, new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port)); + await AssertReceiveAsync(receiver, [1, 2]); - await sender.SendAsync(new ReadOnlyMemory(new byte[1]), new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port)); - await AssertReceiveAsync(receiver); + await sender.SendAsync(new ReadOnlyMemory(data), new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port)); + await AssertReceiveAsync(receiver, data); } } @@ -643,15 +652,16 @@ public async Task SendAsync_ReceiveAsync_Success(bool ipv4) public async Task SendAsync_ReceiveAsync_With_HostName_Success(bool ipv4) { IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback; + byte[] data = [1, 2, 3]; using (var receiver = new UdpClient(new IPEndPoint(address, 0))) using (var sender = new UdpClient(new IPEndPoint(address, 0))) { - await sender.SendAsync(new byte[1], "localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port); - await AssertReceiveAsync(receiver); + await sender.SendAsync(data, "localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port); + await AssertReceiveAsync(receiver, data); - await sender.SendAsync(new ReadOnlyMemory(new byte[1]), "localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port); - await AssertReceiveAsync(receiver); + await sender.SendAsync(new ReadOnlyMemory(data), "localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port); + await AssertReceiveAsync(receiver, data); } } @@ -675,29 +685,30 @@ public async Task ReceiveAsync_Cancel_Throw(bool ipv4) [PlatformSpecific(TestPlatforms.Windows)] // "localhost" resolves to IPv4 & IPV6 on Windows, but may resolve to only one of those on Unix public async Task SendAsync_ReceiveAsync_Connected_Success() { + byte[] data = [1, 2, 3]; + using (var receiver = new UdpClient("localhost", 0)) using (var sender = new UdpClient("localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port)) { - await sender.SendAsync(new byte[1], 1); - await AssertReceiveAsync(receiver); + await sender.SendAsync(data, 2); + await AssertReceiveAsync(receiver, [1, 2]); - await sender.SendAsync(new ReadOnlyMemory(new byte[1])); - await AssertReceiveAsync(receiver); + await sender.SendAsync(new ReadOnlyMemory(data)); + await AssertReceiveAsync(receiver, data); - await sender.SendAsync(new ReadOnlyMemory(new byte[1]), null); - await AssertReceiveAsync(receiver); + await sender.SendAsync(new ReadOnlyMemory(data), null); + await AssertReceiveAsync(receiver, data); - await sender.SendAsync(new ReadOnlyMemory(new byte[1]), null, 0); - await AssertReceiveAsync(receiver); + await sender.SendAsync(new ReadOnlyMemory(data), null, 2); + await AssertReceiveAsync(receiver, [1, 2]); } } - private static async Task AssertReceiveAsync(UdpClient receiver) + private static async Task AssertReceiveAsync(UdpClient receiver, byte[] sentData) { UdpReceiveResult result = await receiver.ReceiveAsync(); Assert.NotNull(result.RemoteEndPoint); - Assert.NotNull(result.Buffer); - Assert.InRange(result.Buffer.Length, 1, int.MaxValue); + Assert.True(Enumerable.SequenceEqual(sentData, result.Buffer)); } [Fact] From a263c3809d95e13b4a6bddaaaede6e707407c7da Mon Sep 17 00:00:00 2001 From: skyoxZ Date: Thu, 11 Apr 2024 21:45:11 +0800 Subject: [PATCH 5/6] Update UdpClientTest.cs --- .../System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs index 8d4d43bdf2167..8136553b03322 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs @@ -699,8 +699,8 @@ public async Task SendAsync_ReceiveAsync_Connected_Success() await sender.SendAsync(new ReadOnlyMemory(data), null); await AssertReceiveAsync(receiver, data); - await sender.SendAsync(new ReadOnlyMemory(data), null, 2); - await AssertReceiveAsync(receiver, [1, 2]); + await sender.SendAsync(new ReadOnlyMemory(data), null, 0); + await AssertReceiveAsync(receiver, data); } } From 5cef840c880951c384f5ba9d5940cc88a2116155 Mon Sep 17 00:00:00 2001 From: skyoxZ Date: Thu, 11 Apr 2024 21:55:02 +0800 Subject: [PATCH 6/6] Update UdpClientTest.cs --- .../tests/FunctionalTests/UdpClientTest.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs index 8136553b03322..4b8db5e71d6d2 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/UdpClientTest.cs @@ -594,17 +594,17 @@ public void Send_Available_Success(bool ipv4) public void BeginEndSend_BeginEndReceive_Success(bool ipv4) { IPAddress address = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback; - byte[] sentData = [1, 2, 3]; + byte[] data = [1, 2, 3]; using (var receiver = new UdpClient(new IPEndPoint(address, 0))) using (var sender = new UdpClient(new IPEndPoint(address, 0))) { - sender.EndSend(sender.BeginSend(sentData, 2, new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port), null, null)); + sender.EndSend(sender.BeginSend(data, 2, new IPEndPoint(address, ((IPEndPoint)receiver.Client.LocalEndPoint).Port), null, null)); IPEndPoint remoteEP = null; - byte[] data = receiver.EndReceive(receiver.BeginReceive(null, null), ref remoteEP); + byte[] receivedData = receiver.EndReceive(receiver.BeginReceive(null, null), ref remoteEP); Assert.NotNull(remoteEP); - Assert.True(Enumerable.SequenceEqual(data, new byte[] {1, 2})); + Assert.True(Enumerable.SequenceEqual(receivedData, new byte[] {1, 2})); } } @@ -612,17 +612,17 @@ public void BeginEndSend_BeginEndReceive_Success(bool ipv4) [PlatformSpecific(TestPlatforms.Windows)] // "localhost" resolves to IPv4 & IPV6 on Windows, but may resolve to only one of those on Unix public void BeginEndSend_BeginEndReceive_Connected_Success() { - byte[] sentData = [1, 2, 3]; + byte[] data = [1, 2, 3]; using (var receiver = new UdpClient("localhost", 0)) using (var sender = new UdpClient("localhost", ((IPEndPoint)receiver.Client.LocalEndPoint).Port)) { - sender.EndSend(sender.BeginSend(sentData, 2, null, null)); + sender.EndSend(sender.BeginSend(data, 2, null, null)); IPEndPoint remoteEP = null; - byte[] data = receiver.EndReceive(receiver.BeginReceive(null, null), ref remoteEP); + byte[] receivedData = receiver.EndReceive(receiver.BeginReceive(null, null), ref remoteEP); Assert.NotNull(remoteEP); - Assert.True(Enumerable.SequenceEqual(data, new byte[] {1, 2})); + Assert.True(Enumerable.SequenceEqual(receivedData, new byte[] {1, 2})); } }