private bool Read(FrameFile ff) { if (_fc1 < 0) { if (!ff.Read(_buf1)) { return(false); } _fc1 = FrameAccessor.FrameCount(_buf1); return(true); } if (_fc2 < 0) { if (!ff.Read(_buf2)) { return(false); } _fc2 = FrameAccessor.FrameCount(_buf2); return(true); } throw new Exception("No free frame buffers"); }
public override IEnumerable <byte[]> Iterator() { var state = State.Idle; var packet = new byte[65516 + 12]; // add some margin 65536 bytes of data plus 6 of header + 6 of timestamp var frameCounter = 0; var packetPtr = 0; // copy pointer into the packet array var framePtr = 0; // copy pointer into the frame array foreach (var frame in FrameIterator()) { var virtualChannel = FrameAccessor.VirtualChannel(frame); //Console.WriteLine("VirtualChannel={0}", virtualChannel); if (virtualChannel != VirtualChannel) { continue; } var frameCount = FrameAccessor.FrameCount(frame); //Console.WriteLine("FrameCount={0}", frameCounter); var firstPacketPtr = FrameAccessor.FirstHeaderPointer(frame); switch (state) { case State.Idle: if (firstPacketPtr == FrameAccessor.FirstHeaderPointerOverflow) { break; // No packet starts here } framePtr = firstPacketPtr + FrameAccessor.MPDUStart; state = State.Running; frameCounter = frameCount; packetPtr = 0; goto case State.Iterate; case State.Running: frameCounter++; if (frameCounter != frameCount && !(frameCount == 0 && frameCounter == 16777216)) { var delta = frameCount - frameCounter; if (delta < 0) { delta += 16777216; } if (LogLevel > 0) { Console.Error.WriteLine(@"Frame sequence error: expected {0}, saw {1}, {2} frames skipped", frameCounter, frameCount, delta); } state = State.Idle; continue; } framePtr = FrameAccessor.MPDUStart; if (firstPacketPtr == 0) { if (packetPtr > 0) { CheckPacketLength(packet, packetPtr); // Get rid of this after debugging yield return(packet); packetPtr = 0; } goto case State.Iterate; } if (firstPacketPtr == FrameAccessor.FirstHeaderPointerOverflow) { AppendToPacket(frame, FrameAccessor.MPDUPacketZoneLength, packet, ref framePtr, ref packetPtr); break; } if (firstPacketPtr == 2047) { // Idle Frame break; } if (firstPacketPtr > 0) { AppendToPacket(frame, firstPacketPtr, packet, ref framePtr, ref packetPtr); { CheckPacketLength(packet, packetPtr); // Get rid of this after debugging yield return(packet); packetPtr = 0; } goto case State.Iterate; } break; case State.Iterate: { var flag = true; while (flag) { var gap = FrameAccessor.MPDUEnd - framePtr; if (gap < PacketAccessor.PacketFixedHeaderLength) { // This packet must extend past for (var i = 0; i < gap; i++) { packet[packetPtr++] = frame[framePtr++]; } flag = false; continue; } for (var i = 0; i < PacketAccessor.PacketFixedHeaderLength; i++) { packet[packetPtr++] = frame[framePtr++]; } var len = PacketAccessor.Length(packet) + 1; // real length is always the field + 1 gap = FrameAccessor.MPDUEnd - framePtr; if (gap <= 0) { flag = false; continue; } if (gap >= len) { // it'll fit AppendToPacket(frame, len, packet, ref framePtr, ref packetPtr); { CheckPacketLength(packet, packetPtr); // Get rid of this after debugging yield return(packet); packetPtr = 0; } flag = (gap > len); // Maybe it fits just barely continue; } // It won't fit AppendToPacket(frame, gap, packet, ref framePtr, ref packetPtr); flag = false; } } break; } } // Should there be a yield break here? }
public static IEnumerable <byte[]> Iterator(IEnumerable <byte[]> frames) { var state = new FrameFileSequence.State[64]; for (var i = 0; i < 63; i++) { state[i] = FrameFileSequence.State.Idle; } var packet = new byte[64][]; const int bufferSize = 65516 + 12; // add some margin 65536 bytes of data plus 6 of header + 6 of timestamp var frameCounter = new int[64]; // initial values of 0 var packetPtr = new int[64]; // copy pointer into the packet array var framePtr = new int[64]; // copy pointer into the frame array foreach (var frame in frames) { var vc = FrameAccessor.VirtualChannel(frame); if (packet[vc] == null) { packet[vc] = new byte[bufferSize]; } //Console.WriteLine("VirtualChannel={0}", vc); //if (vc != 1) continue; var frameCount = FrameAccessor.FrameCount(frame); //Console.WriteLine("FrameCount={0} State={1}", frameCounter[vc], state[vc]); var firstPacketPtr = FrameAccessor.FirstHeaderPointer(frame); switch (state[vc]) { case FrameFileSequence.State.Idle: if (firstPacketPtr == FrameAccessor.FirstHeaderPointerOverflow) { break; // No packet starts here } framePtr[vc] = firstPacketPtr + FrameAccessor.MPDUStart; state[vc] = FrameFileSequence.State.Running; frameCounter[vc] = frameCount; packetPtr[vc] = 0; goto case FrameFileSequence.State.Iterate; case FrameFileSequence.State.Running: frameCounter[vc]++; if (frameCounter[vc] != frameCount && !(frameCount == 0 && frameCounter[vc] == 16777216)) { var delta = frameCount - frameCounter[vc]; if (delta < 0) { delta += 16777216; } //if (LogLevel > 0) // Console.WriteLine(@"Frame sequence error: expected {0}, saw {1}, {2} frames skipped", frameCounter[vc], frameCount, delta); state[vc] = FrameFileSequence.State.Idle; continue; } framePtr[vc] = FrameAccessor.MPDUStart; if (firstPacketPtr == 0) { if (packetPtr[vc] > 0) { CheckPacketLength(packet[vc], packetPtr[vc]); // Get rid of this after debugging yield return(packet[vc]); packetPtr[vc] = 0; } goto case FrameFileSequence.State.Iterate; } if (firstPacketPtr == FrameAccessor.FirstHeaderPointerOverflow) { AppendToPacket(frame, FrameAccessor.MPDUPacketZoneLength, packet[vc], ref framePtr[vc], ref packetPtr[vc]); break; } if (firstPacketPtr == 2047) { // Idle Frame break; } if (firstPacketPtr > 0) { AppendToPacket(frame, firstPacketPtr, packet[vc], ref framePtr[vc], ref packetPtr[vc]); { CheckPacketLength(packet[vc], packetPtr[vc]); // Get rid of this after debugging yield return(packet[vc]); packetPtr[vc] = 0; } goto case FrameFileSequence.State.Iterate; } break; case FrameFileSequence.State.Iterate: { var flag = true; var p = packet[vc]; while (flag) { var gap = FrameAccessor.MPDUEnd - framePtr[vc]; if (gap < PacketAccessor.PacketFixedHeaderLength) { // This packet must extend past for (var i = 0; i < gap; i++) { p[packetPtr[vc]++] = frame[framePtr[vc]++]; } flag = false; continue; } for (var i = 0; i < PacketAccessor.PacketFixedHeaderLength; i++) { p[packetPtr[vc]++] = frame[framePtr[vc]++]; } var len = PacketAccessor.Length(p) + 1; // real length is always the field + 1 gap = FrameAccessor.MPDUEnd - framePtr[vc]; if (gap <= 0) { flag = false; continue; } if (gap >= len) { // it'll fit AppendToPacket(frame, len, p, ref framePtr[vc], ref packetPtr[vc]); { CheckPacketLength(p, packetPtr[vc]); // Get rid of this after debugging yield return(p); packetPtr[vc] = 0; } flag = (gap > len); // Maybe it fits just barely continue; } // It won't fit AppendToPacket(frame, gap, p, ref framePtr[vc], ref packetPtr[vc]); flag = false; } } break; } } // Should there be a yield break here? }