From 55686c7d57a7a7213f89b3aca8ae34ba769a9c21 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 18 Mar 2019 13:40:56 +0700 Subject: [PATCH] Use new Timer inside DonateStrategy. --- src/base/kernel/Process.cpp | 3 +- src/net/strategies/DonateStrategy.cpp | 55 +++++++++++++-------------- src/net/strategies/DonateStrategy.h | 6 ++- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/base/kernel/Process.cpp b/src/base/kernel/Process.cpp index 0bc40e2d..9193d378 100644 --- a/src/base/kernel/Process.cpp +++ b/src/base/kernel/Process.cpp @@ -28,6 +28,7 @@ #include "base/kernel/Process.h" +#include "base/tools/Chrono.h" static size_t location(xmrig::Process::Location location, char *buf, size_t max) @@ -50,7 +51,7 @@ static size_t location(xmrig::Process::Location location, char *buf, size_t max) xmrig::Process::Process(int argc, char **argv) : m_arguments(argc, argv) { - srand(static_cast(static_cast(time(nullptr)) ^ reinterpret_cast(this))); + srand(static_cast(Chrono::currentMSecsSinceEpoch() ^ reinterpret_cast(this))); } diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 7d08fb33..11fca39b 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -28,22 +28,25 @@ #include "base/net/stratum/strategies/FailoverStrategy.h" #include "base/net/stratum/strategies/SinglePoolStrategy.h" #include "base/tools/Buffer.h" -#include "base/tools/Handle.h" +#include "base/tools/Timer.h" #include "common/crypto/keccak.h" #include "common/Platform.h" #include "common/xmrig.h" #include "net/strategies/DonateStrategy.h" -static inline float randomf(float min, float max) { - return (max - min) * ((((float) rand()) / (float) RAND_MAX)) + min; -} +namespace xmrig { + +static inline double randomf(double min, double max) { return (max - min) * (((static_cast(rand())) / static_cast(RAND_MAX))) + min; } +static inline uint64_t random(uint64_t base, double min, double max) { return static_cast(base * randomf(min, max)); } + +} /* namespace xmrig */ xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IStrategyListener *listener) : m_active(false), - m_donateTime(level * 60 * 1000), - m_idleTime((100 - level) * 60 * 1000), + m_donateTime(static_cast(level) * 60 * 1000), + m_idleTime((100 - static_cast(level)) * 60 * 1000), m_strategy(nullptr), m_listener(listener), m_now(0), @@ -55,7 +58,7 @@ xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IS keccak(reinterpret_cast(user), strlen(user), hash); Buffer::toHex(hash, 32, userId); -# ifndef XMRIG_NO_TLS +# ifdef XMRIG_FEATURE_TLS m_pools.push_back(Pool("donate.ssl.xmrig.com", 443, userId, nullptr, false, true, true)); # endif @@ -72,17 +75,15 @@ xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IS m_strategy = new SinglePoolStrategy(m_pools.front(), 1, 2, this, true); } - m_timer = new uv_timer_t; - m_timer->data = this; - uv_timer_init(uv_default_loop(), m_timer); + m_timer = new Timer(this); - idle(m_idleTime * randomf(0.5, 1.5)); + idle(random(m_idleTime, 0.5, 1.5)); } xmrig::DonateStrategy::~DonateStrategy() { - Handle::close(m_timer); + delete m_timer; delete m_strategy; } @@ -107,7 +108,7 @@ void xmrig::DonateStrategy::setAlgo(const xmrig::Algorithm &algo) void xmrig::DonateStrategy::stop() { - uv_timer_stop(m_timer); + m_timer->stop(); m_strategy->stop(); } @@ -128,7 +129,7 @@ void xmrig::DonateStrategy::tick(uint64_t now) void xmrig::DonateStrategy::onActive(IStrategy *strategy, Client *client) { if (!isActive()) { - uv_timer_start(m_timer, DonateStrategy::onTimer, m_donateTime, 0); + m_timer->start(m_donateTime, 0); } m_active = true; @@ -155,9 +156,19 @@ void xmrig::DonateStrategy::onResultAccepted(IStrategy *strategy, Client *client } +void xmrig::DonateStrategy::onTimer(const Timer *) +{ + if (!isActive()) { + return connect(); + } + + suspend(); +} + + void xmrig::DonateStrategy::idle(uint64_t timeout) { - uv_timer_start(m_timer, DonateStrategy::onTimer, timeout, 0); + m_timer->start(timeout, 0); } @@ -172,17 +183,5 @@ void xmrig::DonateStrategy::suspend() m_active = false; m_listener->onPause(this); - idle(m_idleTime); -} - - -void xmrig::DonateStrategy::onTimer(uv_timer_t *handle) -{ - auto strategy = static_cast(handle->data); - - if (!strategy->isActive()) { - return strategy->connect(); - } - - strategy->suspend(); + idle(random(m_idleTime, 0.8, 1.2)); } diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index ab898332..b5ea13c8 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -33,6 +33,7 @@ #include "base/kernel/interfaces/IClientListener.h" #include "base/kernel/interfaces/IStrategy.h" #include "base/kernel/interfaces/IStrategyListener.h" +#include "base/kernel/interfaces/ITimerListener.h" #include "base/net/stratum/Pool.h" @@ -43,7 +44,7 @@ class Client; class IStrategyListener; -class DonateStrategy : public IStrategy, public IStrategyListener +class DonateStrategy : public IStrategy, public IStrategyListener, public ITimerListener { public: DonateStrategy(int level, const char *user, Algo algo, IStrategyListener *listener); @@ -64,6 +65,7 @@ protected: void onJob(IStrategy *strategy, Client *client, const Job &job) override; void onPause(IStrategy *strategy) override; void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override; + void onTimer(const Timer *timer) override; private: void idle(uint64_t timeout); @@ -79,7 +81,7 @@ private: std::vector m_pools; uint64_t m_now; uint64_t m_stop; - uv_timer_t *m_timer; + Timer *m_timer; };