<commit_before>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace System.Net.WebSockets
{
    // This class helps to abstract the internal WebSocket buffer, which is used to interact with the native WebSocket
    // protocol component (WSPC). It helps to shield the details of the layout and the involved pointer arithmetic.
    // The internal WebSocket buffer also contains a segment, which is used by the WebSocketBase class to buffer 
    // payload (parsed by WSPC already) for the application, if the application requested fewer bytes than the
    // WSPC returned. The internal buffer is pinned for the whole lifetime if this class.
    // LAYOUT:
    // | Native buffer              | PayloadReceiveBuffer | PropertyBuffer |
    // | RBS + SBS + 144            | RBS                  | PBS            |
    // | Only WSPC may modify       | Only WebSocketBase may modify         | 
    //
    // *RBS = ReceiveBufferSize, *SBS = SendBufferSize
    // *PBS = PropertyBufferSize (32-bit: 16, 64 bit: 20 bytes)
    internal class WebSocketBuffer : IDisposable
    {
        private const int NativeOverheadBufferSize = 144;
        internal const int MinSendBufferSize = 16;
        internal const int MinReceiveBufferSize = 256;
        internal const int MaxBufferSize = 64 * 1024;
        private static readonly int s_PropertyBufferSize = 3 * sizeof(uint) + IntPtr.Size;

        private readonly int _receiveBufferSize;

        // Indicates the range of the pinned byte[] that can be used by the WSPC (nativeBuffer + pinnedSendBuffer)
        private readonly long _startAddress;
        private readonly long _endAddress;
        private readonly GCHandle _gcHandle;
        private readonly ArraySegment<byte> _internalBuffer;
        private readonly ArraySegment<byte> _nativeBuffer;
        private readonly ArraySegment<byte> _payloadBuffer;
        private readonly ArraySegment<byte> _propertyBuffer;
        private readonly int _sendBufferSize;
        private volatile int _payloadOffset;
        private volatile WebSocketReceiveResult _bufferedPayloadReceiveResult;
        private long _pinnedSendBufferStartAddress;
        private long _pinnedSendBufferEndAddress;
        private ArraySegment<byte> _pinnedSendBuffer;
        private GCHandle _pinnedSendBufferHandle;
        private int _stateWhenDisposing = int.MinValue;
        private int _sendBufferState;

        private WebSocketBuffer(ArraySegment<byte> internalBuffer, int receiveBufferSize, int sendBufferSize)
        {
            Debug.Assert(internalBuffer != null, "'internalBuffer' MUST NOT be NULL.");
            Debug.Assert(receiveBufferSize >= MinReceiveBufferSize,
                "'receiveBufferSize' MUST be at least " + MinReceiveBufferSize.ToString(NumberFormatInfo.InvariantInfo) + ".");
            Debug.Assert(sendBufferSize >= MinSendBufferSize,
                "'sendBufferSize' MUST be at least " + MinSendBufferSize.ToString(NumberFormatInfo.InvariantInfo) + ".");
            Debug.Assert(receiveBufferSize <= MaxBufferSize,
                "'receiveBufferSize' MUST NOT exceed " + MaxBufferSize.ToString(NumberFormatInfo.InvariantInfo) + ".");
            Debug.Assert(sendBufferSize <= MaxBufferSize,
                "'sendBufferSize' MUST NOT exceed  " + MaxBufferSize.ToString(NumberFormatInfo.InvariantInfo) + ".");

            _receiveBufferSize = receiveBufferSize;
            _sendBufferSize = sendBufferSize;
            _internalBuffer = internalBuffer;
            _gcHandle = GCHandle.Alloc(internalBuffer.Array, GCHandleType.Pinned);
            // Size of the internal buffer owned exclusively by the WSPC.
            int nativeBufferSize = _receiveBufferSize + _sendBufferSize + NativeOverheadBufferSize;
            _startAddress = Marshal.UnsafeAddrOfPinnedArrayElement(internalBuffer.Array, internalBuffer.Offset).ToInt64();
            _endAddress = _startAddress + nativeBufferSize;
            _nativeBuffer = new ArraySegment<byte>(internalBuffer.Array, internalBuffer.Offset, nativeBufferSize);
            _payloadBuffer = new ArraySegment<byte>(internalBuffer.Array,
                _nativeBuffer.Offset + _nativeBuffer.Count,
                _receiveBufferSize);
            _propertyBuffer = new ArraySegment<byte>(internalBuffer.Array,
                _payloadBuffer.Offset + _payloadBuffer.Count,
                s_PropertyBufferSize);
            _sendBufferState = SendBufferState.None;<commit_msg>Fix bogus Debug.Assert in WebSocketBuffer (#19103)<commit_after>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace System.Net.WebSockets
{
    // This class helps to abstract the internal WebSocket buffer, which is used to interact with the native WebSocket
    // protocol component (WSPC). It helps to shield the details of the layout and the involved pointer arithmetic.
    // The internal WebSocket buffer also contains a segment, which is used by the WebSocketBase class to buffer 
    // payload (parsed by WSPC already) for the application, if the application requested fewer bytes than the
    // WSPC returned. The internal buffer is pinned for the whole lifetime if this class.
    // LAYOUT:
    // | Native buffer              | PayloadReceiveBuffer | PropertyBuffer |
    // | RBS + SBS + 144            | RBS                  | PBS            |
    // | Only WSPC may modify       | Only WebSocketBase may modify         | 
    //
    // *RBS = ReceiveBufferSize, *SBS = SendBufferSize
    // *PBS = PropertyBufferSize (32-bit: 16, 64 bit: 20 bytes)
    internal class WebSocketBuffer : IDisposable
    {
        private const int NativeOverheadBufferSize = 144;
        internal const int MinSendBufferSize = 16;
        internal const int MinReceiveBufferSize = 256;
        internal const int MaxBufferSize = 64 * 1024;
        private static readonly int s_PropertyBufferSize = 3 * sizeof(uint) + IntPtr.Size;

        private readonly int _receiveBufferSize;

        // Indicates the range of the pinned byte[] that can be used by the WSPC (nativeBuffer + pinnedSendBuffer)
        private readonly long _startAddress;
        private readonly long _endAddress;
        private readonly GCHandle _gcHandle;
        private readonly ArraySegment<byte> _internalBuffer;
        private readonly ArraySegment<byte> _nativeBuffer;
        private readonly ArraySegment<byte> _payloadBuffer;
        private readonly ArraySegment<byte> _propertyBuffer;
        private readonly int _sendBufferSize;
        private volatile int _payloadOffset;
        private volatile WebSocketReceiveResult _bufferedPayloadReceiveResult;
        private long _pinnedSendBufferStartAddress;
        private long _pinnedSendBufferEndAddress;
        private ArraySegment<byte> _pinnedSendBuffer;
        private GCHandle _pinnedSendBufferHandle;
        private int _stateWhenDisposing = int.MinValue;
        private int _sendBufferState;

        private WebSocketBuffer(ArraySegment<byte> internalBuffer, int receiveBufferSize, int sendBufferSize)
        {
            Debug.Assert(internalBuffer.Array != null, "'internalBuffer.Array' MUST NOT be NULL.");
            Debug.Assert(receiveBufferSize >= MinReceiveBufferSize,
                "'receiveBufferSize' MUST be at least " + MinReceiveBufferSize.ToString(NumberFormatInfo.InvariantInfo) + ".");
            Debug.Assert(sendBufferSize >= MinSendBufferSize,
                "'sendBufferSize' MUST be at least " + MinSendBufferSize.ToString(NumberFormatInfo.InvariantInfo) + ".");
            Debug.Assert(receiveBufferSize <= MaxBufferSize,
                "'receiveBufferSize' MUST NOT exceed " + MaxBufferSize.ToString(NumberFormatInfo.InvariantInfo) + ".");
            Debug.Assert(sendBufferSize <= MaxBufferSize,
                "'sendBufferSize' MUST NOT exceed  " + MaxBufferSize.ToString(NumberFormatInfo.InvariantInfo) + ".");

            _receiveBufferSize = receiveBufferSize;
            _sendBufferSize = sendBufferSize;
            _internalBuffer = internalBuffer;
            _gcHandle = GCHandle.Alloc(internalBuffer.Array, GCHandleType.Pinned);
            // Size of the internal buffer owned exclusively by the WSPC.
            int nativeBufferSize = _receiveBufferSize + _sendBufferSize + NativeOverheadBufferSize;
            _startAddress = Marshal.UnsafeAddrOfPinnedArrayElement(internalBuffer.Array, internalBuffer.Offset).ToInt64();
            _endAddress = _startAddress + nativeBufferSize;
            _nativeBuffer = new ArraySegment<byte>(internalBuffer.Array, internalBuffer.Offset, nativeBufferSize);
            _payloadBuffer = new ArraySegment<byte>(internalBuffer.Array,
                _nativeBuffer.Offset + _nativeBuffer.Count,
                _receiveBufferSize);
            _propertyBuffer = new ArraySegment<byte>(internalBuffer.Array,
                _payloadBuffer.Offset + _payloadBuffer.Count,
                s_PropertyBufferSize);
            _sendBufferState = SendBufferState.None;
{'fraction_non_alphanumeric': 0.06665967927890158, 'fraction_numerical': 0.006393459805051882, 'mean_word_length': 2.970869746150645, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4048097', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2275, 'n_words': 730}