Skip to content
Permalink
Browse files

Now /pinglist also includes lowest ping

  • Loading branch information...
UnknownShadow200 committed Jun 23, 2017
1 parent 72cf958 commit 823186c34ecc1f95c20f7cc95cf69446c4406968
Showing with 24 additions and 17 deletions.
  1. +24 −17 fCraft/Network/PingList.cs
@@ -2,13 +2,17 @@
using System;

namespace fCraft {

public sealed class PingList {

public struct PingEntry {
public DateTime TimeSent, TimeReceived;
public ushort Data;
public double Latency { get {
// Half, because received->reply time is actually twice time it takes to send data
return (TimeReceived - TimeSent).TotalMilliseconds * 0.5;
} }
}

public PingEntry[] Entries = new PingEntry[10];


@@ -30,9 +34,9 @@ public struct PingEntry {
}

ushort SetTwoWayPing(int i, ushort prev) {
Entries[i].Data = (ushort)(prev + 1);
Entries[i].TimeSent = DateTime.UtcNow;
return (ushort)(prev + 1);
Entries[i].Data = (ushort)(prev + 1);
Entries[i].TimeSent = DateTime.UtcNow;
return (ushort)(prev + 1);
}

public void Update(ushort data) {
@@ -44,39 +48,42 @@ public struct PingEntry {
}


/// <summary> Gets best ping in milliseconds, or 0 if no ping measures. </summary>
public double BestPingMilliseconds() {
double totalMs = 100000000;
foreach (PingEntry ping in Entries) {
if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue;
totalMs = Math.Min(totalMs, ping.Latency);
}
return totalMs;
}

/// <summary> Gets average ping in milliseconds, or 0 if no ping measures. </summary>
public double AveragePingMilliseconds() {
double totalMs = 0;
int measures = 0;

foreach (PingEntry ping in Entries) {
if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue;

// Half, because received->reply time is actually twice time it takes to send data
totalMs += (ping.TimeReceived - ping.TimeSent).TotalMilliseconds * 0.5;
measures++;
totalMs += ping.Latency; measures++;
}
return measures == 0 ? 0 : (totalMs / measures);
}


/// <summary> Gets worst ping in milliseconds, or 0 if no ping measures. </summary>
public double WorstPingMilliseconds() {
double totalMs = 0;

foreach (PingEntry ping in Entries) {
if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue;

double ms = (ping.TimeReceived - ping.TimeSent).TotalMilliseconds * 0.5;
totalMs = Math.Max(totalMs, ms);
totalMs = Math.Max(totalMs, ping.Latency);
}
return totalMs;
}

public string Format() {
return String.Format("Worst ping {0}ms, average {1}ms",
WorstPingMilliseconds().ToString("N0"),
AveragePingMilliseconds().ToString("N0"));
return String.Format("Lowest ping {0}ms, average {1} ms, highest {2}ms",
(int)BestPingMilliseconds(),
(int)AveragePingMilliseconds(),
(int)WorstPingMilliseconds());
}
}
}

0 comments on commit 823186c

Please sign in to comment.
You can’t perform that action at this time.