Example #1
0
        public void CheckPacketLength(byte[] packet, int packetPtr)
        {
            var len = PacketAccessor.PacketFixedHeaderLength + 1 + PacketAccessor.Length(packet);

            //if (len != packetPtr)
            //    throw new Exception("Packet Length Check Exception");
            if (len != packetPtr)
            {
                Console.Error.WriteLine(@"Packet length check exception: apid={0} timestamp={1}", PacketAccessor.APID(packet),
                                        PacketAccessor.Time42(packet));
            }
        }
Example #2
0
        public static IEnumerable <byte[]> Iterator(Stream stream)
        {
            var buffer = new byte[PacketAccessor.MaximumPacketSize];

            using (var reader = new BinaryReader(stream))
            {
                while (true)
                {
                    // Read the packet header
                    var ptr    = 0;
                    var toread = 6;
                    int read;
                    while (toread > 0)
                    {
                        read = reader.Read(buffer, ptr, toread);
                        if (read == 0)
                        {
                            yield break;
                        }
                        toread -= read;
                        ptr    += read;
                    }

                    // Check whether we've run off the end.
                    if ((buffer[0] | buffer[1] | buffer[2]) == 0)
                    {
                        yield break;
                    }

                    var length = PacketAccessor.Length(buffer);
                    toread = 1 + length;

                    // Read the rest of the packet
                    while (toread > 0)
                    {
                        read = reader.Read(buffer, ptr, toread);
                        if (read == 0)
                        {
                            yield break;
                        }
                        toread -= read;
                        ptr    += read;
                    }

                    yield return(buffer);
                }
            }
        }
Example #3
0
        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?
        }
Example #4
0
        public virtual IEnumerable <byte[]> Iterator()
        {
            var buffer = new byte[PacketAccessor.MaximumPacketSize];

            using (var reader = new BinaryReader(File.Open(_Path, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                var  readerlen = (int)reader.BaseStream.Length;
                long readerpos = 0;

                // Skip the header, if any
                var headerLength = GetHeaderLength();
                for (var i = 0; i < headerLength; i++)
                {
                    reader.ReadByte();
                }
                readerpos += headerLength;

                while (readerpos < readerlen)
                {
                    _Position = readerpos;

                    // Read the packet header
                    var ptr    = 0;
                    var toread = 6;
                    int read;
                    while (toread > 0)
                    {
                        read = reader.Read(buffer, ptr, toread);
                        if (read == 0)
                        {
                            Console.WriteLine(@"Premature EOF: {0}", Path);
                            yield break;
                        }
                        toread -= read;
                        ptr    += read;
                    }

                    // Check whether we've run off the end.
                    if ((buffer[0] | buffer[1] | buffer[2]) == 0)
                    {
                        yield break;
                    }

                    var length = PacketAccessor.Length(buffer);
                    toread = 1 + length;

                    // Read the rest of the packet

                    while (toread > 0)
                    {
                        read = reader.Read(buffer, ptr, toread);
                        if (read == 0)
                        {
                            Console.WriteLine(@"Premature EOF: {0}", Path);
                            yield break;
                        }
                        toread -= read;
                        ptr    += read;
                    }

                    //totalLength = length + 7;
                    //byte[] packet = new byte[totalLength];
                    //Array.Copy(buffer, packet, totalLength);
                    //yield return packet;
                    yield return(buffer);

                    readerpos += ptr;
                }
            }
        }
Example #5
0
        public override IEnumerable <byte[]> Iterator()
        {
            var buffer = new byte[PacketAccessor.MaximumPacketSize]; // Should I allocate the max size here?
            var header = new byte[12];

            using (var reader = new BinaryReader(File.Open(_Path, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                var  readerlen = (int)reader.BaseStream.Length;
                long readerpos = 0;

                // Skip the header, if any
                var headerLength = GetHeaderLength();
                for (var i = 0; i < headerLength; i++)
                {
                    reader.ReadByte();
                }
                readerpos += headerLength;

                while (readerpos < readerlen)
                {
                    int read;

                    // Skip the 12 byte annotation
                    var ptr    = 0;
                    var toread = 12;
                    while (toread > 0)
                    {
                        read = reader.Read(header, ptr, toread);
                        if (read == 0)
                        {
                            if (ptr != 0)
                            {
                                Console.WriteLine(@"Premature EOF: {0}", Path);
                            }
                            yield break;
                        }
                        toread -= read;
                        ptr    += read;
                    }

                    readerpos += 12;
                    _Position  = readerpos;

                    // Read the packet header
                    ptr    = 0;
                    toread = 6;
                    while (toread > 0)
                    {
                        read = reader.Read(buffer, ptr, toread);
                        if (read == 0)
                        {
                            Console.WriteLine(@"Premature EOF: {0}", Path);
                            yield break;
                        }
                        toread -= read;
                        ptr    += read;
                    }

                    // Check whether we've run off the end.
                    if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0)
                    {
                        yield break;
                    }

                    var length = PacketAccessor.Length(buffer);
                    toread = 1 + length;

                    // Read the rest of the packet

                    while (toread > 0)
                    {
                        read = reader.Read(buffer, ptr, toread);
                        if (read == 0)
                        {
                            Console.WriteLine(@"Premature EOF: {0}", Path);
                            yield break;
                        }
                        toread -= read;
                        ptr    += read;
                    }

                    if ((header[3] & ErrorMask) == 0)
                    {
                        yield return(buffer);
                    }

                    readerpos += ptr;
                }
            }
        }
Example #6
0
        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?
        }