|
morph 0.1.0
Asynchronous bridge between GUI thread and business models
|
Connectivity monitor that calls a probe on a timer and fires callbacks on state changes. More...
#include <network_monitor.hpp>
Public Types | |
| using | ProbeFunction = std::function<bool()> |
| Returns true when the network is reachable. | |
| using | Callback = std::function<void()> |
| Called on connectivity state change. | |
| using | Config = NetworkMonitorConfig |
| Alias for the configuration struct. | |
Public Member Functions | |
| NetworkMonitor (::morph::exec::IoLoop &loop, ProbeFunction probe, Callback onOffline, Callback onOnline, Config cfg=Config{}) | |
Starts the monitor on loop. | |
| NetworkMonitor (ProbeFunction probe, Callback onOffline, Callback onOnline, Config cfg=Config{}) | |
| Starts the monitor on a loop of its own, for a caller with no IoLoop to share. Natively that loop owns one thread. | |
| ~NetworkMonitor () | |
| Stops the monitor. Once this returns, no probe or callback of this monitor will run again, and from any thread but the loop's none is running. | |
| NetworkMonitor (const NetworkMonitor &)=delete | |
| NetworkMonitor & | operator= (const NetworkMonitor &)=delete |
| NetworkMonitor (NetworkMonitor &&)=delete | |
| NetworkMonitor & | operator= (NetworkMonitor &&)=delete |
| bool | isOnline () const noexcept |
| Returns true if the monitor currently considers the network reachable. | |
| void | stop () |
| Stops probing. Idempotent. | |
Connectivity monitor that calls a probe on a timer and fires callbacks on state changes.
The probe runs every probeInterval on an exec::IoLoop's thread, as a loop timer. The monitor starts in the online state and transitions to offline only after failureThreshold consecutive failures. It returns to online after onlineThreshold consecutive successes.
The probe and both callbacks run on the loop's thread and must not block it: every socket and timer on the same loop waits while they run. A probe that needs a slow check should start it elsewhere and report the last result it has.
Cross-thread surface: isOnline(), an atomic read, and stop() and the destructor, which run their close on the loop and wait for it. Every other field is touched only on the loop.
The monitor is non-copyable and non-movable. Destroy it to stop monitoring.
Definition at line 52 of file network_monitor.hpp.
| using morph::offline::NetworkMonitor::ProbeFunction = std::function<bool()> |
Returns true when the network is reachable.
Definition at line 55 of file network_monitor.hpp.
| using morph::offline::NetworkMonitor::Callback = std::function<void()> |
Called on connectivity state change.
Definition at line 58 of file network_monitor.hpp.
Alias for the configuration struct.
Definition at line 61 of file network_monitor.hpp.
|
inline |
Starts the monitor on loop.
The monitor starts in the online state. The first failureThreshold consecutive probe failures must occur before onOffline is invoked. Callers that know the initial state is offline should use a probe that starts returning false immediately.
| loop | The application's loop, whose thread runs the probe and the callbacks. Borrowed: it must outlive this monitor. |
| probe | Callable that tests connectivity. Must not throw (exceptions are swallowed) and must not block. Stored and invoked on the loop's thread for this monitor's whole lifetime, so anything the callable refers to must outlive the monitor. |
| onOffline | Called on the loop's thread when the monitor goes offline. Retained on the same terms as probe. |
| onOnline | Called on the loop's thread when the monitor comes back online. Retained on the same terms as probe. |
| cfg | Tuning parameters (interval, thresholds). |
Definition at line 82 of file network_monitor.hpp.
|
inline |
Starts the monitor on a loop of its own, for a caller with no IoLoop to share. Natively that loop owns one thread.
| probe | As for the other constructor. |
| onOffline | As for the other constructor. |
| onOnline | As for the other constructor. |
| cfg | Tuning parameters (interval, thresholds). |
Definition at line 97 of file network_monitor.hpp.
|
inline |
Stops the monitor. Once this returns, no probe or callback of this monitor will run again, and from any thread but the loop's none is running.
May run anywhere, including inside this monitor's own probe or callback: the timer that called it keeps the loop-side state alive until it has returned.
Definition at line 113 of file network_monitor.hpp.
|
inlinenodiscardnoexcept |
Returns true if the monitor currently considers the network reachable.
Reads an atomic flag — safe to call from any thread at any time.
Definition at line 125 of file network_monitor.hpp.
|
inline |
Stops probing. Idempotent.
Runs on the loop: inline when called there (from the probe or a callback, which then finishes normally, and no further probe runs), posted and waited for from any other thread, so that once it returns no probe or callback is running.
Definition at line 133 of file network_monitor.hpp.