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

ConvertNativeBuffer() private method

private ConvertNativeBuffer ( WebSocketProtocolComponent action, Interop buffer, WebSocketProtocolComponent bufferType ) : ArraySegment
action WebSocketProtocolComponent
buffer Interop
bufferType WebSocketProtocolComponent
return ArraySegment
        internal ArraySegment<byte> ConvertNativeBuffer(WebSocketProtocolComponent.Action action,
            Interop.WebSocket.Buffer buffer,
            WebSocketProtocolComponent.BufferType bufferType)
        {
            ThrowIfDisposed();

            IntPtr bufferData;
            uint bufferLength;

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

            if (bufferData == IntPtr.Zero)
            {
                return WebSocketValidate.EmptyPayload;
            }

            if (this.IsNativeBuffer(bufferData, bufferLength))
            {
                return new ArraySegment<byte>(_internalBuffer.Array,
                    this.GetOffset(bufferData),
                    (int)bufferLength);
            }

            Debug.Assert(false, "'buffer' MUST reference a memory segment within the pinned InternalBuffer.");
            // Indicates a violation in the contract with native Websocket.dll and could indicate 
            // memory corruption because the internal buffer is shared between managed and native code
            throw new AccessViolationException();
        }