Unified to throw NotSupportedException when SendFile() for connectionless sockets...
authorrhirano0715 <104247605+rhirano0715@users.noreply.github.com>
Wed, 9 Aug 2023 12:19:38 +0000 (21:19 +0900)
committerGitHub <noreply@github.com>
Wed, 9 Aug 2023 12:19:38 +0000 (05:19 -0700)
* 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

* Change to throws `NotSupportedException` if `!IsConnectionOriented` or `!Connected`.

Before:.

    Throws `NotSupportedException` on UDP.

After:

    Throws `NotSupportedException` if `!IsConnectionOriented` or `!Connected`.

* Changed test case `UdpConnection_ThrowsException` to run regardless of platform.

* Update src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs

Co-authored-by: Karel Zikmund <karelz@microsoft.com>
---------

Co-authored-by: Karel Zikmund <karelz@microsoft.com>
src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs
src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs
src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs

index a04605b..e1891be 100644 (file)
@@ -749,8 +749,8 @@ namespace System.Net.Sockets
 
             if (!IsConnectionOriented)
             {
-                var soex = new SocketException((int)SocketError.NotConnected);
-                return ValueTask.FromException(soex);
+                var ex = new NotSupportedException(SR.net_notconnected);
+                return ValueTask.FromException(ex);
             }
 
             int packetsCount = 0;
index b918ceb..414b0ba 100644 (file)
@@ -1258,7 +1258,7 @@ namespace System.Net.Sockets
         {
             ThrowIfDisposed();
 
-            if (!Connected)
+            if (!IsConnectionOriented || !Connected)
             {
                 throw new NotSupportedException(SR.net_notconnected);
             }
index e3d35f2..7a87613 100644 (file)
@@ -62,7 +62,6 @@ namespace System.Net.Sockets.Tests
         [Theory]
         [InlineData(false)]
         [InlineData(true)]
-        [PlatformSpecific(TestPlatforms.Windows)]
         public async Task UdpConnection_ThrowsException(bool usePreAndPostbufferOverload)
         {
             // Create file to send
@@ -77,16 +76,14 @@ namespace System.Net.Sockets.Tests
 
             client.Connect(listener.LocalEndPoint);
 
-            SocketException ex;
             if (usePreAndPostbufferOverload)
             {
-                ex = await Assert.ThrowsAsync<SocketException>(() => SendFileAsync(client, tempFile.Path, Array.Empty<byte>(), Array.Empty<byte>(), TransmitFileOptions.UseDefaultWorkerThread));
+                await Assert.ThrowsAsync<NotSupportedException>(() => SendFileAsync(client, tempFile.Path, Array.Empty<byte>(), Array.Empty<byte>(), TransmitFileOptions.UseDefaultWorkerThread));
             }
             else
             {
-                ex = await Assert.ThrowsAsync<SocketException>(() => SendFileAsync(client, tempFile.Path));
+                await Assert.ThrowsAsync<NotSupportedException>(() => SendFileAsync(client, tempFile.Path));
             }
-            Assert.Equal(SocketError.NotConnected, ex.SocketErrorCode);
         }
 
         public static IEnumerable<object[]> SendFile_MemberData()