Add MrGameEng.Net: WebSocket transport + delta component replication
CI / build-test (push) Successful in 1m16s
CI / build-test (push) Successful in 1m16s
Browsers can't speak UDP, so WebSocket is the engine's one transport. The server side is a dependency-free RFC 6455 implementation over TcpListener (handshake, frame codec with masking and fragmentation, ping/pong — unit-tested against the RFC example vectors); the client wraps ClientWebSocket, which works on desktop and maps to the browser WebSocket in Blazor WASM. Both sit behind the poll-based INetConnection so simulation systems drain messages from their own thread; client sends are chained fire-and-forget (no blocking — wasm-safe). Replication is server-authoritative: games register unmanaged component types in a ReplicationSchema (same order both sides, up to 32 types), ReplicationServer snapshots entities carrying NetId once per send and ships each connection only the components that changed since its last snapshot — a reliable ordered transport needs no acks for deltas. New connections receive the full state through the same path; despawns are tracked by set difference. ReplicationClient applies snapshots to a local EntityStore and raises EntitySpawned so the game can decorate replicated entities with presentation components. Covered by 16 tests including a real loopback exchange between WebSocketClient and WebSocketServer. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
10898b08a0
commit
3438ed77f6
@@ -0,0 +1,200 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Friflo.Engine.ECS;
|
||||
using MrGameEng.Net;
|
||||
using Xunit;
|
||||
|
||||
namespace MrGameEng.Net.Tests;
|
||||
|
||||
public class ReplicationTests
|
||||
{
|
||||
private struct TestPosition : IComponent
|
||||
{
|
||||
public float X;
|
||||
public float Y;
|
||||
}
|
||||
|
||||
private struct TestHealth : IComponent
|
||||
{
|
||||
public int Value;
|
||||
}
|
||||
|
||||
private sealed class FakeConnection : INetConnection
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public bool IsOpen { get; set; } = true;
|
||||
public readonly ConcurrentQueue<byte[]> Sent = new();
|
||||
|
||||
public void Send(ReadOnlySpan<byte> message) => Sent.Enqueue(message.ToArray());
|
||||
|
||||
public bool TryReceive(out byte[] message) => Sent.TryDequeue(out message!);
|
||||
|
||||
public void Close() => IsOpen = false;
|
||||
}
|
||||
|
||||
private static ReplicationSchema MakeSchema() =>
|
||||
new ReplicationSchema().Register<TestPosition>().Register<TestHealth>();
|
||||
|
||||
[Fact]
|
||||
public void FirstSnapshot_SpawnsEntities_OnTheClient()
|
||||
{
|
||||
var serverStore = new EntityStore();
|
||||
var clientStore = new EntityStore();
|
||||
var server = new ReplicationServer(MakeSchema(), serverStore);
|
||||
var client = new ReplicationClient(MakeSchema(), clientStore);
|
||||
var connection = new FakeConnection();
|
||||
|
||||
serverStore.CreateEntity(
|
||||
new NetId { Value = server.NextNetId() },
|
||||
new TestPosition { X = 10f, Y = 20f },
|
||||
new TestHealth { Value = 7 }
|
||||
);
|
||||
serverStore.CreateEntity(
|
||||
new NetId { Value = server.NextNetId() },
|
||||
new TestPosition { X = -3f, Y = 4f }
|
||||
);
|
||||
|
||||
server.Send([connection]);
|
||||
client.Pump(connection);
|
||||
|
||||
Assert.Equal(2, client.EntityCount);
|
||||
var first = FindByNetId(clientStore, 1);
|
||||
Assert.Equal(10f, first.GetComponent<TestPosition>().X);
|
||||
Assert.Equal(7, first.GetComponent<TestHealth>().Value);
|
||||
var second = FindByNetId(clientStore, 2);
|
||||
Assert.False(second.HasComponent<TestHealth>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnchangedWorld_SendsNothing()
|
||||
{
|
||||
var serverStore = new EntityStore();
|
||||
var server = new ReplicationServer(MakeSchema(), serverStore);
|
||||
var connection = new FakeConnection();
|
||||
serverStore.CreateEntity(
|
||||
new NetId { Value = server.NextNetId() },
|
||||
new TestPosition { X = 1f, Y = 1f }
|
||||
);
|
||||
|
||||
server.Send([connection]);
|
||||
Assert.Single(connection.Sent);
|
||||
|
||||
server.Send([connection]);
|
||||
Assert.Single(connection.Sent); // дельта пустая — второго сообщения нет
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ChangedComponent_IsTheOnlyThingResent()
|
||||
{
|
||||
var serverStore = new EntityStore();
|
||||
var clientStore = new EntityStore();
|
||||
var server = new ReplicationServer(MakeSchema(), serverStore);
|
||||
var client = new ReplicationClient(MakeSchema(), clientStore);
|
||||
var connection = new FakeConnection();
|
||||
|
||||
var entity = serverStore.CreateEntity(
|
||||
new NetId { Value = server.NextNetId() },
|
||||
new TestPosition { X = 1f, Y = 1f },
|
||||
new TestHealth { Value = 100 }
|
||||
);
|
||||
server.Send([connection]);
|
||||
client.Pump(connection);
|
||||
|
||||
entity.AddComponent(new TestPosition { X = 5f, Y = 6f }); // здоровье не трогаем
|
||||
server.Send([connection]);
|
||||
|
||||
Assert.True(connection.Sent.TryDequeue(out var delta));
|
||||
// Запись: type(1) + count(4) + netId(4) + op(1) + mask(4) + TestPosition(8) — без TestHealth.
|
||||
Assert.Equal(22, delta!.Length);
|
||||
|
||||
client.Apply(delta);
|
||||
var replicated = FindByNetId(clientStore, 1);
|
||||
Assert.Equal(5f, replicated.GetComponent<TestPosition>().X);
|
||||
Assert.Equal(100, replicated.GetComponent<TestHealth>().Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeletedEntity_DespawnsOnTheClient()
|
||||
{
|
||||
var serverStore = new EntityStore();
|
||||
var clientStore = new EntityStore();
|
||||
var server = new ReplicationServer(MakeSchema(), serverStore);
|
||||
var client = new ReplicationClient(MakeSchema(), clientStore);
|
||||
var connection = new FakeConnection();
|
||||
|
||||
var entity = serverStore.CreateEntity(
|
||||
new NetId { Value = server.NextNetId() },
|
||||
new TestPosition { X = 1f, Y = 1f }
|
||||
);
|
||||
server.Send([connection]);
|
||||
client.Pump(connection);
|
||||
Assert.Equal(1, client.EntityCount);
|
||||
|
||||
entity.DeleteEntity();
|
||||
server.Send([connection]);
|
||||
client.Pump(connection);
|
||||
|
||||
Assert.Equal(0, client.EntityCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LateJoiner_GetsFullState()
|
||||
{
|
||||
var serverStore = new EntityStore();
|
||||
var server = new ReplicationServer(MakeSchema(), serverStore);
|
||||
var early = new FakeConnection { Id = 1 };
|
||||
var late = new FakeConnection { Id = 2 };
|
||||
|
||||
serverStore.CreateEntity(
|
||||
new NetId { Value = server.NextNetId() },
|
||||
new TestPosition { X = 9f, Y = 9f }
|
||||
);
|
||||
server.Send([early]);
|
||||
server.Send([early, late]); // мир не менялся: early — тишина, late — полный стейт
|
||||
|
||||
Assert.Single(early.Sent);
|
||||
Assert.Single(late.Sent);
|
||||
|
||||
var clientStore = new EntityStore();
|
||||
var client = new ReplicationClient(MakeSchema(), clientStore);
|
||||
client.Pump(late);
|
||||
Assert.Equal(1, client.EntityCount);
|
||||
Assert.Equal(9f, FindByNetId(clientStore, 1).GetComponent<TestPosition>().X);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EntitySpawned_FiresOncePerEntity()
|
||||
{
|
||||
var serverStore = new EntityStore();
|
||||
var clientStore = new EntityStore();
|
||||
var server = new ReplicationServer(MakeSchema(), serverStore);
|
||||
var client = new ReplicationClient(MakeSchema(), clientStore);
|
||||
var connection = new FakeConnection();
|
||||
var spawns = 0;
|
||||
client.EntitySpawned += _ => spawns++;
|
||||
|
||||
var entity = serverStore.CreateEntity(
|
||||
new NetId { Value = server.NextNetId() },
|
||||
new TestPosition { X = 1f, Y = 1f }
|
||||
);
|
||||
server.Send([connection]);
|
||||
client.Pump(connection);
|
||||
entity.AddComponent(new TestPosition { X = 2f, Y = 2f });
|
||||
server.Send([connection]);
|
||||
client.Pump(connection);
|
||||
|
||||
Assert.Equal(1, spawns);
|
||||
}
|
||||
|
||||
private static Entity FindByNetId(EntityStore store, int netId)
|
||||
{
|
||||
foreach (var entity in store.Entities)
|
||||
{
|
||||
if (entity.HasComponent<NetId>() && entity.GetComponent<NetId>().Value == netId)
|
||||
{
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Entity with NetId {netId} not found.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user