From f2a848cbcc41f29fb62ee67aef95136ae1a650da Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Sun, 7 Apr 2024 03:31:51 +0100 Subject: core: Event loop rework (#5466) * Event loop rework * revert missed --- src/managers/eventLoop/EventLoopTimer.cpp | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/managers/eventLoop/EventLoopTimer.cpp (limited to 'src/managers/eventLoop/EventLoopTimer.cpp') diff --git a/src/managers/eventLoop/EventLoopTimer.cpp b/src/managers/eventLoop/EventLoopTimer.cpp new file mode 100644 index 00000000..ebfb696c --- /dev/null +++ b/src/managers/eventLoop/EventLoopTimer.cpp @@ -0,0 +1,53 @@ +#include "EventLoopTimer.hpp" +#include +#include "EventLoopManager.hpp" + +CEventLoopTimer::CEventLoopTimer(std::optional timeout, std::function self, void* data)> cb_, + void* data_) : + cb(cb_), + data(data_) { + + if (!timeout.has_value()) + expires.reset(); + else + expires = std::chrono::system_clock::now() + *timeout; +} + +void CEventLoopTimer::updateTimeout(std::optional timeout) { + if (!timeout.has_value()) { + expires.reset(); + g_pEventLoopManager->nudgeTimers(); + return; + } + + expires = std::chrono::system_clock::now() + *timeout; + + g_pEventLoopManager->nudgeTimers(); +} + +bool CEventLoopTimer::passed() { + if (!expires.has_value()) + return false; + return std::chrono::system_clock::now() > *expires; +} + +void CEventLoopTimer::cancel() { + wasCancelled = true; + expires.reset(); +} + +bool CEventLoopTimer::cancelled() { + return wasCancelled; +} + +void CEventLoopTimer::call(std::shared_ptr self) { + expires.reset(); + cb(self, data); +} + +float CEventLoopTimer::leftUs() { + if (!expires.has_value()) + return std::numeric_limits::max(); + + return std::chrono::duration_cast(*expires - std::chrono::system_clock::now()).count(); +} -- cgit v1.2.3