Skip to content

Commit 032ac9e

Browse files
Move two way ping code to separate class, reduce ping entries to 10.
1 parent e77633a commit 032ac9e

File tree

5 files changed

+96
-82
lines changed

5 files changed

+96
-82
lines changed

fCraft/Commands/InfoCommands.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,8 +1782,8 @@ private static void ExtraInfoHandler(Player player, CommandReader cmd) {
17821782
}
17831783
}
17841784

1785-
if (target != null && target.AveragePingMilliseconds() != 0) {
1786-
player.Message(target.FormatPing());
1785+
if (target != null && target.Ping.AveragePingMilliseconds() != 0) {
1786+
player.Message(target.Ping.Format());
17871787
}
17881788
}
17891789

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

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

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

21132113
for (int i = 0; i < list.Length; i++) {
21142114
player.Message(" &7{1}&S - {0}", list[i].Info.ClassyName,
2115-
list[i].FormatPing().PadLeft(pad, '0'));
2115+
list[i].Ping.Format().PadLeft(pad, '0'));
21162116
}
21172117
player.Message("Showing players {0}-{1} (out of {2}).", offset + 1, offset + list.Length, candidates.Count());
21182118
}

fCraft/Network/PingList.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// ProCraft Copyright 2014-2016 Joseph Beauvais <123DMWM@gmail.com>
2+
using System;
3+
4+
namespace fCraft {
5+
6+
public sealed class PingList {
7+
8+
public struct PingEntry {
9+
public DateTime TimeSent, TimeReceived;
10+
public ushort Data;
11+
}
12+
public PingEntry[] Entries = new PingEntry[10];
13+
14+
15+
public ushort NextTwoWayPingData() {
16+
// Find free ping slot
17+
for (int i = 0; i < Entries.Length; i++) {
18+
if (Entries[i].TimeSent.Ticks != 0) continue;
19+
20+
ushort prev = i > 0 ? Entries[i - 1].Data : (ushort)0;
21+
return SetTwoWayPing(i, prev);
22+
}
23+
24+
// Remove oldest ping slot
25+
for (int i = 0; i < Entries.Length - 1; i++) {
26+
Entries[i] = Entries[i + 1];
27+
}
28+
int j = Entries.Length - 1;
29+
return SetTwoWayPing(j, Entries[j].Data);
30+
}
31+
32+
ushort SetTwoWayPing(int i, ushort prev) {
33+
Entries[i].Data = (ushort)(prev + 1);
34+
Entries[i].TimeSent = DateTime.UtcNow;
35+
return (ushort)(prev + 1);
36+
}
37+
38+
public void Update(ushort data) {
39+
for (int i = 0; i < Entries.Length; i++ ) {
40+
if (Entries[i].Data != data) continue;
41+
Entries[i].TimeReceived = DateTime.UtcNow;
42+
return;
43+
}
44+
}
45+
46+
47+
/// <summary> Gets average ping in milliseconds, or 0 if no ping measures. </summary>
48+
public double AveragePingMilliseconds() {
49+
double totalMs = 0;
50+
int measures = 0;
51+
52+
foreach (PingEntry ping in Entries) {
53+
if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue;
54+
55+
totalMs += (ping.TimeReceived - ping.TimeSent).TotalMilliseconds;
56+
measures++;
57+
}
58+
return measures == 0 ? 0 : (totalMs / measures);
59+
}
60+
61+
62+
/// <summary> Gets worst ping in milliseconds, or 0 if no ping measures. </summary>
63+
public double WorstPingMilliseconds() {
64+
double totalMs = 0;
65+
66+
foreach (PingEntry ping in Entries) {
67+
if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue;
68+
69+
double ms = (ping.TimeReceived - ping.TimeSent).TotalMilliseconds;
70+
totalMs = Math.Max(totalMs, ms);
71+
}
72+
return totalMs;
73+
}
74+
75+
public string Format() {
76+
return String.Format(" Worst ping {0}ms, average {1}ms",
77+
WorstPingMilliseconds().ToString("N0"),
78+
AveragePingMilliseconds().ToString("N0"));
79+
}
80+
}
81+
}

fCraft/Network/Player.Networking.cs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void IoLoop() {
158158

159159
if( pingCounter > PingInterval ) {
160160
if( Supports( CpeExt.TwoWayPing ) ) {
161-
SendNow( Packet.MakeTwoWayPing( true, NextTwoWayPingData() ) );
161+
SendNow( Packet.MakeTwoWayPing( true, Ping.NextTwoWayPingData() ) );
162162
} else {
163163
writer.Write( OpCode.Ping );
164164
BytesSent++;
@@ -320,29 +320,7 @@ void IoLoop() {
320320
}
321321
}
322322
#endregion
323-
324-
ushort NextTwoWayPingData() {
325-
// Find free ping slot
326-
for( int i = 0; i < PingList.Length; i++ ) {
327-
if( PingList[i].TimeSent.Ticks != 0 ) continue;
328-
329-
ushort prev = i > 0 ? PingList[i - 1].Data : (ushort)0;
330-
return SetTwoWayPing( i, prev );
331-
}
332-
333-
// Remove oldest ping slot
334-
for( int i = 0; i < PingList.Length - 1; i++ ) {
335-
PingList[i] = PingList[i + 1];
336-
}
337-
int j = PingList.Length - 1;
338-
return SetTwoWayPing( j, PingList[j].Data );
339-
}
340-
341-
ushort SetTwoWayPing( int i, ushort prev ) {
342-
PingList[i].Data = (ushort)(prev + 1);
343-
PingList[i].TimeSent = DateTime.UtcNow;
344-
return (ushort)(prev + 1);
345-
}
323+
346324

347325
void ProcessPingPacket() {
348326
BytesReceived++;
@@ -554,18 +532,13 @@ void ProcessTwoWayPingPacket() {
554532
bool serverToClient = reader.ReadByte() != 0;
555533
ushort data = reader.ReadUInt16();
556534
BytesReceived += 3;
557-
558-
// Client-> server ping, immediately reply.
535+
559536
if( !serverToClient ) {
537+
// Client-> server ping, immediately send reply.
560538
SendNow( Packet.MakeTwoWayPing( false, data ) );
561-
return;
562-
}
563-
564-
// Got a response for a server->client ping, set the time received.
565-
for( int i = 0; i < PingList.Length; i++ ) {
566-
if( PingList[i].Data != data ) continue;
567-
PingList[i].TimeReceived = DateTime.UtcNow;
568-
break;
539+
} else {
540+
// Server -> client ping, set time received for reply.
541+
Ping.Update(data);
569542
}
570543
}
571544

fCraft/Player/Player.cs

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,7 @@ public bool IsStaff {
164164
internal int currentBDStep = -1;
165165
internal BlockDefinition currentBD;
166166

167-
public struct Ping {
168-
public DateTime TimeSent, TimeReceived;
169-
public ushort Data;
170-
}
171-
public Ping[] PingList = new Ping[20];
167+
public PingList Ping = new PingList();
172168

173169
public string LastMotdMessage;
174170
public Player LastPlayerGreeted = Player.Console;
@@ -2175,7 +2171,6 @@ internal void CheckBlock(ref byte block) {
21752171
#endregion
21762172

21772173

2178-
21792174
#region Kick
21802175

21812176
/// <summary> Advanced kick command. </summary>
@@ -2257,42 +2252,6 @@ public void Kick( [NotNull] Player player, [CanBeNull] string reason, LeaveReaso
22572252

22582253
#endregion
22592254

2260-
2261-
2262-
/// <summary> Gets average ping in milliseconds, or 0 if no ping measures. </summary>
2263-
public double AveragePingMilliseconds() {
2264-
double totalMs = 0;
2265-
int measures = 0;
2266-
2267-
foreach (Ping ping in PingList) {
2268-
if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue;
2269-
2270-
totalMs += (ping.TimeReceived - ping.TimeSent).TotalMilliseconds;
2271-
measures++;
2272-
}
2273-
return measures == 0 ? 0 : (totalMs / measures);
2274-
}
2275-
2276-
2277-
/// <summary> Gets worst ping in milliseconds, or 0 if no ping measures. </summary>
2278-
public double WorstPingMilliseconds() {
2279-
double totalMs = 0;
2280-
2281-
foreach (Ping ping in PingList) {
2282-
if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue;
2283-
2284-
double ms = (ping.TimeReceived - ping.TimeSent).TotalMilliseconds;
2285-
totalMs = Math.Max(totalMs, ms);
2286-
}
2287-
return totalMs;
2288-
}
2289-
2290-
public string FormatPing() {
2291-
return String.Format(" Worst ping {0}ms, average {1}ms",
2292-
WorstPingMilliseconds().ToString("N0"),
2293-
AveragePingMilliseconds().ToString("N0"));
2294-
}
2295-
22962255

22972256
[CanBeNull]
22982257
public string LastUsedPlayerName { get; set; }

fCraft/fCraft.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
<Compile Include="Network\IRCHandlers.cs" />
151151
<Compile Include="Network\LevelChunkStream.cs" />
152152
<Compile Include="Network\Packet.CPE.cs" />
153+
<Compile Include="Network\PingList.cs" />
153154
<Compile Include="Network\Player.Handshake.cs" />
154155
<Compile Include="Player\ChatFilter.cs" />
155156
<Compile Include="Player\PlayerHacks.cs" />

0 commit comments

Comments
 (0)