using System; using System.Collections; using System.Collections.Generic; using GameCore.Network; using GameCore.TinyECS; using Unity.Collections; using Unity.Jobs; using Unity.Networking.Transport; using UnityEngine; namespace ECSTest.Server { public class ServerMain : MonoBehaviour { public string address = "127.0.0.1"; public NetworkDriverAsset driverAsset; private NetworkDriver driver; private NetworkPipeline unreliablePipeline; private NetworkPipeline reliablePipeline; private JobHandle networkJobHandle; private NativeList connectionList; private NativeList disconnectIndexList; private World world; private JobHandle gameLogicJobHandle; private void OnEnable() { driver = driverAsset.CreateServerDriver(address, out unreliablePipeline, out reliablePipeline); connectionList = new NativeList(16, Allocator.Persistent); disconnectIndexList = new NativeList(16, Allocator.Persistent); var cannonMeta = EntityDataBlockMeta.Create(); var cannonDataBlockChain = new EntityDataBlockChain(cannonMeta, Allocator.Persistent); world.cannonDatabase = cannonDataBlockChain.CreateEntityDatabase(EntityType.Cannon, 16); var bulletMeta = EntityDataBlockMeta.Create(); var bulletDataBlockChain = new EntityDataBlockChain(bulletMeta, Allocator.Persistent); world.bulletDatabase = bulletDataBlockChain.CreateEntityDatabase(EntityType.Bullet, 16); } private void OnDisable() { networkJobHandle.Complete(); gameLogicJobHandle.Complete(); driver.Dispose(); connectionList.Dispose(); disconnectIndexList.Dispose(); world.Dispose(); } void Update() { networkJobHandle.Complete(); var concurrentDriver = driver.ToConcurrent(); networkJobHandle = driver.ScheduleUpdate(); networkJobHandle = new ServerConnectionJob() { driver = driver, connectionList = connectionList, disconnectIndexList = disconnectIndexList, }.Schedule(networkJobHandle); networkJobHandle = new ServerReceiveJob() { driver = concurrentDriver, connectionList = connectionList, disconnectIndexList = disconnectIndexList.AsParallelWriter(), }.Schedule(connectionList, 16, networkJobHandle); if (gameLogicJobHandle.IsCompleted) { gameLogicJobHandle.Complete(); gameLogicJobHandle = new TestWorldJob() { world = world, }.Schedule(networkJobHandle); } } } }