/// <summary> /// Parses the binary image. /// </summary> /// <param name="buffer">Binary image to parse.</param> /// <param name="startIndex">Start index into <paramref name="buffer"/> to begin parsing.</param> /// <param name="length">Length of valid data within <paramref name="buffer"/>.</param> /// <returns>The length of the data that was parsed.</returns> /// <remarks> /// This method is overridden to parse from cumulated frame images. /// </remarks> public override int ParseBinaryImage(byte[] buffer, int startIndex, int length) { // If frame image collector was used, make sure and parse from entire frame image... if (m_frameHeader != null) { // If all configuration frame images have been received, we can safely start parsing if (m_frameHeader.IsLastFrame) { FrameImageCollector frameImages = m_frameHeader.FrameImages; if (frameImages != null) { // Each individual frame will already have had a CRC check, so we implement standard parse to // bypass ChannelBase CRC frame validation on cumulative frame image buffer = frameImages.BinaryImage; length = frameImages.BinaryLength; startIndex = 0; // Parse out header, body and footer images startIndex += ParseHeaderImage(buffer, startIndex, length); startIndex += ParseBodyImage(buffer, startIndex, length - startIndex); startIndex += ParseFooterImage(buffer, startIndex, length - startIndex); // Include 2 bytes for CRC that was already validated return(startIndex + 2); } } // There are more configuration frame images coming, keep parser moving by returning total // frame length that was already parsed. return(State.ParsedBinaryLength); } return(base.ParseBinaryImage(buffer, startIndex, length)); }
// Static Methods // Cumulates frame images internal static void CumulateFrameImage(CommonFrameHeader parsedFrameHeader, byte[] buffer, int offset, ref FrameImageCollector frameImages) { // If this is the first frame, cumulate all partial frames together as one complete frame if (parsedFrameHeader.IsFirstFrame) { frameImages = new FrameImageCollector(); } try { // Append next frame image frameImages.AppendFrameImage(buffer, offset, parsedFrameHeader.FrameLength); } catch { // Stop accumulation if CRC check fails frameImages = null; throw; } // Store a reference to frame image collection in common header state so configuration frame // can be parsed from entire combined binary image collection when last frame is received parsedFrameHeader.FrameImages = frameImages; // Clear local reference to frame image collection if this is the last frame if (parsedFrameHeader.IsLastFrame) { frameImages = null; } }