diff --git a/src/App.h b/src/App.h index 21e57725f..86f281e89 100644 --- a/src/App.h +++ b/src/App.h @@ -31,14 +31,12 @@ #include "base/kernel/interfaces/ISignalListener.h" -class Httpd; - - namespace xmrig { class Console; class Controller; +class Httpd; class Network; class Process; class Signals; diff --git a/src/base/base.cmake b/src/base/base.cmake index 79181f05f..08d868053 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -11,6 +11,7 @@ set(HEADERS_BASE src/base/kernel/interfaces/ISignalListener.h src/base/kernel/interfaces/IStrategy.h src/base/kernel/interfaces/IStrategyListener.h + src/base/kernel/interfaces/ITimerListener.h src/base/kernel/interfaces/IWatcherListener.h src/base/kernel/Process.h src/base/kernel/Signals.h @@ -30,6 +31,7 @@ set(HEADERS_BASE src/base/tools/Chrono.h src/base/tools/Handle.h src/base/tools/String.h + src/base/tools/Timer.h ) set(SOURCES_BASE @@ -50,6 +52,7 @@ set(SOURCES_BASE src/base/tools/Arguments.cpp src/base/tools/Buffer.cpp src/base/tools/String.cpp + src/base/tools/Timer.cpp ) diff --git a/src/base/io/Watcher.cpp b/src/base/io/Watcher.cpp index b8ea4c3b9..f5ce9647a 100644 --- a/src/base/io/Watcher.cpp +++ b/src/base/io/Watcher.cpp @@ -29,37 +29,31 @@ #include "base/kernel/interfaces/IWatcherListener.h" #include "base/io/Watcher.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_timer = new Timer(this); + m_fsEvent = new uv_fs_event_t; + m_fsEvent->data = this; uv_fs_event_init(uv_default_loop(), m_fsEvent); - m_timer = new uv_timer_t; - uv_timer_init(uv_default_loop(), m_timer); - - m_fsEvent->data = m_timer->data = this; - start(); } xmrig::Watcher::~Watcher() { - Handle::close(m_timer); + delete m_timer; + Handle::close(m_fsEvent); } -void xmrig::Watcher::onTimer(uv_timer_t *handle) -{ - static_cast(handle->data)->reload(); -} - - void xmrig::Watcher::onFsEvent(uv_fs_event_t *handle, const char *filename, int, int) { if (!filename) { @@ -72,8 +66,8 @@ void xmrig::Watcher::onFsEvent(uv_fs_event_t *handle, const char *filename, int, void xmrig::Watcher::queueUpdate() { - uv_timer_stop(m_timer); - uv_timer_start(m_timer, xmrig::Watcher::onTimer, kDelay, 0); + m_timer->stop(); + m_timer->start(kDelay, 0); } diff --git a/src/base/io/Watcher.h b/src/base/io/Watcher.h index 4fec4c687..6438cb7a2 100644 --- a/src/base/io/Watcher.h +++ b/src/base/io/Watcher.h @@ -26,30 +26,33 @@ #define XMRIG_WATCHER_H +#include "base/kernel/interfaces/ITimerListener.h" #include "base/tools/String.h" typedef struct uv_fs_event_s uv_fs_event_t; -typedef struct uv_timer_s uv_timer_t; namespace xmrig { class IWatcherListener; +class Timer; -class Watcher +class Watcher : public ITimerListener { public: Watcher(const String &path, IWatcherListener *listener); - ~Watcher(); + ~Watcher() override; + +protected: + inline void onTimer(const Timer *) override { reload(); } private: constexpr static int kDelay = 500; static void onFsEvent(uv_fs_event_t *handle, const char *filename, int events, int status); - static void onTimer(uv_timer_t *handle); void queueUpdate(); void reload(); @@ -57,11 +60,12 @@ private: IWatcherListener *m_listener; String m_path; + Timer *m_timer; uv_fs_event_t *m_fsEvent; - uv_timer_t *m_timer; }; } /* namespace xmrig */ + #endif /* XMRIG_WATCHER_H */ diff --git a/src/base/kernel/interfaces/ITimerListener.h b/src/base/kernel/interfaces/ITimerListener.h new file mode 100644 index 000000000..1b6337d63 --- /dev/null +++ b/src/base/kernel/interfaces/ITimerListener.h @@ -0,0 +1,47 @@ +/* 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 , + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_ITIMERLISTENER_H +#define XMRIG_ITIMERLISTENER_H + + +namespace xmrig { + + +class Timer; + + +class ITimerListener +{ +public: + virtual ~ITimerListener() = default; + + virtual void onTimer(const Timer *timer) = 0; +}; + + +} /* namespace xmrig */ + + +#endif // XMRIG_ITIMERLISTENER_H diff --git a/src/base/tools/String.cpp b/src/base/tools/String.cpp index 9884f0aca..c908fe9a9 100644 --- a/src/base/tools/String.cpp +++ b/src/base/tools/String.cpp @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify diff --git a/src/base/tools/String.h b/src/base/tools/String.h index 0c191dfde..a640469a1 100644 --- a/src/base/tools/String.h +++ b/src/base/tools/String.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 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 diff --git a/src/base/tools/Timer.cpp b/src/base/tools/Timer.cpp new file mode 100644 index 000000000..d06df1636 --- /dev/null +++ b/src/base/tools/Timer.cpp @@ -0,0 +1,91 @@ +/* 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 , + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/kernel/interfaces/ITimerListener.h" +#include "base/tools/Handle.h" +#include "base/tools/Timer.h" + + +xmrig::Timer::Timer(ITimerListener *listener) : + m_listener(listener), + m_timer(nullptr) +{ + init(); +} + + +xmrig::Timer::Timer(ITimerListener *listener, uint64_t timeout, uint64_t repeat) : + m_listener(listener), + m_timer(nullptr) +{ + init(); + start(timeout, repeat); +} + + +xmrig::Timer::~Timer() +{ + Handle::close(m_timer); +} + + +uint64_t xmrig::Timer::repeat() const +{ + return uv_timer_get_repeat(m_timer); +} + + +void xmrig::Timer::setRepeat(uint64_t repeat) +{ + uv_timer_set_repeat(m_timer, repeat); +} + + +void xmrig::Timer::start(uint64_t timeout, uint64_t repeat) +{ + uv_timer_start(m_timer, onTimer, timeout, repeat); +} + + +void xmrig::Timer::stop() +{ + uv_timer_stop(m_timer); +} + + +void xmrig::Timer::init() +{ + m_timer = new uv_timer_t; + m_timer->data = this; + uv_timer_init(uv_default_loop(), m_timer); +} + + +void xmrig::Timer::onTimer(uv_timer_t *handle) +{ + const Timer *timer = static_cast(handle->data); + + timer->m_listener->onTimer(timer); +} diff --git a/src/base/tools/Timer.h b/src/base/tools/Timer.h new file mode 100644 index 000000000..e0e210f52 --- /dev/null +++ b/src/base/tools/Timer.h @@ -0,0 +1,66 @@ +/* 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 , + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_TIMER_H +#define XMRIG_TIMER_H + + +#include + + +typedef struct uv_timer_s uv_timer_t; + + +namespace xmrig { + + +class ITimerListener; + + +class Timer +{ +public: + Timer(ITimerListener *listener); + Timer(ITimerListener *listener, uint64_t timeout, uint64_t repeat); + ~Timer(); + + uint64_t repeat() const; + void setRepeat(uint64_t repeat); + void start(uint64_t timeout, uint64_t repeat); + void stop(); + +private: + void init(); + + static void onTimer(uv_timer_t *handle); + + ITimerListener *m_listener; + uv_timer_t *m_timer; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_TIMER_H */ diff --git a/src/common/api/Httpd.cpp b/src/common/api/Httpd.cpp index cca2b056f..7b5e8578d 100644 --- a/src/common/api/Httpd.cpp +++ b/src/common/api/Httpd.cpp @@ -29,13 +29,14 @@ #include "api/Api.h" #include "base/tools/Handle.h" +#include "base/tools/Timer.h" #include "common/api/Httpd.h" #include "common/api/HttpReply.h" #include "common/api/HttpRequest.h" #include "common/log/Log.h" -Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) : +xmrig::Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) : m_idle(true), m_IPv6(IPv6), m_restricted(restricted), @@ -43,13 +44,11 @@ Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) : m_port(port), m_daemon(nullptr) { - m_timer = new uv_timer_t; - uv_timer_init(uv_default_loop(), m_timer); - m_timer->data = this; + m_timer = new Timer(this); } -Httpd::~Httpd() +xmrig::Httpd::~Httpd() { stop(); @@ -61,7 +60,7 @@ Httpd::~Httpd() } -bool Httpd::start() +bool xmrig::Httpd::start() { if (!m_port) { return false; @@ -85,23 +84,23 @@ bool Httpd::start() } # if MHD_VERSION >= 0x00093900 - uv_timer_start(m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval); + m_timer->start(kIdleInterval, kIdleInterval); # else - uv_timer_start(m_timer, Httpd::onTimer, kActiveInterval, kActiveInterval); + m_timer->start(kActiveInterval, kActiveInterval); # endif return true; } -void Httpd::stop() +void xmrig::Httpd::stop() { - xmrig::Handle::close(m_timer); + delete m_timer; m_timer = nullptr; } -int Httpd::process(xmrig::HttpRequest &req) +int xmrig::Httpd::process(HttpRequest &req) { xmrig::HttpReply reply; if (!req.process(m_accessToken, m_restricted, reply)) { @@ -118,27 +117,27 @@ int Httpd::process(xmrig::HttpRequest &req) } -void Httpd::run() +void xmrig::Httpd::run() { MHD_run(m_daemon); # if MHD_VERSION >= 0x00093900 const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS); if (m_idle && info->num_connections) { - uv_timer_set_repeat(m_timer, kActiveInterval); + m_timer->setRepeat(kActiveInterval); m_idle = false; } else if (!m_idle && !info->num_connections) { - uv_timer_set_repeat(m_timer, kIdleInterval); + m_timer->setRepeat(kIdleInterval); m_idle = true; } # endif } -int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *uploadData, size_t *uploadSize, void **con_cls) +int xmrig::Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *, const char *uploadData, size_t *uploadSize, void **con_cls) { - xmrig::HttpRequest req(connection, url, method, uploadData, uploadSize, con_cls); + HttpRequest req(connection, url, method, uploadData, uploadSize, con_cls); if (req.method() == xmrig::HttpRequest::Options) { return req.end(MHD_HTTP_OK, nullptr); @@ -150,9 +149,3 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url return static_cast(cls)->process(req); } - - -void Httpd::onTimer(uv_timer_t *handle) -{ - static_cast(handle->data)->run(); -} diff --git a/src/common/api/Httpd.h b/src/common/api/Httpd.h index 82ca15416..f0b6ce872 100644 --- a/src/common/api/Httpd.h +++ b/src/common/api/Httpd.h @@ -26,7 +26,10 @@ #define XMRIG_HTTPD_H -#include +#include + + +#include "base/kernel/interfaces/ITimerListener.h" struct MHD_Connection; @@ -34,31 +37,33 @@ struct MHD_Daemon; struct MHD_Response; -class UploadCtx; - - namespace xmrig { - class HttpRequest; -} -class Httpd +class HttpRequest; +class Timer; + + +class Httpd : public ITimerListener { public: Httpd(int port, const char *accessToken, bool IPv6, bool restricted); - ~Httpd(); + ~Httpd() override; + bool start(); void stop(); +protected: + void onTimer(const Timer *) override { run(); } + private: constexpr static const int kIdleInterval = 200; constexpr static const int kActiveInterval = 25; - int process(xmrig::HttpRequest &req); + int process(HttpRequest &req); void run(); static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *uploadData, size_t *uploadSize, void **con_cls); - static void onTimer(uv_timer_t *handle); bool m_idle; bool m_IPv6; @@ -66,7 +71,11 @@ private: const char *m_accessToken; const int m_port; MHD_Daemon *m_daemon; - uv_timer_t *m_timer; + Timer *m_timer; }; + +} /* namespace xmrig */ + + #endif /* XMRIG_HTTPD_H */ diff --git a/src/net/Network.cpp b/src/net/Network.cpp index efadb365d..61f6080b6 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -35,7 +35,7 @@ #include "base/net/stratum/Client.h" #include "base/net/stratum/SubmitResult.h" #include "base/tools/Chrono.h" -#include "base/tools/Handle.h" +#include "base/tools/Timer.h" #include "common/log/Log.h" #include "core/Config.h" #include "core/Controller.h" @@ -58,16 +58,13 @@ xmrig::Network::Network(Controller *controller) : m_donate = new DonateStrategy(controller->config()->donateLevel(), pools.data().front().user(), controller->config()->algorithm().algo(), this); } - m_timer = new uv_timer_t; - m_timer->data = this; - uv_timer_init(uv_default_loop(), m_timer); - uv_timer_start(m_timer, Network::onTick, kTickInterval, kTickInterval); + m_timer = new Timer(this, kTickInterval, kTickInterval); } xmrig::Network::~Network() { - Handle::close(m_timer); + delete m_timer; if (m_donate) { delete m_donate; @@ -215,9 +212,3 @@ void xmrig::Network::tick() Api::tick(m_state); # endif } - - -void xmrig::Network::onTick(uv_timer_t *handle) -{ - static_cast(handle->data)->tick(); -} diff --git a/src/net/Network.h b/src/net/Network.h index e83e60e22..8afc54924 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -27,11 +27,11 @@ #include -#include #include "api/NetworkState.h" #include "base/kernel/interfaces/IStrategyListener.h" +#include "base/kernel/interfaces/ITimerListener.h" #include "common/interfaces/IControllerListener.h" #include "interfaces/IJobResultListener.h" @@ -43,7 +43,7 @@ class Controller; class IStrategy; -class Network : public IJobResultListener, public IStrategyListener, public IControllerListener +class Network : public IJobResultListener, public IStrategyListener, public IControllerListener, public ITimerListener { public: Network(Controller *controller); @@ -52,6 +52,8 @@ public: void connect(); protected: + inline void onTimer(const Timer *) override { tick(); } + void onActive(IStrategy *strategy, Client *client) override; void onConfigChanged(Config *config, Config *previousConfig) override; void onJob(IStrategy *strategy, Client *client, const Job &job) override; @@ -66,12 +68,10 @@ private: void setJob(Client *client, const Job &job, bool donate); void tick(); - static void onTick(uv_timer_t *handle); - IStrategy *m_donate; IStrategy *m_strategy; NetworkState m_state; - uv_timer_t *m_timer; + Timer *m_timer; };