Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified to throw NotSupportedException when SendFile() for connectionless sockets #87108

Merged
merged 5 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,11 @@ public ValueTask SendFileAsync(string? fileName, CancellationToken cancellationT
/// <exception cref="SocketException">An error occurred when attempting to access the socket.</exception>
public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory<byte> preBuffer, ReadOnlyMemory<byte> postBuffer, TransmitFileOptions flags, CancellationToken cancellationToken = default)
{
if (_protocolType == ProtocolType.Udp)
{
throw new NotSupportedException(SR.net_notconnected);
}
Copy link
Member

@antonfirsov antonfirsov Jul 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the check if (!IsConnectionOriented) do the job already? In #47472 I only tested the sync overload.


if (cancellationToken.IsCancellationRequested)
{
return ValueTask.FromCanceled(cancellationToken);
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,11 @@ public void SendFile(string? fileName, byte[]? preBuffer, byte[]? postBuffer, Tr
/// <exception cref="SocketException">An error occurred when attempting to access the socket.</exception>
public void SendFile(string? fileName, ReadOnlySpan<byte> preBuffer, ReadOnlySpan<byte> postBuffer, TransmitFileOptions flags)
{
if (_protocolType == ProtocolType.Udp)
{
throw new NotSupportedException(SR.net_notconnected);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The async variant checks for if (!IsConnectionOriented) while the sync version checks (!Connected). I think that - instead of limiting the error case explicitly to UDP - we should consolidate these checks, throwing in case of (!IsConnectionOriented || !Connected).


ThrowIfDisposed();

if (!Connected)
Expand Down Expand Up @@ -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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that BeginSendFile calls into SendFileAsync, this check is redundant.


ThrowIfDisposed();

if (!Connected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case should not be [PlatformSpecific(TestPlatforms.Windows)] anymore.

}
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