Skip to content

Commit

Permalink
Replace RouteTracer implementation with NetworkManager
Browse files Browse the repository at this point in the history
  • Loading branch information
kenelin committed Sep 13, 2021
1 parent a07ca00 commit 83b662f
Showing 1 changed file with 24 additions and 40 deletions.
64 changes: 24 additions & 40 deletions source/Htc.Vita.Core/Net/RouteTracer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.NetworkInformation;
using Htc.Vita.Core.Log;
using System.Threading;

namespace Htc.Vita.Core.Net
{
Expand Down Expand Up @@ -47,48 +45,34 @@ public static partial class RouteTracer
currentTimeoutInMilli = 1;
}

var pingOptions = new PingOptions(
1,
true
var traceRouteResult = NetworkManager.GetInstance().TraceRoute(
hostNameOrIpAddress,
currentMaxHop,
currentTimeoutInMilli,
CancellationToken.None
);
var pingReplyTime = new Stopwatch();
var traceRouteStatus = traceRouteResult.Status;
if (traceRouteStatus != NetworkManager.TraceRouteStatus.Ok)
{
return result;
}

try
var hops = traceRouteResult.Route.Hops;
foreach (var routeHopInfo in hops)
{
using (var ping = new Ping())
if (routeHopInfo == null)
{
while (true)
{
pingReplyTime.Start();
var reply = ping.Send(
hostNameOrIpAddress,
currentTimeoutInMilli,
new byte[] { 0 },
pingOptions
);
pingReplyTime.Stop();

result.Add(new Hop
{
Node = pingOptions.Ttl,
IP = reply?.Address?.ToString(),
Hostname = Dns.GetInstance().GetHostEntry(reply?.Address)?.HostName,
Time = pingReplyTime.ElapsedMilliseconds,
Status = reply?.Status.ToString()
});

pingOptions.Ttl++;
pingReplyTime.Reset();
if (pingOptions.Ttl > currentMaxHop || IPStatus.Success == reply?.Status)
{
break;
}
}
continue;
}
}
catch (Exception e)
{
Logger.GetInstance(typeof(RouteTracer)).Error($"Can not trace route: {e.Message}");

result.Add(new Hop
{
Hostname = routeHopInfo.Hostname,
IP = routeHopInfo.Address?.ToString(),
Node = routeHopInfo.Node,
Status = routeHopInfo.Status.ToString(),
Time = routeHopInfo.TimeInMilli
});
}

return result;
Expand Down

0 comments on commit 83b662f

Please sign in to comment.