2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2022-2023 Mnemosyne LLC.
|
2022-08-08 22:53:20 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2022-08-08 22:53:20 +00:00
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/timer-ev.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/utils-ev.h"
|
2022-08-08 22:53:20 +00:00
|
|
|
|
|
|
|
using namespace std::literals;
|
|
|
|
|
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
|
|
|
class EvTimer final : public Timer
|
|
|
|
{
|
|
|
|
public:
|
2022-08-13 17:11:07 +00:00
|
|
|
explicit EvTimer(struct event_base* base)
|
2022-08-08 22:53:20 +00:00
|
|
|
: base_{ base }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EvTimer(EvTimer&&) = delete;
|
|
|
|
EvTimer(EvTimer const&) = delete;
|
|
|
|
EvTimer& operator=(EvTimer&&) = delete;
|
|
|
|
EvTimer& operator=(EvTimer const&) = delete;
|
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
~EvTimer() override = default;
|
2022-08-08 22:53:20 +00:00
|
|
|
|
|
|
|
void stop() override
|
|
|
|
{
|
2022-11-08 04:42:42 +00:00
|
|
|
if (!is_running_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_del(evtimer_.get());
|
|
|
|
|
|
|
|
is_running_ = false;
|
2022-08-08 22:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void start() override
|
|
|
|
{
|
2022-11-08 04:42:42 +00:00
|
|
|
if (is_running_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
auto const secs = duration_cast<seconds>(interval_);
|
|
|
|
auto tv = timeval{};
|
|
|
|
tv.tv_sec = secs.count();
|
|
|
|
tv.tv_usec = static_cast<decltype(tv.tv_usec)>(duration_cast<microseconds>(interval_ - secs).count());
|
|
|
|
evtimer_add(evtimer_.get(), &tv);
|
|
|
|
|
|
|
|
is_running_ = true;
|
2022-08-08 22:53:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_callback(std::function<void()> callback) override
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
|
|
|
callback_ = std::move(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::chrono::milliseconds interval() const noexcept override
|
|
|
|
{
|
|
|
|
return interval_;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_interval(std::chrono::milliseconds interval) override
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
TR_ASSERT_MSG(interval.count() > 0 || !is_repeating(), "repeating timers must have a positive interval");
|
2022-08-08 22:53:20 +00:00
|
|
|
|
2022-11-08 04:42:42 +00:00
|
|
|
if (interval_ == interval)
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
2022-11-08 04:42:42 +00:00
|
|
|
return;
|
2022-08-08 22:53:20 +00:00
|
|
|
}
|
2022-11-08 04:42:42 +00:00
|
|
|
|
|
|
|
interval_ = interval;
|
|
|
|
applyChanges();
|
2022-08-08 22:53:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] bool is_repeating() const noexcept override
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
|
|
|
return is_repeating_;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_repeating(bool repeating) override
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
2022-11-08 04:42:42 +00:00
|
|
|
if (is_repeating_ == repeating)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-08 22:53:20 +00:00
|
|
|
is_repeating_ = repeating;
|
2022-11-08 04:42:42 +00:00
|
|
|
applyChanges();
|
2022-08-08 22:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-11-10 18:04:49 +00:00
|
|
|
[[nodiscard]] static constexpr short events(bool is_repeating) noexcept
|
2022-11-08 22:20:21 +00:00
|
|
|
{
|
|
|
|
return static_cast<short>(EV_TIMEOUT | (is_repeating ? EV_PERSIST : 0));
|
|
|
|
}
|
|
|
|
|
2022-11-08 04:42:42 +00:00
|
|
|
void applyChanges()
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
2022-11-08 04:42:42 +00:00
|
|
|
auto const old_events = event_get_events(evtimer_.get());
|
2023-05-06 04:11:05 +00:00
|
|
|
auto const new_events = events(is_repeating());
|
2022-11-08 04:42:42 +00:00
|
|
|
auto const was_running = isRunning();
|
2022-08-08 22:53:20 +00:00
|
|
|
|
2022-11-08 04:42:42 +00:00
|
|
|
if (was_running)
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_events != old_events)
|
|
|
|
{
|
|
|
|
[[maybe_unused]] auto const val = event_assign(evtimer_.get(), base_, -1, new_events, &EvTimer::onTimer, this);
|
|
|
|
TR_ASSERT(val == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (was_running)
|
|
|
|
{
|
|
|
|
start();
|
|
|
|
}
|
2022-08-08 22:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void onTimer(evutil_socket_t /*unused*/, short /*unused*/, void* vself)
|
|
|
|
{
|
|
|
|
static_cast<EvTimer*>(vself)->handleTimer();
|
|
|
|
}
|
|
|
|
|
2022-11-08 22:20:21 +00:00
|
|
|
void handleTimer()
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
2022-11-08 22:20:21 +00:00
|
|
|
is_running_ = is_repeating_;
|
|
|
|
|
2022-08-08 22:53:20 +00:00
|
|
|
TR_ASSERT(callback_);
|
|
|
|
callback_();
|
|
|
|
}
|
|
|
|
|
2022-11-08 04:42:42 +00:00
|
|
|
[[nodiscard]] constexpr bool isRunning() const noexcept
|
|
|
|
{
|
|
|
|
return is_running_;
|
|
|
|
}
|
|
|
|
|
2022-08-08 22:53:20 +00:00
|
|
|
std::chrono::milliseconds interval_ = 100ms;
|
2022-11-08 04:42:42 +00:00
|
|
|
bool is_repeating_ = false;
|
|
|
|
bool is_running_ = false;
|
2022-11-08 22:20:21 +00:00
|
|
|
std::function<void()> callback_;
|
|
|
|
|
|
|
|
struct event_base* const base_;
|
|
|
|
evhelpers::event_unique_ptr const evtimer_{ event_new(base_, -1, events(is_repeating_), &EvTimer::onTimer, this) };
|
2022-08-08 22:53:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Timer> EvTimerMaker::create()
|
|
|
|
{
|
|
|
|
return std::make_unique<EvTimer>(event_base_);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace libtransmission
|