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

Utilize new APIs in the WebSocket implementation#101953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
CarnaViire merged 19 commits intodotnet:mainfromPaulusParssinen:use-new-apis-in-ws
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
19 commits
Select commitHold shift + click to select a range
46cb6dd
Use BinaryPrimitives in ManagedWebSocket
PaulusParssinenMay 6, 2024
a091b09
Use MemoryMarshal.CreateSpan instead of .ctor in WebSocketInflater
PaulusParssinenMay 6, 2024
4708726
Apply PR suggestion
PaulusParssinenMay 7, 2024
a8662ea
Merge branch 'main' into use-new-apis-in-ws
PaulusParssinenMay 7, 2024
76466ff
Merge branch 'main' into use-new-apis-in-ws
PaulusParssinenMay 8, 2024
2868bd6
Use BinaryPrimitives in more places
PaulusParssinenMay 9, 2024
505f111
Restore the behavior where mask is stored in machine-endianness
PaulusParssinenMay 9, 2024
820dc04
Remove unused using directives
PaulusParssinenMay 9, 2024
79ab34b
Read the payload length as unsigned for 16-bit case
PaulusParssinenMay 9, 2024
c515ba9
Consolidate some validation logic to WebSocketValidata.cs
PaulusParssinenMay 9, 2024
053f0ac
Remove unused ValidateBuffer
PaulusParssinenMay 9, 2024
953fe66
Use new throw helpers in ValueWebSocketReceiveResult
PaulusParssinenMay 9, 2024
e3e7a01
Use ValueTask.FromException
PaulusParssinenMay 9, 2024
9888a37
Revert "Remove unused ValidateBuffer"
PaulusParssinenMay 9, 2024
9783f98
Oops
PaulusParssinenMay 9, 2024
26da64c
Fix ArgumentNullException for ValidateArraySegment
PaulusParssinenMay 9, 2024
92b05a3
Revert "Consolidate some validation logic to WebSocketValidata.cs"
PaulusParssinenMay 9, 2024
7509610
Revert ValueWebSocketReceiveResult.ThrowMessageTypeOutOfRange
PaulusParssinenMay 9, 2024
b0af53a
Revert "Remove unused using directives"
PaulusParssinenMay 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Revert "Consolidate some validation logic to WebSocketValidata.cs"
This reverts commitc515ba9.
  • Loading branch information
@PaulusParssinen
PaulusParssinen committedMay 9, 2024
commit92b05a3303d3fa4ab116d6cb4706df105d8edf1c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,22 +3,12 @@

using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;

namespace System.Net.WebSockets
{
internal static partial class WebSocketValidate
{
/// <summary>Valid states to be in when calling SendAsync.</summary>
private static readonly WebSocketState[] s_validSendStates = [WebSocketState.Open, WebSocketState.CloseReceived];
/// <summary>Valid states to be in when calling ReceiveAsync.</summary>
private static readonly WebSocketState[] s_validReceiveStates = [WebSocketState.Open, WebSocketState.CloseSent];
/// <summary>Valid states to be in when calling CloseOutputAsync.</summary>
private static readonly WebSocketState[] s_validCloseOutputStates = [WebSocketState.Open, WebSocketState.CloseReceived];
/// <summary>Valid states to be in when calling CloseAsync.</summary>
private static readonly WebSocketState[] s_validCloseStates = [WebSocketState.Open, WebSocketState.CloseReceived, WebSocketState.CloseSent];

/// <summary>
/// The minimum value for window bits that the websocket per-message-deflate extension can support.<para />
/// For the current implementation of deflate(), a windowBits value of 8 (a window size of 256 bytes) is not supported.
Expand DownExpand Up@@ -47,19 +37,7 @@ internal static partial class WebSocketValidate
private static readonly SearchValues<char> s_validSubprotocolChars =
SearchValues.Create("!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~");

internal static void ThrowIfInvalidSendState(WebSocketState currentState, bool isDisposed)
=> ThrowIfInvalidState(currentState, isDisposed, s_validSendStates);

internal static void ThrowIfInvalidReceiveState(WebSocketState currentState, bool isDisposed)
=> ThrowIfInvalidState(currentState, isDisposed, s_validReceiveStates);

internal static void ThrowIfInvalidCloseOutputState(WebSocketState currentState, bool isDisposed)
=> ThrowIfInvalidState(currentState, isDisposed, s_validCloseOutputStates);

internal static void ThrowIfInvalidCloseState(WebSocketState currentState, bool isDisposed)
=> ThrowIfInvalidState(currentState, isDisposed, s_validCloseStates);

private static void ThrowIfInvalidState(WebSocketState currentState, bool isDisposed, WebSocketState[] validStates)
internal static void ThrowIfInvalidState(WebSocketState currentState, bool isDisposed, WebSocketState[] validStates)
{
string validStatesText = string.Empty;

Expand DownExpand Up@@ -138,20 +116,22 @@ internal static void ValidateCloseStatus(WebSocketCloseStatus closeStatus, strin
}
}

internal static void ValidateArraySegment(ArraySegment<byte> arraySegment,[CallerArgumentExpression(nameof(arraySegment))]string? parameterName = null)
internal static void ValidateArraySegment(ArraySegment<byte> arraySegment, string parameterName)
{
Debug.Assert(!string.IsNullOrEmpty(parameterName), "'parameterName' MUST NOT be NULL or string.Empty");

string arrayParameterName = parameterName + "." + nameof(arraySegment.Array);
ArgumentNullException.ThrowIfNull(arraySegment.Array, arrayParameterName);

string offsetParameterName = parameterName + "." + nameof(arraySegment.Offset);
ArgumentOutOfRangeException.ThrowIfNegative(arraySegment.Offset, offsetParameterName);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arraySegment.Offset, arraySegment.Array.Length, offsetParameterName);

string countParameterName = parameterName + "." + nameof(arraySegment.Count);
ArgumentOutOfRangeException.ThrowIfNegative(arraySegment.Count, countParameterName);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arraySegment.Count, arraySegment.Array.Length - arraySegment.Offset, countParameterName);
if (arraySegment.Array == null)
{
throw new ArgumentNullException(parameterName + "." + nameof(arraySegment.Array));
}
if (arraySegment.Offset < 0 || arraySegment.Offset > arraySegment.Array.Length)
{
throw new ArgumentOutOfRangeException(parameterName + "." + nameof(arraySegment.Offset));
}
if (arraySegment.Count < 0 || arraySegment.Count > (arraySegment.Array.Length - arraySegment.Offset))
{
throw new ArgumentOutOfRangeException(parameterName + "." + nameof(arraySegment.Count));
}
}

internal static void ValidateBuffer(byte[] buffer, int offset, int count)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,6 +30,15 @@ internal sealed partial class ManagedWebSocket : WebSocket
/// <summary>Encoding for the payload of text messages: UTF-8 encoding that throws if invalid bytes are discovered, per the RFC.</summary>
private static readonly UTF8Encoding s_textEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

/// <summary>Valid states to be in when calling SendAsync.</summary>
private static readonly WebSocketState[] s_validSendStates = { WebSocketState.Open, WebSocketState.CloseReceived };
/// <summary>Valid states to be in when calling ReceiveAsync.</summary>
private static readonly WebSocketState[] s_validReceiveStates = { WebSocketState.Open, WebSocketState.CloseSent };
/// <summary>Valid states to be in when calling CloseOutputAsync.</summary>
private static readonly WebSocketState[] s_validCloseOutputStates = { WebSocketState.Open, WebSocketState.CloseReceived };
/// <summary>Valid states to be in when calling CloseAsync.</summary>
private static readonly WebSocketState[] s_validCloseStates = { WebSocketState.Open, WebSocketState.CloseReceived, WebSocketState.CloseSent };

/// <summary>The maximum size in bytes of a message frame header that includes mask bytes.</summary>
internal const int MaxMessageHeaderLength = 14;
/// <summary>The maximum size of a control message payload.</summary>
Expand DownExpand Up@@ -256,7 +265,7 @@ public override Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType m
nameof(messageType));
}

WebSocketValidate.ValidateArraySegment(buffer);
WebSocketValidate.ValidateArraySegment(buffer, nameof(buffer));

return SendAsync(buffer, messageType, endOfMessage ? WebSocketMessageFlags.EndOfMessage : default, cancellationToken).AsTask();
}
Expand All@@ -276,7 +285,7 @@ public override ValueTask SendAsync(ReadOnlyMemory<byte> buffer, WebSocketMessag

try
{
WebSocketValidate.ThrowIfInvalidSendState(_state, _disposed);
WebSocketValidate.ThrowIfInvalidState(_state, _disposed, s_validSendStates);
}
catch (Exception exc)
{
Expand DownExpand Up@@ -309,11 +318,11 @@ public override ValueTask SendAsync(ReadOnlyMemory<byte> buffer, WebSocketMessag

public override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buffer, CancellationToken cancellationToken)
{
WebSocketValidate.ValidateArraySegment(buffer);
WebSocketValidate.ValidateArraySegment(buffer, nameof(buffer));

try
{
WebSocketValidate.ThrowIfInvalidReceiveState(_state, _disposed);
WebSocketValidate.ThrowIfInvalidState(_state, _disposed, s_validReceiveStates);

return ReceiveAsyncPrivate<WebSocketReceiveResult>(buffer, cancellationToken).AsTask();
}
Expand All@@ -327,7 +336,7 @@ public override ValueTask<ValueWebSocketReceiveResult> ReceiveAsync(Memory<byte>
{
try
{
WebSocketValidate.ThrowIfInvalidReceiveState(_state, _disposed);
WebSocketValidate.ThrowIfInvalidState(_state, _disposed, s_validReceiveStates);

return ReceiveAsyncPrivate<ValueWebSocketReceiveResult>(buffer, cancellationToken);
}
Expand All@@ -343,7 +352,7 @@ public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? status

try
{
WebSocketValidate.ThrowIfInvalidCloseState(_state, _disposed);
WebSocketValidate.ThrowIfInvalidState(_state, _disposed, s_validCloseStates);
}
catch (Exception exc)
{
Expand All@@ -361,7 +370,7 @@ public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string?

private async Task CloseOutputAsyncCore(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
{
WebSocketValidate.ThrowIfInvalidCloseOutputState(_state, _disposed);
WebSocketValidate.ThrowIfInvalidState(_state, _disposed, s_validCloseOutputStates);

await SendCloseFrameAsync(closeStatus, statusDescription, cancellationToken).ConfigureAwait(false);

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp