-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebSocket Feedback Follow-up (#107662)
* Fixes * State validation update * Roll back dispose changes, fix mutex logging * Roll back observe changes * Add internal flags enum for states * Update src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketStateHelper.cs Co-authored-by: Miha Zupan <[email protected]> * Feedback --------- Co-authored-by: Miha Zupan <[email protected]>
- Loading branch information
1 parent
24e7d1b
commit b6b0fb1
Showing
8 changed files
with
112 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocketStates.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Net.WebSockets | ||
{ | ||
[Flags] | ||
internal enum ManagedWebSocketStates | ||
{ | ||
None = 0, | ||
|
||
// WebSocketState.None = 0 -- this state is invalid for the managed implementation | ||
// WebSocketState.Connecting = 1 -- this state is invalid for the managed implementation | ||
Open = 0x04, // WebSocketState.Open = 2 | ||
CloseSent = 0x08, // WebSocketState.CloseSent = 3 | ||
CloseReceived = 0x10, // WebSocketState.CloseReceived = 4 | ||
Closed = 0x20, // WebSocketState.Closed = 5 | ||
Aborted = 0x40, // WebSocketState.Aborted = 6 | ||
|
||
All = Open | CloseSent | CloseReceived | Closed | Aborted | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketStateHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
|
||
namespace System.Net.WebSockets | ||
{ | ||
internal static class WebSocketStateHelper | ||
{ | ||
/// <summary>Valid states to be in when calling SendAsync.</summary> | ||
internal const ManagedWebSocketStates ValidSendStates = ManagedWebSocketStates.Open | ManagedWebSocketStates.CloseReceived; | ||
/// <summary>Valid states to be in when calling ReceiveAsync.</summary> | ||
internal const ManagedWebSocketStates ValidReceiveStates = ManagedWebSocketStates.Open | ManagedWebSocketStates.CloseSent; | ||
/// <summary>Valid states to be in when calling CloseOutputAsync.</summary> | ||
internal const ManagedWebSocketStates ValidCloseOutputStates = ManagedWebSocketStates.Open | ManagedWebSocketStates.CloseReceived; | ||
/// <summary>Valid states to be in when calling CloseAsync.</summary> | ||
internal const ManagedWebSocketStates ValidCloseStates = ManagedWebSocketStates.Open | ManagedWebSocketStates.CloseReceived | ManagedWebSocketStates.CloseSent; | ||
|
||
internal static bool IsValidSendState(WebSocketState state) => ValidSendStates.HasFlag(ToFlag(state)); | ||
|
||
internal static void ThrowIfInvalidState(WebSocketState currentState, bool isDisposed, Exception? innerException, ManagedWebSocketStates validStates) | ||
{ | ||
ManagedWebSocketStates state = ToFlag(currentState); | ||
|
||
if ((state & validStates) == 0) | ||
{ | ||
string invalidStateMessage = SR.Format( | ||
SR.net_WebSockets_InvalidState, currentState, validStates); | ||
|
||
throw new WebSocketException(WebSocketError.InvalidState, invalidStateMessage, innerException); | ||
} | ||
|
||
if (innerException is not null) | ||
{ | ||
Debug.Assert(state == ManagedWebSocketStates.Aborted); | ||
throw new OperationCanceledException(nameof(WebSocketState.Aborted), innerException); | ||
} | ||
|
||
// Ordering is important to maintain .NET 4.5 WebSocket implementation exception behavior. | ||
ObjectDisposedException.ThrowIf(isDisposed, typeof(WebSocket)); | ||
} | ||
|
||
private static ManagedWebSocketStates ToFlag(WebSocketState value) | ||
{ | ||
ManagedWebSocketStates flag = (ManagedWebSocketStates)(1 << (int)value); | ||
Debug.Assert(Enum.IsDefined(flag)); | ||
return flag; | ||
} | ||
} | ||
} |