Beispiel #1
0
        public void TestVar()
        {
            var ema = new ExponentialMovingAverage(10);

            ema.Add(5);
            ema.Add(6);
            ema.Add(7);

            Assert.That(ema.Var, Is.EqualTo(0.6134).Within(0.0001f));
        }
Beispiel #2
0
        public void TestMovingAverage()
        {
            var ema = new ExponentialMovingAverage(10);

            ema.Add(5);
            ema.Add(6);

            Assert.That(ema.Value, Is.EqualTo(5.1818).Within(0.0001f));
            Assert.That(ema.Var, Is.EqualTo(0.1487).Within(0.0001f));
        }
Beispiel #3
0
        // Executed at the client when we receive a Pong message
        // find out how long it took since we sent the Ping
        // and update time offset
        internal static void OnClientPong(NetworkPongMessage msg)
        {
            double now = LocalTime();

            // how long did this message take to come back
            double newRtt = now - msg.clientTime;

            _rtt.Add(newRtt);

            // the difference in time between the client and the server
            // but subtract half of the rtt to compensate for latency
            // half of rtt is the best approximation we have
            double newOffset = now - newRtt * 0.5f - msg.serverTime;

            double newOffsetMin = now - newRtt - msg.serverTime;
            double newOffsetMax = now - msg.serverTime;

            offsetMin = Math.Max(offsetMin, newOffsetMin);
            offsetMax = Math.Min(offsetMax, newOffsetMax);

            if (_offset.Value < offsetMin || _offset.Value > offsetMax)
            {
                // the old offset was offrange,  throw it away and use new one
                _offset = new ExponentialMovingAverage(PingWindowSize);
                _offset.Add(newOffset);
            }
            else if (newOffset >= offsetMin || newOffset <= offsetMax)
            {
                // new offset looks reasonable,  add to the average
                _offset.Add(newOffset);
            }
        }
Beispiel #4
0
        public void TestInitial()
        {
            var ema = new ExponentialMovingAverage(10);

            ema.Add(3);

            Assert.That(ema.Value, Is.EqualTo(3));
            Assert.That(ema.Var, Is.EqualTo(0));
        }
Beispiel #5
0
        // Executed at the client when we receive a Pong message
        // find out how long it took since we sent the Ping
        // and update time offset
        internal static void OnClientPong(NetworkMessage netMsg)
        {
            NetworkPongMessage pongMsg = netMsg.ReadMessage <NetworkPongMessage>();

            // how long did this message take to come back
            double rtt = LocalTime() - pongMsg.clientTime;
            // the difference in time between the client and the server
            // but subtract half of the rtt to compensate for latency
            // half of rtt is the best approximation we have
            double offset = LocalTime() - rtt * 0.5f - pongMsg.serverTime;

            _rtt.Add(rtt);
            _offset.Add(offset);
        }
        /**
         * Check for internal networked messages like ping or disconnect request
         */
        private void handleInternalMessages()
        {
            if (lastInternalMessageFrame == Time.frameCount)
            {
                return; //already processed this frame
            }
            lastInternalMessageFrame = Time.frameCount;

            int            connectionId;
            TransportEvent transportEvent;

            Byte[] data;

            while (ReceiveInternal(out connectionId, out data))
            {
                try
                {
                    SteamClient steamClient = steamConnectionMap.fromConnectionID[connectionId];

                    if (steamClient.state == SteamClient.ConnectionState.CONNECTED)
                    {
                        float senderTime;

                        NetworkReader reader = new NetworkReader(data);
                        switch (reader.ReadByte())
                        {
                        case (byte)InternalMessages.PING:
                            //ping .. send a pong
                            senderTime = reader.ReadSingle();
                            sendInternalPong(steamClient, senderTime);
                            break;

                        case (byte)InternalMessages.PONG:
                            //pong .. update when we last received a pong
                            senderTime           = reader.ReadSingle();
                            steamClient.lastPong = Time.time;

                            double rtt = LocalTime() - senderTime;
                            _rtt.Add(rtt);
                            break;

                        case (byte)InternalMessages.DISCONNECT:
                            if (LogFilter.Debug)
                            {
                                Debug.LogWarning("Received an instruction to Disconnect");
                            }
                            //requested to disconnect
                            if (mode == Mode.CLIENT || mode == Mode.SERVER)
                            {
                                //disconnect this client
                                closeSteamConnection(steamClient);
                            }
                            break;
                        }
                    }
                }
                catch (KeyNotFoundException)
                {
                    //shouldnt happen - ignore
                }
            }

            //check all connections to check they are healthy
            foreach (var connection in steamConnectionMap)
            {
                SteamClient steamClient = connection.Value;

                if (steamClient.state == SteamClient.ConnectionState.CONNECTED)
                {
                    if (Time.time - steamClient.lastPong > PONG_TIMEOUT)
                    {
                        //idle too long - disconnect
                        Debug.LogError("Connection " + steamClient.connectionID + " timed out - going to disconnect");
                        internalDisconnect(steamClient);
                        continue;
                    }
                    if (Time.time - steamClient.lastPing > PING_FREQUENCY)
                    {
                        //time to ping
                        sendInternalPing(steamClient);
                    }
                }
            }
        }