Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -11,7 +11,7 @@ internal static partial class Interop
{
internal static partial class Winsock
{
[DllImport(Interop.Libraries.Ws2_32, SetLastError = true)]
[DllImport(Libraries.Ws2_32, SetLastError = true)]
internal static extern unsafe SocketError WSARecv(
SafeHandle socketHandle,
WSABuffer* buffer,
Expand All @@ -21,22 +21,6 @@ internal static extern unsafe SocketError WSARecv(
NativeOverlapped* overlapped,
IntPtr completionRoutine);

internal static unsafe SocketError WSARecv(
SafeHandle socketHandle,
ref WSABuffer buffer,
int bufferCount,
out int bytesTransferred,
ref SocketFlags socketFlags,
NativeOverlapped* overlapped,
IntPtr completionRoutine)
{
// We intentionally do NOT copy this back after the function completes:
// We don't want to cause a race in async scenarios.
// The WSABuffer struct should be unchanged anyway.
WSABuffer localBuffer = buffer;
return WSARecv(socketHandle, &localBuffer, bufferCount, out bytesTransferred, ref socketFlags, overlapped, completionRoutine);
}

internal static unsafe SocketError WSARecv(
SafeHandle socketHandle,
Span<WSABuffer> buffers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal static partial class Interop
{
internal static partial class Winsock
{
[DllImport(Interop.Libraries.Ws2_32, SetLastError = true)]
[DllImport(Libraries.Ws2_32, SetLastError = true)]
internal static extern unsafe SocketError WSASend(
SafeHandle socketHandle,
WSABuffer* buffers,
Expand All @@ -21,22 +21,6 @@ internal static extern unsafe SocketError WSASend(
NativeOverlapped* overlapped,
IntPtr completionRoutine);

internal static unsafe SocketError WSASend(
SafeHandle socketHandle,
ref WSABuffer buffer,
int bufferCount,
out int bytesTransferred,
SocketFlags socketFlags,
NativeOverlapped* overlapped,
IntPtr completionRoutine)
{
// We intentionally do NOT copy this back after the function completes:
// We don't want to cause a race in async scenarios.
// The WSABuffer struct should be unchanged anyway.
WSABuffer localBuffer = buffer;
return WSASend(socketHandle, &localBuffer, bufferCount, out bytesTransferred, socketFlags, overlapped, completionRoutine);
}

internal static unsafe SocketError WSASend(
SafeHandle socketHandle,
Span<WSABuffer> buffers,
Expand Down
9 changes: 6 additions & 3 deletions src/libraries/Common/src/System/Threading/Tasks/TaskToApm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static IAsyncResult Begin(Task task, AsyncCallback? callback, object? sta
/// <param name="asyncResult">The IAsyncResult to unwrap.</param>
public static void End(IAsyncResult asyncResult)
{
if (asyncResult is TaskAsyncResult twar)
if (GetTask(asyncResult) is Task t)
{
twar._task.GetAwaiter().GetResult();
t.GetAwaiter().GetResult();
return;
}

Expand All @@ -50,7 +50,7 @@ public static void End(IAsyncResult asyncResult)
/// <param name="asyncResult">The IAsyncResult to unwrap.</param>
public static TResult End<TResult>(IAsyncResult asyncResult)
{
if (asyncResult is TaskAsyncResult twar && twar._task is Task<TResult> task)
if (GetTask(asyncResult) is Task<TResult> task)
{
return task.GetAwaiter().GetResult();
}
Expand All @@ -59,6 +59,9 @@ public static TResult End<TResult>(IAsyncResult asyncResult)
return default!; // unreachable
}

/// <summary>Gets the task represented by the IAsyncResult.</summary>
public static Task? GetTask(IAsyncResult asyncResult) => (asyncResult as TaskAsyncResult)?._task;

/// <summary>Throws an argument exception for the invalid <paramref name="asyncResult"/>.</summary>
[DoesNotReturn]
private static void ThrowArgumentException(IAsyncResult asyncResult) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ public async Task EndWrite_NullAsyncResult_ThrowsArgumentNullException(bool igno
}
}

[PlatformSpecific(TestPlatforms.Windows)] // Unix implementation uses Socket.Begin/EndSend, which doesn't fail in this case
[Fact]
public async Task EndWrite_InvalidAsyncResult_ThrowsArgumentException()
{
Expand All @@ -562,6 +563,7 @@ public async Task EndWrite_InvalidAsyncResult_ThrowsArgumentException()
}
}

[PlatformSpecific(TestPlatforms.Windows)] // Unix implementation uses Socket.Begin/EndSend, which doesn't fail in this case
[Fact]
public async Task EndWrite_CalledTwice_ThrowsInvalidOperationException()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,34 @@ internal Task<int> SendToAsync(ArraySegment<byte> buffer, SocketFlags socketFlag
return tcs.Task;
}

private static void ValidateBufferArguments(byte[] buffer, int offset, int size)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if ((uint)offset > (uint)buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(offset));
}
if ((uint)size > (uint)(buffer.Length - offset))
{
throw new ArgumentOutOfRangeException(nameof(size));
}
}

/// <summary>Validates the supplied array segment, throwing if its array or indices are null or out-of-bounds, respectively.</summary>
private static void ValidateBuffer(ArraySegment<byte> buffer)
{
if (buffer.Array == null)
{
throw new ArgumentNullException(nameof(buffer.Array));
}
if ((uint)buffer.Offset > buffer.Array.Length)
if ((uint)buffer.Offset > (uint)buffer.Array.Length)
{
throw new ArgumentOutOfRangeException(nameof(buffer.Offset));
}
if ((uint)buffer.Count > buffer.Array.Length - buffer.Offset)
if ((uint)buffer.Count > (uint)(buffer.Array.Length - buffer.Offset))
{
throw new ArgumentOutOfRangeException(nameof(buffer.Count));
}
Expand Down
Loading