Use new Timer inside DonateStrategy.

This commit is contained in:
XMRig 2019-03-18 13:40:56 +07:00
parent 9a6a5a94b5
commit 55686c7d57
3 changed files with 33 additions and 31 deletions

View file

@ -28,6 +28,7 @@
#include "base/kernel/Process.h" #include "base/kernel/Process.h"
#include "base/tools/Chrono.h"
static size_t location(xmrig::Process::Location location, char *buf, size_t max) 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) : xmrig::Process::Process(int argc, char **argv) :
m_arguments(argc, argv) m_arguments(argc, argv)
{ {
srand(static_cast<unsigned int>(static_cast<uintptr_t>(time(nullptr)) ^ reinterpret_cast<uintptr_t>(this))); srand(static_cast<unsigned int>(Chrono::currentMSecsSinceEpoch() ^ reinterpret_cast<uintptr_t>(this)));
} }

View file

@ -28,22 +28,25 @@
#include "base/net/stratum/strategies/FailoverStrategy.h" #include "base/net/stratum/strategies/FailoverStrategy.h"
#include "base/net/stratum/strategies/SinglePoolStrategy.h" #include "base/net/stratum/strategies/SinglePoolStrategy.h"
#include "base/tools/Buffer.h" #include "base/tools/Buffer.h"
#include "base/tools/Handle.h" #include "base/tools/Timer.h"
#include "common/crypto/keccak.h" #include "common/crypto/keccak.h"
#include "common/Platform.h" #include "common/Platform.h"
#include "common/xmrig.h" #include "common/xmrig.h"
#include "net/strategies/DonateStrategy.h" #include "net/strategies/DonateStrategy.h"
static inline float randomf(float min, float max) { namespace xmrig {
return (max - min) * ((((float) rand()) / (float) RAND_MAX)) + min;
} static inline double randomf(double min, double max) { return (max - min) * (((static_cast<double>(rand())) / static_cast<double>(RAND_MAX))) + min; }
static inline uint64_t random(uint64_t base, double min, double max) { return static_cast<uint64_t>(base * randomf(min, max)); }
} /* namespace xmrig */
xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IStrategyListener *listener) : xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IStrategyListener *listener) :
m_active(false), m_active(false),
m_donateTime(level * 60 * 1000), m_donateTime(static_cast<uint64_t>(level) * 60 * 1000),
m_idleTime((100 - level) * 60 * 1000), m_idleTime((100 - static_cast<uint64_t>(level)) * 60 * 1000),
m_strategy(nullptr), m_strategy(nullptr),
m_listener(listener), m_listener(listener),
m_now(0), m_now(0),
@ -55,7 +58,7 @@ xmrig::DonateStrategy::DonateStrategy(int level, const char *user, Algo algo, IS
keccak(reinterpret_cast<const uint8_t *>(user), strlen(user), hash); keccak(reinterpret_cast<const uint8_t *>(user), strlen(user), hash);
Buffer::toHex(hash, 32, userId); 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)); m_pools.push_back(Pool("donate.ssl.xmrig.com", 443, userId, nullptr, false, true, true));
# endif # 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_strategy = new SinglePoolStrategy(m_pools.front(), 1, 2, this, true);
} }
m_timer = new uv_timer_t; m_timer = new Timer(this);
m_timer->data = this;
uv_timer_init(uv_default_loop(), m_timer);
idle(m_idleTime * randomf(0.5, 1.5)); idle(random(m_idleTime, 0.5, 1.5));
} }
xmrig::DonateStrategy::~DonateStrategy() xmrig::DonateStrategy::~DonateStrategy()
{ {
Handle::close(m_timer); delete m_timer;
delete m_strategy; delete m_strategy;
} }
@ -107,7 +108,7 @@ void xmrig::DonateStrategy::setAlgo(const xmrig::Algorithm &algo)
void xmrig::DonateStrategy::stop() void xmrig::DonateStrategy::stop()
{ {
uv_timer_stop(m_timer); m_timer->stop();
m_strategy->stop(); m_strategy->stop();
} }
@ -128,7 +129,7 @@ void xmrig::DonateStrategy::tick(uint64_t now)
void xmrig::DonateStrategy::onActive(IStrategy *strategy, Client *client) void xmrig::DonateStrategy::onActive(IStrategy *strategy, Client *client)
{ {
if (!isActive()) { if (!isActive()) {
uv_timer_start(m_timer, DonateStrategy::onTimer, m_donateTime, 0); m_timer->start(m_donateTime, 0);
} }
m_active = true; 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) 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_active = false;
m_listener->onPause(this); m_listener->onPause(this);
idle(m_idleTime); idle(random(m_idleTime, 0.8, 1.2));
}
void xmrig::DonateStrategy::onTimer(uv_timer_t *handle)
{
auto strategy = static_cast<DonateStrategy*>(handle->data);
if (!strategy->isActive()) {
return strategy->connect();
}
strategy->suspend();
} }

View file

@ -33,6 +33,7 @@
#include "base/kernel/interfaces/IClientListener.h" #include "base/kernel/interfaces/IClientListener.h"
#include "base/kernel/interfaces/IStrategy.h" #include "base/kernel/interfaces/IStrategy.h"
#include "base/kernel/interfaces/IStrategyListener.h" #include "base/kernel/interfaces/IStrategyListener.h"
#include "base/kernel/interfaces/ITimerListener.h"
#include "base/net/stratum/Pool.h" #include "base/net/stratum/Pool.h"
@ -43,7 +44,7 @@ class Client;
class IStrategyListener; class IStrategyListener;
class DonateStrategy : public IStrategy, public IStrategyListener class DonateStrategy : public IStrategy, public IStrategyListener, public ITimerListener
{ {
public: public:
DonateStrategy(int level, const char *user, Algo algo, IStrategyListener *listener); 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 onJob(IStrategy *strategy, Client *client, const Job &job) override;
void onPause(IStrategy *strategy) override; void onPause(IStrategy *strategy) override;
void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override; void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override;
void onTimer(const Timer *timer) override;
private: private:
void idle(uint64_t timeout); void idle(uint64_t timeout);
@ -79,7 +81,7 @@ private:
std::vector<Pool> m_pools; std::vector<Pool> m_pools;
uint64_t m_now; uint64_t m_now;
uint64_t m_stop; uint64_t m_stop;
uv_timer_t *m_timer; Timer *m_timer;
}; };