using Unity.Collections; using Unity.Entities; namespace Unity.NetCode.Extensions { [WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation, WorldSystemFilterFlags.ServerSimulation)] public partial struct ConnectionMonitorSystem : ISystem { public void OnCreate(ref SystemState state) { state.RequireForUpdate(); } public void OnUpdate(ref SystemState state) { var ecb = new EntityCommandBuffer(Allocator.Temp); foreach (var (connectionState, entity) in SystemAPI .Query>() .WithNone() .WithEntityAccess()) { ecb.AddComponent(entity); } foreach (var (networkId, entity) in SystemAPI .Query>() .WithNone() .WithEntityAccess()) { var connectionInfoRequest = new ConnectionStateInfoRequest { State = (int)ConnectionState.State.Connected, }; var requestEntity = ecb.CreateEntity(); ecb.AddComponent(requestEntity); ecb.AddComponent(requestEntity, connectionInfoRequest); ecb.AddComponent(entity, new InitializedConnection()); UnityEngine.Debug.Log($"[Server] New connection ID:{networkId.ValueRO.Value}"); } foreach (var (connectionState, entity) in SystemAPI .Query>() .WithNone() .WithEntityAccess()) { var id = connectionState.ValueRO.NetworkId; var reasonId = (int)connectionState.ValueRO.DisconnectReason; var reason = DisconnectReasonEnumToString.Convert(reasonId); var disconnectedSuccessInfoRequest = new ConnectionStateInfoRequest { ReasonID = reasonId, State = (int)ConnectionState.State.Disconnected, }; var requestEntity = ecb.CreateEntity(); ecb.AddComponent(requestEntity); ecb.AddComponent(requestEntity, disconnectedSuccessInfoRequest); ecb.RemoveComponent(entity); UnityEngine.Debug.Log($"[Server] Connection disconnected ID:{id} Reason:{reason}"); } ecb.Playback(state.EntityManager); } } }