Skip to content

Commit

Permalink
Unified to throw NotSupportedException when SendFile() for connection…
Browse files Browse the repository at this point in the history
…less sockets (#87108)

* 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>
  • Loading branch information
rhirano0715 and karelz authored Aug 9, 2023
1 parent b355725 commit 24f1513
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,8 @@ public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory<byte> preBuffer,

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ public void SendFile(string? fileName, ReadOnlySpan<byte> preBuffer, ReadOnlySpa
{
ThrowIfDisposed();

if (!Connected)
if (!IsConnectionOriented || !Connected)
{
throw new NotSupportedException(SR.net_notconnected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -77,16 +76,14 @@ public async Task UdpConnection_ThrowsException(bool usePreAndPostbufferOverload

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()
Expand Down

0 comments on commit 24f1513

Please sign in to comment.