public void Events_ServerToClient_SendReliable() { TestTransport.Reset(); var serverTransport = new TestTransport("127.0.0.1", 1); var clientTransport = new TestTransport("127.0.0.1", 2); var snapshotConsumer = new NullSnapshotConsumer(); var serverCallbacks = new ServerCallbacks(this); var clientCallbacks = new ClientCallbacks(this); var server = new NetworkServer(serverTransport); var client = new NetworkClient(clientTransport); server.Update(serverCallbacks); client.Connect("127.0.0.1:1"); server.InitializeMap((ref NetworkWriter data) => { data.WriteString("name", "TestMap"); }); server.Update(serverCallbacks); server.SendData(); client.Update(clientCallbacks, snapshotConsumer); var RUNS = 1000; eventSent = 0; eventReceived = 0; lastEventSent = null; for (int i = 0; i < RUNS; ++i) { server.Update(serverCallbacks); if (eventSent == eventReceived && i < RUNS - 32) { server.QueueEvent(1, (ushort)EventType.MyEvent, true, (ref NetworkWriter writer) => { lastEventSent = new MyEvent(); lastEventSent.Serialize(ref writer); }); ++eventSent; } server.SendData(); if (i % 3 == 0) { clientTransport.DropPackages(); } client.Update(clientCallbacks, snapshotConsumer); Assert.IsTrue(eventReceived <= eventSent); client.SendData(); } Assert.AreEqual(eventSent, eventReceived); }
public void Events_ServerToClient_BroadcastUnreliable() { TestTransport.Reset(); var snapshotConsumer = new NullSnapshotConsumer(); var serverTransport = new TestTransport("127.0.0.1", 1); var server = new NetworkServer(serverTransport); var serverCallbacks = new ServerCallbacks(this); var clientCallbacks = new ClientCallbacks(this); const int NUM_CLIENTS = 3; var clientTransports = new TestTransport[NUM_CLIENTS]; var clients = new NetworkClient[NUM_CLIENTS]; for (int i = 0; i < NUM_CLIENTS; ++i) { clientTransports[i] = new TestTransport("127.0.0.1", i + 2); clients[i] = new NetworkClient(clientTransports[i]); clients[i].Connect("127.0.0.1:1"); } server.InitializeMap((ref NetworkWriter data) => { data.WriteString("name", "TestMap"); }); server.Update(serverCallbacks); var RUNS = 1000; eventSent = 0; eventReceived = 0; lastEventSent = null; for (int i = 0; i < RUNS; ++i) { server.Update(serverCallbacks); if (eventSent == eventReceived * NUM_CLIENTS && i < RUNS - 2) { server.QueueEventBroadcast((ushort)EventType.MyEvent, false, (ref NetworkWriter writer) => { lastEventSent = new MyEvent(); lastEventSent.Serialize(ref writer); }); ++eventSent; } server.SendData(); foreach (var client in clients) { client.Update(clientCallbacks, snapshotConsumer); client.SendData(); } } Assert.AreEqual(eventSent, eventReceived / NUM_CLIENTS); }
public void Events_ClientToServer_SendUnreliable() { TestTransport.Reset(); var serverTransport = new TestTransport(0); var clientTransport = new TestTransport(1); var serverCallbacks = new ServerCallbacks(this); var clientCallbacks = new ClientCallbacks(this); var server = new NetworkServer(serverTransport); var client = new NetworkClient(clientTransport); client.Connect("0"); server.InitializeMap((ref NetworkWriter data) => { data.WriteString("name", "TestMap"); }); server.Update(serverCallbacks); var RUNS = 1000; eventSent = 0; eventReceived = 0; lastEventSent = null; for (int i = 0; i < RUNS; ++i) { server.Update(serverCallbacks); server.SendData(); client.Update(clientCallbacks); if (eventSent == eventReceived && i < RUNS - 2) { client.QueueEvent((ushort)EventType.MyEvent, false, (ref NetworkWriter writer) => { lastEventSent = new MyEvent(); lastEventSent.Serialize(ref writer); }); ++eventSent; } client.SendData(); } Assert.AreEqual(eventReceived, eventSent); }