From 597853104507ca68350b3ffca3778834bd5f676b Mon Sep 17 00:00:00 2001 From: Daniel Chalmers Date: Wed, 4 Nov 2026 13:45:01 -0600 Subject: [PATCH] Check whether the router answers during outages - Add RouterCheck, which pings the IPv4 gateway while internet pings fail, one ping at a time - Add a router line to the latency tooltip, Copy and screen reader text while the clock is up - Skip the check when there's no IPv4 gateway --- Network Monitor.Tests/RouterCheckTests.cs | 81 ++++++++++++++++ Network Monitor/Monitors/LatencyMonitor.cs | 39 +++++++- Network Monitor/Monitors/NetworkStatus.cs | 6 ++ Network Monitor/Monitors/RouterCheck.cs | 104 +++++++++++++++++++++ README.md | 2 +- 5 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 Network Monitor.Tests/RouterCheckTests.cs create mode 100644 Network Monitor/Monitors/RouterCheck.cs diff --git a/Network Monitor.Tests/RouterCheckTests.cs b/Network Monitor.Tests/RouterCheckTests.cs new file mode 100644 index 0000000..98fda78 --- /dev/null +++ b/Network Monitor.Tests/RouterCheckTests.cs @@ -0,0 +1,81 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Network_Monitor.Monitors; + +namespace Network_Monitor.Tests; + +public class RouterCheckTests +{ + private const string Router = "192.168.1.1"; + + /// + /// Pings that finish when told to, so each test decides how the router answers. + /// + private readonly Queue> _pings = new(); + + private RouterCheck CreateCheck() => new(_ => + { + var ping = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _pings.Enqueue(ping); + return ping.Task; + }); + + private async Task AnswerAsync(bool answered) + { + _pings.Dequeue().SetResult(answered); + + // The reply is noted on another thread. + await Task.Delay(50); + } + + [Fact] + public void Check_WithNoRouter_SaysNothing() + { + var check = CreateCheck(); + + Assert.Null(check.Check(null)); + Assert.Empty(_pings); + } + + [Fact] + public void Check_BeforeTheFirstReply_SaysItsChecking() + { + var check = CreateCheck(); + + Assert.Equal("Checking your router…", check.Check(Router)?.Note); + } + + [Fact] + public async Task Check_WhenTheRouterAnswers_SaysTheProblemIsPastIt() + { + var check = CreateCheck(); + + check.Check(Router); + await AnswerAsync(true); + + Assert.Equal("Your router is fine; the problem is past it", check.Check(Router)?.Note); + } + + [Fact] + public async Task Check_WhenTheRouterDoesntAnswer_SaysSo() + { + var check = CreateCheck(); + + check.Check(Router); + await AnswerAsync(false); + + Assert.Equal("Your router isn't answering", check.Check(Router)?.Note); + } + + [Fact] + public void Check_WhileAPingIsWaiting_DoesntSendAnother() + { + var check = CreateCheck(); + + check.Check(Router); + check.Check(Router); + check.Check(Router); + + Assert.Single(_pings); + } +} diff --git a/Network Monitor/Monitors/LatencyMonitor.cs b/Network Monitor/Monitors/LatencyMonitor.cs index 70f5d8a..e47b8ba 100644 --- a/Network Monitor/Monitors/LatencyMonitor.cs +++ b/Network Monitor/Monitors/LatencyMonitor.cs @@ -32,6 +32,11 @@ public class LatencyMonitor : Monitor /// private readonly ConcurrentBag _idlePings = new(); + /// + /// Pings the router while the internet isn't answering, to tell whether an outage is on your own network or past it. + /// + private readonly RouterCheck _routerCheck; + private int _pingsOut; /// @@ -49,6 +54,11 @@ public class LatencyMonitor : Monitor /// private (DateTime Start, DateTime End)? _lastOutage; + /// + /// What the latest tick found out about the router, said along with the outage clock, or null. + /// + private (string Note, string Spoken)? _routerNote; + public LatencyMonitor(string host, TimeSpan timeout) : base(true) { Name = "Latency"; @@ -61,6 +71,7 @@ public LatencyMonitor(string host, TimeSpan timeout) : base(true) _host = host; _timeout = (int)timeout.TotalMilliseconds; _maxPingsOut = (int)Math.Ceiling(timeout.TotalSeconds) + 2; + _routerCheck = new RouterCheck(timeout); } protected override string GetDisplayValue() @@ -85,6 +96,7 @@ protected override string GetDisplayValue() StartPing(); RememberOutage(view, snapshot.LastReplyAt, now); + _routerNote = CheckRouter(view); _view = view; IsStale = view.IsStale; return view.Value; @@ -107,6 +119,9 @@ protected override string GetDetails() if (view.NoteLine is not null) lines.Add(view.NoteLine); + if (_routerNote is var (routerNote, _)) + lines.Add(routerNote); + if (successes.Length > 0) lines.Add($"Min/Avg/Max: {FormatRoundtrip(successes.Min())} / {FormatRoundtrip((long)Math.Round(successes.Average(), MidpointRounding.AwayFromZero))} / {FormatRoundtrip(successes.Max())} ms"); @@ -157,6 +172,21 @@ private void RememberOutage(LatencyView view, DateTime? lastReplyAt, DateTime no } } + /// + /// Pings the router while the outage clock is up, and returns what it showed. + /// Not on a network that seems to block ping, where the internet never answers and the router would be pinged for good. + /// + private (string Note, string Spoken)? CheckRouter(LatencyView view) + { + if (!view.IsOutage) + { + _routerCheck.Stop(); + return null; + } + + return _routerCheck.Check(NetworkStatus.Router); + } + protected override IReadOnlyList GetHistory() => LatencyHistory.GetFinished(_history.GetSnapshot().Slots).Select(s => s >= 0 ? (double?)s : null).ToArray(); @@ -221,8 +251,13 @@ private async void SendPing(LatencyHistory.Ticket ticket) } } - protected override string GetSpokenValue(string displayValue) => - displayValue == _view.Value ? $"{Name}, {_view.Spoken}" : base.GetSpokenValue(displayValue); + protected override string GetSpokenValue(string displayValue) + { + if (displayValue != _view.Value) + return base.GetSpokenValue(displayValue); + + return _routerNote is var (_, routerSpoken) ? $"{Name}, {_view.Spoken}, {routerSpoken}" : $"{Name}, {_view.Spoken}"; + } /// /// Returns a latency reading as a screen reader should say it. diff --git a/Network Monitor/Monitors/NetworkStatus.cs b/Network Monitor/Monitors/NetworkStatus.cs index 60c0e4b..78fde18 100644 --- a/Network Monitor/Monitors/NetworkStatus.cs +++ b/Network Monitor/Monitors/NetworkStatus.cs @@ -104,6 +104,12 @@ public static bool IsReconnecting } } + /// + /// The IPv4 address of the router to ping when the internet stops answering, to tell a drop on your own network from one past it: the router of the adapter internet traffic leaves through. + /// Null when there isn't one, like with no route to the internet or on some mobile broadband. + /// + public static string Router => _currentNetwork.FirstOrDefault(x => x.IsIPv4)?.Gateway; + /// /// Whether a ping has ever come back through the network internet traffic leaves through now. /// If so, failing pings are an outage; if not, the network may just block ping, like many work VPNs and public Wi-Fi. diff --git a/Network Monitor/Monitors/RouterCheck.cs b/Network Monitor/Monitors/RouterCheck.cs new file mode 100644 index 0000000..5e9a4f6 --- /dev/null +++ b/Network Monitor/Monitors/RouterCheck.cs @@ -0,0 +1,104 @@ +using System; +using System.Net.NetworkInformation; +using System.Threading.Tasks; + +namespace Network_Monitor.Monitors; + +/// +/// Pings the router during an outage, which tells whether a drop is on your own network or past it, like at your provider. +/// +/// +/// Nothing is sent while the internet answers, and during an outage the router gets one ping at a time, at most one a second. +/// +public sealed class RouterCheck +{ + private readonly Func> _ping; + private readonly object _lock = new(); + + /// + /// Whether the router's latest ping during this outage came back, or null if none has finished yet. + /// + private bool? _answered; + + private bool _isOut; + + /// How long to wait for each reply. + public RouterCheck(TimeSpan timeout) : this(address => PingAsync(address, (int)timeout.TotalMilliseconds)) + { + } + + /// Sends a ping and returns whether it came back, in place of a real one. + public RouterCheck(Func> ping) + { + _ping = ping; + } + + /// + /// Pings unless it's still waiting for a reply, and returns a tooltip line about it and what a screen reader says. + /// Called on the clock tick during an outage. + /// + /// The IPv4 address of the router internet traffic leaves through, or null if there's none. + /// Null when there's no router to check. + public (string Note, string Spoken)? Check(string router) + { + if (router is null) + return null; + + lock (_lock) + { + if (!_isOut) + { + _isOut = true; + _ = SendAsync(router); + } + + return _answered switch + { + // Not "the internet", since 8.8.8.8 itself may be what's down. + true => ("Your router is fine; the problem is past it", "your router is fine, the problem is past it"), + false => ("Your router isn't answering", "your router isn't answering"), + null => ("Checking your router…", "checking your router"), + }; + } + } + + /// + /// Forgets how the router answered during this outage, once the internet answers again, so the next one starts fresh. + /// + public void Stop() + { + lock (_lock) + _answered = null; + } + + /// + /// Pings the router and notes how it went. + /// It never fails, so nothing waits for the task it returns. + /// + private async Task SendAsync(string router) + { + var answered = false; + + try + { + answered = await _ping(router).ConfigureAwait(false); + } + catch + { + // A ping that can't be sent, like with no route to the router, got no answer. + } + + lock (_lock) + { + _isOut = false; + _answered = answered; + } + } + + private static async Task PingAsync(string address, int timeout) + { + using var ping = new Ping(); + var reply = await ping.SendPingAsync(address, timeout).ConfigureAwait(false); + return reply.Status == IPStatus.Success; + } +} diff --git a/README.md b/README.md index 9bf3b00..5ddfc2d 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Hover for a 60-second sparkline and the stats behind it: min/avg/max, jitter, pa - **Sharp on every monitor:** stays crisp on monitors with different scaling, like a laptop beside a monitor. - **Copy on demand:** double-click, or right-click → **Copy**, to grab the current numbers. - **Starts with Windows:** optionally launch on sign-in, and check for updates in one click from the menu. -- **Honest outages:** when pings stop, a clock counts the time since the last reply; hover for when it started. +- **Honest outages:** a clock counts from the last reply, and hover says whether your router still answers. - **Light on resources:** one tiny reading per second, published exactly when the system clock ticks. ## 🚀 Install