protected override JobHandle OnUpdate(JobHandle inputDep)
    {
        if (!m_DriverSystem.ClientDriver.IsCreated)
        {
            return(inputDep);
        }
        PingClientUIBehaviour.UpdateStats(m_pingStats[0], m_pingStats[1]);
        if (PingClientUIBehaviour.ServerEndPoint.IsValid && m_ConnectionGroup.IsEmptyIgnoreFilter)
        {
            var conJob = new ConnectJob
            {
                driver        = m_DriverSystem.ClientDriver,
                serverEP      = PingClientUIBehaviour.ServerEndPoint,
                commandBuffer = m_Barrier.CreateCommandBuffer()
            };
            inputDep = conJob.Schedule(inputDep);
            m_Barrier.AddJobHandleForProducer(inputDep);
            return(inputDep);
        }
        var pingJob = new PingJob
        {
            driver        = m_DriverSystem.ClientDriver,
            serverEP      = PingClientUIBehaviour.ServerEndPoint,
            pendingPings  = m_pendingPings,
            pingStats     = m_pingStats,
            fixedTime     = Time.fixedTime,
            commandBuffer = m_Barrier.CreateCommandBuffer()
        };
        var handle = pingJob.ScheduleSingle(this, inputDep);

        m_Barrier.AddJobHandleForProducer(handle);
        return(handle);
    }
    void FixedUpdate()
    {
        // Update the ping client UI with the ping statistics computed by teh job scheduled previous frame since that
        // is now guaranteed to have completed
        PingClientUIBehaviour.UpdateStats(m_numPingsSent, m_lastPingTime);

        // Update the NetworkDriver. It schedules a job so we must wait for that job with Complete
        m_ClientDriver.ScheduleUpdate().Complete();

        // If the client ui indicates we should be sending pings but we do not have an active connection we create one
        if (PingClientUIBehaviour.ServerEndPoint.IsValid && !m_clientToServerConnection.IsCreated)
        {
            m_clientToServerConnection = m_ClientDriver.Connect(PingClientUIBehaviour.ServerEndPoint);
        }
        // If the client ui indicates we should not be sending pings but we do have a connection we close that connection
        if (!PingClientUIBehaviour.ServerEndPoint.IsValid && m_clientToServerConnection.IsCreated)
        {
            m_clientToServerConnection.Disconnect(m_ClientDriver);
            m_clientToServerConnection = default(NetworkConnection);
        }

        DataStreamReader strm;

        NetworkEvent.Type cmd;
        // Process all events on the connection. If the connection is invalid it will return Empty immediately
        while ((cmd = m_clientToServerConnection.PopEvent(m_ClientDriver, out strm)) != NetworkEvent.Type.Empty)
        {
            if (cmd == NetworkEvent.Type.Connect)
            {
                // When we get the connect message we can start sending data to the server
                // Set the ping id to a sequence number for the new ping we are about to send
                m_pendingPing = new PendingPing {
                    id = m_numPingsSent, time = Time.fixedTime
                };
                // Create a 4 byte data stream which we can store our ping sequence number in
                if (m_ClientDriver.BeginSend(m_clientToServerConnection, out var pingData) == 0)
                {
                    pingData.WriteInt(m_numPingsSent);
                    m_ClientDriver.EndSend(pingData);
                }
                // Update the number of sent pings
                ++m_numPingsSent;
            }
            else if (cmd == NetworkEvent.Type.Data)
            {
                // When the pong message is received we calculate the ping time and disconnect
                m_lastPingTime = (int)((Time.fixedTime - m_pendingPing.time) * 1000);
                m_clientToServerConnection.Disconnect(m_ClientDriver);
                m_clientToServerConnection = default(NetworkConnection);
            }
            else if (cmd == NetworkEvent.Type.Disconnect)
            {
                // If the server disconnected us we clear out connection
                m_clientToServerConnection = default(NetworkConnection);
            }
        }
    }
Example #3
0
 protected override JobHandle OnUpdate(JobHandle inputDep)
 {
     if (!m_DriverSystem.ClientDriver.IsCreated)
     {
         return(inputDep);
     }
     PingClientUIBehaviour.UpdateStats(m_pingStats[0], m_pingStats[1]);
     if (PingClientUIBehaviour.ServerEndPoint.IsValid && m_ConnectionGroup.IsEmptyIgnoreFilter)
     {
         inputDep.Complete();
         var ent = EntityManager.CreateEntity();
         EntityManager.AddComponentData(ent, new PingClientConnectionComponentData {
             connection = m_DriverSystem.ClientDriver.Connect(PingClientUIBehaviour.ServerEndPoint)
         });
         return(default);
Example #4
0
    protected override JobHandle OnUpdate(JobHandle inputDep)
    {
        if (!m_DriverSystem.ClientDriver.IsCreated)
        {
            return(inputDep);
        }
        PingClientUIBehaviour.UpdateStats(m_pingStats[0], m_pingStats[1]);
        var pingJob = new PingJob
        {
            driver             = m_DriverSystem.ClientDriver,
            connections        = connectionList.connections,
            connectionEntities = connectionList.entities,
            serverEP           = PingClientUIBehaviour.ServerEndPoint,
            pendingPings       = m_pendingPings,
            pingStats          = m_pingStats,
            fixedTime          = Time.fixedTime,
            commandBuffer      = m_Barrier.CreateCommandBuffer()
        };

        return(pingJob.Schedule(inputDep));
    }
    void FixedUpdate()
    {
        // Wait for the previous frames ping to complete before starting a new one, the Complete in LateUpdate is not
        // enough since we can get multiple FixedUpdate per frame on slow clients
        m_updateHandle.Complete();

        // Update the ping client UI with the ping statistics computed by teh job scheduled previous frame since that
        // is now guaranteed to have completed
        PingClientUIBehaviour.UpdateStats(m_pingStats[0], m_pingStats[1]);
        var pingJob = new PingJob
        {
            driver       = m_ClientDriver,
            connection   = m_clientToServerConnection,
            serverEP     = PingClientUIBehaviour.ServerEndPoint,
            pendingPings = m_pendingPings,
            pingStats    = m_pingStats,
            fixedTime    = Time.fixedTime
        };

        // Schedule a chain with the driver update followed by the ping job
        m_updateHandle = m_ClientDriver.ScheduleUpdate();
        m_updateHandle = pingJob.Schedule(m_updateHandle);
    }
Example #6
0
    void FixedUpdate()
    {
        // Wait for the previous frames ping to complete before starting a new one, the Complete in LateUpdate is not
        // enough since we can get multiple FixedUpdate per frame on slow clients
        m_updateHandle.Complete();

        var serverEP = PingClientUIBehaviour.ServerEndPoint;

        // If the client ui indicates we should be sending pings but we do not have an active connection we create one
        if (serverEP.IsValid && !m_clientToServerConnection[0].IsCreated)
        {
            m_clientToServerConnection[0] = m_ClientDriver.Connect(serverEP);
        }
        // If the client ui indicates we should not be sending pings but we do have a connection we close that connection
        if (!serverEP.IsValid && m_clientToServerConnection[0].IsCreated)
        {
            m_clientToServerConnection[0].Disconnect(m_ClientDriver);
            m_clientToServerConnection[0] = default(NetworkConnection);
        }

        // Update the ping client UI with the ping statistics computed by teh job scheduled previous frame since that
        // is now guaranteed to have completed
        PingClientUIBehaviour.UpdateStats(m_pingStats[0], m_pingStats[1]);
        var pingJob = new PingJob
        {
            driver       = m_ClientDriver,
            connection   = m_clientToServerConnection,
            pendingPings = m_pendingPings,
            pingStats    = m_pingStats,
            fixedTime    = Time.fixedTime
        };

        // Schedule a chain with the driver update followed by the ping job
        m_updateHandle = m_ClientDriver.ScheduleUpdate();
        m_updateHandle = pingJob.Schedule(m_updateHandle);
    }
Example #7
0
    protected override void OnUpdate()
    {
        if (!m_DriverSystem.ClientDriver.IsCreated)
        {
            return;
        }
        PingClientUIBehaviour.UpdateStats(m_PingStats[0], m_PingStats[1]);
        if (PingClientUIBehaviour.ServerEndPoint.IsValid && m_ConnectionGroup.IsEmptyIgnoreFilter)
        {
            Dependency.Complete();
            var ent = EntityManager.CreateEntity();
            EntityManager.AddComponentData(ent, new PingClientConnectionComponentData {
                connection = m_DriverSystem.ClientDriver.Connect(PingClientUIBehaviour.ServerEndPoint)
            });
            return;
        }

        var driver        = m_DriverSystem.ClientDriver;
        var serverEP      = PingClientUIBehaviour.ServerEndPoint;
        var pendingPings  = m_PendingPings;
        var pingStats     = m_PingStats;
        var frameTime     = Time.ElapsedTime;
        var commandBuffer = m_Barrier.CreateCommandBuffer();

        Entities.ForEach((Entity entity, ref PingClientConnectionComponentData connection) =>
        {
            if (!serverEP.IsValid)
            {
                connection.connection.Disconnect(driver);
                commandBuffer.DestroyEntity(entity);
                return;
            }

            DataStreamReader strm;
            NetworkEvent.Type cmd;
            while ((cmd = connection.connection.PopEvent(driver, out strm)) != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Connect)
                {
                    pendingPings[0] = new PendingPing {
                        id = pingStats[0], time = frameTime
                    };

                    if (driver.BeginSend(connection.connection, out var pingData) == 0)
                    {
                        pingData.WriteInt(pingStats[0]);
                        driver.EndSend(pingData);
                    }
                    pingStats[0] = pingStats[0] + 1;
                }
                else if (cmd == NetworkEvent.Type.Data)
                {
                    pingStats[1] = (int)((frameTime - pendingPings[0].time) * 1000);
                    connection.connection.Disconnect(driver);
                    commandBuffer.DestroyEntity(entity);
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    commandBuffer.DestroyEntity(entity);
                }
            }
        }).Schedule();
        m_Barrier.AddJobHandleForProducer(Dependency);
        m_ServerConnectionGroup.AddDependency(Dependency);
    }