public virtual void Parse() { m_Index_Unk0 = bReader.ReadInt32(); long unkDataCount = bReader.ReadInt64(); List <byte> unkData = new List <byte>(); for (int iUD = 0; iUD < unkDataCount; iUD++) { unkData.Add(bReader.ReadByte()); } m_AdditionalData = unkData.ToArray(); if (m_AdditionalData.Length > 0) { for (int i = 0; i < m_AdditionalData[m_AdditionalData.Length - 1]; i++) { FrameHeader nFrameHeader = new FrameHeader(bReader); nFrameHeader.Parse(); m_FrameHeaders.Add(nFrameHeader); } } }
/// <summary> /// Process messages until the socket is closed. /// </summary> internal void Run() { byte[] buffer = new byte[MaxFrameSize]; long index = 0; FrameType lastFrame = FrameType.Unknown; while (!m_closed) { // Read the header FrameHeader header = FrameHeader.Parse(m_input); if (header == null) { Close(); return; } // Figure out what to do with the frame bool readData = true; switch (header.OpCode) { case ContinuationFrame: if (lastFrame == FrameType.Unknown) { // Continuation with no start frame, just drop the connection Close(); return; } break; case TextFrame: // Start of a text frame index = 0; lastFrame = FrameType.Text; break; case BinaryFrame: // Start of a binary frame index = 0; lastFrame = FrameType.Text; break; case CloseFrame: // Close the connection Close(); return; case PingFrame: // Request for a Pong response SendPong(header); continue; default: // Just ignore it readData = false; break; } // If the packet is too large just discard the payload if ((index + header.Length) > MaxFrameSize) { readData = false; index = MaxFrameSize; } // Read or consume the data if (readData) { if (!ReadData(header, buffer, (int)index)) { Close(); } index += header.Length; } else if (!ConsumeData(header)) { Close(); } // Did we wind up closing the connection? if (m_closed) { return; } // Is this the end of the data sequence? if (header.Finished) { // If it was too large we ignore it if (index >= MaxFrameSize) { continue; } // If it wasn't a text frame we ignore it if (lastFrame != FrameType.Text) { continue; } // TODO: Send out the notification for new data string message = Encoding.UTF8.GetString(buffer, 0, (int)index); index = 0; DataReceivedHandler handler = DataReceived; if (handler != null) { handler(this, message); } } } }