Esempio n. 1
0
        public void PacketStreamTests(bool collatedPackets, bool compressed)
        {
            Stream    sceneStream = SceneGenerator.Generate(collatedPackets, compressed);
            Stopwatch timer       = new Stopwatch();

            timer.Start();

            PacketStreamReader packetStream = new PacketStreamReader(sceneStream);
            PacketBuffer       packet       = null;

            // Reading
            Console.WriteLine("Reading to end of stream");
            long bytesRead    = 0;
            uint packetCount1 = 0;
            uint packetCount2 = 0;

            while (!packetStream.EndOfStream)
            {
                packet = packetStream.NextPacket(ref bytesRead);
                if (!packetStream.EndOfStream)
                {
                    Assert.NotNull(packet);                                   //, "Failed to extract packet.");
                    Assert.Equal(PacketBufferStatus.Complete, packet.Status); //, "Unpexected packet status: {0}", packet.Status.ToString());
                    ++packetCount1;
                }
            }
            Console.WriteLine("Read {0} packets", packetCount1);

            Console.WriteLine("Resetting");
            packetStream.Reset();
            packetCount2 = 0;
            while (!packetStream.EndOfStream)
            {
                packet = packetStream.NextPacket(ref bytesRead);
                if (!packetStream.EndOfStream)
                {
                    Assert.NotNull(packet);                                   //, "Failed to extract packet.");
                    Assert.Equal(PacketBufferStatus.Complete, packet.Status); //, "Unpexected packet status: {0}", packet.Status.ToString());
                    ++packetCount2;
                }
            }
            Console.WriteLine("Read {0} packets", packetCount2);

            Assert.Equal(packetCount2, packetCount1);//, "Packet count mismatch after reset and replay.");

            timer.Stop();
            Console.WriteLine("Elapsed: {0}", timer.Elapsed);
        }
Esempio n. 2
0
        /// <summary>
        /// Restore the given keyframe.
        /// </summary>
        /// <param name="keyframe">To restore</param>
        /// <returns>True on success.</returns>
        /// <remarks>
        /// On success, the <see cref="PacketQueue"/> is cleared, a reset packet pushed followed by the
        /// decoded keyframe packets. On failure <see cref="ResetStream()"/> is called which includes
        /// queueing a reset packet.
        /// </remarks>
        private bool RestoreKeyframe(Keyframe keyframe)
        {
            if (!keyframe.Valid || string.IsNullOrEmpty(keyframe.TemporaryFilePath) || !File.Exists(keyframe.TemporaryFilePath))
            {
                return(false);
            }

            PacketStreamReader keyframeStream = null;

            try
            {
                // Ensure stream has been reset.
                keyframeStream = new PacketStreamReader(
                    new FileStream(keyframe.TemporaryFilePath, FileMode.Open, FileAccess.Read));
                System.Collections.Generic.List <PacketBuffer> decodedPackets = new System.Collections.Generic.List <PacketBuffer>();
                _packetStream.Seek(keyframe.StreamOffset, SeekOrigin.Begin);
                long streamPos = _packetStream.Position;

                // Decode the keyframe data.
                if (streamPos == keyframe.StreamOffset)
                {
                    long         processedBytes = 0;
                    PacketBuffer packet         = null;
                    try
                    {
                        while ((packet = keyframeStream.NextPacket(ref processedBytes)) != null)
                        {
                            decodedPackets.Add(packet);
                        }
                    }
                    catch (TesIOException e)
                    {
                        Log.Exception(e);
                        return(false);
                    }

                    uint currentFrame       = keyframe.OffsetFrameNumber;
                    uint droppedPacketCount = 0;

                    // The stream seek position may not exactly match the frame end marker such as when it appears within a
                    // collated packet. We must therefore continue to read messages from the main file stream (after seeking)
                    // until we are at the keyframe number. We can ignore all these messages as the keyframe includes their
                    // side effects.
                    while (currentFrame < keyframe.FrameNumber)
                    {
                        long bytesRead = 0;
                        packet = _packetStream.NextPacket(ref bytesRead);
                        if (packet != null)
                        {
                            if (packet.Status == PacketBufferStatus.Complete)
                            {
                                ++droppedPacketCount;
                                if (packet.Header.RoutingID == (ushort)RoutingID.Control &&
                                    packet.Header.MessageID == (ushort)ControlMessageID.EndFrame)
                                {
                                    ++currentFrame;
                                }
                            }
                            else
                            {
                                // Failed.
                                Log.Error("Incomplete packet during processing of extra keyframe packets");
                                return(false);
                            }
                        }
                        else
                        {
                            // Failed.
                            Log.Error("Failed to process extra keyframe packets");
                            return(false);
                        }
                    }

                    // We are now up to where we should be. Send reset and migrate the loaded packets into the packet queue for
                    // processing.
                    ResetQueue(currentFrame);
                    _packetQueue.Enqueue(decodedPackets);
                    _currentFrame = currentFrame;

                    Log.Diag("Dropped {0} additional packets to catch up to frame {1}.", droppedPacketCount, _currentFrame);
                    Log.Info("Restored frame: {0} -> {1}", _currentFrame, _targetFrame);
                    if (_targetFrame == _currentFrame)
                    {
                        _targetFrame = 0;
                    }
                    return(true);
                }
            }
            catch (System.Exception e)
            {
                if (keyframeStream != null)
                {
                    // Explicitly close the stream to make sure we aren't hanging on to handles we shouldn't.
                    keyframeStream.Close();
                    keyframeStream = null;
                }
                Log.Exception(e);
            }

            Log.Error("Failed to decode keyframe for frame {0}", keyframe.FrameNumber);
            return(false);
        }
Esempio n. 3
0
        public void Run()
        {
            int                infoSearchPacketLimit = 10; // Don't look for info packets beyond this packet count. Should be first up.
            PacketBuffer       packetBuffer          = new PacketBuffer(4 * 1024);
            PacketStreamReader packetStream          = new PacketStreamReader(new FileStream(TargetFile, FileMode.Open, FileAccess.Read));

            Console.CancelKeyPress += new ConsoleCancelEventHandler(ControlCHandler);
            Console.WriteLine(string.Format("Reading {0}", TargetFile));

            PacketBuffer packet          = null;
            long         bytesRead       = 0;
            bool         foundFrameCount = false;
            bool         foundServerInfo = false;

            while (!Quit && !packetStream.EndOfStream)
            {
                Log.Flush();
                packet = packetStream.NextPacket(ref bytesRead);

                if (packet == null)
                {
                    if (!packetStream.EndOfStream)
                    {
                        Console.Error.WriteLine(string.Format("Null packet at {0}:{1}", _actualFrameCount, _packetCount));
                    }
                    continue;
                }

                if (packet.Status == PacketBufferStatus.Complete)
                {
                    ++_packetCount;
                    //Console.WriteLine("Msg: {0} {1}", completedPacket.Header.RoutingID, completedPacket.Header.MessageID);
                    switch (packet.Header.RoutingID)
                    {
                    case (ushort)RoutingID.Control:
                        switch (packet.Header.MessageID)
                        {
                        case (ushort)ControlMessageID.EndFrame:
                            ++_actualFrameCount;
                            break;

                        case (ushort)ControlMessageID.FrameCount:
                            if (foundFrameCount)
                            {
                                Console.Error.WriteLine(string.Format("Found additional FrameCount message at frame {0}:{1}",
                                                                      _actualFrameCount, _packetCount));
                            }
                            else
                            {
                                _frameCountPacketNumber = _packetCount;
                            }
                            HandleFrameCount(packet);
                            foundFrameCount = true;
                            break;
                        }
                        break;

                    case (ushort)RoutingID.ServerInfo:
                        if (foundServerInfo)
                        {
                            Console.Error.WriteLine(string.Format("Found additional ServerInfo message at frame {0}:{1}",
                                                                  _actualFrameCount, _packetCount));
                        }
                        else
                        {
                            _serverInfoPacket = _packetCount;
                        }
                        HandleServerInfo(packet);
                        foundServerInfo = true;
                        break;
                    }
                }
                else
                {
                    Console.Error.WriteLine(string.Format("Invalid packet static at {0}:{1}", _actualFrameCount, _packetCount));
                    Console.Error.WriteLine(string.Format("Invalid packet status is {0} ({1})", packet.Status.ToString(), (int)packet.Status));;

                    if (packet.Header.RoutingID == (ushort)RoutingID.CollatedPacket && packet.Header.PayloadSize > 0)
                    {
                        NetworkReader         packetReader = new NetworkReader(packet.CreateReadStream(true));
                        CollatedPacketMessage msg          = new CollatedPacketMessage();
                        if (msg.Read(packetReader))
                        {
                            Console.WriteLine(string.Format("Failed collated packet message:"));
                            Console.WriteLine(string.Format("  Flags: {0}", msg.Flags));
                            Console.WriteLine(string.Format("  Reserved: {0}", msg.Reserved));
                            Console.WriteLine(string.Format("  UncompressedBytes: {0}", msg.UncompressedBytes));
                            Console.WriteLine(string.Format("  PacketSize: {0}", packet.Header.PacketSize));
                        }
                    }
                }
            }

            if (!Decode)
            {
                if (foundServerInfo && foundFrameCount)
                {
                    Quit = true;
                }
            }

            if (_packetCount >= infoSearchPacketLimit)
            {
                if (!foundServerInfo)
                {
                    Quit = true;
                    Console.Error.WriteLine(string.Format("Failed to locate ServerInfo packet within {0} packets.", infoSearchPacketLimit));
                }
                if (!foundFrameCount)
                {
                    Quit = true;
                    Console.Error.WriteLine(string.Format("Failed to locate FrameCount packet within {0} packets.", infoSearchPacketLimit));
                }
            }

            Console.WriteLine(string.Format("Processed {0} packets{1}\n", _packetCount, Decode ? "" : " (info only)"));
            if (Decode)
            {
                if (_reportedFrameCount != _actualFrameCount)
                {
                    Console.Error.WriteLine(string.Format("Frame count mismatch. Expected {0}, processed {1}", _reportedFrameCount, _actualFrameCount));
                }
            }
        }
Esempio n. 4
0
        public void CollationTest(bool compress)
        {
            // Allocate encoder.
            CollatedPacketEncoder encoder = new CollatedPacketEncoder(compress);

            // Create a mesh object to generate some messages.
            List <Vector3> vertices = new List <Vector3>();
            List <Vector3> normals  = new List <Vector3>();
            List <int>     indices  = new List <int>();

            Common.MakeLowResSphere(vertices, indices, normals);

            MeshShape mesh = new MeshShape(Net.MeshDrawType.Triangles, vertices.ToArray(), indices.ToArray());

            mesh.ID      = 42;
            mesh.Normals = normals.ToArray();

            // Use the encoder as a connection.
            // The Create() call will pack the mesh create message and multiple data messages.
            int wroteBytes = encoder.Create(mesh);

            Assert.True(wroteBytes > 0);
            Assert.True(encoder.FinaliseEncoding());

            // Allocate a reader. Contains a CollatedPacketDecoder.
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(encoder.Buffer, 0, encoder.Count);
            PacketStreamReader     decoder      = new PacketStreamReader(memoryStream);

            PacketBuffer packet;
            MeshShape    readMesh       = new MeshShape();
            int          packetCount    = 0;
            long         processedBytes = 0;

            while ((packet = decoder.NextPacket(ref processedBytes)) != null)
            {
                NetworkReader reader = new NetworkReader(packet.CreateReadStream(true));
                ++packetCount;
                Assert.True(packet.ValidHeader);
                Assert.Equal(packet.Header.Marker, PacketHeader.PacketMarker);
                Assert.Equal(packet.Header.VersionMajor, PacketHeader.PacketVersionMajor);
                Assert.Equal(packet.Header.VersionMinor, PacketHeader.PacketVersionMinor);

                Assert.Equal(packet.Header.RoutingID, mesh.RoutingID);

                // Peek the shape ID.
                uint shapeId = packet.PeekUInt32(PacketHeader.Size);
                Assert.Equal(shapeId, mesh.ID);

                switch ((ObjectMessageID)packet.Header.MessageID)
                {
                case ObjectMessageID.Create:
                    Assert.True(readMesh.ReadCreate(packet, reader));
                    break;

                case ObjectMessageID.Update:
                    Assert.True(readMesh.ReadUpdate(packet, reader));
                    break;

                case ObjectMessageID.Data:
                    Assert.True(readMesh.ReadData(packet, reader));
                    break;
                }
            }

            Assert.True(packetCount > 0);
            // FIXME: Does not match, but results are fine. processedBytes is 10 greater than wroteBytes.
            //Assert.Equal(processedBytes, wroteBytes);

            // Validate what we've read back.
            ShapeTestFramework.ValidateShape(readMesh, mesh, new Dictionary <ulong, Resource>());
        }