From cb8c7860e7b043526345eb87ae288b34f2fbde97 Mon Sep 17 00:00:00 2001 From: rhirano0715 Date: Sun, 4 Jun 2023 14:33:20 +0900 Subject: [PATCH 1/4] Unified to throw NotSupportedException when SendFile() for connectionless sockets The issue was that the Socket.SendFile() threw inconsistent exceptions when the platform was Windows and the protocol was UDP. The first call would throw a SocketException, while the second call would throw a NotSupportedException. With this commit, SendFile() will consistently throw NotSupportException on all platforms when the protocol is UDP. Fix #47472 --- .../src/System/Net/Sockets/Socket.Tasks.cs | 5 +++++ .../src/System/Net/Sockets/Socket.cs | 10 ++++++++++ .../tests/FunctionalTests/SendFile.cs | 6 ++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs index 9631bb66e8ca5e..f6aee31735daa1 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs @@ -670,6 +670,11 @@ public ValueTask SendFileAsync(string? fileName, CancellationToken cancellationT /// An error occurred when attempting to access the socket. public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory preBuffer, ReadOnlyMemory postBuffer, TransmitFileOptions flags, CancellationToken cancellationToken = default) { + if (_protocolType == ProtocolType.Udp) + { + throw new NotSupportedException(SR.net_notconnected); + } + if (cancellationToken.IsCancellationRequested) { return ValueTask.FromCanceled(cancellationToken); diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index 6c10d2c5c3eec6..6f104197da4139 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -1254,6 +1254,11 @@ public void SendFile(string? fileName, byte[]? preBuffer, byte[]? postBuffer, Tr /// An error occurred when attempting to access the socket. public void SendFile(string? fileName, ReadOnlySpan preBuffer, ReadOnlySpan postBuffer, TransmitFileOptions flags) { + if (_protocolType == ProtocolType.Udp) + { + throw new NotSupportedException(SR.net_notconnected); + } + ThrowIfDisposed(); if (!Connected) @@ -2333,6 +2338,11 @@ public IAsyncResult BeginSendFile(string? fileName, AsyncCallback? callback, obj public IAsyncResult BeginSendFile(string? fileName, byte[]? preBuffer, byte[]? postBuffer, TransmitFileOptions flags, AsyncCallback? callback, object? state) { + if (_protocolType == ProtocolType.Udp) + { + throw new NotSupportedException(SR.net_notconnected); + } + ThrowIfDisposed(); if (!Connected) diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs index e3d35f285e0630..ced3362e7fd040 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs @@ -77,16 +77,14 @@ public async Task UdpConnection_ThrowsException(bool usePreAndPostbufferOverload client.Connect(listener.LocalEndPoint); - SocketException ex; if (usePreAndPostbufferOverload) { - ex = await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path, Array.Empty(), Array.Empty(), TransmitFileOptions.UseDefaultWorkerThread)); + await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path, Array.Empty(), Array.Empty(), TransmitFileOptions.UseDefaultWorkerThread)); } else { - ex = await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path)); + await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path)); } - Assert.Equal(SocketError.NotConnected, ex.SocketErrorCode); } public static IEnumerable SendFile_MemberData() From a17198628e16fac4aeb48315ea85120afdf4d6b4 Mon Sep 17 00:00:00 2001 From: rhirano0715 Date: Wed, 19 Jul 2023 19:39:16 +0900 Subject: [PATCH 2/4] Change to throws `NotSupportedException` if `!IsConnectionOriented` or `!Connected`. Before:. Throws `NotSupportedException` on UDP. After: Throws `NotSupportedException` if `!IsConnectionOriented` or `!Connected`. --- .../src/System/Net/Sockets/Socket.Tasks.cs | 9 ++------- .../src/System/Net/Sockets/Socket.cs | 12 +----------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs index f6aee31735daa1..e5078e906c129e 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs @@ -670,11 +670,6 @@ public ValueTask SendFileAsync(string? fileName, CancellationToken cancellationT /// An error occurred when attempting to access the socket. public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory preBuffer, ReadOnlyMemory postBuffer, TransmitFileOptions flags, CancellationToken cancellationToken = default) { - if (_protocolType == ProtocolType.Udp) - { - throw new NotSupportedException(SR.net_notconnected); - } - if (cancellationToken.IsCancellationRequested) { return ValueTask.FromCanceled(cancellationToken); @@ -682,8 +677,8 @@ public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory preBuffer, if (!IsConnectionOriented) { - var soex = new SocketException((int)SocketError.NotConnected); - return ValueTask.FromException(soex); + var nsex = new NotSupportedException(SR.net_notconnected); + return ValueTask.FromException(nsex); } int packetsCount = 0; diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index 8082397d4f9d79..9c9afb9ffd4241 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -1245,14 +1245,9 @@ public void SendFile(string? fileName, byte[]? preBuffer, byte[]? postBuffer, Tr /// An error occurred when attempting to access the socket. public void SendFile(string? fileName, ReadOnlySpan preBuffer, ReadOnlySpan postBuffer, TransmitFileOptions flags) { - if (_protocolType == ProtocolType.Udp) - { - throw new NotSupportedException(SR.net_notconnected); - } - ThrowIfDisposed(); - if (!Connected) + if (!IsConnectionOriented || !Connected) { throw new NotSupportedException(SR.net_notconnected); } @@ -2329,11 +2324,6 @@ public IAsyncResult BeginSendFile(string? fileName, AsyncCallback? callback, obj public IAsyncResult BeginSendFile(string? fileName, byte[]? preBuffer, byte[]? postBuffer, TransmitFileOptions flags, AsyncCallback? callback, object? state) { - if (_protocolType == ProtocolType.Udp) - { - throw new NotSupportedException(SR.net_notconnected); - } - ThrowIfDisposed(); if (!Connected) From 7bd5d7c693394e1d3e0005e9f94e29e9d911e483 Mon Sep 17 00:00:00 2001 From: rhirano0715 Date: Wed, 19 Jul 2023 19:46:05 +0900 Subject: [PATCH 3/4] Changed test case `UdpConnection_ThrowsException` to run regardless of platform. --- .../System.Net.Sockets/tests/FunctionalTests/SendFile.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs index ced3362e7fd040..7a8761397fbf56 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs @@ -62,7 +62,6 @@ public async Task FileDoesNotExist_ThrowsFileNotFoundException(bool useOverloadW [Theory] [InlineData(false)] [InlineData(true)] - [PlatformSpecific(TestPlatforms.Windows)] public async Task UdpConnection_ThrowsException(bool usePreAndPostbufferOverload) { // Create file to send From 3954c40ce3aacbc32d9c15572f907afc92e0fc21 Mon Sep 17 00:00:00 2001 From: rhirano0715 <104247605+rhirano0715@users.noreply.github.com> Date: Tue, 1 Aug 2023 22:09:34 +0900 Subject: [PATCH 4/4] Update src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs Co-authored-by: Karel Zikmund --- .../System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs index e5078e906c129e..3e7f051a18036b 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs @@ -677,8 +677,8 @@ public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory preBuffer, if (!IsConnectionOriented) { - var nsex = new NotSupportedException(SR.net_notconnected); - return ValueTask.FromException(nsex); + var ex = new NotSupportedException(SR.net_notconnected); + return ValueTask.FromException(ex); } int packetsCount = 0;