Added packet loss stats to NetworkInfo and made several other improvements.

This commit is contained in:
Karrar
2024-08-25 01:58:57 +03:00
parent 7e3a31bf94
commit d294f65bd7
18 changed files with 562 additions and 25 deletions

View File

@@ -17,14 +17,21 @@ namespace Netick.Samples
public bool AutoStart;
public bool AutoConnect;
public bool ShowDisconnectButton = true;
public bool ShowConnectButton = true;
[Header("Network")]
[Range(0, 65535)]
public int Port;
public string ServerIPAddress = "127.0.0.1";
[Header("Headless Server FPS")]
public bool Cap = true;
public int FPS = 450;
[Header("UI")]
public bool ShowDisconnectButton = true;
public bool ShowConnectButton = true;
public Vector2 Offset = new Vector2(36, 0);
private void Reset()
{
if (Port == 0)
@@ -35,6 +42,7 @@ namespace Netick.Samples
{
if (Application.isBatchMode)
{
Application.targetFrameRate = FPS;
Network.StartAsServer(Transport, Port, SandboxPrefab);
}
@@ -72,24 +80,27 @@ namespace Netick.Samples
{
if (Sandbox != null && Sandbox.IsClient)
{
if (!Sandbox.IsVisible)
return;
if (Sandbox.IsConnected)
{
if (ShowDisconnectButton)
{
GUI.Label(new Rect(10, 130, 200, 50), $"Connected to {Sandbox.ServerEndPoint}");
GUI.Label(new Rect(Offset.x, Offset.y + 170, 200, 50), $"Connected to {Sandbox.ServerEndPoint}");
if (GUI.Button(new Rect(10, 220, 200, 50), "Disconnect"))
if (GUI.Button(new Rect(Offset.x, Offset.y + 220, 200, 50), "Disconnect"))
Sandbox.DisconnectFromServer();
}
}
else if (ShowConnectButton)
{
if (GUI.Button(new Rect(10, 10, 200, 50), "Connect"))
if (GUI.Button(new Rect(Offset.x, Offset.y + 40, 200, 50), "Connect"))
Sandbox.Connect(Port, ServerIPAddress);
ServerIPAddress = GUI.TextField(new Rect(10, 70, 200, 50), ServerIPAddress);
Port = int.Parse(GUI.TextField(new Rect(10, 130, 200, 50), Port.ToString()));
ServerIPAddress = GUI.TextField(new Rect(Offset.x, Offset.y + 100, 200, 50), ServerIPAddress);
Port = int.Parse(GUI.TextField(new Rect(Offset.x, Offset.y+ 160, 200, 50), Port.ToString()));
}
}
@@ -97,23 +108,23 @@ namespace Netick.Samples
return;
}
if (GUI.Button(new Rect(10, 10, 200, 50), "Run Host"))
if (GUI.Button(new Rect(Offset.x, Offset.y + 40, 200, 50), "Run Host"))
{
Network.StartAsHost(Transport, Port, SandboxPrefab);
}
if (GUI.Button(new Rect(10, 70, 200, 50), "Run Client"))
if (GUI.Button(new Rect(Offset.x, Offset.y + 100, 200, 50), "Run Client"))
{
var sandbox = Network.StartAsClient(Transport, Port, SandboxPrefab);
sandbox.Connect(Port, ServerIPAddress);
}
if (GUI.Button(new Rect(10, 130, 200, 50), "Run Server"))
if (GUI.Button(new Rect(Offset.x, Offset.y + 160, 200, 50), "Run Server"))
{
Network.StartAsServer(Transport, Port, SandboxPrefab);
}
if (GUI.Button(new Rect(10, 190, 200, 50), "Run Host + Client"))
if (GUI.Button(new Rect(Offset.x, Offset.y + 220, 200, 50), "Run Host + Client"))
{
var sandboxes = Network.StartAsMultiplePeers(Transport, Port, SandboxPrefab, StartServerInMultiplePeersMode, true, Clients);
@@ -124,7 +135,7 @@ namespace Netick.Samples
}
}
ServerIPAddress = GUI.TextField(new Rect(10, 250, 200, 50), ServerIPAddress);
ServerIPAddress = GUI.TextField(new Rect(Offset.x, Offset.y + 280, 200, 50), ServerIPAddress);
}
}

View File

@@ -7,27 +7,86 @@ namespace Netick.Samples
{
[AddComponentMenu("Netick/Network Info")]
public class NetworkInfo : NetworkEventsListener
{
{
[Header("Network Stats")]
public Vector2 Offset = new Vector2(27, 20);
[Header("Network Conditions Icons")]
public float MediumLatencyThreshold = 150;
public float HighLatencyThreshold = 250;
public float MediumPacketLossThreshold = 1;
public float HighPacketLossThreshold = 10;
public Vector2 Icon1Offset = new Vector2(-80, 30);
public Vector2 Icon2Offset = new Vector2(-80, 70);
public Vector2 Icon3Offset = new Vector2(-80, 110);
public float IconSize = 30;
private Texture _packetLossIcon;
private Texture _latencyIcon;
private Texture _serverLagIcon;
private void Awake()
{
_packetLossIcon = Resources.Load<Texture>("Network Icons/PacketLoss");
_latencyIcon = Resources.Load<Texture>("Network Icons/Latency");
_serverLagIcon = Resources.Load<Texture>("Network Icons/ServerLag");
}
private void OnGUI()
{
if (Network.Instance != null)
if (Network.IsRunning)
{
if (Sandbox != null && Sandbox.IsConnected)
{
Draw(0, "In", Sandbox.InKBps.ToString(), "KB/s");
Draw(1, "Out", Sandbox.OutKBps.ToString(), "KB/s");
Draw(2, "RTT", (Sandbox.RTT * 1000f).ToString(), "ms");
Draw(3, "Interp Delay", (Sandbox.InterpolationDelay * 1000f).ToString(), "ms");
Draw(4, "Resims", Sandbox.Monitor.Resimulations.Average.ToString(), "Ticks");
Draw(5, "Delta time", (Time.deltaTime * 1000f).ToString(), "ms");
DrawText(0, "RTT", (Sandbox.RTT * 1000f).ToString(), "ms");
DrawText(1, "In", Sandbox.InKBps.ToString(), "KB/s");
DrawText(2, "Out", Sandbox.OutKBps.ToString(), "KB/s");
DrawText(3, "In Loss", (Sandbox.InPacketLoss * 100f).ToString(), "%");
DrawText(4, "Out Loss", (Sandbox.OutPacketLoss * 100f).ToString(), "%");
DrawText(5, "Interp Delay", (Sandbox.InterpolationDelay * 1000f).ToString(), "ms");
DrawText(6, "Resims", Sandbox.Monitor.Resimulations.Average.ToString(), "Ticks");
DrawText(7, "Srv Tick Time", (Sandbox.Monitor.ServerTickTime.Max * 1000f).ToString(), "ms");
DrawText(8, "Delta time", (Time.deltaTime * 1000f).ToString(), "ms");
DrawIcons();
}
}
}
private void Draw(int offset, string title, string content, string unit)
private void DrawText(int offset, string title, string content, string unit)
{
GUI.Label(new Rect(10, 10 + (15 * offset), 200, 50), $"{title}: ");
GUI.Label(new Rect(130, 10 + (15 * offset), 200, 50), $"{content} {unit}");
GUI.Label(new Rect(Offset.x + 10, Offset.y + 10 + (15 * offset), 200, 50), $"{title}: ");
GUI.Label(new Rect(Offset.x + 130, Offset.y + 10 + (15 * offset), 200, 50), $"{content} {unit}");
}
public void DrawIcons()
{
var pktLossIconPos = Icon1Offset + (Screen.width * Vector2.right);
var latencyIconPos = Icon2Offset + (Screen.width * Vector2.right);
var serverLagIconPos = Icon3Offset + (Screen.width * Vector2.right);
var pktLoss = Mathf.Max(Sandbox.InPacketLoss, Sandbox.OutPacketLoss) * 100; // multiplying by 100 to convert from a decimal to a percentage.
var rtt = Sandbox.RTT * 1000f; // multiplying by 1000 to convert from seconds to milliseconds.
if (pktLoss >= MediumPacketLossThreshold)
{
Color color = pktLoss < HighPacketLossThreshold ? Color.yellow : Color.red;
GUI.DrawTexture(new Rect(pktLossIconPos, Vector2.one * IconSize), _packetLossIcon, ScaleMode.ScaleToFit, true, 1f, color, 0f, 0f);
}
if (rtt >= MediumLatencyThreshold)
{
Color color = rtt >= HighLatencyThreshold ? Color.red : Color.yellow;
GUI.DrawTexture(new Rect(latencyIconPos, Vector2.one * IconSize), _latencyIcon, ScaleMode.ScaleToFit, true, 1f, color, 0f, 0f);
}
if (Sandbox.Monitor.ServerTickTime.Max >= Sandbox.FixedDeltaTime)
{
Color color = Sandbox.Monitor.ServerTickTime.Average >= Sandbox.FixedDeltaTime ? Color.red : Color.yellow;
GUI.DrawTexture(new Rect(serverLagIconPos, Vector2.one * IconSize), _serverLagIcon, ScaleMode.ScaleToFit, true, 1f, color, 0f, 0f);
}
}
}
}