protected override void OnDestroy()
 {
     // Destroy NetworkDrivers if the manager is destroyed with live entities
     if (ServerDriver.IsCreated)
     {
         ServerDriver.Dispose();
     }
     if (ClientDriver.IsCreated)
     {
         ClientDriver.Dispose();
     }
 }
Example #2
0
        static void Main(string[] args)
        {
            //This pattern allows us to adapte one class to interface of another class without changing of adapteed class (we only can change the description of the methods and casting I/O parameters to the disered form
            //If we have class with needed features but it doesn't support desired interface we should follow this designed pattern



            //Imagine if we have Driver that can move by every transport that implements ITronsportFunction interface(basically we moving by car) , but we encountered situation where
            //we need to take off car and sit on elk cause we want to get through the mountines, we have elk but he doesn't support our desired interface so we can't move by him currently now ,
            //but we can try to adapte elk to the car


            var driver             = new ClientDriver();
            ITransportFunction car = new Car();

            driver.Drive(car);
            ITransportFunction elk = new ElkAdapter();

            driver.Drive(elk);
            Console.ReadLine();
        }
    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 #4
0
 public ClientHelper()
 {
     Driver = new ClientDriver();
     Driver.Start().Wait();
     this.isRunning = true;
 }