42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using ProjectM.Simulation;
|
|
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.NetCode;
|
|
using Unity.Networking.Transport;
|
|
|
|
namespace ProjectM.Server
|
|
{
|
|
/// <summary>
|
|
/// Server-world half of the M4 LAN host flow. When the per-world <see cref="ConnectionConfig"/>
|
|
/// requests <see cref="ConnectionMode.Host"/>, creates a <see cref="NetworkStreamRequestListen"/>
|
|
/// entity (netcode binds the server driver and starts listening) and clears the request. Replaces the
|
|
/// old <c>AutoConnectPort</c> auto-listen. Not Burst-compiled: a cold path that fires only on an
|
|
/// explicit host request.
|
|
/// </summary>
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
|
public partial struct ServerConnectionControlSystem : ISystem
|
|
{
|
|
public void OnCreate(ref SystemState state)
|
|
{
|
|
state.RequireForUpdate<ConnectionConfig>();
|
|
}
|
|
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
var cfgRef = SystemAPI.GetSingletonRW<ConnectionConfig>();
|
|
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();
|
|
}
|
|
}
|
|
}
|