System.Net.WebSockets.WebSocketProtocolComponent.MarshalHttpHeaders C# (CSharp) Method

MarshalHttpHeaders() private static method

private static MarshalHttpHeaders ( IntPtr nativeHeadersPtr, int nativeHeaderCount ) : Interop.WebSocket.HttpHeader[]
nativeHeadersPtr IntPtr
nativeHeaderCount int
return Interop.WebSocket.HttpHeader[]
        private static Interop.WebSocket.HttpHeader[] MarshalHttpHeaders(IntPtr nativeHeadersPtr,
            int nativeHeaderCount)
        {
            Debug.Assert(nativeHeaderCount >= 0, "'nativeHeaderCount' MUST NOT be negative.");
            Debug.Assert(nativeHeadersPtr != IntPtr.Zero || nativeHeaderCount == 0,
                "'nativeHeaderCount' MUST be 0.");

            Interop.WebSocket.HttpHeader[] httpHeaders = new Interop.WebSocket.HttpHeader[nativeHeaderCount];

            // structure of Interop.WebSocket.HttpHeader:
            //   Name = string*
            //   NameLength = uint*
            //   Value = string*
            //   ValueLength = uint*
            // NOTE - All fields in the object are pointers to the actual value, hence the use of 
            //        4 * IntPtr.Size to get to the next header. 
            int httpHeaderStructSize = 4 * IntPtr.Size;

            for (int i = 0; i < nativeHeaderCount; i++)
            {
                int offset = httpHeaderStructSize * i;
                IntPtr currentHttpHeaderPtr = IntPtr.Add(nativeHeadersPtr, offset);
                MarshalAndVerifyHttpHeader(currentHttpHeaderPtr, ref httpHeaders[i]);
            }

            Debug.Assert(httpHeaders != null);
            Debug.Assert(httpHeaders.Length == nativeHeaderCount);

            return httpHeaders;
        }