From bd8776b7eef4659ab45543bb5072be2c38abcefd Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 28 Jun 2017 06:17:02 +0300 Subject: [PATCH] Add classes SinglePoolStrategy, DonateStrategy, IStrategy, IStrategyListener. --- CMakeLists.txt | 10 +- src/interfaces/IStrategy.h | 37 +++++ src/interfaces/IStrategyListener.h | 35 +++++ src/net/Client.h | 1 + src/net/Network.cpp | 169 ++++++++++------------ src/net/Network.h | 14 +- src/net/Url.cpp | 8 +- src/net/strategies/DonateStrategy.cpp | 73 ++++++++++ src/net/strategies/DonateStrategy.h | 61 ++++++++ src/net/strategies/SinglePoolStrategy.cpp | 59 ++++++++ src/net/strategies/SinglePoolStrategy.h | 55 +++++++ 11 files changed, 414 insertions(+), 108 deletions(-) create mode 100644 src/interfaces/IStrategy.h create mode 100644 src/interfaces/IStrategyListener.h create mode 100644 src/net/strategies/DonateStrategy.cpp create mode 100644 src/net/strategies/DonateStrategy.h create mode 100644 src/net/strategies/SinglePoolStrategy.cpp create mode 100644 src/net/strategies/SinglePoolStrategy.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ddcd2e52..63d33455d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,8 @@ set(HEADERS src/interfaces/IClientListener.h src/interfaces/IJobResultListener.h src/interfaces/ILogBackend.h + src/interfaces/IStrategy.h + src/interfaces/IStrategyListener.h src/interfaces/IWorker.h src/log/ConsoleLog.h src/log/FileLog.h @@ -25,6 +27,8 @@ set(HEADERS src/net/JobResult.h src/net/Network.h src/net/Url.h + src/net/strategies/DonateStrategy.h + src/net/strategies/SinglePoolStrategy.h src/Options.h src/Summary.h src/version.h @@ -60,6 +64,8 @@ set(SOURCES src/net/Job.cpp src/net/Network.cpp src/net/Url.cpp + src/net/strategies/DonateStrategy.cpp + src/net/strategies/SinglePoolStrategy.cpp src/Options.cpp src/Summary.cpp src/workers/DoubleWorker.cpp @@ -114,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") @@ -128,7 +134,7 @@ endif() # https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html if (CMAKE_CXX_COMPILER_ID MATCHES GNU) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall -Wno-strict-aliasing") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -std=c++14 -fno-exceptions -fno-rtti") diff --git a/src/interfaces/IStrategy.h b/src/interfaces/IStrategy.h new file mode 100644 index 000000000..6b3b9560e --- /dev/null +++ b/src/interfaces/IStrategy.h @@ -0,0 +1,37 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 __ISTRATEGY_H__ +#define __ISTRATEGY_H__ + + +class IStrategy +{ +public: + virtual ~IStrategy() {} + + virtual void connect() = 0; +}; + + +#endif // __ISTRATEGY_H__ diff --git a/src/interfaces/IStrategyListener.h b/src/interfaces/IStrategyListener.h new file mode 100644 index 000000000..dd33424ee --- /dev/null +++ b/src/interfaces/IStrategyListener.h @@ -0,0 +1,35 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 __ISTRATEGYLISTENER_H__ +#define __ISTRATEGYLISTENER_H__ + + +class IStrategyListener +{ +public: + virtual ~IStrategyListener() {} +}; + + +#endif // __ISTRATEGYLISTENER_H__ diff --git a/src/net/Client.h b/src/net/Client.h index 2e5303fb1..a4ff846d2 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -60,6 +60,7 @@ public: void send(char *data); void setUrl(const Url *url); void submit(const JobResult &result); + inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; } inline const char *host() const { return m_url.host(); } inline const Job &job() const { return m_job; } diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 4b186bfd5..b04c035eb 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -28,36 +28,36 @@ #include "log/Log.h" #include "net/Client.h" #include "net/Network.h" +#include "net/strategies/DonateStrategy.h" +#include "net/strategies/SinglePoolStrategy.h" #include "net/Url.h" #include "Options.h" #include "workers/Workers.h" Network::Network(const Options *options) : - m_donate(false), + m_donateActive(false), m_options(options), - m_pool(0), - m_diff(0) + m_donate(nullptr) { Workers::setListener(this); - - m_pools.reserve(2); m_agent = userAgent(); - addPool(std::make_unique().get()); - addPool(m_options->pools().front()); -// addPool(m_options->backupUrl()); + const std::vector &pools = options->pools(); - m_timer.data = this; - uv_timer_init(uv_default_loop(), &m_timer); + m_strategy = new SinglePoolStrategy(pools.front(), m_agent, this); + + if (m_options->donateLevel() > 0) { + m_donate = new DonateStrategy(m_agent, this); + } } Network::~Network() { - for (auto client : m_pools) { - delete client; - } +// for (auto client : m_pools) { +// delete client; +// } free(m_agent); } @@ -65,39 +65,35 @@ Network::~Network() void Network::connect() { - m_pools[1]->connect(); - - if (m_options->donateLevel()) { - uv_timer_start(&m_timer, Network::onTimer, (100 - m_options->donateLevel()) * 60 * 1000, 0); - } + m_strategy->connect(); } void Network::onClose(Client *client, int failures) { - const int id = client->id(); - if (id == 0) { - if (failures == -1) { - stopDonate(); - } +// const int id = client->id(); +// if (id == 0) { +// if (failures == -1) { +// stopDonate(); +// } - return; - } +// return; +// } - if (m_pool == id) { - m_pool = 0; - Workers::pause(); - } +// if (m_pool == id) { +// m_pool = 0; +// Workers::pause(); +// } - if (id == 1 && m_pools.size() > 2 && failures == m_options->retries()) { - m_pools[2]->connect(); - } +// if (id == 1 && m_pools.size() > 2 && failures == m_options->retries()) { +// m_pools[2]->connect(); +// } } void Network::onJobReceived(Client *client, const Job &job) { - if (m_donate && client->id() != 0) { + if (m_donateActive && client->id() != 0) { return; } @@ -107,50 +103,35 @@ 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"); - } +// if (m_options->colors()) { +// LOG_NOTICE("\x1B[01;32mSHARE FOUND"); +// } +// else { +// LOG_NOTICE("SHARE FOUND"); +// } - m_pools[result.poolId]->submit(result); +// m_pools[result.poolId]->submit(result); } void Network::onLoginSuccess(Client *client) { - const int id = client->id(); - if (id == 0) { - return startDonate(); - } +// const int id = client->id(); +// if (id == 0) { +// return startDonate(); +// } - if (id == 2 && m_pool) { // primary pool is already active - m_pools[2]->disconnect(); - return; - } +// if (id == 2 && m_pool) { // primary pool is already active +// m_pools[2]->disconnect(); +// return; +// } - LOG_INFO(m_options->colors() ? "\x1B[01;37muse pool: \x1B[01;36m%s:%d" : "use pool: %s:%d", client->host(), client->port()); - m_pool = id; +// LOG_INFO(m_options->colors() ? "\x1B[01;37muse pool: \x1B[01;36m%s:%d" : "use pool: %s:%d", client->host(), client->port()); +// m_pool = id; - if (m_pool == 1 && m_pools.size() > 2) { // try disconnect from backup pool - m_pools[2]->disconnect(); - } -} - - -void Network::addPool(const Url *url) -{ - if (!url) { - return; - } - - Client *client = new Client(m_pools.size(), m_agent, this); - client->setUrl(url); - client->setRetryPause(m_options->retryPause() * 1000); -// client->setKeepAlive(m_options->keepAlive()); - - m_pools.push_back(client); +// if (m_pool == 1 && m_pools.size() > 2) { // try disconnect from backup pool +// m_pools[2]->disconnect(); +// } } @@ -170,48 +151,48 @@ void Network::setJob(Client *client, const Job &job) void Network::startDonate() { - if (m_donate) { + if (m_donateActive) { return; } LOG_NOTICE("dev donate started"); - m_donate = true; + m_donateActive = true; } void Network::stopDonate() { - if (!m_donate) { - return; - } +// if (!m_donateActive) { +// return; +// } - LOG_NOTICE("dev donate finished"); +// LOG_NOTICE("dev donate finished"); - m_donate = false; - if (!m_pool) { - return; - } +// m_donateActive = false; +// if (!m_pool) { +// return; +// } - Client *client = m_pools[m_pool]; - if (client->isReady()) { - setJob(client, client->job()); - } +// Client *client = m_pools[m_pool]; +// if (client->isReady()) { +// setJob(client, client->job()); +// } } -void Network::onTimer(uv_timer_t *handle) -{ - auto net = static_cast(handle->data); +//void Network::onTimer(uv_timer_t *handle) +//{ +// auto net = static_cast(handle->data); - if (!net->m_donate) { - auto url = std::make_unique("donate.xmrig.com", net->m_options->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443); - net->m_pools[0]->connect(url.get()); +// if (!net->m_donateActive) { +// auto url = std::make_unique("donate.xmrig.com", net->m_options->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443); +// net->m_pools[0]->connect(url.get()); - uv_timer_start(&net->m_timer, Network::onTimer, net->m_options->donateLevel() * 60 * 1000, 0); - return; - } +// uv_timer_start(&net->m_timer, Network::onTimer, net->m_options->donateLevel() * 60 * 1000, 0); +// return; +// } - net->m_pools[0]->disconnect(); - uv_timer_start(&net->m_timer, Network::onTimer, (100 - net->m_options->donateLevel()) * 60 * 1000, 0); -} +// net->m_pools[0]->disconnect(); +// uv_timer_start(&net->m_timer, Network::onTimer, (100 - net->m_options->donateLevel()) * 60 * 1000, 0); +//} diff --git a/src/net/Network.h b/src/net/Network.h index eb20035d6..3fec005af 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -31,13 +31,15 @@ #include "interfaces/IClientListener.h" #include "interfaces/IJobResultListener.h" +#include "interfaces/IStrategyListener.h" +class IStrategy; class Options; class Url; -class Network : public IClientListener, public IJobResultListener +class Network : public IClientListener, public IJobResultListener, public IStrategyListener { public: Network(const Options *options); @@ -59,15 +61,11 @@ private: void startDonate(); void stopDonate(); - static void onTimer(uv_timer_t *handle); - - bool m_donate; + bool m_donateActive; char *m_agent; const Options *m_options; - int m_pool; - std::vector m_pools; - uint64_t m_diff; - uv_timer_t m_timer; + IStrategy *m_donate; + IStrategy *m_strategy; }; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index 1b6d8b0b9..06e44aaae 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -69,10 +69,10 @@ Url::Url(const char *url) : Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash) : - m_keepAlive(false), - m_nicehash(false), - m_password(nullptr), - m_user(nullptr), + m_keepAlive(keepAlive), + m_nicehash(nicehash), + m_password(password ? strdup(password) : nullptr), + m_user(user ? strdup(user) : nullptr), m_port(port) { m_host = strdup(host); diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp new file mode 100644 index 000000000..5cbd319f0 --- /dev/null +++ b/src/net/strategies/DonateStrategy.cpp @@ -0,0 +1,73 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 "net/Client.h" +#include "net/strategies/DonateStrategy.h" +#include "Options.h" + + +DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : + m_listener(listener) +{ + Url *url = new Url("donate.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443, Options::i()->pools().front()->user()); + + m_client = new Client(-1, agent, this); + m_client->setUrl(url); + m_client->setRetryPause(Options::i()->retryPause() * 1000); + + delete url; + + m_timer.data = this; + uv_timer_init(uv_default_loop(), &m_timer); + + uv_timer_start(&m_timer, DonateStrategy::onTimer, (100 - Options::i()->donateLevel()) * 60 * 1000, 0); +} + + +void DonateStrategy::connect() +{ +} + + +void DonateStrategy::onClose(Client *client, int failures) +{ + +} + + +void DonateStrategy::onJobReceived(Client *client, const Job &job) +{ + +} + + +void DonateStrategy::onLoginSuccess(Client *client) +{ +} + + +void DonateStrategy::onTimer(uv_timer_t *handle) +{ + +} diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h new file mode 100644 index 000000000..c010f7fff --- /dev/null +++ b/src/net/strategies/DonateStrategy.h @@ -0,0 +1,61 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 __DONATESTRATEGY_H__ +#define __DONATESTRATEGY_H__ + + +#include + + +#include "interfaces/IClientListener.h" +#include "interfaces/IStrategy.h" + + +class Client; +class IStrategyListener; +class Url; + + +class DonateStrategy : public IStrategy, public IClientListener +{ +public: + DonateStrategy(const char *agent, IStrategyListener *listener); + +public: + void connect() override; + +protected: + void onClose(Client *client, int failures) override; + void onJobReceived(Client *client, const Job &job) override; + void onLoginSuccess(Client *client) override; + +private: + static void onTimer(uv_timer_t *handle); + + Client *m_client; + IStrategyListener *m_listener; + uv_timer_t m_timer; +}; + +#endif /* __DONATESTRATEGY_H__ */ diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp new file mode 100644 index 000000000..677a35183 --- /dev/null +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -0,0 +1,59 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 "net/Client.h" +#include "net/strategies/SinglePoolStrategy.h" +#include "Options.h" + + +SinglePoolStrategy::SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener) : + m_listener(listener) +{ + m_client = new Client(0, agent, this); + m_client->setUrl(url); + m_client->setRetryPause(Options::i()->retryPause() * 1000); +} + + +void SinglePoolStrategy::connect() +{ + m_client->connect(); +} + + +void SinglePoolStrategy::onClose(Client *client, int failures) +{ + +} + + +void SinglePoolStrategy::onJobReceived(Client *client, const Job &job) +{ + +} + + +void SinglePoolStrategy::onLoginSuccess(Client *client) +{ +} diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h new file mode 100644 index 000000000..ff289ae94 --- /dev/null +++ b/src/net/strategies/SinglePoolStrategy.h @@ -0,0 +1,55 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 __SINGLEPOOLSTRATEGY_H__ +#define __SINGLEPOOLSTRATEGY_H__ + + +#include "interfaces/IClientListener.h" +#include "interfaces/IStrategy.h" + + +class Client; +class IStrategyListener; +class Url; + + +class SinglePoolStrategy : public IStrategy, public IClientListener +{ +public: + SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener); + +public: + void connect() override; + +protected: + void onClose(Client *client, int failures) override; + void onJobReceived(Client *client, const Job &job) override; + void onLoginSuccess(Client *client) override; + +private: + Client *m_client; + IStrategyListener *m_listener; +}; + +#endif /* __SINGLEPOOLSTRATEGY_H__ */