Example #1
0
        public void SerializeCard_Test()
        {
            foreach (CardValue value in Enum.GetValues(typeof(CardValue)))
            {
                foreach (CardColor color in Enum.GetValues(typeof(CardColor)))
                {
                    var card = new Card(value: value, color: color);
                    Assert.AreEqual(value, card.Value);
                    Assert.AreEqual(color, card.Color);

                    var s = new HandSerializer();

                    var bytes = new byte[16];
                    int index = 3;

                    s.Serialize(card: card, outputData: bytes, index: ref index);

                    // Ensure nothing written before start index and after end index
                    Assert.AreEqual(0, bytes[0]);
                    Assert.AreEqual(0, bytes[1]);
                    Assert.AreEqual(0, bytes[2]);

                    for (var i = index; i < bytes.Length; i++)
                    {
                        Assert.AreEqual(0, bytes[i]);
                    }

                    index = 3;
                    var deserializedCard = s.DeserializeCard(inputData: bytes, index: ref index);
                    Assert.IsNotNull(deserializedCard);
                    Assert.AreEqual(card.Value, deserializedCard.Value);
                    Assert.AreEqual(card.Color, deserializedCard.Color);
                }
            }
        }
Example #2
0
        protected virtual void recoverObjectData()
        {
            // Instantiating the Container and the lists that come with it
            if (!recovered)
            {
                recovered                   = true;
                recoveredContainer          = HandSerializer.ReadFromBinaryFile <FrameContainer>(@"C:\Users\Brian\Desktop\test.bin");
                recoveredFrames             = recoveredContainer.playbackFrames;
                recoveredOffsets            = recoveredContainer.playbackOffsets;
                recoveredTimeStamps         = recoveredContainer.playbackTimestamps;
                recoveredInterpolationTimes = recoveredContainer.playbackInterpolationTimes;

                Debug.Log("hey!");
            }

            // Setting the values each time... Hopefully this works?
            if (whichFrame < 300)
            {
                Debug.Log("Playing back frame " + whichFrame);
                whichFrame++;
            }

            if (whichFrame == 300)
            {
                Debug.Log("Finished Playback!");
            }
        }
Example #3
0
        // NOTES: Need to serialize Physics data after Picture data for this to work for some strange reason.
        // Also, I think it records a little less than you would think it does (from the debugging console).

        protected virtual void CaptureObjectValues(Frame captureFrame, long captureOffset, long captureTimeStamp, long captureInterpolationTime)
        {
            if (!allCaptured)
            {
                if (amountCaptured > 300)
                {
                    allCaptured = true;
                    Debug.Log("All Object data captured!");

                    // Setting the properties of the class then serializing the container
                    testContainer.playbackFrames             = capturedFrames;
                    testContainer.playbackOffsets            = capturedOffsets;
                    testContainer.playbackTimestamps         = capturedTimeStamps;
                    testContainer.playbackInterpolationTimes = capturedInterpolationTimes;
                    HandSerializer.WriteToBinaryFile(@"C:\Users\Brian\Desktop\test.bin", testContainer);
                }

                else
                {
                    //// Copying the Frame object
                    Frame clonedFrame = ObjectCopier.Clone(captureFrame);

                    // Adding to the individual lists
                    capturedFrames.Add(clonedFrame);
                    capturedOffsets.Add(captureOffset);
                    capturedTimeStamps.Add(captureTimeStamp);
                    capturedInterpolationTimes.Add(captureInterpolationTime);

                    // Incrementing the amountCaptured value and debugging purposes
                    amountCaptured++;
                    Debug.Log("Capturing in progress: " + amountCaptured);
                }
            }
        }
Example #4
0
        public void Serializehand_Test()
        {
            foreach (CardValue value1 in Enum.GetValues(typeof(CardValue)))
            {
                foreach (CardValue value2 in Enum.GetValues(typeof(CardValue)))
                {
                    foreach (CardColor color1 in Enum.GetValues(typeof(CardColor)))
                    {
                        foreach (CardColor color2 in Enum.GetValues(typeof(CardColor)))
                        {
                            var card1 = new Card(value: value1, color: color1);
                            var card2 = new Card(value: value2, color: color2);
                            if (card1.Value == card2.Value && card1.Color == card2.Color)
                            {
                                continue; // Hand cannot consist of 2 identical cards
                            }
                            var hand = new Hand(card1: card1, card2: card2);

                            var s = new HandSerializer();

                            var bytes = new byte[16];
                            int index = 3;

                            s.Serialize(hand: hand, outputData: bytes, index: ref index);

                            // Ensure nothing written before start index and after end index
                            Assert.AreEqual(0, bytes[0]);
                            Assert.AreEqual(0, bytes[1]);
                            Assert.AreEqual(0, bytes[2]);

                            for (var i = index; i < bytes.Length; i++)
                            {
                                Assert.AreEqual(0, bytes[i]);
                            }

                            index = 3;
                            var deserializedHand = s.DeserializeHand(inputData: bytes, index: ref index);
                            Assert.IsNotNull(deserializedHand);

                            Assert.AreEqual(card1.Value, deserializedHand.Card1.Value);
                            Assert.AreEqual(card1.Color, deserializedHand.Card1.Color);
                            Assert.AreEqual(card2.Value, deserializedHand.Card2.Value);
                            Assert.AreEqual(card2.Color, deserializedHand.Card2.Color);
                        }
                    }
                }
            }
        }
        private static void DoRoundtrip()
        {
            var client = new TcpClient();

            Console.WriteLine("Connecting...");
            client.Connect("localhost", 7676);
            Console.WriteLine("Connected. awaiting hand...");
            using (var r = new BinaryReader(client.GetStream()))
            {
                var handBytes  = r.ReadBytes(count: 4);
                var serializer = new HandSerializer();
                var index      = 0;
                var hand       = serializer.DeserializeHand(inputData: handBytes, index: ref index);
                Console.WriteLine("Received hand: " + hand);
            }
            Console.WriteLine("Done");
        }
        static void Main(string[] args)
        {
            var deck = new Deck();
            var _handsByPlayerMac = new Dictionary <string, Hand>();
            // Parse("192.168.1.118")
            var listener = new TcpListener(localaddr: IPAddress.Parse("192.168.1.118"), port: 7676);
            var random   = new Random();

            while (true)
            {
                listener.Start();
                Console.WriteLine("Awaiting client to connect...");
                var client = listener.AcceptTcpClient();
                client.NoDelay = true;

                // Determine mac
                IPEndPoint remoteIpEndPoint = client.Client.RemoteEndPoint as IPEndPoint;
                var        mac       = GetMacAddress(remoteIpEndPoint.Address);
                var        macString = string.Join(",", mac);

                // Only draw a new hand for the client if he didnt get one before
                if (false == _handsByPlayerMac.ContainsKey(macString))
                {
                    _handsByPlayerMac.Add(macString, deck.DrawHand());
                }

                // Use map to resolve
                var data       = new byte[4];
                var serializer = new HandSerializer();
                int index      = 0;
                var hand       = _handsByPlayerMac[macString];
                serializer.Serialize(hand: hand, outputData: data, index: ref index);

                Console.WriteLine("Sending hand: " + hand + " to client " + client.Client.RemoteEndPoint + ", mac: " + macString);

                client.Client.Send(data);

                Console.WriteLine("Closing connection again...");
                client.Close();
            }
        }
Example #7
0
        protected virtual void FixedUpdate()
        {
            if (_frameOptimization == FrameOptimizationMode.ReuseUpdateForPhysics)
            {
                DispatchFixedFrameEvent(_transformedUpdateFrame);
                return;
            }

            if (_useInterpolation)
            {
                long timestamp;
                switch (_frameOptimization)
                {
                case FrameOptimizationMode.None: // I think that this is the one that actually matters. (aka is getting used)
                    // By default we use Time.fixedTime to ensure that our hands are on the same
                    // timeline as Update.  We add an extrapolation value to help compensate
                    // for latency.
                    float extrapolatedTime = Time.fixedTime + CalculatePhysicsExtrapolation();
                    timestamp = (long)(extrapolatedTime * S_TO_NS) + _unityToLeapOffset;
                    break;

                case FrameOptimizationMode.ReusePhysicsForUpdate:
                    // If we are re-using physics frames for update, we don't even want to care
                    // about Time.fixedTime, just grab the most recent interpolated timestamp
                    // like we are in Update.
                    timestamp = CalculateInterpolationTime() + (ExtrapolationAmount * 1000);
                    break;

                default:
                    throw new System.InvalidOperationException(
                              "Unexpected frame optimization mode: " + _frameOptimization);
                }
                if (capturing)
                {
                    _leapController.GetInterpolatedFrame(_untransformedFixedFrame, timestamp); //BRIAN CHECK THIS OUT

                    if (amountCaptured < 300)
                    {
                        capturedObjectStamps.Add(timestamp);
                        Frame clonedFrame = ObjectCopier.Clone(_untransformedFixedFrame);
                        capturedObjects.Add(clonedFrame);
                    }
                    else
                    {
                        if (allCaptured && !allObjectCaptured)
                        {
                            objectContainer.playbackFrames     = capturedObjects;
                            objectContainer.playbackTimestamps = capturedObjectStamps;
                            HandSerializer.WriteToBinaryFile(@"C:\Users\Brian\Desktop\move.bin", testContainer);
                            Debug.Log("All Physics Data Captured!");
                            allObjectCaptured = true;
                        }
                    }
                }
                else
                {
                    if (!objectRecovered)
                    {
                        objectRecovered          = true;
                        recoveredObjectContainer = HandSerializer.ReadFromBinaryFile <FrameContainer>(@"C:\Users\Brian\Desktop\move.bin");
                        recoveredObjectFrames    = recoveredObjectContainer.playbackFrames;
                        recoveredObjectStamps    = recoveredObjectContainer.playbackTimestamps;
                    }
                    else
                    {
                        _untransformedFixedFrame = recoveredObjectFrames[whichFrame];
                        timestamp = recoveredObjectStamps[whichFrame];

                        _leapController.GetInterpolatedFrame(_untransformedFixedFrame, timestamp);
                    }
                }
            }
            else
            {
                _leapController.Frame(_untransformedFixedFrame);
            }

            if (_untransformedFixedFrame != null)
            {
                transformFrame(_untransformedFixedFrame, _transformedFixedFrame);

                DispatchFixedFrameEvent(_transformedFixedFrame);
            }
        }