//Update just handles checking the session state of all current connections, and if anyone has timed out/disconnected
    //remove them from the connection list and do a bit of cleaup
    public void FixedUpdate()
    {
        _networkSimulationTimer -= Time.fixedDeltaTime;
        foreach (var kvp in connections)
        {
            kvp.Value.timeSinceLastMsg += Time.fixedDeltaTime;
        }
        //network loop
        if (_networkSimulationTimer <= 0f)
        {
            _networkSimulationTimer = networkSimulationTimer;
            List <ulong> disconnects = new List <ulong>();

            foreach (var kvp in connections)
            {
                SteamConnection c = kvp.Value;

                if (me.HasAuthOver(c))                        //only send keepalives if you're the responsible one in this relationship
                {
                    if (c.timeSinceLastMsg >= keepAliveTimer) //15 seconds?
                    {
                        c.Ping();
                    }
                }



                Facepunch.Steamworks.Client.Instance.Networking.GetSessionState(c.steamID, out c.connectionState);

                if (c.connectionState.ConnectionActive == 0 && c.connectionState.Connecting == 0)
                {
                    disconnects.Add(c.steamID);
                }
            }

            for (int i = 0; i < disconnects.Count; i++)
            {
                Disconnect(disconnects[i]);
            }

            NetworkSend();
        }
    }
Beispiel #2
0
    //Update just handles checking the session state of all current connections, and if anyone has timed out/disconnected
    //remove them from the connection list and do a bit of cleaup
    public void FixedUpdate()
    {
        _networkSimulationTimer -= Time.fixedDeltaTime;
        foreach (var kvp in connections)
        {
            kvp.Value.timeSinceLastMsg += Time.fixedDeltaTime;
        }

        _sendConnectionMetadataTimer -= Time.deltaTime;
        if (_sendConnectionMetadataTimer <= 0f)
        {
            _sendConnectionMetadataTimer = SendConnectionMetadataTimer;
            Core.net.me.BroadcastMetadata();
        }

        //network loop
        if (_networkSimulationTimer <= 0f)
        {
            _networkSimulationTimer = networkSimulationTimer;
            List <ulong> disconnects = new List <ulong>();

            foreach (var kvp in connections)
            {
                SteamConnection c = kvp.Value;

                if (me.HasAuthOver(c))                        //only send keepalives if you're the responsible one in this relationship
                {
                    if (c.timeSinceLastMsg >= keepAliveTimer) //15 seconds?
                    {
                        c.Ping();
                    }
                }



                Facepunch.Steamworks.Client.Instance.Networking.GetSessionState(c.steamID, out c.connectionState);

                if (c.connectionState.ConnectionActive == 0 && c.connectionState.Connecting == 0)
                {
                    disconnects.Add(c.steamID);
                }
            }

            for (int i = 0; i < disconnects.Count; i++)
            {
                Disconnect(disconnects[i]);
            }

            NetworkSend();
        }

        //measuring bits in and bits out
        int bIn = 0;

        for (int i = 0; i < bitsInBuffer.Count; i++)
        {
            bitsInBuffer[i].timeInBuffer += Time.deltaTime;
            if (bitsInBuffer[i].timeInBuffer >= bandwidthBuffer)
            {
                bitsInBuffer.RemoveAt(i);
                i--;
            }
            else
            {
                bIn += bitsInBuffer[i].bits;
            }
        }

        int bOut = 0;

        for (int i = 0; i < bitsOutBuffer.Count; i++)
        {
            bitsOutBuffer[i].timeInBuffer += Time.deltaTime;
            if (bitsOutBuffer[i].timeInBuffer >= bandwidthBuffer)
            {
                bitsOutBuffer.RemoveAt(i);
                i--;
            }
            else
            {
                bOut += bitsOutBuffer[i].bits;
            }
        }

        bytesInPerSecond  = (int)((float)bIn / (8f * bandwidthBuffer));
        bytesOutPerSecond = (int)((float)bOut / (8f * bandwidthBuffer));

        if (Input.GetKeyDown(KeyCode.L))
        {
            Core.net.LoadScene(2);
        }
        if (Input.GetKeyDown(KeyCode.K))
        {
            Core.net.LoadScene(3);
        }
    }