Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
- Added Network Statistic calculation (byte per sec etc.) by default…
Browse files Browse the repository at this point in the history
… it's enabled

 - Added a sample (NetworkStatistic.cs) how to display those statistics
  • Loading branch information
TiToMoskito committed Jan 19, 2022
1 parent 1651e0d commit 866d45e
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
1.2.0
- Simulation code was restructured (Thanks to Punfish)
- Unreliable packets can be out of order no
- Fixed Latency is now stable
- Fixed Latency is now stable
1.3.0
- Added Network Statistic calculation (byte per sec etc.) by default it's enabled
- Added a sample (NetworkStatistic.cs) how to display those statistics
157 changes: 151 additions & 6 deletions FishyLatency.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using FishNet;
using FishNet.Managing;
using FishNet.Transporting;
using FishNet.Utility.Performance;
Expand All @@ -13,9 +12,13 @@ public class FishyLatency : Transport
#region Serialized
[SerializeField] Transport m_transport;

[Header("Network Statistics")]
[Tooltip("Enable or disable the packet calculation")]
[SerializeField] bool m_enabledStatistic = true;

[Header("Settings")]
[Tooltip("Enable or disable the simulation")]
[SerializeField] bool m_enabled = true;
[SerializeField] bool m_enabledSimulation = true;

[Tooltip("Additional amount of latency for all packets")]
[Range(0, 1)]
Expand All @@ -36,6 +39,28 @@ public class FishyLatency : Transport
[SerializeField] double m_outOfOrder = 0;
#endregion

#region Attributes
// Sent Packets / bytes per second
public int SentPacketsClient => m_sentPackets[0];
public int SentPacketsServer => m_sentPackets[1];

public string SentBytesClient => FormatBytes(m_sentBytes[0]);
public string SentBytesServer => FormatBytes(m_sentBytes[1]);

public int SentBytesRawClient => m_sentBytes[0];
public int SentBytesRawServer => m_sentBytes[1];

// Received Packets / bytes per second
public int ReceivedPacketsClient => m_receivedPackets[0];
public int ReceivedPacketsServer => m_receivedPackets[1];

public string ReceivedBytesClient => FormatBytes(m_receivedBytes[0]);
public string ReceivedBytesServer => FormatBytes(m_receivedBytes[1]);

public int ReceivedBytesRawClient => m_receivedBytes[0];
public int ReceivedBytesRawServer => m_receivedBytes[1];
#endregion

#region Private
private Message? m_nextToServerReliable = null;
private Message? m_nextToServerUnreliable = null;
Expand Down Expand Up @@ -72,6 +97,12 @@ public ArraySegment<byte> GetSegment()
}

private readonly System.Random m_random = new System.Random();

// Used to keep track of how many packets get sent / received per second
// 0 = Client | 1 = Server
private int[] m_sentPackets, m_receivedPackets, m_sentBytes, m_receivedBytes;
private int[] m_sentPacketsCount, m_receivedPacketsCount, m_sentBytesCount, m_receivedBytesCount;
private float m_calculationTime = 0;
#endregion

#region Initialization and Unity
Expand All @@ -90,14 +121,21 @@ public override void Initialize(NetworkManager networkManager)
m_transport.OnRemoteConnectionState += OnRemoteConnectionState;
m_transport.OnClientReceivedData += OnClientReceivedData;
m_transport.OnServerReceivedData += OnServerReceivedData;

InitializeCalculation();
}

private void Update()
{
UpdateCalculation();
}

private void OnDestroy()
{
m_transport.Shutdown();
Shutdown();
}
#endregion
#endregion

#region ConnectionStates
/// <summary>
Expand Down Expand Up @@ -214,7 +252,7 @@ public override void HandleServerReceivedDataArgs(ServerReceivedDataArgs receive
public override void SendToServer(byte channelId, ArraySegment<byte> segment)
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
if(m_enabled)
if(m_enabledSimulation)
Add(channelId, segment);
else
m_transport.SendToServer(channelId, segment);
Expand All @@ -231,7 +269,7 @@ public override void SendToServer(byte channelId, ArraySegment<byte> segment)
public override void SendToClient(byte channelId, ArraySegment<byte> segment, int connectionId)
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
if (m_enabled)
if (m_enabledSimulation)
Add(channelId, segment, true, connectionId);
else
m_transport.SendToClient(channelId, segment, connectionId);
Expand Down Expand Up @@ -350,13 +388,15 @@ public override bool StopConnection(int connectionId, bool immediately)
/// Stops both client and server.
/// </summary>
public override void Shutdown()
{
{
m_transport.OnClientConnectionState -= OnClientConnectionState;
m_transport.OnServerConnectionState -= OnServerConnectionState;
m_transport.OnRemoteConnectionState -= OnRemoteConnectionState;
m_transport.OnClientReceivedData -= OnClientReceivedData;
m_transport.OnServerReceivedData -= OnServerReceivedData;

DeinitializeCalculation();

#if UNITY_EDITOR || DEVELOPMENT_BUILD
m_toServerReliablePackets.Clear();
m_toServerUnreliablePackets.Clear();
Expand Down Expand Up @@ -460,6 +500,8 @@ private void IterateCollection(List<Message> c, bool server)
else
m_transport.SendToServer(msg.channelId, msg.GetSegment());

AddSendPacketToCalc(msg.message.Length, server);

c.RemoveAt(0);
}
}
Expand All @@ -474,5 +516,108 @@ private bool CheckOutOfOrder()
return m_outOfOrder > 0 && m_random.NextDouble() < m_outOfOrder;
}
#endregion

#region Packet Calculation
private void InitializeCalculation()
{
if (m_enabledStatistic)
{
m_transport.OnClientReceivedData += OnClientReceivedDataCalc;
m_transport.OnServerReceivedData += OnServerReceivedDataCalc;

m_sentPackets = new int[2];
m_receivedPackets = new int[2];
m_sentBytes = new int[2];
m_receivedBytes = new int[2];

m_sentPacketsCount = new int[2];
m_receivedPacketsCount = new int[2];
m_sentBytesCount = new int[2];
m_receivedBytesCount = new int[2];
}
}

private void DeinitializeCalculation()
{
if (m_enabledStatistic)
{
m_enabledStatistic = false;
m_transport.OnClientReceivedData -= OnClientReceivedDataCalc;
m_transport.OnServerReceivedData -= OnServerReceivedDataCalc;

m_sentPackets = new int[2];
m_receivedPackets = new int[2];
m_sentBytes = new int[2];
m_receivedBytes = new int[2];

m_sentPacketsCount = new int[2];
m_receivedPacketsCount = new int[2];
m_sentBytesCount = new int[2];
m_receivedBytesCount = new int[2];
}
}

private void UpdateCalculation()
{
if (m_enabledStatistic)
{
m_calculationTime += Time.deltaTime;

if ((m_calculationTime % 60) >= 1)
{
m_calculationTime = 0;

m_sentPackets[0] = m_sentPacketsCount[0];
m_sentBytes[0] = m_sentBytesCount[0];
m_receivedPackets[0] = m_receivedPacketsCount[0];
m_receivedBytes[0] = m_receivedBytesCount[0];

m_sentPackets[1] = m_sentPacketsCount[1];
m_sentBytes[1] = m_sentBytesCount[1];
m_receivedPackets[1] = m_receivedPacketsCount[1];
m_receivedBytes[1] = m_receivedBytesCount[1];

m_sentPacketsCount = new int[2];
m_receivedPacketsCount = new int[2];
m_sentBytesCount = new int[2];
m_receivedBytesCount = new int[2];
}
}
}

private void AddSendPacketToCalc(int length, bool asServer)
{
m_sentPacketsCount[asServer ? 1 : 0]++;
m_sentBytesCount[asServer ? 1 : 0] += length;
}

private void AddReceivePacketToCalc(int length, bool asServer)
{
m_receivedPacketsCount[asServer ? 1 : 0]++;
m_receivedBytesCount[asServer ? 1 : 0] += length;
}

private void OnClientReceivedDataCalc(ClientReceivedDataArgs receivedDataArgs)
{
AddReceivePacketToCalc(receivedDataArgs.Data.Array.Length, false);
}

private void OnServerReceivedDataCalc(ServerReceivedDataArgs receivedDataArgs)
{
AddReceivePacketToCalc(receivedDataArgs.Data.Array.Length, true);
}

string FormatBytes(long byteCount)
{
string[] suf = { "b", "kB", "mb", "GB", "TB", "PB", "EB" };

if (byteCount == 0)
return "0 " + suf[0];
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
return (Math.Sign(byteCount) * num).ToString() + " " + suf[place];
}
#endregion
}
}
52 changes: 52 additions & 0 deletions NetworkStatistic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using BeardedMonkeys;
using FishNet;
using UnityEngine;

public class NetworkStatistic : MonoBehaviour
{
[Tooltip("The transport which contains the calculation.")]
[SerializeField]
private FishyLatency m_transport;

private GUIStyle _style = new GUIStyle();

private void OnGUI()
{
_style.normal.textColor = Color.magenta;
_style.fontSize = 24;
_style.fontStyle = FontStyle.Bold;

float width = 85f;
float height = 15f;

float horizontal = 10f;
float vertical = 10f;

if(InstanceFinder.IsServer)
{
GUI.Label(new Rect(horizontal, vertical, width, height), $"Received Packets: {m_transport.ReceivedPacketsServer}/s", _style);

vertical += 25f;
GUI.Label(new Rect(horizontal, vertical, width, height), $"Received Bytes: {m_transport.ReceivedBytesServer}/s", _style);

vertical += 25f;
GUI.Label(new Rect(horizontal, vertical, width, height), $"Sent Packets: {m_transport.SentPacketsServer}/s", _style);

vertical += 25f;
GUI.Label(new Rect(horizontal, vertical, width, height), $"Sent Bytes: {m_transport.SentBytesServer}/s", _style);
}
else
{
GUI.Label(new Rect(horizontal, vertical, width, height), $"Received Packets: {m_transport.ReceivedPacketsClient}/s", _style);

vertical += 25f;
GUI.Label(new Rect(horizontal, vertical, width, height), $"Received Bytes: {m_transport.ReceivedBytesClient}/s", _style);

vertical += 25f;
GUI.Label(new Rect(horizontal, vertical, width, height), $"Sent Packets: {m_transport.SentPacketsClient}/s", _style);

vertical += 25f;
GUI.Label(new Rect(horizontal, vertical, width, height), $"Sent Bytes: {m_transport.SentBytesClient}/s", _style);
}
}
}
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0
1.3.0

0 comments on commit 866d45e

Please sign in to comment.