using ProjectM.Simulation;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using Unity.Networking.Transport;
namespace ProjectM.Server
{
///
/// Server-world half of the M4 LAN host flow. When the per-world
/// requests , creates a
/// entity (netcode binds the server driver and starts listening) and clears the request. Replaces the
/// old AutoConnectPort auto-listen. Not Burst-compiled: a cold path that fires only on an
/// explicit host request.
///
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct ServerConnectionControlSystem : ISystem
{
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate();
}
public void OnUpdate(ref SystemState state)
{
var cfgRef = SystemAPI.GetSingletonRW();
if (!cfgRef.ValueRO.Requested || cfgRef.ValueRO.Mode != ConnectionMode.Host)
return;
cfgRef.ValueRW.Requested = false;
var endpoint = NetworkEndpoint.AnyIpv4.WithPort(cfgRef.ValueRO.Port);
var ecb = new EntityCommandBuffer(Allocator.Temp);
var req = ecb.CreateEntity();
ecb.AddComponent(req, new NetworkStreamRequestListen { Endpoint = endpoint });
ecb.Playback(state.EntityManager);
ecb.Dispose();
}
}
}