Exemple #1
0
        public bool TranslatePacket(BasePacketHeader packet)
        {
            bool toReturn = false;

            if (packet != null)
            {
                switch (packet.messageHeader.MessageType)
                {
                case MessageHeaderMessage.Types.MessageType.RigidBodyUpdate:
                    FrameSnapshot snapshot = FindOrCreateSnapshotForFrame(packet.messageHeader.FrameId);

                    ProcessRigidBodyFrameUpdate(packet, snapshot);
                    toReturn = true;
                    break;

                case MessageHeaderMessage.Types.MessageType.ShapeCreated:
                    ProcessShapeAdded(packet);
                    toReturn = true;
                    break;

                case MessageHeaderMessage.Types.MessageType.FrameStats:
                    ProcessFrameStats(packet);


                    toReturn = true;
                    break;

                default:
                    break;
                }
            }

            return(toReturn);
        }
Exemple #2
0
        public BasePacketHeader ReceiveData()
        {
            BasePacketHeader basePacket = null;

            if (clientSocket != null && clientSocket.Connected && clientSocket.Available > 0)
            {
                basePacket = new BasePacketHeader();

                int numberOfReadBytes = clientSocket.Receive(basePacket.PacketBytes);


                if (numberOfReadBytes > 0)
                {
                    Console.WriteLine($"bytes, {basePacket.PacketBytes[0]}, {basePacket.PacketBytes[1]}, {basePacket.PacketBytes[2]}, {basePacket.PacketBytes[3]}, {basePacket.PacketBytes[4]}, {basePacket.PacketBytes[5]}, {basePacket.PacketBytes[6]}, {basePacket.PacketBytes[7]}");

                    int byteIndex  = 0;
                    int headerSize = BitConverter.ToInt32(basePacket.PacketBytes, byteIndex); byteIndex += 4;

                    basePacket.messageHeader     = MessageHeaderMessage.Parser.ParseFrom(basePacket.PacketBytes, byteIndex, headerSize);
                    basePacket.startOfPacketData = headerSize + 4;
                }
            }

            return(basePacket);
        }
Exemple #3
0
        private void ProcessRigidBodyFrameUpdate(BasePacketHeader packet, FrameSnapshot snapshot)
        {
            RigidBodyListPacket rigidBodyList = RigidBodyListPacket.Parser.ParseFrom(packet.PacketBytes, packet.startOfPacketData, packet.messageHeader.DataSize);

            foreach (RigidBodyPacket packetBody in rigidBodyList.RigidBodies)
            {
                // todo: error handle - what if the rigid body already exists?
                snapshot.AddRigidBody(packetBody);
            }
        }
Exemple #4
0
        private void ProcessFrameStats(BasePacketHeader packet)
        {
            FrameStatsMessage frameStatsMessage = FrameStatsMessage.Parser.ParseFrom(packet.PacketBytes, packet.startOfPacketData, packet.messageHeader.DataSize);

            FrameStats frameStats = new FrameStats();

            frameStats.ImportFromPacket(frameStatsMessage);

            ConstructedFrameStats.Add(packet.messageHeader.FrameId, frameStats);
        }
Exemple #5
0
        private static void recv(object inputPayload)
        {
            TelemetryReceiver payload = (TelemetryReceiver)inputPayload;

            // while we're connected and no request has been made to stop processing
            while (payload.DataStream.Connected && !payload.StopThreadSwitch)
            {
                Telemetry.Network.BasePacketHeader basePacket = payload.DataStream.ReceiveData();

                if (basePacket != null)
                {
                    if (payload.Translator.TranslatePacket(basePacket))
                    {
                        // todo: error - don't pull out packets that aren't complete
                        foreach (Tuple <bool, FrameSnapshot> snapshot in payload.Translator.ConstructedSnaphots.Values)
                        {
                            lock (payload.LockObject)
                            {
                                payload.ReceivedFrameSnapshots.Enqueue(snapshot.Item2);
                            }
                        }

                        foreach (FrameStats stats in payload.Translator.ConstructedFrameStats.Values)
                        {
                            lock (payload.LockObject)
                            {
                                payload.ReceivedFrameStats.Enqueue(stats);
                            }
                        }

                        foreach (PacketTranslator.CollectedFrameShapes frameShapeList in payload.Translator.AddedShapes.Values)
                        {
                            lock (payload.LockObject)
                            {
                                payload.ReceivedShapes.Enqueue(frameShapeList);
                            }
                        }

                        payload.Translator.ConstructedSnaphots.Clear();
                        payload.Translator.AddedShapes.Clear();
                        payload.Translator.ConstructedFrameStats.Clear();
                    }
                    else
                    {
                        Console.WriteLine($"Error: read unknown packet type: {basePacket.PacketBytes}");
                    }
                }
                else
                {
                    Console.WriteLine("No data to receive");
                    // todo - this number should be driven by the frame update time sent over or by a config option
                    Thread.Sleep(5); // we're running faster/as fast as the producer of the data - take a brief break
                }
            }
        }
Exemple #6
0
        private void ProcessShapeAdded(BasePacketHeader packet)
        {
            ShapeCreatedMessage shapeCreatedPacket = ShapeCreatedMessage.Parser.ParseFrom(packet.PacketBytes, packet.startOfPacketData, packet.messageHeader.DataSize);

            switch (shapeCreatedPacket.ShapeType)
            {
            case ShapeTypePacket.Obb:
            {
                ObbShapePacket createdObbPacket = ObbShapePacket.Parser.ParseFrom(packet.PacketBytes, packet.startOfPacketData + shapeCreatedPacket.CalculateSize(), shapeCreatedPacket.ShapeSize);

                ObbShape createdObb = new ObbShape();
                createdObb.ImportFromPacket(createdObbPacket);

                RegisterNewlyReceivedShape(packet, createdObb);
            }
            break;

            case ShapeTypePacket.Sphere:
                break;

            case ShapeTypePacket.Cone:
                break;

            case ShapeTypePacket.ConvexHull:
            {
                ConvexHullShapePacket createdConvexHullPacket = ConvexHullShapePacket.Parser.ParseFrom(packet.PacketBytes, packet.startOfPacketData + shapeCreatedPacket.CalculateSize(), shapeCreatedPacket.ShapeSize);

                ConvexHullShape createdConvexHull = new ConvexHullShape();
                createdConvexHull.ImportFromPacket(createdConvexHullPacket);

                RegisterNewlyReceivedShape(packet, createdConvexHull);
            }
            break;

            case ShapeTypePacket.Tetrahedron:
            {
                TetrahedronShapePacket createdTetrahedronPacket = TetrahedronShapePacket.Parser.ParseFrom(packet.PacketBytes, packet.startOfPacketData + shapeCreatedPacket.CalculateSize(), shapeCreatedPacket.ShapeSize);

                TetrahedronShape createdTetrahedron = new TetrahedronShape();
                createdTetrahedron.ImportFromPacket(createdTetrahedronPacket);

                RegisterNewlyReceivedShape(packet, createdTetrahedron);
            }
            break;

            default:
                break;
            }
        }
Exemple #7
0
        private void RegisterNewlyReceivedShape(BasePacketHeader packet, BaseShape createdShape)
        {
            CollectedFrameShapes collectedFrameShapes = null;

            if (AddedShapes.TryGetValue(packet.messageHeader.FrameId, out collectedFrameShapes))
            {
                collectedFrameShapes.Shapes.Add(createdShape);
            }
            else
            {
                collectedFrameShapes         = new CollectedFrameShapes();
                collectedFrameShapes.FrameId = packet.messageHeader.FrameId;
                collectedFrameShapes.Shapes  = new List <BaseShape>();

                collectedFrameShapes.Shapes.Add(createdShape);
                AddedShapes.Add(collectedFrameShapes.FrameId, collectedFrameShapes);
            }
        }