diff --git a/CMakeLists.txt b/CMakeLists.txt index 63d33455d..cecd78e10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,7 +120,7 @@ else() endif() add_definitions(/DUNICODE) -add_definitions(/DAPP_DEBUG) +#add_definitions(/DAPP_DEBUG) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") diff --git a/src/interfaces/IStrategy.h b/src/interfaces/IStrategy.h index 6b3b9560e..eddf300b3 100644 --- a/src/interfaces/IStrategy.h +++ b/src/interfaces/IStrategy.h @@ -25,12 +25,17 @@ #define __ISTRATEGY_H__ +class JobResult; + + class IStrategy { public: virtual ~IStrategy() {} - virtual void connect() = 0; + virtual bool isActive() const = 0; + virtual void connect() = 0; + virtual void submit(const JobResult &result) = 0; }; diff --git a/src/interfaces/IStrategyListener.h b/src/interfaces/IStrategyListener.h index dd33424ee..5fbc78c5d 100644 --- a/src/interfaces/IStrategyListener.h +++ b/src/interfaces/IStrategyListener.h @@ -25,10 +25,19 @@ #define __ISTRATEGYLISTENER_H__ +class Client; +class IStrategy; +class Job; + + class IStrategyListener { public: virtual ~IStrategyListener() {} + + virtual void onActive(Client *client) = 0; + virtual void onJob(Client *client, const Job &job) = 0; + virtual void onPause(IStrategy *strategy) = 0; }; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index b04c035eb..364431046 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -69,8 +69,19 @@ void Network::connect() } -void Network::onClose(Client *client, int failures) +void Network::onActive(Client *client) { + if (client->id() == -1) { + LOG_NOTICE("dev donate started"); + return; + } + + LOG_INFO(m_options->colors() ? "\x1B[01;37muse pool: \x1B[01;36m%s:%d" : "use pool: %s:%d", client->host(), client->port()); +} + + +//void Network::onClose(Client *client, int failures) +//{ // const int id = client->id(); // if (id == 0) { // if (failures == -1) { @@ -88,12 +99,12 @@ void Network::onClose(Client *client, int failures) // if (id == 1 && m_pools.size() > 2 && failures == m_options->retries()) { // m_pools[2]->connect(); // } -} +//} -void Network::onJobReceived(Client *client, const Job &job) +void Network::onJob(Client *client, const Job &job) { - if (m_donateActive && client->id() != 0) { + if (m_donate && m_donate->isActive() && client->id() != -1) { return; } @@ -103,19 +114,18 @@ void Network::onJobReceived(Client *client, const Job &job) void Network::onJobResult(const JobResult &result) { -// if (m_options->colors()) { -// LOG_NOTICE("\x1B[01;32mSHARE FOUND"); -// } -// else { -// LOG_NOTICE("SHARE FOUND"); -// } + LOG_NOTICE(m_options->colors() ? "\x1B[01;32mSHARE FOUND" : "SHARE FOUND"); -// m_pools[result.poolId]->submit(result); + if (result.poolId == -1 && m_donate) { + return m_donate->submit(result); + } + + m_strategy->submit(result); } -void Network::onLoginSuccess(Client *client) -{ +//void Network::onLoginSuccess(Client *client) +//{ // const int id = client->id(); // if (id == 0) { // return startDonate(); @@ -132,6 +142,15 @@ void Network::onLoginSuccess(Client *client) // if (m_pool == 1 && m_pools.size() > 2) { // try disconnect from backup pool // m_pools[2]->disconnect(); // } +//} + + +void Network::onPause(IStrategy *strategy) +{ + if ((m_donate && !m_donate->isActive()) || !m_strategy->isActive()) { + LOG_ERR("no active pools, pause mining"); + Workers::pause(); + } } diff --git a/src/net/Network.h b/src/net/Network.h index 3fec005af..4eb86cc3d 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -39,7 +39,7 @@ class Options; class Url; -class Network : public IClientListener, public IJobResultListener, public IStrategyListener +class Network : public IJobResultListener, public IStrategyListener { public: Network(const Options *options); @@ -50,10 +50,10 @@ public: static char *userAgent(); protected: - void onClose(Client *client, int failures) override; - void onJobReceived(Client *client, const Job &job) override; + void onActive(Client *client) override; + void onJob(Client *client, const Job &job) override; void onJobResult(const JobResult &result) override; - void onLoginSuccess(Client *client) override; + void onPause(IStrategy *strategy) override; private: void addPool(const Url *url); diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 5cbd319f0..9b7fb12ba 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -22,12 +22,14 @@ */ +#include "interfaces/IStrategyListener.h" #include "net/Client.h" #include "net/strategies/DonateStrategy.h" #include "Options.h" DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : + m_active(false), m_listener(listener) { Url *url = new Url("donate.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443, Options::i()->pools().front()->user()); @@ -45,11 +47,23 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : } +bool DonateStrategy::isActive() const +{ + return m_active; +} + + void DonateStrategy::connect() { } +void DonateStrategy::submit(const JobResult &result) +{ + m_client->submit(result); +} + + void DonateStrategy::onClose(Client *client, int failures) { @@ -58,12 +72,14 @@ void DonateStrategy::onClose(Client *client, int failures) void DonateStrategy::onJobReceived(Client *client, const Job &job) { - + m_listener->onJob(client, job); } void DonateStrategy::onLoginSuccess(Client *client) { + m_active = true; + m_listener->onActive(client); } diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index c010f7fff..128757cab 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -43,7 +43,9 @@ public: DonateStrategy(const char *agent, IStrategyListener *listener); public: + bool isActive() const override; void connect() override; + void submit(const JobResult &result) override; protected: void onClose(Client *client, int failures) override; @@ -53,6 +55,7 @@ protected: private: static void onTimer(uv_timer_t *handle); + bool m_active; Client *m_client; IStrategyListener *m_listener; uv_timer_t m_timer; diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index 677a35183..c7d2a8d03 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -22,12 +22,14 @@ */ +#include "interfaces/IStrategyListener.h" #include "net/Client.h" #include "net/strategies/SinglePoolStrategy.h" #include "Options.h" SinglePoolStrategy::SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener) : + m_active(false), m_listener(listener) { m_client = new Client(0, agent, this); @@ -36,24 +38,43 @@ SinglePoolStrategy::SinglePoolStrategy(const Url *url, const char *agent, IStrat } +bool SinglePoolStrategy::isActive() const +{ + return m_active; +} + + void SinglePoolStrategy::connect() { m_client->connect(); } +void SinglePoolStrategy::submit(const JobResult &result) +{ + m_client->submit(result); +} + + void SinglePoolStrategy::onClose(Client *client, int failures) { + if (!isActive()) { + return; + } + m_active = false; + m_listener->onPause(this); } void SinglePoolStrategy::onJobReceived(Client *client, const Job &job) { - + m_listener->onJob(client, job); } void SinglePoolStrategy::onLoginSuccess(Client *client) { + m_active = true; + m_listener->onActive(client); } diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h index ff289ae94..fb8eca60d 100644 --- a/src/net/strategies/SinglePoolStrategy.h +++ b/src/net/strategies/SinglePoolStrategy.h @@ -40,7 +40,9 @@ public: SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener); public: + bool isActive() const override; void connect() override; + void submit(const JobResult &result) override; protected: void onClose(Client *client, int failures) override; @@ -48,6 +50,7 @@ protected: void onLoginSuccess(Client *client) override; private: + bool m_active; Client *m_client; IStrategyListener *m_listener; }; diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 0990fc45e..ffeddb00c 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -50,7 +50,7 @@ public: static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; } static inline bool isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; } static inline uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); } - static inline void pause() { m_paused = 1; } + static inline void pause() { m_paused = 1; m_sequence++; } static inline void setListener(IJobResultListener *listener) { m_listener = listener; } private: