Remove function event_timeout_clear_ret as it is unused.
Cleanup event_timeout_trigger a bit. Do an instant return false if the
timeout is not defined and inline local_now and use
event_timeout_remaining instead of local duplicated code.
Add doxygen comments for all timeout function, especially for the
event_timeout_trigger function that is hard to understand otherwise.
Patch v2: add many fixes/correction suggested by Frank
Signed-off-by: Arne Schwabe
---
src/openvpn/interval.c | 50 ++--
src/openvpn/interval.h | 103 +
src/openvpn/openvpn.h | 4 +-
3 files changed, 102 insertions(+), 55 deletions(-)
diff --git a/src/openvpn/interval.c b/src/openvpn/interval.c
index 2f0fc42dd..8f3c1c604 100644
--- a/src/openvpn/interval.c
+++ b/src/openvpn/interval.c
@@ -41,44 +41,46 @@ interval_init(struct interval *top, int horizon, int
refresh)
top->horizon = horizon;
}
+
bool
event_timeout_trigger(struct event_timeout *et,
struct timeval *tv,
const int et_const_retry)
{
+if (!et->defined)
+{
+return false;
+}
+
bool ret = false;
-const time_t local_now = now;
+time_t wakeup = event_timeout_remaining(et);
-if (et->defined)
+if (wakeup <= 0)
{
-time_t wakeup = et->last - local_now + et->n;
-if (wakeup <= 0)
-{
#if INTERVAL_DEBUG
-dmsg(D_INTERVAL, "EVENT event_timeout_trigger (%d) etcr=%d", et->n,
- et_const_retry);
+dmsg(D_INTERVAL, "EVENT event_timeout_trigger (%d) etcr=%d", et->n,
+ et_const_retry);
#endif
-if (et_const_retry < 0)
-{
-et->last = local_now;
-wakeup = et->n;
-ret = true;
-}
-else
-{
-wakeup = et_const_retry;
-}
+if (et_const_retry < 0)
+{
+et->last = now;
+wakeup = et->n;
+ret = true;
}
-
-if (tv && wakeup < tv->tv_sec)
+else
{
+wakeup = et_const_retry;
+}
+}
+
+if (tv && wakeup < tv->tv_sec)
+{
#if INTERVAL_DEBUG
-dmsg(D_INTERVAL, "EVENT event_timeout_wakeup (%d/%d) etcr=%d",
- (int) wakeup, et->n, et_const_retry);
+dmsg(D_INTERVAL, "EVENT event_timeout_wakeup (%d/%d) etcr=%d",
+ (int) wakeup, et->n, et_const_retry);
#endif
-tv->tv_sec = wakeup;
-tv->tv_usec = 0;
-}
+tv->tv_sec = wakeup;
+tv->tv_usec = 0;
}
return ret;
}
diff --git a/src/openvpn/interval.h b/src/openvpn/interval.h
index f58bfacf6..e6f0bece1 100644
--- a/src/openvpn/interval.h
+++ b/src/openvpn/interval.h
@@ -135,9 +135,9 @@ interval_action(struct interval *top)
struct event_timeout
{
-bool defined;
-interval_t n;
-time_t last; /* time of last event */
+bool defined; /**< This timeout is active */
+interval_t n; /**< periodic interval for periodic timeouts */
+time_t last;/**< time of last event */
};
static inline bool
@@ -145,7 +145,12 @@ event_timeout_defined(const struct event_timeout *et)
{
return et->defined;
}
-
+/**
+ * Clears the timeout and reset all values to 0. Following timer checks will
+ * not trigger.
+ *
+ * @param ettimer struct
+ */
static inline void
event_timeout_clear(struct event_timeout *et)
{
@@ -154,22 +159,32 @@ event_timeout_clear(struct event_timeout *et)
et->last = 0;
}
-static inline struct event_timeout
-event_timeout_clear_ret(void)
-{
-struct event_timeout ret;
-event_timeout_clear();
-return ret;
-}
+/**
+ * Initialises a timer struct. The timer will become true/trigger after
+ * last + n seconds.
+ *
+ *
+ * @param etTimer struct
+ * @param n Interval of the timer for periodic timer. A negative
+ * value for n will be interpreted as 0.
+ * @param last Sets the base time of the timer.
+ */
static inline void
-event_timeout_init(struct event_timeout *et, interval_t n, const time_t
local_now)
+event_timeout_init(struct event_timeout *et, interval_t n, const time_t last)
{
et->defined = true;
et->n = (n >= 0) ? n : 0;
-et->last = local_now;
+et->last = last;
}
+/**
+ * Resets a timer.
+ *
+ * Sets the last time the timer has been triggered for the calculation of the
+ * next event.
+ * @param et
+ */
static inline void
event_timeout_reset(struct event_timeout *et)
{
@@ -179,42 +194,70 @@ event_timeout_reset(struct event_timeout *et)
}
}
+/**
+ * Sets the interval n of a timeout.
+ * @param et
+ * @param n Interval value of the timer, negative values
+ * will be interpreted as 0
+ *
+ * @note you might need to call reset_coarse_timers after this
+ */
static inline void
event_timeout_modify_wakeup(struct event_timeout *et,