diff options
author | Max Kellermann <max@musicpd.org> | 2021-05-26 11:57:41 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2021-05-26 11:57:41 +0200 |
commit | d2bd12822f6672f0a43f201c46764325ff537b71 (patch) | |
tree | 41f20274bf47876a7fe5492c0894bbc497c53a5f /src/zeroconf | |
parent | 8a9d678bac2fa23af850343975059297bcbbae2a (diff) | |
parent | 7a6823dcdfc7056d3c0e93f31aea49a99a2fddd9 (diff) |
Merge branch 'v0.22.x'
Diffstat (limited to 'src/zeroconf')
-rw-r--r-- | src/zeroconf/avahi/Poll.cxx | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/zeroconf/avahi/Poll.cxx b/src/zeroconf/avahi/Poll.cxx index 0cec88f17..1d0f39895 100644 --- a/src/zeroconf/avahi/Poll.cxx +++ b/src/zeroconf/avahi/Poll.cxx @@ -89,13 +89,13 @@ public: :event(_loop, BIND_THIS_METHOD(OnTimeout)), callback(_callback), userdata(_userdata) { if (tv != nullptr) - event.Schedule(ToSteadyClockDuration(*tv)); + Schedule(*tv); } static void TimeoutUpdate(AvahiTimeout *t, const struct timeval *tv) noexcept { if (tv != nullptr) - t->event.Schedule(ToSteadyClockDuration(*tv)); + t->Schedule(*tv); else t->event.Cancel(); } @@ -105,6 +105,30 @@ public: } private: + [[gnu::pure]] + Event::Duration AbsoluteToDuration(const struct timeval &tv) noexcept { + if (tv.tv_sec == 0) + /* schedule immediately */ + return {}; + + struct timeval now; + if (gettimeofday(&now, nullptr) < 0) + /* shouldn't ever fail, but if it does, do + something reasonable */ + return std::chrono::seconds(1); + + auto d = ToSteadyClockDuration(tv) + - ToSteadyClockDuration(now); + if (d.count() < 0) + return {}; + + return d; + } + + void Schedule(const struct timeval &tv) noexcept { + event.Schedule(AbsoluteToDuration(tv)); + } + void OnTimeout() noexcept { callback(this, userdata); } |