1313using System . Threading ;
1414using System . Threading . Tasks ;
1515
16- // NOTE: This file is shared between CoreFX and ASP.NET. Be very thoughtful when changing it.
17-
1816namespace System . Net . WebSockets
1917{
2018 /// <summary>A managed implementation of a web socket that sends and receives data via a <see cref="Stream"/>.</summary>
@@ -26,7 +24,7 @@ namespace System.Net.WebSockets
2624 /// a send operation while another is in progress or a receive operation while another is in progress will
2725 /// result in an exception.
2826 /// </remarks>
29- internal sealed class ManagedWebSocket : WebSocket
27+ internal sealed partial class ManagedWebSocket : WebSocket
3028 {
3129 /// <summary>Creates a <see cref="ManagedWebSocket"/> from a <see cref="Stream"/> connected to a websocket endpoint.</summary>
3230 /// <param name="stream">The connected Stream.</param>
@@ -264,10 +262,10 @@ public override Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType m
264262
265263 WebSocketValidate . ValidateArraySegment ( buffer , nameof ( buffer ) ) ;
266264
267- return SendAsync ( ( ReadOnlyMemory < byte > ) buffer , messageType , endOfMessage , cancellationToken ) ;
265+ return SendPrivateAsync ( ( ReadOnlyMemory < byte > ) buffer , messageType , endOfMessage , cancellationToken ) ;
268266 }
269267
270- public override Task SendAsync ( ReadOnlyMemory < byte > buffer , WebSocketMessageType messageType , bool endOfMessage , CancellationToken cancellationToken )
268+ private Task SendPrivateAsync ( ReadOnlyMemory < byte > buffer , WebSocketMessageType messageType , bool endOfMessage , CancellationToken cancellationToken )
271269 {
272270 if ( messageType != WebSocketMessageType . Text && messageType != WebSocketMessageType . Binary )
273271 {
@@ -321,29 +319,6 @@ public override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buf
321319 }
322320 }
323321
324- public override ValueTask < ValueWebSocketReceiveResult > ReceiveAsync ( Memory < byte > buffer , CancellationToken cancellationToken )
325- {
326- try
327- {
328- WebSocketValidate . ThrowIfInvalidState ( _state , _disposed , s_validReceiveStates ) ;
329-
330- Debug . Assert ( ! Monitor . IsEntered ( StateUpdateLock ) , $ "{ nameof ( StateUpdateLock ) } must never be held when acquiring { nameof ( ReceiveAsyncLock ) } ") ;
331- lock ( ReceiveAsyncLock ) // synchronize with receives in CloseAsync
332- {
333- ThrowIfOperationInProgress ( _lastReceiveAsync ) ;
334- ValueTask < ValueWebSocketReceiveResult > t = ReceiveAsyncPrivate < ValueWebSocketReceiveResultGetter , ValueWebSocketReceiveResult > ( buffer , cancellationToken ) ;
335- _lastReceiveAsync =
336- t . IsCompletedSuccessfully ? ( t . Result . MessageType == WebSocketMessageType . Close ? s_cachedCloseTask : Task . CompletedTask ) :
337- t . AsTask ( ) ;
338- return t ;
339- }
340- }
341- catch ( Exception exc )
342- {
343- return new ValueTask < ValueWebSocketReceiveResult > ( Task . FromException < ValueWebSocketReceiveResult > ( exc ) ) ;
344- }
345- }
346-
347322 public override Task CloseAsync ( WebSocketCloseStatus closeStatus , string statusDescription , CancellationToken cancellationToken )
348323 {
349324 WebSocketValidate . ValidateCloseStatus ( closeStatus , statusDescription ) ;
@@ -545,7 +520,7 @@ private void SendKeepAliveFrameAsync()
545520 Task t = SendFrameLockAcquiredNonCancelableAsync ( MessageOpcode . Ping , true , Memory < byte > . Empty ) ;
546521
547522 // "Observe" any exception, ignoring it to prevent the unobserved exception event from being raised.
548- if ( ! t . IsCompletedSuccessfully )
523+ if ( t . Status != TaskStatus . RanToCompletion )
549524 {
550525 t . ContinueWith ( p => { Exception ignored = p . Exception ; } ,
551526 CancellationToken . None ,
@@ -1068,16 +1043,7 @@ private async Task CloseAsyncPrivate(WebSocketCloseStatus closeStatus, string st
10681043 // a race condition here, e.g. if there's a in-flight receive that completes after we check, but that's fine: worst
10691044 // case is we then await it, find that it's not what we need, and try again.
10701045 receiveTask = _lastReceiveAsync ;
1071- if ( receiveTask == null ||
1072- ( receiveTask . Status == TaskStatus . RanToCompletion &&
1073- ! ( receiveTask is Task < WebSocketReceiveResult > wsrr && wsrr . Result . MessageType == WebSocketMessageType . Close ) &&
1074- ! ( receiveTask is Task < ValueWebSocketReceiveResult > vwsrr && vwsrr . Result . MessageType == WebSocketMessageType . Close ) ) )
1075- {
1076- ValueTask < ValueWebSocketReceiveResult > vt = ReceiveAsyncPrivate < ValueWebSocketReceiveResultGetter , ValueWebSocketReceiveResult > ( closeBuffer , cancellationToken ) ;
1077- _lastReceiveAsync = receiveTask =
1078- vt . IsCompletedSuccessfully ? ( vt . Result . MessageType == WebSocketMessageType . Close ? s_cachedCloseTask : Task . CompletedTask ) :
1079- vt . AsTask ( ) ;
1080- }
1046+ _lastReceiveAsync = receiveTask = ValidateAndReceiveAsync ( receiveTask , closeBuffer , cancellationToken ) ;
10811047 }
10821048
10831049 // Wait for whatever receive task we have. We'll then loop around again to re-check our state.
@@ -1453,12 +1419,5 @@ private interface IWebSocketReceiveResultGetter<TResult>
14531419 public WebSocketReceiveResult GetResult ( int count , WebSocketMessageType messageType , bool endOfMessage , WebSocketCloseStatus ? closeStatus , string closeDescription ) =>
14541420 new WebSocketReceiveResult ( count , messageType , endOfMessage , closeStatus , closeDescription ) ;
14551421 }
1456-
1457- /// <summary><see cref="IWebSocketReceiveResultGetter{TResult}"/> implementation for <see cref="ValueWebSocketReceiveResult"/>.</summary>
1458- private readonly struct ValueWebSocketReceiveResultGetter : IWebSocketReceiveResultGetter < ValueWebSocketReceiveResult >
1459- {
1460- public ValueWebSocketReceiveResult GetResult ( int count , WebSocketMessageType messageType , bool endOfMessage , WebSocketCloseStatus ? closeStatus , string closeDescription ) =>
1461- new ValueWebSocketReceiveResult ( count , messageType , endOfMessage ) ; // closeStatus/closeDescription are ignored
1462- }
14631422 }
14641423}
0 commit comments