Skip to content

Commit

Permalink
Add IpHlpApi tracker, modify base PingTracker (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
karashiiro committed Feb 1, 2022
1 parent 7fbe2b4 commit 0bc725d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
20 changes: 15 additions & 5 deletions PingPlugin/PingTrackers/AggregatePingTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,31 @@ namespace PingPlugin.PingTrackers
{
public class AggregatePingTracker : PingTracker
{
private const string COMTrackerKey = "COM";
private const string IpHlpApiTrackerKey = "IpHlpApi";

private readonly IDictionary<string, TrackerInfo> trackerInfos;
private readonly DecisionTree<TrackerInfo> decisionTree;

public AggregatePingTracker(PingConfiguration config, ClientState clientState) : base(config, clientState)
{
// Define trackers
this.trackerInfos = new Dictionary<string, TrackerInfo>();

var comPing = new ComponentModelPingTracker(config, clientState) { Verbose = false };
RegisterTracker("COM", comPing);
RegisterTracker(COMTrackerKey, new ComponentModelPingTracker(config, clientState) { Verbose = false });
RegisterTracker(IpHlpApiTrackerKey, new IpHlpApiPingTracker(config, clientState) { Verbose = false });

// Create decision tree to solve tracker selection problem
this.decisionTree = new DecisionTree<TrackerInfo>(
() => TreeResult<TrackerInfo>.FromObject(this.trackerInfos["COM"]));
() =>
{
if (this.trackerInfos[COMTrackerKey].LastRTT < this.trackerInfos[IpHlpApiTrackerKey].LastRTT)
{
return TreeResult<TrackerInfo>.FromObject(this.trackerInfos[COMTrackerKey]);
}

return TreeResult<TrackerInfo>.FromObject(this.trackerInfos[IpHlpApiTrackerKey]);
});
}

protected override async Task PingLoop(CancellationToken token)
Expand All @@ -35,7 +46,6 @@ protected override async Task PingLoop(CancellationToken token)
{
// Process result
NextRTTCalculation(bestTracker.LastRTT);
SendMessage();
}
}

Expand Down
3 changes: 2 additions & 1 deletion PingPlugin/PingTrackers/ComponentModelPingTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ protected override async Task PingLoop(CancellationToken token)
{
var pingReply = await this.ping.SendPingAsync(SeAddress);
if (pingReply.Status == IPStatus.Success && pingReply.RoundtripTime > 0)
{
NextRTTCalculation((ulong)pingReply.RoundtripTime);
SendMessage();
}
}

await Task.Delay(3000, token);
Expand Down
70 changes: 70 additions & 0 deletions PingPlugin/PingTrackers/IpHlpApiPingTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Dalamud.Game.ClientState;

namespace PingPlugin.PingTrackers
{
public class IpHlpApiPingTracker : PingTracker
{
public IpHlpApiPingTracker(PingConfiguration config, ClientState clientState) : base(config, clientState)
{
}

protected override async Task PingLoop(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
if (SeAddress != null)
{
var addressRaw = BitConverter.ToUInt64(SeAddress.GetAddressBytes());
var rtt = GetAddressLastRTT(addressRaw);
var error = (WinError)Marshal.GetLastWin32Error();
if (error == WinError.NO_ERROR)
{
NextRTTCalculation(rtt);
}
}

await Task.Delay(3000, token);
}
}

private static ulong GetAddressLastRTT(ulong address)
{
var hopCount = 0UL;
var rtt = 0UL;
GetRTTAndHopCount(address, ref hopCount, 51, ref rtt);
return rtt;
}

[DllImport("Iphlpapi.dll", EntryPoint = "GetRTTAndHopCount", SetLastError = true)]
private static extern ulong GetRTTAndHopCount(ulong address, ref ulong hopCount, ulong maxHops, ref ulong rtt);

private enum WinError
{
UNKNOWN = -1,
NO_ERROR = 0,
ACCESS_DENIED = 5,
NOT_ENOUGH_MEMORY = 8,
OUTOFMEMORY = 14,
NOT_SUPPORTED = 50,
INVALID_PARAMETER = 87,
ERROR_INVALID_NETNAME = 1214,
WSAEINTR = 10004,
WSAEACCES = 10013,
WSAEFAULT = 10014,
WSAEINVAL = 10022,
WSAEWOULDBLOCK = 10035,
WSAEINPROGRESS = 10036,
WSAEALREADY = 10037,
WSAENOTSOCK = 10038,
WSAENETUNREACH = 10051,
WSAENETRESET = 10052,
WSAECONNABORTED = 10053,
WSAECONNRESET = 10054,
IP_REQ_TIMED_OUT = 11010,
}
}
}
3 changes: 2 additions & 1 deletion PingPlugin/PingTrackers/PingTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected void NextRTTCalculation(ulong nextRTT)
CalcAverage();

LastRTT = nextRTT;
SendMessage();
}

protected void CalcAverage() => AverageRTT = RTTTimes.Average();
Expand Down Expand Up @@ -80,7 +81,7 @@ private async Task AddressUpdateLoop(CancellationToken token)
}
}

protected void SendMessage()
private void SendMessage()
{
var del = OnPingUpdated;
del?.Invoke(new PingStatsPayload
Expand Down

0 comments on commit 0bc725d

Please sign in to comment.