/// <summary> Gets average ping in milliseconds, or 0 if no ping measures. </summary> public static int AveragePingMilliseconds() { double totalMs = 0; int measures = 0; for (int i = 0; i < Entries.Length; i++) { PingEntry ping = Entries[i]; if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) { continue; } totalMs += ping.Latency; measures++; } return(measures == 0 ? 0 : (int)(totalMs / measures)); }
// Everyone gets notified whenever a new client joins the server private void _on_player_connected(int id) { if (GetTree().IsNetworkServer()) { // Send the server info to the player RpcId(id, nameof(getServerInfo), serverinfo.name + ";" + serverinfo.max_players); PingEntry pingEntry = new PingEntry(); // Setup the timer pingEntry.timer.OneShot = true; pingEntry.timer.WaitTime = PINGINTERVAL; pingEntry.timer.ProcessMode = Timer.TimerProcessMode.Idle; pingEntry.timer.Connect("timeout", this, nameof(onPingInterval)); pingEntry.timer.Name = "ping_timer_" + id; // Timers need to be part of the tree otherwise they are not updated and never fire up the timeout event AddChild(pingEntry.timer); // Add the entry to the dictionary pingEntries.Add(id, pingEntry); // Just to ensure, start the timer (in theory is should run but...) pingEntry.timer.Start(); } }
bool Valid(int i) { PingEntry e = Entries[i]; return(e.TimeSent.Ticks != 0 && e.TimeReceived.Ticks != 0); }