System.Net.WebSockets.WebSocketBuffer.ConvertPinnedSendPayloadFromNative C# (CSharp) Method

ConvertPinnedSendPayloadFromNative() private method

private ConvertPinnedSendPayloadFromNative ( Interop buffer, WebSocketProtocolComponent bufferType ) : ArraySegment
buffer Interop
bufferType WebSocketProtocolComponent
return ArraySegment
        internal ArraySegment<byte> ConvertPinnedSendPayloadFromNative(Interop.WebSocket.Buffer buffer,
            WebSocketProtocolComponent.BufferType bufferType)
        {
            if (!IsPinnedSendPayloadBuffer(buffer, bufferType))
            {
                // Indicates a violation in the API contract that could indicate 
                // memory corruption because the pinned sendbuffer is shared between managed and native code
                throw new AccessViolationException();
            }

            Debug.Assert(Marshal.UnsafeAddrOfPinnedArrayElement(_pinnedSendBuffer.Array,
                _pinnedSendBuffer.Offset).ToInt64() == _pinnedSendBufferStartAddress,
                "'m_PinnedSendBuffer.Array' MUST be pinned during the entire send operation.");

            IntPtr bufferData;
            uint bufferSize;

            UnwrapWebSocketBuffer(buffer, bufferType, out bufferData, out bufferSize);

            int internalOffset = (int)(bufferData.ToInt64() - _pinnedSendBufferStartAddress);

            return new ArraySegment<byte>(_pinnedSendBuffer.Array, _pinnedSendBuffer.Offset + internalOffset, (int)bufferSize);
        }