@@ -73,7 +73,6 @@ private sealed class CacheSet
73
73
private int _closeTimeout = Socket . DefaultCloseTimeout ;
74
74
private int _disposed ; // 0 == false, anything else == true
75
75
76
- #region Constructors
77
76
public Socket ( SocketType socketType , ProtocolType protocolType )
78
77
: this ( OSSupportsIPv6 ? AddressFamily . InterNetworkV6 : AddressFamily . InterNetwork , socketType , protocolType )
79
78
{
@@ -242,9 +241,10 @@ private static SafeSocketHandle ValidateHandle(SafeSocketHandle handle) =>
242
241
handle is null ? throw new ArgumentNullException ( nameof ( handle ) ) :
243
242
handle . IsInvalid ? throw new ArgumentException ( SR . Arg_InvalidHandle , nameof ( handle ) ) :
244
243
handle ;
245
- #endregion
246
244
247
- #region Properties
245
+ //
246
+ // Properties
247
+ //
248
248
249
249
// The CLR allows configuration of these properties, separately from whether the OS supports IPv4/6. We
250
250
// do not provide these config options, so SupportsIPvX === OSSupportsIPvX.
@@ -761,9 +761,10 @@ internal bool CanTryAddressFamily(AddressFamily family)
761
761
{
762
762
return ( family == _addressFamily ) || ( family == AddressFamily . InterNetwork && IsDualMode ) ;
763
763
}
764
- #endregion
765
764
766
- #region Public Methods
765
+ //
766
+ // Public Methods
767
+ //
767
768
768
769
// Associates a socket with an end point.
769
770
public void Bind ( EndPoint localEP )
@@ -2116,43 +2117,14 @@ public IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback? req
2116
2117
public IAsyncResult BeginConnect ( IPAddress [ ] addresses , int port , AsyncCallback ? requestCallback , object ? state ) =>
2117
2118
TaskToApm . Begin ( ConnectAsync ( addresses , port ) , requestCallback , state ) ;
2118
2119
2119
- public IAsyncResult BeginDisconnect ( bool reuseSocket , AsyncCallback ? callback , object ? state )
2120
+ public void EndConnect ( IAsyncResult asyncResult )
2120
2121
{
2121
2122
ThrowIfDisposed ( ) ;
2122
-
2123
- // Start context-flowing op. No need to lock - we don't use the context till the callback.
2124
- DisconnectOverlappedAsyncResult asyncResult = new DisconnectOverlappedAsyncResult ( this , state , callback ) ;
2125
- asyncResult . StartPostingAsyncOp ( false ) ;
2126
-
2127
- // Post the disconnect.
2128
- DoBeginDisconnect ( reuseSocket , asyncResult ) ;
2129
-
2130
- // Finish flowing (or call the callback), and return.
2131
- asyncResult . FinishPostingAsyncOp ( ) ;
2132
- return asyncResult ;
2123
+ TaskToApm . End ( asyncResult ) ;
2133
2124
}
2134
2125
2135
- private void DoBeginDisconnect ( bool reuseSocket , DisconnectOverlappedAsyncResult asyncResult )
2136
- {
2137
- SocketError errorCode = SocketError . Success ;
2138
-
2139
- errorCode = SocketPal . DisconnectAsync ( this , _handle , reuseSocket , asyncResult ) ;
2140
-
2141
- if ( errorCode == SocketError . Success )
2142
- {
2143
- SetToDisconnected ( ) ;
2144
- _remoteEndPoint = null ;
2145
- _localEndPoint = null ;
2146
- }
2147
-
2148
- if ( NetEventSource . Log . IsEnabled ( ) ) NetEventSource . Info ( this , $ "UnsafeNclNativeMethods.OSSOCK.DisConnectEx returns:{ errorCode } ") ;
2149
-
2150
- // If the call failed, update our status and throw
2151
- if ( ! CheckErrorAndUpdateStatus ( errorCode ) )
2152
- {
2153
- throw new SocketException ( ( int ) errorCode ) ;
2154
- }
2155
- }
2126
+ public IAsyncResult BeginDisconnect ( bool reuseSocket , AsyncCallback ? callback , object ? state ) =>
2127
+ TaskToApmBeginWithSyncExceptions ( DisconnectAsync ( reuseSocket ) . AsTask ( ) , callback , state ) ;
2156
2128
2157
2129
public void Disconnect ( bool reuseSocket )
2158
2130
{
@@ -2175,47 +2147,12 @@ public void Disconnect(bool reuseSocket)
2175
2147
_localEndPoint = null ;
2176
2148
}
2177
2149
2178
- public void EndConnect ( IAsyncResult asyncResult )
2150
+ public void EndDisconnect ( IAsyncResult asyncResult )
2179
2151
{
2180
2152
ThrowIfDisposed ( ) ;
2181
2153
TaskToApm . End ( asyncResult ) ;
2182
2154
}
2183
2155
2184
- public void EndDisconnect ( IAsyncResult asyncResult )
2185
- {
2186
- ThrowIfDisposed ( ) ;
2187
-
2188
- if ( asyncResult == null )
2189
- {
2190
- throw new ArgumentNullException ( nameof ( asyncResult ) ) ;
2191
- }
2192
-
2193
- //get async result and check for errors
2194
- LazyAsyncResult ? castedAsyncResult = asyncResult as LazyAsyncResult ;
2195
- if ( castedAsyncResult == null || castedAsyncResult . AsyncObject != this )
2196
- {
2197
- throw new ArgumentException ( SR . net_io_invalidasyncresult , nameof ( asyncResult ) ) ;
2198
- }
2199
- if ( castedAsyncResult . EndCalled )
2200
- {
2201
- throw new InvalidOperationException ( SR . Format ( SR . net_io_invalidendcall , nameof ( EndDisconnect ) ) ) ;
2202
- }
2203
-
2204
- //wait for completion if it hasn't occurred
2205
- castedAsyncResult . InternalWaitForCompletion ( ) ;
2206
- castedAsyncResult . EndCalled = true ;
2207
-
2208
- if ( NetEventSource . Log . IsEnabled ( ) ) NetEventSource . Info ( this ) ;
2209
-
2210
- //
2211
- // if the asynchronous native call failed asynchronously
2212
- // we'll throw a SocketException
2213
- //
2214
- if ( ( SocketError ) castedAsyncResult . ErrorCode != SocketError . Success )
2215
- {
2216
- UpdateStatusAfterSocketErrorAndThrowException ( ( SocketError ) castedAsyncResult . ErrorCode ) ;
2217
- }
2218
- }
2219
2156
2220
2157
public IAsyncResult BeginSend ( byte [ ] buffer , int offset , int size , SocketFlags socketFlags , AsyncCallback ? callback , object ? state )
2221
2158
{
@@ -2668,7 +2605,10 @@ public void Shutdown(SocketShutdown how)
2668
2605
InternalSetBlocking ( _willBlockInternal ) ;
2669
2606
}
2670
2607
2671
- #region Async methods
2608
+ //
2609
+ // Async methods
2610
+ //
2611
+
2672
2612
public bool AcceptAsync ( SocketAsyncEventArgs e )
2673
2613
{
2674
2614
ThrowIfDisposed ( ) ;
@@ -2889,7 +2829,9 @@ public static void CancelConnectAsync(SocketAsyncEventArgs e)
2889
2829
e . CancelConnectAsync ( ) ;
2890
2830
}
2891
2831
2892
- public bool DisconnectAsync ( SocketAsyncEventArgs e )
2832
+ public bool DisconnectAsync ( SocketAsyncEventArgs e ) => DisconnectAsync ( e , default ) ;
2833
+
2834
+ private bool DisconnectAsync ( SocketAsyncEventArgs e , CancellationToken cancellationToken )
2893
2835
{
2894
2836
// Throw if socket disposed
2895
2837
ThrowIfDisposed ( ) ;
@@ -2904,7 +2846,7 @@ public bool DisconnectAsync(SocketAsyncEventArgs e)
2904
2846
SocketError socketError = SocketError . Success ;
2905
2847
try
2906
2848
{
2907
- socketError = e . DoOperationDisconnect ( this , _handle ) ;
2849
+ socketError = e . DoOperationDisconnect ( this , _handle , cancellationToken ) ;
2908
2850
}
2909
2851
catch
2910
2852
{
@@ -3155,10 +3097,10 @@ private bool SendToAsync(SocketAsyncEventArgs e, CancellationToken cancellationT
3155
3097
3156
3098
return socketError == SocketError . IOPending ;
3157
3099
}
3158
- #endregion
3159
- #endregion
3160
3100
3161
- #region Internal and private properties
3101
+ //
3102
+ // Internal and private properties
3103
+ //
3162
3104
3163
3105
private CacheSet Caches
3164
3106
{
@@ -3174,9 +3116,10 @@ private CacheSet Caches
3174
3116
}
3175
3117
3176
3118
internal bool Disposed => _disposed != 0 ;
3177
- #endregion
3178
3119
3179
- #region Internal and private methods
3120
+ //
3121
+ // Internal and private methods
3122
+ //
3180
3123
3181
3124
internal static void GetIPProtocolInformation ( AddressFamily addressFamily , Internals . SocketAddress socketAddress , out bool isIPv4 , out bool isIPv6 )
3182
3125
{
@@ -3889,6 +3832,16 @@ private static SocketError GetSocketErrorFromFaultedTask(Task t)
3889
3832
} ;
3890
3833
}
3891
3834
3892
- #endregion
3835
+ // Helper to maintain existing behavior of Socket APM methods to throw synchronously from Begin*.
3836
+ private static IAsyncResult TaskToApmBeginWithSyncExceptions ( Task task , AsyncCallback ? callback , object ? state )
3837
+ {
3838
+ if ( task . IsFaulted )
3839
+ {
3840
+ task . GetAwaiter ( ) . GetResult ( ) ;
3841
+ Debug . Fail ( "Task faulted but GetResult did not throw???" ) ;
3842
+ }
3843
+
3844
+ return TaskToApm . Begin ( task , callback , state ) ;
3845
+ }
3893
3846
}
3894
3847
}
0 commit comments