LatencySimulation: SimulateLatency helper function

This commit is contained in:
vis2k 2021-03-16 15:26:42 +08:00
parent de076348c0
commit 1281f72ac2

View File

@ -60,6 +60,20 @@ public void Awake()
void OnEnable() { wrap.enabled = true; }
void OnDisable() { wrap.enabled = false; }
// helper function to simulate latency
float SimulateLatency(int channeldId)
{
switch (channeldId)
{
case Channels.Reliable:
return reliableLatency;
case Channels.Unreliable:
return unreliableLatency;
default:
return 0;
}
}
// helper function to simulate a send with latency/loss/scramble
void SimulateSend(int connectionId, int channelId, ArraySegment<byte> segment, float latency, List<QueuedMessage> reliableQueue, List<QueuedMessage> unreliableQueue)
{
@ -133,9 +147,7 @@ public override void ClientDisconnect()
public override void ClientSend(int channelId, ArraySegment<byte> segment)
{
float latency = channelId == Channels.Reliable
? reliableLatency
: unreliableLatency;
float latency = SimulateLatency(channelId);
SimulateSend(0, channelId, segment, latency, reliableClientToServer, unreliableClientToServer);
}
@ -149,9 +161,7 @@ public override void ClientSend(int channelId, ArraySegment<byte> segment)
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment)
{
float latency = channelId == Channels.Reliable
? reliableLatency
: unreliableLatency;
float latency = SimulateLatency(channelId);
SimulateSend(connectionId, channelId, segment, latency, reliableServerToClient, unreliableServerToClient);
}