Skip to content
Permalink
Browse files

Move two way ping code to separate class, reduce ping entries to 10.

  • Loading branch information...
UnknownShadow200 committed Jun 21, 2017
1 parent e77633a commit 032ac9eb40926a8d0941d61c7ed1a626a6a1fc84
@@ -1782,8 +1782,8 @@ static void EmotesHandler(Player player, CommandReader cmd)
}
}

if (target != null && target.AveragePingMilliseconds() != 0) {
player.Message(target.FormatPing());
if (target != null && target.Ping.AveragePingMilliseconds() != 0) {
player.Message(target.Ping.Format());
}
}

@@ -2099,20 +2099,20 @@ static void clpHandler(Player player, CommandReader cmd)
if (!int.TryParse(offsetStr, out offset)) offset = 0;

Player[] candidates = Server.Players.CanBeSeen(player).Union(player)
.Where(p => p.AveragePingMilliseconds() != 0)
.OrderBy(p => p.AveragePingMilliseconds()).Reverse().ToArray();
.Where(p => p.Ping.AveragePingMilliseconds() != 0)
.OrderBy(p => p.Ping.AveragePingMilliseconds()).Reverse().ToArray();
if (candidates.Length < 1) {
player.Message("No online players have clients supporting measuring ping.");
return;
}

Player[] list = candidates.Skip(fixOffset(offset, candidates.Count())).Take(10).ToArray();
int pad = list[0].FormatPing().Length;
int pad = list[0].Ping.Format().Length;
player.Message("Ping/Latency List:");

for (int i = 0; i < list.Length; i++) {
player.Message(" &7{1}&S - {0}", list[i].Info.ClassyName,
list[i].FormatPing().PadLeft(pad, '0'));
list[i].Ping.Format().PadLeft(pad, '0'));
}
player.Message("Showing players {0}-{1} (out of {2}).", offset + 1, offset + list.Length, candidates.Count());
}
@@ -0,0 +1,81 @@
// ProCraft Copyright 2014-2016 Joseph Beauvais <123DMWM@gmail.com>
using System;

namespace fCraft {

public sealed class PingList {

public struct PingEntry {
public DateTime TimeSent, TimeReceived;
public ushort Data;
}
public PingEntry[] Entries = new PingEntry[10];


public ushort NextTwoWayPingData() {
// Find free ping slot
for (int i = 0; i < Entries.Length; i++) {
if (Entries[i].TimeSent.Ticks != 0) continue;

ushort prev = i > 0 ? Entries[i - 1].Data : (ushort)0;
return SetTwoWayPing(i, prev);
}

// Remove oldest ping slot
for (int i = 0; i < Entries.Length - 1; i++) {
Entries[i] = Entries[i + 1];
}
int j = Entries.Length - 1;
return SetTwoWayPing(j, Entries[j].Data);
}

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

public void Update(ushort data) {
for (int i = 0; i < Entries.Length; i++ ) {
if (Entries[i].Data != data) continue;
Entries[i].TimeReceived = DateTime.UtcNow;
return;
}
}


/// <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;

totalMs += (ping.TimeReceived - ping.TimeSent).TotalMilliseconds;
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;
totalMs = Math.Max(totalMs, ms);
}
return totalMs;
}

public string Format() {
return String.Format(" Worst ping {0}ms, average {1}ms",
WorstPingMilliseconds().ToString("N0"),
AveragePingMilliseconds().ToString("N0"));
}
}
}
@@ -158,7 +158,7 @@ public sealed partial class Player {

if( pingCounter > PingInterval ) {
if( Supports( CpeExt.TwoWayPing ) ) {
SendNow( Packet.MakeTwoWayPing( true, NextTwoWayPingData() ) );
SendNow( Packet.MakeTwoWayPing( true, Ping.NextTwoWayPingData() ) );
} else {
writer.Write( OpCode.Ping );
BytesSent++;
@@ -320,29 +320,7 @@ public sealed partial class Player {
}
}
#endregion

ushort NextTwoWayPingData() {
// Find free ping slot
for( int i = 0; i < PingList.Length; i++ ) {
if( PingList[i].TimeSent.Ticks != 0 ) continue;

ushort prev = i > 0 ? PingList[i - 1].Data : (ushort)0;
return SetTwoWayPing( i, prev );
}

// Remove oldest ping slot
for( int i = 0; i < PingList.Length - 1; i++ ) {
PingList[i] = PingList[i + 1];
}
int j = PingList.Length - 1;
return SetTwoWayPing( j, PingList[j].Data );
}

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


void ProcessPingPacket() {
BytesReceived++;
@@ -554,18 +532,13 @@ public sealed partial class Player {
bool serverToClient = reader.ReadByte() != 0;
ushort data = reader.ReadUInt16();
BytesReceived += 3;

// Client-> server ping, immediately reply.

if( !serverToClient ) {
// Client-> server ping, immediately send reply.
SendNow( Packet.MakeTwoWayPing( false, data ) );
return;
}

// Got a response for a server->client ping, set the time received.
for( int i = 0; i < PingList.Length; i++ ) {
if( PingList[i].Data != data ) continue;
PingList[i].TimeReceived = DateTime.UtcNow;
break;
} else {
// Server -> client ping, set time received for reply.
Ping.Update(data);
}
}

@@ -164,11 +164,7 @@ public sealed partial class Player : IClassy {
internal int currentBDStep = -1;
internal BlockDefinition currentBD;

public struct Ping {
public DateTime TimeSent, TimeReceived;
public ushort Data;
}
public Ping[] PingList = new Ping[20];
public PingList Ping = new PingList();

public string LastMotdMessage;
public Player LastPlayerGreeted = Player.Console;
@@ -2175,7 +2171,6 @@ public void ResetIdleTimer()
#endregion



#region Kick

/// <summary> Advanced kick command. </summary>
@@ -2257,42 +2252,6 @@ public void ResetIdleTimer()

#endregion



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

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

totalMs += (ping.TimeReceived - ping.TimeSent).TotalMilliseconds;
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 (Ping ping in PingList) {
if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue;

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

public string FormatPing() {
return String.Format(" Worst ping {0}ms, average {1}ms",
WorstPingMilliseconds().ToString("N0"),
AveragePingMilliseconds().ToString("N0"));
}


[CanBeNull]
public string LastUsedPlayerName { get; set; }
@@ -150,6 +150,7 @@
<Compile Include="Network\IRCHandlers.cs" />
<Compile Include="Network\LevelChunkStream.cs" />
<Compile Include="Network\Packet.CPE.cs" />
<Compile Include="Network\PingList.cs" />
<Compile Include="Network\Player.Handshake.cs" />
<Compile Include="Player\ChatFilter.cs" />
<Compile Include="Player\PlayerHacks.cs" />

0 comments on commit 032ac9e

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