Skip to content

Commit 20d747b

Browse files
committed
add doxygen
1 parent 156c083 commit 20d747b

1 file changed

Lines changed: 89 additions & 5 deletions

File tree

include/vix/net/NetworkProbe.hpp

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* @file NetworkProbe.hpp
44
* @author Gaspard Kirira
55
*
6-
* Copyright 2025, Gaspard Kirira. All rights reserved.
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
78
* https://github.com/vixcpp/vix
9+
*
810
* Use of this source code is governed by a MIT license
911
* that can be found in the License file.
1012
*
@@ -18,38 +20,120 @@
1820

1921
namespace vix::net
2022
{
21-
23+
/**
24+
* @brief Lightweight network connectivity probe with caching.
25+
*
26+
* NetworkProbe encapsulates a user-provided probe function and adds:
27+
* - rate limiting (minimum probe interval)
28+
* - cached online/offline state with separate TTLs
29+
* - monotonic-time based decisions
30+
*
31+
* It is designed to be used by offline-first systems to avoid excessive
32+
* network checks while still reacting quickly to connectivity changes.
33+
*/
2234
class NetworkProbe
2335
{
2436
public:
37+
/**
38+
* @brief Probe function type.
39+
*
40+
* The function should return true if the network is considered online,
41+
* false otherwise.
42+
*/
2543
using ProbeFn = std::function<bool()>; // returns true if online
2644

45+
/**
46+
* @brief Configuration for the network probe.
47+
*/
2748
struct Config
2849
{
50+
/**
51+
* @brief Minimum interval between two actual probe executions.
52+
*/
2953
std::int64_t min_interval_ms{1000};
30-
std::int64_t online_ttl_ms{2000}; // cached online validity
31-
std::int64_t offline_ttl_ms{500}; // cached offline validity
54+
55+
/**
56+
* @brief Time-to-live for a cached "online" result.
57+
*/
58+
std::int64_t online_ttl_ms{2000};
59+
60+
/**
61+
* @brief Time-to-live for a cached "offline" result.
62+
*/
63+
std::int64_t offline_ttl_ms{500};
3264
};
3365

66+
/**
67+
* @brief Construct a NetworkProbe.
68+
*
69+
* @param cfg Probe configuration.
70+
* @param fn Probe function invoked to test connectivity.
71+
*/
3472
NetworkProbe(Config cfg, ProbeFn fn);
3573

74+
/**
75+
* @brief Check whether the network is currently considered online.
76+
*
77+
* This method may return a cached result if the TTL has not expired.
78+
*
79+
* @param now_ms Current time in milliseconds.
80+
* @return true if online, false otherwise.
81+
*/
3682
bool isOnline(std::int64_t now_ms) const;
83+
84+
/**
85+
* @brief Force a probe refresh if allowed by rate limiting.
86+
*
87+
* @param now_ms Current time in milliseconds.
88+
* @return Latest online state.
89+
*/
3790
bool refresh(std::int64_t now_ms);
91+
92+
/**
93+
* @brief Return the last known online state.
94+
*
95+
* This does not trigger a probe.
96+
*/
3897
bool lastKnownOnline() const noexcept { return last_online_; }
98+
99+
/**
100+
* @brief Timestamp of the last probe attempt.
101+
*/
39102
std::int64_t lastProbeAtMs() const noexcept { return last_probe_at_ms_; }
40103

41104
private:
105+
/**
106+
* @brief Check whether a new probe is allowed at the given time.
107+
*/
42108
bool canProbe(std::int64_t now_ms) const noexcept;
43109

44110
private:
111+
/**
112+
* @brief Stored probe configuration.
113+
*/
45114
Config cfg_;
115+
116+
/**
117+
* @brief User-provided probe function.
118+
*/
46119
ProbeFn probe_;
47120

121+
/**
122+
* @brief Last known online state.
123+
*/
48124
bool last_online_{false};
125+
126+
/**
127+
* @brief Timestamp of the last probe execution.
128+
*/
49129
std::int64_t last_probe_at_ms_{0};
130+
131+
/**
132+
* @brief Timestamp of the last cached state update.
133+
*/
50134
std::int64_t last_update_ms_{0};
51135
};
52136

53137
} // namespace vix::net
54138

55-
#endif
139+
#endif // VIX_NETWORK_PROBE_HPP

0 commit comments

Comments
 (0)