106 }, managedWebSocket);
113 if ((uint)(num - 1) <= 6u)
234 result = awaiter3.GetResult();
316 _003Cheader_003E5__3.Processed = _003Cheader_003E5__3.PayloadLength == 0;
340 if (!awaiter3.IsCompleted)
361 num4 = span[managedWebSocket._receiveBufferOffset + 1] & 0x7F;
362 if (managedWebSocket.
_isServer || num4 > 125)
364 int minimumRequiredBytes = 2 + (managedWebSocket._isServer ? 4 : 0) + ((num4 > 125) ? ((num4 == 126) ? 2 : 8) : 0);
398 managedWebSocket._receivedMaskOffsetOffset = 0;
513 managedWebSocket._receiveBufferOffset = 0;
518 int result = awaiter.GetResult();
522 managedWebSocket._receiveBufferCount += num2;
531 if (!awaiter.IsCompleted)
576 WebSocketState.CloseReceived
582 WebSocketState.CloseSent
588 WebSocketState.CloseReceived
595 WebSocketState.CloseSent
674 target.SendKeepAliveFrameAsync();
683 if (dangerousDeflateOptions !=
null)
933 if (
_deflater !=
null && !disableCompression)
948 num2 = num.GetValueOrDefault() + 4;
950 if (payloadBuffer.
Length > 0)
978 sendBuffer[0] = (byte)opcode;
981 sendBuffer[0] |= 128;
983 if (compressed && opcode != 0)
988 if (payload.
Length <= 125)
990 sendBuffer[1] = (byte)payload.
Length;
993 else if (payload.
Length <= 65535)
996 sendBuffer[2] = (byte)(payload.
Length / 256);
997 sendBuffer[3] = (byte)payload.
Length;
1002 sendBuffer[1] = 127;
1003 int num2 = payload.
Length;
1004 for (
int num3 = 9; num3 >= 2; num3--)
1006 sendBuffer[num3] = (byte)num2;
1013 sendBuffer[1] |= 128;
1024 [AsyncStateMachine(typeof(_003CReceiveAsyncPrivate_003Ed__63<>))]
1030 stateMachine._003C_003E4__this =
this;
1031 stateMachine.payloadBuffer = payloadBuffer;
1033 stateMachine._003C_003E1__state = -1;
1063 string closeStatusDescription =
string.Empty;
1144 if (closeStatus < WebSocketCloseStatus.NormalClosure || closeStatus >= (
WebSocketCloseStatus)5000)
1152 if ((uint)(closeStatus - 1000) <= 3u || (uint)(closeStatus - 1007) <= 4u)
1187 messageHeader.PayloadLength = 0
L;
1188 for (
int i = 0; i < 8; i++)
1190 messageHeader.PayloadLength = (messageHeader.PayloadLength << 8) | span[
_receiveBufferOffset + i];
1219 switch (messageHeader.
Opcode)
1255 messageHeader.Processed = messageHeader.PayloadLength == 0
L && !messageHeader.
Compressed;
1256 resultHeader = messageHeader;
1281 receiveTask = ReceiveAsyncPrivate<ValueWebSocketReceiveResult>(closeBuffer,
cancellationToken);
1294 await receiveTask.
ConfigureAwait(continueOnCapturedContext:
false);
1314 if (
string.IsNullOrEmpty(closeStatusDescription))
1324 ushort num2 = (ushort)closeStatus;
1325 buffer[0] = (byte)(num2 >> 8);
1326 buffer[1] = (byte)(num2 & 0xFFu);
1360 [AsyncStateMachine(typeof(_003CEnsureBufferContainsAsync_003Ed__74))]
1366 stateMachine._003C_003E4__this =
this;
1367 stateMachine.minimumRequiredBytes = minimumRequiredBytes;
1369 stateMachine.throwOnPrematureClosure = throwOnPrematureClosure;
1370 stateMachine._003C_003E1__state = -1;
1381 if (throwOnPrematureClosure)
1395 if (sendBuffer !=
null)
1417 byte* ptr3 = ptr + toMask.
Length;
1418 byte* ptr4 = (
byte*)(&mask);
1419 if (ptr3 - ptr2 >= 4)
1421 while ((ulong)ptr2 % 4uL != 0
L)
1423 byte* intPtr = ptr2++;
1424 *intPtr ^= ptr4[maskIndex];
1425 maskIndex = (maskIndex + 1) & 3;
1430 for (; (ulong)ptr2 % (ulong)(uint)
Vector<byte>.Count != 0
L; ptr2 += 4)
1445 for (; ptr3 - ptr2 >= 4; ptr2 += 4)
1450 while (ptr2 != ptr3)
1452 byte* intPtr2 = ptr2++;
1453 *intPtr2 ^= ptr4[maskIndex];
1454 maskIndex = (maskIndex + 1) & 3;
1468 while (num < span.
Length)
1470 if (!
state.SequenceInProgress)
1472 state.SequenceInProgress =
true;
1475 if ((b & 0x80) == 0)
1477 state.AdditionalBytesExpected = 0;
1478 state.CurrentDecodeBits = b & 0x7F;
1479 state.ExpectedValueMin = 0;
1483 if ((b & 0xC0) == 128)
1487 if ((b & 0xE0) == 192)
1489 state.AdditionalBytesExpected = 1;
1490 state.CurrentDecodeBits = b & 0x1F;
1491 state.ExpectedValueMin = 128;
1493 else if ((b & 0xF0) == 224)
1495 state.AdditionalBytesExpected = 2;
1496 state.CurrentDecodeBits = b & 0xF;
1497 state.ExpectedValueMin = 2048;
1501 if ((b & 0xF8) != 240)
1505 state.AdditionalBytesExpected = 3;
1506 state.CurrentDecodeBits = b & 7;
1507 state.ExpectedValueMin = 65536;
1511 while (
state.AdditionalBytesExpected > 0 && num < span.
Length)
1513 byte b2 = span[num];
1514 if ((b2 & 0xC0) != 128)
1519 state.AdditionalBytesExpected--;
1520 state.CurrentDecodeBits = (state.CurrentDecodeBits << 6) | (b2 & 0x3F);
1521 if (
state.AdditionalBytesExpected == 1 &&
state.CurrentDecodeBits >= 864 &&
state.CurrentDecodeBits <= 895)
1525 if (
state.AdditionalBytesExpected == 2 &&
state.CurrentDecodeBits >= 272)
1530 if (
state.AdditionalBytesExpected == 0)
1532 state.SequenceInProgress =
false;
1533 if (
state.CurrentDecodeBits <
state.ExpectedValueMin)
1539 if (endOfMessage &&
state.SequenceInProgress)
static readonly bool IsLittleEndian
static int ToInt32(byte[] value, int startIndex)
static ArrayPool< T > Shared
Task WriteAsync(byte[] buffer, int offset, int count)
Task< int > ReadAsync(byte[] buffer, int offset, int count)
static byte Min(byte val1, byte val2)
ReadOnlySpan< byte > Deflate(ReadOnlySpan< byte > payload, bool endOfMessage)
void AddBytes(int totalBytesReceived, bool endOfMessage)
unsafe bool Inflate(Span< byte > output, out int written)
void Prepare(long payloadLength, int userBufferLength)
int AdditionalBytesExpected
async ValueTask HandleReceivedPingPongAsync(MessageHeader header, CancellationToken cancellationToken)
static int CombineMaskBytes(Span< byte > buffer, int maskOffset)
static readonly WebSocketState[] s_validCloseOutputStates
static int WriteHeader(MessageOpcode opcode, byte[] sendBuffer, ReadOnlySpan< byte > payload, bool endOfMessage, bool useMask, bool compressed)
async ValueTask SendCloseFrameAsync(WebSocketCloseStatus closeStatus, string closeStatusDescription, CancellationToken cancellationToken)
readonly Utf8MessageState _utf8TextState
readonly Timer _keepAliveTimer
async ValueTask CloseWithReceiveErrorAndThrowAsync(WebSocketCloseStatus closeStatus, WebSocketError error, string errorMessage=null, Exception innerException=null)
ManagedWebSocket(Stream stream, WebSocketCreationOptions options)
string TryParseMessageHeaderFromReceiveBuffer(out MessageHeader resultHeader)
async ValueTask WaitForWriteTaskAsync(ValueTask writeTask)
ManagedWebSocket(Stream stream, bool isServer, string subprotocol, TimeSpan keepAliveInterval)
readonly Memory< byte > _receiveBuffer
void ThrowIfEOFUnexpected(bool throwOnPrematureClosure)
static readonly WebSocketState[] s_validSendStates
static readonly UTF8Encoding s_textEncoding
static bool IsValidCloseStatus(WebSocketCloseStatus closeStatus)
override ValueTask SendAsync(ReadOnlyMemory< byte > buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
static int ApplyMask(Span< byte > toMask, byte[] mask, int maskOffset, int maskOffsetIndex)
override string SubProtocol
ValueTask EnsureBufferContainsAsync(int minimumRequiredBytes, CancellationToken cancellationToken, bool throwOnPrematureClosure=true)
async ValueTask HandleReceivedCloseAsync(MessageHeader header, CancellationToken cancellationToken)
WebSocketCloseStatus? _closeStatus
static Exception CreateOperationCanceledException(Exception innerException, CancellationToken cancellationToken=default(CancellationToken))
readonly string _subprotocol
static bool TryValidateUtf8(Span< byte > span, bool endOfMessage, Utf8MessageState state)
int WriteFrameToSendBuffer(MessageOpcode opcode, bool endOfMessage, bool disableCompression, ReadOnlySpan< byte > payloadBuffer)
async Task CloseOutputAsyncCore(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
int _receivedMaskOffsetOffset
async ValueTask SendFrameFallbackAsync(MessageOpcode opcode, bool endOfMessage, bool disableCompression, ReadOnlyMemory< byte > payloadBuffer, Task lockTask, CancellationToken cancellationToken)
override Task< WebSocketReceiveResult > ReceiveAsync(ArraySegment< byte > buffer, CancellationToken cancellationToken)
readonly WebSocketDeflater _deflater
TResult GetReceiveResult< TResult >(int count, WebSocketMessageType messageType, bool endOfMessage)
void AllocateSendBuffer(int minLength)
override ValueTask< ValueWebSocketReceiveResult > ReceiveAsync(Memory< byte > buffer, CancellationToken cancellationToken)
static unsafe int ApplyMask(Span< byte > toMask, int mask, int maskIndex)
ValueTask SendFrameAsync(MessageOpcode opcode, bool endOfMessage, bool disableCompression, ReadOnlyMemory< byte > payloadBuffer, CancellationToken cancellationToken)
readonly AsyncMutex _sendMutex
override Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
bool _lastSendHadDisableCompression
ValueTask SendFrameLockAcquiredNonCancelableAsync(MessageOpcode opcode, bool endOfMessage, bool disableCompression, ReadOnlyMemory< byte > payloadBuffer)
static readonly WebSocketState[] s_validCloseStates
override Task SendAsync(ArraySegment< byte > buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
static void WriteRandomMask(byte[] buffer, int offset)
override? WebSocketCloseStatus CloseStatus
override ValueTask SendAsync(ReadOnlyMemory< byte > buffer, WebSocketMessageType messageType, WebSocketMessageFlags messageFlags, CancellationToken cancellationToken)
override string CloseStatusDescription
readonly AsyncMutex _receiveMutex
async ValueTask WaitForServerToCloseConnectionAsync(CancellationToken cancellationToken)
static readonly WebSocketState[] s_validReceiveStates
ValueTask< TResult > ReceiveAsyncPrivate< TResult >(Memory< byte > payloadBuffer, CancellationToken cancellationToken)
void ConsumeFromBuffer(int count)
async Task CloseAsyncPrivate(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
string _closeStatusDescription
MessageHeader _lastReceiveHeader
bool _lastSendWasFragment
readonly WebSocketInflater _inflater
void SendKeepAliveFrameAsync()
override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
bool ServerContextTakeover
bool ClientContextTakeover
static void ValidateArraySegment(ArraySegment< byte > arraySegment, string parameterName)
static void ValidateCloseStatus(WebSocketCloseStatus closeStatus, string statusDescription)
static void ThrowIfInvalidState(WebSocketState currentState, bool isDisposed, WebSocketState[] validStates)
static uint RotateRight(uint value, int offset)
static uint RotateLeft(uint value, int offset)
static bool IsHardwareAccelerated
static string net_Websockets_ContinuationFromFinalFrame
static string net_Websockets_InvalidControlMessage
static string net_WebSockets_Argument_InvalidMessageType
static string net_WebSockets_Argument_MessageFlagsHasDifferentCompressionOptions
static string net_Websockets_PerMessageCompressedFlagWhenNotEnabled
static string net_Websockets_InvalidPayloadLength
static string Format(string resourceFormat, object p1)
static string net_Websockets_NonContinuationAfterNonFinalFrame
static string net_Websockets_UnknownOpcode
static string net_Websockets_ClientReceivedMaskedFrame
static string net_Websockets_PerMessageCompressedFlagInContinuation
static string net_Websockets_ReservedBitsSet
Task EnterAsync(CancellationToken cancellationToken)
static TaskScheduler Default
Task ContinueWith(Action< Task< TResult > > continuationAction)
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
new Task< TResult > WaitAsync(CancellationToken cancellationToken)
static Task FromException(Exception exception)
AggregateException? Exception
bool Dispose(WaitHandle notifyObject)
bool TryGetTarget([MaybeNullWhen(false)][NotNullWhen(true)] out T target)
void SetStateMachine(IAsyncStateMachine stateMachine)
Memory< T > Slice(int start)
ConfiguredValueTaskAwaitable< int >.ConfiguredValueTaskAwaiter _003C_003Eu__1
void SetStateMachine(IAsyncStateMachine stateMachine)
CancellationToken cancellationToken
bool throwOnPrematureClosure
PoolingAsyncValueTaskMethodBuilder _003C_003Et__builder
ManagedWebSocket _003C_003E4__this
PoolingAsyncValueTaskMethodBuilder< TResult > _003C_003Et__builder
ConfiguredTaskAwaitable.ConfiguredTaskAwaiter _003C_003Eu__1
ConfiguredValueTaskAwaitable< int >.ConfiguredValueTaskAwaiter _003C_003Eu__3
void SetStateMachine(IAsyncStateMachine stateMachine)
MessageHeader _003Cheader_003E5__3
CancellationTokenRegistration _003Cregistration_003E5__2
Memory< byte > payloadBuffer
ManagedWebSocket _003C_003E4__this
ConfiguredValueTaskAwaitable.ConfiguredValueTaskAwaiter _003C_003Eu__2
int _003CtotalBytesReceived_003E5__4
CancellationToken cancellationToken
unsafe ReadOnlySpan< T > Span
static ReadOnlyMemory< T > Empty
void CopyTo(Span< T > destination)
void SetException(Exception exception)
void SetStateMachine(IAsyncStateMachine stateMachine)
static PoolingAsyncValueTaskMethodBuilder Create()
void CopyTo(Span< T > destination)
Span< T > Slice(int start)
CancellationTokenRegistration Register(Action callback)
static CancellationToken None
bool IsCompletedSuccessfully
ConfiguredValueTaskAwaitable ConfigureAwait(bool continueOnCapturedContext)
static ValueTask FromException(Exception exception)
ValueTaskAwaiter GetAwaiter()
static TimeSpan FromMilliseconds(double value)
static readonly TimeSpan Zero