protected override JobHandle OnUpdate(JobHandle inputDep)
    {
        var commandBuffer = m_Barrier.CreateCommandBuffer();

        // Destroy drivers if the PingDriverComponents were removed
        if (!m_DestroyedDriverGroup.IsEmptyIgnoreFilter)
        {
            inputDep.Complete();
            var destroyedDriverEntity = m_DestroyedDriverGroup.ToEntityArray(Allocator.TempJob);
            var destroyedDriverList   = m_DestroyedDriverGroup.ToComponentDataArray <PingDriverComponentData>(Allocator.TempJob);
            for (int i = 0; i < destroyedDriverList.Length; ++i)
            {
                if (destroyedDriverList[i].isServer != 0)
                {
                    var serverConnectionList = m_ServerConnectionGroup.ToEntityArray(Allocator.TempJob);
                    // Also destroy all active connections when the driver dies
                    for (int con = 0; con < serverConnectionList.Length; ++con)
                    {
                        commandBuffer.DestroyEntity(serverConnectionList[con]);
                    }
                    serverConnectionList.Dispose();
                    ServerDriver.Dispose();
                }
                else
                {
                    ClientDriver.Dispose();
                }

                commandBuffer.RemoveComponent <PingDriverStateComponent>(destroyedDriverEntity[i]);
            }

            destroyedDriverList.Dispose();
            destroyedDriverEntity.Dispose();
        }

        // Create drivers if new PingDriverComponents were added
        if (!m_NewDriverGroup.IsEmptyIgnoreFilter)
        {
            inputDep.Complete();
            var newDriverEntity = m_NewDriverGroup.ToEntityArray(Allocator.TempJob);
            var newDriverList   = m_NewDriverGroup.ToComponentDataArray <PingDriverComponentData>(Allocator.TempJob);
            for (int i = 0; i < newDriverList.Length; ++i)
            {
                if (newDriverList[i].isServer != 0)
                {
                    if (ServerDriver.IsCreated)
                    {
                        throw new InvalidOperationException("Cannot create multiple server drivers");
                    }
                    var drv  = new UdpNetworkDriver(new INetworkParameter[0]);
                    var addr = NetworkEndPoint.AnyIpv4;
                    addr.Port = 9000;
                    if (drv.Bind(addr) != 0)
                    {
                        throw new Exception("Failed to bind to port 9000");
                    }
                    else
                    {
                        drv.Listen();
                    }
                    ServerDriver           = drv;
                    ConcurrentServerDriver = ServerDriver.ToConcurrent();
                }
                else
                {
                    if (ClientDriver.IsCreated)
                    {
                        throw new InvalidOperationException("Cannot create multiple client drivers");
                    }
                    ClientDriver           = new UdpNetworkDriver(new INetworkParameter[0]);
                    ConcurrentClientDriver = ClientDriver.ToConcurrent();
                }

                commandBuffer.AddComponent(newDriverEntity[i],
                                           new PingDriverStateComponent {
                    isServer = newDriverList[i].isServer
                });
            }
            newDriverList.Dispose();
            newDriverEntity.Dispose();
        }

        JobHandle clientDep = default(JobHandle);
        JobHandle serverDep = default(JobHandle);

        // Go through and update all drivers, also accept all incoming connections for server drivers
        if (ServerDriver.IsCreated)
        {
            // Schedule a chain with driver update, a job to accept all connections and finally a job to delete all invalid connections
            serverDep = ServerDriver.ScheduleUpdate(inputDep);
            var acceptJob = new DriverAcceptJob
            {
                driver = ServerDriver, commandBuffer = commandBuffer
            };
            serverDep = acceptJob.Schedule(serverDep);
            var cleanupJob = new DriverCleanupJob
            {
                commandBuffer = m_Barrier.CreateCommandBuffer().ToConcurrent()
            };
            serverDep = cleanupJob.Schedule(this, serverDep);
            m_Barrier.AddJobHandleForProducer(serverDep);
        }

        if (ClientDriver.IsCreated)
        {
            clientDep = ClientDriver.ScheduleUpdate(inputDep);
        }

        return(JobHandle.CombineDependencies(clientDep, serverDep));
    }
Example #2
0
    protected override JobHandle OnUpdate(JobHandle inputDep)
    {
        inputDep.Complete();
        var commandBuffer = m_Barrier.CreateCommandBuffer();

        // Destroy drivers if the PingDriverComponents were removed
        for (int i = 0; i < destroyedDriverList.state.Length; ++i)
        {
            if (destroyedDriverList.state[i].isServer != 0)
            {
                // Allso destroy all active connections when the driver dies
                for (int con = 0; con < serverConnectionList.connections.Length; ++con)
                {
                    commandBuffer.DestroyEntity(serverConnectionList.entities[con]);
                }
                ServerDriver.Dispose();
            }
            else
            {
                ClientDriver.Dispose();
            }
            commandBuffer.RemoveComponent <PingDriverStateComponent>(destroyedDriverList.entities[i]);
        }
        // Create drivers if new PingDriverComponents were added
        for (int i = 0; i < newDriverList.drivers.Length; ++i)
        {
            if (newDriverList.drivers[i].isServer != 0)
            {
                if (ServerDriver.IsCreated)
                {
                    throw new InvalidOperationException("Cannot create multiple server drivers");
                }
                var drv = new UdpCNetworkDriver(new INetworkParameter[0]);
                if (drv.Bind(new IPEndPoint(IPAddress.Any, 9000)) != 0)
                {
                    throw new Exception("Failed to bind to port 9000");
                }
                else
                {
                    drv.Listen();
                }
                ServerDriver = drv;
            }
            else
            {
                if (ClientDriver.IsCreated)
                {
                    throw new InvalidOperationException("Cannot create multiple client drivers");
                }
                ClientDriver = new UdpCNetworkDriver(new INetworkParameter[0]);
            }
            commandBuffer.AddComponent(newDriverList.entities[i], new PingDriverStateComponent {
                isServer = newDriverList.drivers[i].isServer
            });
        }

        JobHandle clientDep = default(JobHandle);
        JobHandle serverDep = default(JobHandle);

        // Go through and update all drivers, also accept all incoming connections for server drivers
        for (int i = 0; i < driverList.drivers.Length; ++i)
        {
            if (driverList.drivers[i].isServer != 0)
            {
                // Schedule a chain with driver update, a job to accept all connections and finally a job to delete all invalid connections
                serverDep = ServerDriver.ScheduleUpdate();
                var acceptJob = new DriverAcceptJob
                {
                    driver = ServerDriver, commandBuffer = commandBuffer
                };
                serverDep = acceptJob.Schedule(serverDep);
                var cleanupJob = new DriverCleanupJob
                {
                    serverConnections        = serverConnectionList.connections,
                    serverConnectionEntities = serverConnectionList.entities,
                    commandBuffer            = commandBuffer
                };
                serverDep = cleanupJob.Schedule(serverDep);
            }
            else
            {
                clientDep = ClientDriver.ScheduleUpdate();
            }
        }

        return(JobHandle.CombineDependencies(clientDep, serverDep));
    }