Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita512e8b

Browse files
committed
Reimplement Socket.Begin/EndSend/Receive on Send/ReceiveAsync
1 parenteb187d1 commita512e8b

File tree

9 files changed

+116
-649
lines changed

9 files changed

+116
-649
lines changed

‎src/libraries/Common/src/Interop/Windows/WinSock/Interop.WSARecv.cs‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal static partial class Interop
1111
{
1212
internalstaticpartialclassWinsock
1313
{
14-
[DllImport(Interop.Libraries.Ws2_32,SetLastError=true)]
14+
[DllImport(Libraries.Ws2_32,SetLastError=true)]
1515
internalstaticexternunsafeSocketErrorWSARecv(
1616
SafeHandlesocketHandle,
1717
WSABuffer*buffer,
@@ -21,22 +21,6 @@ internal static extern unsafe SocketError WSARecv(
2121
NativeOverlapped*overlapped,
2222
IntPtrcompletionRoutine);
2323

24-
internalstaticunsafeSocketErrorWSARecv(
25-
SafeHandlesocketHandle,
26-
refWSABufferbuffer,
27-
intbufferCount,
28-
outintbytesTransferred,
29-
refSocketFlagssocketFlags,
30-
NativeOverlapped*overlapped,
31-
IntPtrcompletionRoutine)
32-
{
33-
// We intentionally do NOT copy this back after the function completes:
34-
// We don't want to cause a race in async scenarios.
35-
// The WSABuffer struct should be unchanged anyway.
36-
WSABufferlocalBuffer=buffer;
37-
returnWSARecv(socketHandle,&localBuffer,bufferCount,outbytesTransferred,refsocketFlags,overlapped,completionRoutine);
38-
}
39-
4024
internalstaticunsafeSocketErrorWSARecv(
4125
SafeHandlesocketHandle,
4226
Span<WSABuffer>buffers,

‎src/libraries/Common/src/Interop/Windows/WinSock/Interop.WSASend.cs‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal static partial class Interop
1111
{
1212
internalstaticpartialclassWinsock
1313
{
14-
[DllImport(Interop.Libraries.Ws2_32,SetLastError=true)]
14+
[DllImport(Libraries.Ws2_32,SetLastError=true)]
1515
internalstaticexternunsafeSocketErrorWSASend(
1616
SafeHandlesocketHandle,
1717
WSABuffer*buffers,
@@ -21,22 +21,6 @@ internal static extern unsafe SocketError WSASend(
2121
NativeOverlapped*overlapped,
2222
IntPtrcompletionRoutine);
2323

24-
internalstaticunsafeSocketErrorWSASend(
25-
SafeHandlesocketHandle,
26-
refWSABufferbuffer,
27-
intbufferCount,
28-
outintbytesTransferred,
29-
SocketFlagssocketFlags,
30-
NativeOverlapped*overlapped,
31-
IntPtrcompletionRoutine)
32-
{
33-
// We intentionally do NOT copy this back after the function completes:
34-
// We don't want to cause a race in async scenarios.
35-
// The WSABuffer struct should be unchanged anyway.
36-
WSABufferlocalBuffer=buffer;
37-
returnWSASend(socketHandle,&localBuffer,bufferCount,outbytesTransferred,socketFlags,overlapped,completionRoutine);
38-
}
39-
4024
internalstaticunsafeSocketErrorWSASend(
4125
SafeHandlesocketHandle,
4226
Span<WSABuffer>buffers,

‎src/libraries/Common/src/System/Threading/Tasks/TaskToApm.cs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public static IAsyncResult Begin(Task task, AsyncCallback? callback, object? sta
3737
/// <param name="asyncResult">The IAsyncResult to unwrap.</param>
3838
publicstaticvoidEnd(IAsyncResultasyncResult)
3939
{
40-
if(asyncResultisTaskAsyncResulttwar)
40+
if(GetTask(asyncResult)isTaskt)
4141
{
42-
twar._task.GetAwaiter().GetResult();
42+
t.GetAwaiter().GetResult();
4343
return;
4444
}
4545

@@ -50,7 +50,7 @@ public static void End(IAsyncResult asyncResult)
5050
/// <param name="asyncResult">The IAsyncResult to unwrap.</param>
5151
publicstaticTResultEnd<TResult>(IAsyncResultasyncResult)
5252
{
53-
if(asyncResultisTaskAsyncResulttwar&&twar._taskisTask<TResult>task)
53+
if(GetTask(asyncResult)isTask<TResult>task)
5454
{
5555
returntask.GetAwaiter().GetResult();
5656
}
@@ -59,6 +59,9 @@ public static TResult End<TResult>(IAsyncResult asyncResult)
5959
returndefault!;// unreachable
6060
}
6161

62+
/// <summary>Gets the task represented by the IAsyncResult.</summary>
63+
publicstaticTask?GetTask(IAsyncResultasyncResult)=>(asyncResultasTaskAsyncResult)?._task;
64+
6265
/// <summary>Throws an argument exception for the invalid <paramref name="asyncResult"/>.</summary>
6366
[DoesNotReturn]
6467
privatestaticvoidThrowArgumentException(IAsyncResultasyncResult)=>

‎src/libraries/System.Net.HttpListener/tests/HttpResponseStreamTests.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ public async Task EndWrite_NullAsyncResult_ThrowsArgumentNullException(bool igno
547547
}
548548
}
549549

550+
[PlatformSpecific(TestPlatforms.Windows)]// Unix implementation uses Socket.Begin/EndSend, which doesn't fail in this case
550551
[Fact]
551552
publicasyncTaskEndWrite_InvalidAsyncResult_ThrowsArgumentException()
552553
{
@@ -562,6 +563,7 @@ public async Task EndWrite_InvalidAsyncResult_ThrowsArgumentException()
562563
}
563564
}
564565

566+
[PlatformSpecific(TestPlatforms.Windows)]// Unix implementation uses Socket.Begin/EndSend, which doesn't fail in this case
565567
[Fact]
566568
publicasyncTaskEndWrite_CalledTwice_ThrowsInvalidOperationException()
567569
{

‎src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,18 +359,34 @@ internal Task<int> SendToAsync(ArraySegment<byte> buffer, SocketFlags socketFlag
359359
returntcs.Task;
360360
}
361361

362+
privatestaticvoidValidateBufferArguments(byte[]buffer,intoffset,intsize)
363+
{
364+
if(buffer==null)
365+
{
366+
thrownewArgumentNullException(nameof(buffer));
367+
}
368+
if((uint)offset>(uint)buffer.Length)
369+
{
370+
thrownewArgumentOutOfRangeException(nameof(offset));
371+
}
372+
if((uint)size>(uint)(buffer.Length-offset))
373+
{
374+
thrownewArgumentOutOfRangeException(nameof(size));
375+
}
376+
}
377+
362378
/// <summary>Validates the supplied array segment, throwing if its array or indices are null or out-of-bounds, respectively.</summary>
363379
privatestaticvoidValidateBuffer(ArraySegment<byte>buffer)
364380
{
365381
if(buffer.Array==null)
366382
{
367383
thrownewArgumentNullException(nameof(buffer.Array));
368384
}
369-
if((uint)buffer.Offset>buffer.Array.Length)
385+
if((uint)buffer.Offset>(uint)buffer.Array.Length)
370386
{
371387
thrownewArgumentOutOfRangeException(nameof(buffer.Offset));
372388
}
373-
if((uint)buffer.Count>buffer.Array.Length-buffer.Offset)
389+
if((uint)buffer.Count>(uint)(buffer.Array.Length-buffer.Offset))
374390
{
375391
thrownewArgumentOutOfRangeException(nameof(buffer.Count));
376392
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp