From 45084cb718c4de039fc78e780ecbadb14fa8af61 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 13 May 2022 03:18:25 +0700 Subject: [PATCH] Update Watcher class. --- src/base/io/Watcher.cpp | 81 ++++++++++++++++++++++++----------------- src/base/io/Watcher.h | 39 ++++++++++++-------- 2 files changed, 70 insertions(+), 50 deletions(-) diff --git a/src/base/io/Watcher.cpp b/src/base/io/Watcher.cpp index f5ce9647..78cf06b4 100644 --- a/src/base/io/Watcher.cpp +++ b/src/base/io/Watcher.cpp @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,37 +14,45 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * Additional permission under GNU GPL version 3 section 7 + * + * If you modify this Program, or any covered work, by linking or combining + * it with OpenSSL (or a modified version of that library), containing parts + * covered by the terms of OpenSSL License and SSLeay License, the licensors + * of this Program grant you additional permission to convey the resulting work. */ - -#include - - -#include "base/kernel/interfaces/IWatcherListener.h" #include "base/io/Watcher.h" +#include "base/kernel/interfaces/IWatcherListener.h" #include "base/tools/Handle.h" #include "base/tools/Timer.h" xmrig::Watcher::Watcher(const String &path, IWatcherListener *listener) : - m_listener(listener), - m_path(path) + m_path(path), + m_listener(listener) { - m_timer = new Timer(this); + m_timer = std::make_shared(this); - m_fsEvent = new uv_fs_event_t; - m_fsEvent->data = this; - uv_fs_event_init(uv_default_loop(), m_fsEvent); - - start(); + startTimer(); } xmrig::Watcher::~Watcher() { - delete m_timer; + Handle::close(m_event); +} - Handle::close(m_fsEvent); + +void xmrig::Watcher::onTimer(const Timer * /*timer*/) +{ + if (m_event) { + reload(); + } + else { + start(); + } } @@ -60,14 +62,7 @@ void xmrig::Watcher::onFsEvent(uv_fs_event_t *handle, const char *filename, int, return; } - static_cast(handle->data)->queueUpdate(); -} - - -void xmrig::Watcher::queueUpdate() -{ - m_timer->stop(); - m_timer->start(kDelay, 0); + static_cast(handle->data)->startTimer(); } @@ -75,8 +70,8 @@ void xmrig::Watcher::reload() { m_listener->onFileChanged(m_path); -# ifndef _WIN32 - uv_fs_event_stop(m_fsEvent); +# ifndef XMRIG_OS_WIN + stop(); start(); # endif } @@ -84,5 +79,23 @@ void xmrig::Watcher::reload() void xmrig::Watcher::start() { - uv_fs_event_start(m_fsEvent, xmrig::Watcher::onFsEvent, m_path, 0); + if (!m_event) { + m_event = new uv_fs_event_t; + m_event->data = this; + uv_fs_event_init(uv_default_loop(), m_event); + } + + uv_fs_event_start(m_event, onFsEvent, m_path, 0); +} + + +void xmrig::Watcher::startTimer() +{ + m_timer->singleShot(kDelay); +} + + +void xmrig::Watcher::stop() +{ + uv_fs_event_stop(m_event); } diff --git a/src/base/io/Watcher.h b/src/base/io/Watcher.h index 6438cb7a..94fecc73 100644 --- a/src/base/io/Watcher.h +++ b/src/base/io/Watcher.h @@ -1,12 +1,6 @@ /* XMRig - * Copyright 2010 Jeff Garzik - * Copyright 2012-2014 pooler - * Copyright 2014 Lucas Jones - * Copyright 2014-2016 Wolf9466 - * Copyright 2016 Jay D Dee - * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright (c) 2018-2022 SChernykh + * Copyright (c) 2016-2022 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +14,13 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * Additional permission under GNU GPL version 3 section 7 + * + * If you modify this Program, or any covered work, by linking or combining + * it with OpenSSL (or a modified version of that library), containing parts + * covered by the terms of OpenSSL License and SSLeay License, the licensors + * of this Program grant you additional permission to convey the resulting work. */ #ifndef XMRIG_WATCHER_H @@ -30,7 +31,10 @@ #include "base/tools/String.h" -typedef struct uv_fs_event_s uv_fs_event_t; +#include + + +using uv_fs_event_t = struct uv_fs_event_s; namespace xmrig { @@ -43,29 +47,32 @@ class Timer; class Watcher : public ITimerListener { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(Watcher) + Watcher(const String &path, IWatcherListener *listener); ~Watcher() override; protected: - inline void onTimer(const Timer *) override { reload(); } + void onTimer(const Timer *timer) override; private: constexpr static int kDelay = 500; static void onFsEvent(uv_fs_event_t *handle, const char *filename, int events, int status); - void queueUpdate(); void reload(); void start(); + void startTimer(); + void stop(); + const String m_path; IWatcherListener *m_listener; - String m_path; - Timer *m_timer; - uv_fs_event_t *m_fsEvent; + std::shared_ptr m_timer; + uv_fs_event_t *m_event = nullptr; }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* XMRIG_WATCHER_H */ +#endif // XMRIG_WATCHER_H