mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
LatencySimulation: add latency spikes
This commit is contained in:
parent
f3997b6030
commit
c4e4bf3148
@ -24,6 +24,8 @@ public class LatencySimulation : Transport
|
|||||||
[Header("Reliable Messages")]
|
[Header("Reliable Messages")]
|
||||||
[Tooltip("Reliable latency in seconds")]
|
[Tooltip("Reliable latency in seconds")]
|
||||||
public float reliableLatency = 0;
|
public float reliableLatency = 0;
|
||||||
|
[Tooltip("Simulate latency spikes with % of latency for % of messages.")]
|
||||||
|
[Range(0, 1)] public float reliableLatencySpikes;
|
||||||
// note: packet loss over reliable manifests itself in latency.
|
// note: packet loss over reliable manifests itself in latency.
|
||||||
// don't need (and can't add) a loss option here.
|
// don't need (and can't add) a loss option here.
|
||||||
// note: reliable is ordered by definition. no need to scramble.
|
// note: reliable is ordered by definition. no need to scramble.
|
||||||
@ -31,6 +33,8 @@ public class LatencySimulation : Transport
|
|||||||
[Header("Unreliable Messages")]
|
[Header("Unreliable Messages")]
|
||||||
[Tooltip("Unreliable latency in seconds")]
|
[Tooltip("Unreliable latency in seconds")]
|
||||||
public float unreliableLatency = 0;
|
public float unreliableLatency = 0;
|
||||||
|
[Tooltip("Simulate latency spikes with % of latency for % of messages.")]
|
||||||
|
[Range(0, 1)] public float unreliableLatencySpikes;
|
||||||
[Tooltip("Packet loss in %")]
|
[Tooltip("Packet loss in %")]
|
||||||
[Range(0, 1)] public float unreliableLoss;
|
[Range(0, 1)] public float unreliableLoss;
|
||||||
[Tooltip("Scramble unreliable messages, just like over the real network. Mirror unreliable is unordered.")]
|
[Tooltip("Scramble unreliable messages, just like over the real network. Mirror unreliable is unordered.")]
|
||||||
@ -60,6 +64,19 @@ public void Awake()
|
|||||||
void OnEnable() { wrap.enabled = true; }
|
void OnEnable() { wrap.enabled = true; }
|
||||||
void OnDisable() { wrap.enabled = false; }
|
void OnDisable() { wrap.enabled = false; }
|
||||||
|
|
||||||
|
// helper function to simulate latency & spike with spike probability
|
||||||
|
float SimulateLatency(float latency, float spikesPercent)
|
||||||
|
{
|
||||||
|
// will this one spike?
|
||||||
|
bool spike = random.NextDouble() < spikesPercent;
|
||||||
|
|
||||||
|
// if it spiked, add spike latency by percent of original latency
|
||||||
|
float add = spike ? latency * spikesPercent : 0;
|
||||||
|
|
||||||
|
// return latency + spike
|
||||||
|
return latency + add;
|
||||||
|
}
|
||||||
|
|
||||||
// helper function to simulate a send with latency/loss/scramble
|
// helper function to simulate a send with latency/loss/scramble
|
||||||
void SimulateSend(int connectionId, int channelId, ArraySegment<byte> segment, List<QueuedMessage> reliableQueue, List<QueuedMessage> unreliableQueue)
|
void SimulateSend(int connectionId, int channelId, ArraySegment<byte> segment, List<QueuedMessage> reliableQueue, List<QueuedMessage> unreliableQueue)
|
||||||
{
|
{
|
||||||
@ -78,8 +95,8 @@ void SimulateSend(int connectionId, int channelId, ArraySegment<byte> segment, L
|
|||||||
switch (channelId)
|
switch (channelId)
|
||||||
{
|
{
|
||||||
case Channels.DefaultReliable:
|
case Channels.DefaultReliable:
|
||||||
// simulate latency
|
// simulate latency & spikes
|
||||||
message.timeToSend = Time.time + reliableLatency;
|
message.timeToSend = Time.time + SimulateLatency(reliableLatency, reliableLatencySpikes);
|
||||||
reliableQueue.Add(message);
|
reliableQueue.Add(message);
|
||||||
break;
|
break;
|
||||||
case Channels.DefaultUnreliable:
|
case Channels.DefaultUnreliable:
|
||||||
@ -93,8 +110,8 @@ void SimulateSend(int connectionId, int channelId, ArraySegment<byte> segment, L
|
|||||||
int last = unreliableQueue.Count;
|
int last = unreliableQueue.Count;
|
||||||
int index = unreliableScramble ? random.Next(0, last + 1) : last;
|
int index = unreliableScramble ? random.Next(0, last + 1) : last;
|
||||||
|
|
||||||
// simulate latency
|
// simulate latency & spikes
|
||||||
message.timeToSend = Time.time + unreliableLatency;
|
message.timeToSend = Time.time + SimulateLatency(unreliableLatency, unreliableLatencySpikes);
|
||||||
unreliableQueue.Insert(index, message);
|
unreliableQueue.Insert(index, message);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user