Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 23fa2e8

Browse files
committed
Simplify InnerStart* pattern
The handling of async operations, and in particular the support for initializing it, is currently split across a bunch of methods, making it difficult to track what's happening where. It's also largely unnecessary complexity. Simplify it.
1 parent 7ef465b commit 23fa2e8

File tree

4 files changed

+72
-287
lines changed

4 files changed

+72
-287
lines changed

src/System.Net.Sockets/src/System/Net/Sockets/Socket.cs

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,14 +3757,10 @@ public bool AcceptAsync(SocketAsyncEventArgs e)
37573757
SafeCloseSocket acceptHandle;
37583758
e.AcceptSocket = GetOrCreateAcceptSocket(e.AcceptSocket, true, "AcceptSocket", out acceptHandle);
37593759

3760-
// Prepare for the native call.
3761-
e.StartOperationCommon(this);
3760+
// Prepare for and make the native call.
3761+
e.StartOperationCommon(this, SocketAsyncOperation.Accept);
37623762
e.StartOperationAccept();
3763-
3764-
// Local variables for sync completion.
37653763
SocketError socketError = SocketError.Success;
3766-
3767-
// Make the native call.
37683764
try
37693765
{
37703766
socketError = e.DoOperationAccept(this, _handle, acceptHandle);
@@ -3825,8 +3821,8 @@ public bool ConnectAsync(SocketAsyncEventArgs e)
38253821

38263822
MultipleConnectAsync multipleConnectAsync = new SingleSocketMultipleConnectAsync(this, true);
38273823

3828-
e.StartOperationCommon(this);
3829-
e.StartOperationWrapperConnect(multipleConnectAsync);
3824+
e.StartOperationCommon(this, SocketAsyncOperation.Connect);
3825+
e.StartOperationConnect(multipleConnectAsync);
38303826

38313827
pending = multipleConnectAsync.StartConnectAsync(e, dnsEP);
38323828
}
@@ -3863,7 +3859,7 @@ public bool ConnectAsync(SocketAsyncEventArgs e)
38633859
}
38643860

38653861
// Prepare for the native call.
3866-
e.StartOperationCommon(this);
3862+
e.StartOperationCommon(this, SocketAsyncOperation.Connect);
38673863
e.StartOperationConnect();
38683864

38693865
// Make the native call.
@@ -3926,8 +3922,8 @@ public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType
39263922
multipleConnectAsync = new SingleSocketMultipleConnectAsync(attemptSocket, false);
39273923
}
39283924

3929-
e.StartOperationCommon(attemptSocket);
3930-
e.StartOperationWrapperConnect(multipleConnectAsync);
3925+
e.StartOperationCommon(attemptSocket, SocketAsyncOperation.Connect);
3926+
e.StartOperationConnect(multipleConnectAsync);
39313927

39323928
pending = multipleConnectAsync.StartConnectAsync(e, dnsEP);
39333929
}
@@ -3960,10 +3956,8 @@ public bool DisconnectAsync(SocketAsyncEventArgs e)
39603956
throw new ObjectDisposedException(GetType().FullName);
39613957
}
39623958

3963-
// Prepare for the native call.
3964-
e.StartOperationCommon(this);
3965-
e.StartOperationDisconnect();
3966-
3959+
// Prepare for and make the native call.
3960+
e.StartOperationCommon(this, SocketAsyncOperation.Disconnect);
39673961
SocketError socketError = SocketError.Success;
39683962
try
39693963
{
@@ -3995,14 +3989,9 @@ public bool ReceiveAsync(SocketAsyncEventArgs e)
39953989
throw new ArgumentNullException(nameof(e));
39963990
}
39973991

3998-
// Prepare for the native call.
3999-
e.StartOperationCommon(this);
4000-
e.StartOperationReceive();
4001-
4002-
// Local vars for sync completion of native call.
3992+
// Prepare for and make the native call.
3993+
e.StartOperationCommon(this, SocketAsyncOperation.Receive);
40033994
SocketError socketError;
4004-
4005-
// Wrap native methods with try/catch so event args object can be cleaned up.
40063995
try
40073996
{
40083997
socketError = e.DoOperationReceive(_handle, out _);
@@ -4053,13 +4042,9 @@ public bool ReceiveFromAsync(SocketAsyncEventArgs e)
40534042
// e.m_SocketAddres for Create to work later.
40544043
e.RemoteEndPoint = endPointSnapshot;
40554044

4056-
// Prepare for the native call.
4057-
e.StartOperationCommon(this);
4058-
e.StartOperationReceiveFrom();
4059-
4060-
// Make the native call.
4045+
// Prepare for and make the native call.
4046+
e.StartOperationCommon(this, SocketAsyncOperation.ReceiveFrom);
40614047
SocketError socketError;
4062-
40634048
try
40644049
{
40654050
socketError = e.DoOperationReceiveFrom(_handle, out _);
@@ -4112,13 +4097,9 @@ public bool ReceiveMessageFromAsync(SocketAsyncEventArgs e)
41124097

41134098
SetReceivingPacketInformation();
41144099

4115-
// Prepare for the native call.
4116-
e.StartOperationCommon(this);
4117-
e.StartOperationReceiveMessageFrom();
4118-
4119-
// Make the native call.
4100+
// Prepare for and make the native call.
4101+
e.StartOperationCommon(this, SocketAsyncOperation.ReceiveMessageFrom);
41204102
SocketError socketError;
4121-
41224103
try
41234104
{
41244105
socketError = e.DoOperationReceiveMessageFrom(this, _handle);
@@ -4149,14 +4130,9 @@ public bool SendAsync(SocketAsyncEventArgs e)
41494130
throw new ArgumentNullException(nameof(e));
41504131
}
41514132

4152-
// Prepare for the native call.
4153-
e.StartOperationCommon(this);
4154-
e.StartOperationSend();
4155-
4156-
// Local vars for sync completion of native call.
4133+
// Prepare for and make the native call.
4134+
e.StartOperationCommon(this, SocketAsyncOperation.Send);
41574135
SocketError socketError;
4158-
4159-
// Wrap native methods with try/catch so event args object can be cleaned up.
41604136
try
41614137
{
41624138
socketError = e.DoOperationSend(_handle);
@@ -4196,33 +4172,18 @@ public bool SendPacketsAsync(SocketAsyncEventArgs e)
41964172
throw new NotSupportedException(SR.net_notconnected);
41974173
}
41984174

4199-
// Prepare for the native call.
4200-
e.StartOperationCommon(this);
4201-
e.StartOperationSendPackets();
4202-
4203-
// Make the native call.
4175+
// Prepare for and make the native call.
4176+
e.StartOperationCommon(this, SocketAsyncOperation.SendPackets);
42044177
SocketError socketError;
4205-
4206-
Debug.Assert(e.SendPacketsDescriptorCount != null);
4207-
4208-
if (e.SendPacketsDescriptorCount > 0)
4178+
try
42094179
{
4210-
try
4211-
{
4212-
socketError = e.DoOperationSendPackets(this, _handle);
4213-
}
4214-
catch (Exception)
4215-
{
4216-
// Clear in-use flag on event args object.
4217-
e.Complete();
4218-
throw;
4219-
}
4180+
socketError = e.DoOperationSendPackets(this, _handle);
42204181
}
4221-
else
4182+
catch (Exception)
42224183
{
4223-
// No buffers or files to send.
4224-
e.FinishOperationSyncSuccess(0, SocketFlags.None);
4225-
socketError = SocketError.Success;
4184+
// Clear in-use flag on event args object.
4185+
e.Complete();
4186+
throw;
42264187
}
42274188

42284189
bool pending = (socketError == SocketError.IOPending);
@@ -4252,14 +4213,9 @@ public bool SendToAsync(SocketAsyncEventArgs e)
42524213
EndPoint endPointSnapshot = e.RemoteEndPoint;
42534214
e._socketAddress = SnapshotAndSerialize(ref endPointSnapshot);
42544215

4255-
// Prepare for the native call.
4256-
e.StartOperationCommon(this);
4257-
e.StartOperationSendTo();
4258-
4259-
// Make the native call.
4216+
// Prepare for and make the native call.
4217+
e.StartOperationCommon(this, SocketAsyncOperation.SendTo);
42604218
SocketError socketError;
4261-
4262-
// Wrap native methods with try/catch so event args object can be cleaned up.
42634219
try
42644220
{
42654221
socketError = e.DoOperationSendTo(_handle);

src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Unix.cs

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public partial class SocketAsyncEventArgs : EventArgs, IDisposable
1515
private SocketFlags _receivedFlags;
1616
private Action<int, byte[], int, SocketFlags, SocketError> _transferCompletionCallback;
1717

18-
internal int? SendPacketsDescriptorCount => _sendPacketsElements?.Length;
19-
2018
private void InitializeInternals()
2119
{
2220
// No-op for *nix.
@@ -53,11 +51,6 @@ private void FinishOperationSync(SocketError socketError, int bytesTransferred,
5351
}
5452
}
5553

56-
private void InnerStartOperationAccept()
57-
{
58-
_acceptedFileDescriptor = (IntPtr)(-1);
59-
}
60-
6154
private void AcceptCompletionCallback(IntPtr acceptedFileDescriptor, byte[] socketAddress, int socketAddressSize, SocketError socketError)
6255
{
6356
CompleteAcceptOperation(acceptedFileDescriptor, socketAddress, socketAddressSize, socketError);
@@ -79,6 +72,8 @@ internal unsafe SocketError DoOperationAccept(Socket socket, SafeCloseSocket han
7972
throw new PlatformNotSupportedException(SR.net_sockets_accept_receive_notsupported);
8073
}
8174

75+
_acceptedFileDescriptor = (IntPtr)(-1);
76+
8277
Debug.Assert(acceptHandle == null, $"Unexpected acceptHandle: {acceptHandle}");
8378

8479
IntPtr acceptedFd;
@@ -94,11 +89,6 @@ internal unsafe SocketError DoOperationAccept(Socket socket, SafeCloseSocket han
9489
return socketError;
9590
}
9691

97-
private void InnerStartOperationConnect()
98-
{
99-
// No-op for *nix.
100-
}
101-
10292
private void ConnectCompletionCallback(SocketError socketError)
10393
{
10494
CompletionCallback(0, SocketFlags.None, socketError);
@@ -138,14 +128,11 @@ private void CompleteTransferOperation(int bytesTransferred, byte[] socketAddres
138128
_receivedFlags = receivedFlags;
139129
}
140130

141-
private void InnerStartOperationReceive()
131+
internal unsafe SocketError DoOperationReceive(SafeCloseSocket handle, out SocketFlags flags)
142132
{
143133
_receivedFlags = System.Net.Sockets.SocketFlags.None;
144134
_socketAddressSize = 0;
145-
}
146135

147-
internal unsafe SocketError DoOperationReceive(SafeCloseSocket handle, out SocketFlags flags)
148-
{
149136
int bytesReceived;
150137
SocketError errorCode;
151138
if (_bufferList == null)
@@ -166,14 +153,11 @@ internal unsafe SocketError DoOperationReceive(SafeCloseSocket handle, out Socke
166153
return errorCode;
167154
}
168155

169-
private void InnerStartOperationReceiveFrom()
156+
internal unsafe SocketError DoOperationReceiveFrom(SafeCloseSocket handle, out SocketFlags flags)
170157
{
171158
_receivedFlags = System.Net.Sockets.SocketFlags.None;
172159
_socketAddressSize = 0;
173-
}
174160

175-
internal unsafe SocketError DoOperationReceiveFrom(SafeCloseSocket handle, out SocketFlags flags)
176-
{
177161
SocketError errorCode;
178162
int bytesReceived = 0;
179163
int socketAddressLen = _socketAddress.Size;
@@ -195,13 +179,6 @@ internal unsafe SocketError DoOperationReceiveFrom(SafeCloseSocket handle, out S
195179
return errorCode;
196180
}
197181

198-
private void InnerStartOperationReceiveMessageFrom()
199-
{
200-
_receiveMessageFromPacketInfo = default(IPPacketInformation);
201-
_receivedFlags = System.Net.Sockets.SocketFlags.None;
202-
_socketAddressSize = 0;
203-
}
204-
205182
private void ReceiveMessageFromCompletionCallback(int bytesTransferred, byte[] socketAddress, int socketAddressSize, SocketFlags receivedFlags, IPPacketInformation ipPacketInformation, SocketError errorCode)
206183
{
207184
CompleteReceiveMessageFromOperation(bytesTransferred, socketAddress, socketAddressSize, receivedFlags, ipPacketInformation, errorCode);
@@ -221,6 +198,10 @@ private void CompleteReceiveMessageFromOperation(int bytesTransferred, byte[] so
221198

222199
internal unsafe SocketError DoOperationReceiveMessageFrom(Socket socket, SafeCloseSocket handle)
223200
{
201+
_receiveMessageFromPacketInfo = default(IPPacketInformation);
202+
_receivedFlags = System.Net.Sockets.SocketFlags.None;
203+
_socketAddressSize = 0;
204+
224205
bool isIPv4, isIPv6;
225206
Socket.GetIPProtocolInformation(socket.AddressFamily, _socketAddress, out isIPv4, out isIPv6);
226207

@@ -237,14 +218,11 @@ internal unsafe SocketError DoOperationReceiveMessageFrom(Socket socket, SafeClo
237218
return socketError;
238219
}
239220

240-
private void InnerStartOperationSend()
221+
internal unsafe SocketError DoOperationSend(SafeCloseSocket handle)
241222
{
242223
_receivedFlags = System.Net.Sockets.SocketFlags.None;
243224
_socketAddressSize = 0;
244-
}
245225

246-
internal unsafe SocketError DoOperationSend(SafeCloseSocket handle)
247-
{
248226
int bytesSent;
249227
SocketError errorCode;
250228
if (_bufferList == null)
@@ -265,11 +243,6 @@ internal unsafe SocketError DoOperationSend(SafeCloseSocket handle)
265243
return errorCode;
266244
}
267245

268-
private void InnerStartOperationSendPackets()
269-
{
270-
// nop
271-
}
272-
273246
internal SocketError DoOperationSendPackets(Socket socket, SafeCloseSocket handle)
274247
{
275248
Debug.Assert(_sendPacketsElements != null);
@@ -329,14 +302,11 @@ internal SocketError DoOperationSendPackets(Socket socket, SafeCloseSocket handl
329302
return SocketError.IOPending;
330303
}
331304

332-
private void InnerStartOperationSendTo()
305+
internal SocketError DoOperationSendTo(SafeCloseSocket handle)
333306
{
334307
_receivedFlags = System.Net.Sockets.SocketFlags.None;
335308
_socketAddressSize = 0;
336-
}
337309

338-
internal SocketError DoOperationSendTo(SafeCloseSocket handle)
339-
{
340310
int bytesSent;
341311
int socketAddressLen = _socketAddress.Size;
342312
SocketError errorCode;

0 commit comments

Comments
 (0)