From e9d2e194f33a84865c02bd66b756a2a40051999f Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 12 Oct 2019 00:24:47 +0700 Subject: [PATCH] Added SelfSelectClient stub. --- src/base/base.cmake | 2 + src/base/kernel/interfaces/IClient.h | 2 +- src/base/kernel/interfaces/IClientListener.h | 2 +- src/base/net/stratum/BaseClient.h | 1 + src/base/net/stratum/Client.cpp | 8 +- src/base/net/stratum/Client.h | 5 +- src/base/net/stratum/DaemonClient.h | 7 +- src/base/net/stratum/Pool.cpp | 42 ++++++++- src/base/net/stratum/Pool.h | 6 +- src/base/net/stratum/SelfSelectClient.cpp | 40 +++++++++ src/base/net/stratum/SelfSelectClient.h | 87 +++++++++++++++++++ .../stratum/strategies/FailoverStrategy.cpp | 23 ++--- .../net/stratum/strategies/FailoverStrategy.h | 3 + .../stratum/strategies/SinglePoolStrategy.cpp | 23 +---- .../stratum/strategies/SinglePoolStrategy.h | 3 + src/net/strategies/DonateStrategy.cpp | 4 +- src/net/strategies/DonateStrategy.h | 2 +- 17 files changed, 209 insertions(+), 51 deletions(-) create mode 100644 src/base/net/stratum/SelfSelectClient.cpp create mode 100644 src/base/net/stratum/SelfSelectClient.h diff --git a/src/base/base.cmake b/src/base/base.cmake index 6a59c8acf..615d9ac5e 100644 --- a/src/base/base.cmake +++ b/src/base/base.cmake @@ -139,6 +139,7 @@ if (WITH_HTTP) src/base/net/http/HttpResponse.h src/base/net/http/HttpServer.h src/base/net/stratum/DaemonClient.h + src/base/net/stratum/SelfSelectClient.h src/base/net/tools/TcpServer.h ) @@ -154,6 +155,7 @@ if (WITH_HTTP) src/base/net/http/HttpResponse.cpp src/base/net/http/HttpServer.cpp src/base/net/stratum/DaemonClient.cpp + src/base/net/stratum/SelfSelectClient.cpp src/base/net/tools/TcpServer.cpp ) diff --git a/src/base/kernel/interfaces/IClient.h b/src/base/kernel/interfaces/IClient.h index c872a37ad..78e363485 100644 --- a/src/base/kernel/interfaces/IClient.h +++ b/src/base/kernel/interfaces/IClient.h @@ -26,7 +26,7 @@ #define XMRIG_ICLIENT_H -#include +#include namespace xmrig { diff --git a/src/base/kernel/interfaces/IClientListener.h b/src/base/kernel/interfaces/IClientListener.h index 3583be5ad..45b0bcfd2 100644 --- a/src/base/kernel/interfaces/IClientListener.h +++ b/src/base/kernel/interfaces/IClientListener.h @@ -26,7 +26,7 @@ #define XMRIG_ICLIENTLISTENER_H -#include +#include #include "rapidjson/fwd.h" diff --git a/src/base/net/stratum/BaseClient.h b/src/base/net/stratum/BaseClient.h index 56bdc1263..51da11133 100644 --- a/src/base/net/stratum/BaseClient.h +++ b/src/base/net/stratum/BaseClient.h @@ -46,6 +46,7 @@ class BaseClient : public IClient public: BaseClient(int id, IClientListener *listener); +protected: inline bool isEnabled() const override { return m_enabled; } inline const Job &job() const override { return m_job; } inline const Pool &pool() const override { return m_pool; } diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index 287ce4a5e..70362bb8e 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -22,11 +22,11 @@ * along with this program. If not, see . */ -#include -#include +#include +#include #include -#include -#include +#include +#include #include diff --git a/src/base/net/stratum/Client.h b/src/base/net/stratum/Client.h index ff2bf7f6a..32121aa17 100644 --- a/src/base/net/stratum/Client.h +++ b/src/base/net/stratum/Client.h @@ -40,10 +40,11 @@ #include "base/net/stratum/SubmitResult.h" #include "base/net/tools/RecvBuf.h" #include "base/net/tools/Storage.h" +#include "base/tools/Object.h" #include "crypto/common/Algorithm.h" -typedef struct bio_st BIO; +using BIO = struct bio_st; namespace xmrig { @@ -56,6 +57,8 @@ class JobResult; class Client : public BaseClient, public IDnsListener, public ILineListener { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(Client) + constexpr static uint64_t kConnectTimeout = 20 * 1000; constexpr static uint64_t kResponseTimeout = 20 * 1000; diff --git a/src/base/net/stratum/DaemonClient.h b/src/base/net/stratum/DaemonClient.h index 00b62e39a..781ea1b68 100644 --- a/src/base/net/stratum/DaemonClient.h +++ b/src/base/net/stratum/DaemonClient.h @@ -27,9 +27,10 @@ #define XMRIG_DAEMONCLIENT_H -#include "base/net/stratum/BaseClient.h" -#include "base/kernel/interfaces/ITimerListener.h" #include "base/kernel/interfaces/IHttpListener.h" +#include "base/kernel/interfaces/ITimerListener.h" +#include "base/net/stratum/BaseClient.h" +#include "base/tools/Object.h" namespace xmrig { @@ -38,6 +39,8 @@ namespace xmrig { class DaemonClient : public BaseClient, public ITimerListener, public IHttpListener { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(DaemonClient) + DaemonClient(int id, IClientListener *listener); ~DaemonClient() override; diff --git a/src/base/net/stratum/Pool.cpp b/src/base/net/stratum/Pool.cpp index ccce16c99..1570d5fb7 100644 --- a/src/base/net/stratum/Pool.cpp +++ b/src/base/net/stratum/Pool.cpp @@ -33,9 +33,17 @@ #include "base/net/stratum/Pool.h" #include "base/io/json/Json.h" #include "base/io/log/Log.h" +#include "base/kernel/Platform.h" +#include "base/net/stratum/Client.h" #include "rapidjson/document.h" +#ifdef XMRIG_FEATURE_HTTP +# include "base/net/stratum/DaemonClient.h" +# include "base/net/stratum/SelfSelectClient.h" +#endif + + namespace xmrig { static const char *kAlgo = "algo"; @@ -128,7 +136,13 @@ bool xmrig::Pool::isEnabled() const # endif # ifndef XMRIG_FEATURE_HTTP - if (isDaemon()) { + if (m_mode == MODE_DAEMON) { + return false; + } +# endif + +# ifndef XMRIG_FEATURE_HTTP + if (m_mode == MODE_SELF_SELECT) { return false; } # endif @@ -158,6 +172,32 @@ bool xmrig::Pool::isEqual(const Pool &other) const } +xmrig::IClient *xmrig::Pool::createClient(int id, IClientListener *listener) const +{ + IClient *client = nullptr; + + if (m_mode == MODE_POOL) { + client = new Client(id, Platform::userAgent(), listener); + } +# ifdef XMRIG_FEATURE_HTTP + else if (m_mode == MODE_DAEMON) { + client = new DaemonClient(id, listener); + } + else if (m_mode == MODE_SELF_SELECT) { + client = new SelfSelectClient(id, Platform::userAgent(), listener); + } +# endif + + assert(client != nullptr); + + if (client) { + client->setPool(*this); + } + + return client; +} + + rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const { using namespace rapidjson; diff --git a/src/base/net/stratum/Pool.h b/src/base/net/stratum/Pool.h index 3c7436022..ca375c076 100644 --- a/src/base/net/stratum/Pool.h +++ b/src/base/net/stratum/Pool.h @@ -39,6 +39,10 @@ namespace xmrig { +class IClient; +class IClientListener; + + class Pool { public: @@ -67,7 +71,6 @@ public: bool tls = false ); - inline bool isDaemon() const { return m_mode == MODE_DAEMON; } inline bool isNicehash() const { return m_flags.test(FLAG_NICEHASH); } inline bool isTLS() const { return m_flags.test(FLAG_TLS); } inline bool isValid() const { return m_url.isValid(); } @@ -94,6 +97,7 @@ public: bool isEnabled() const; bool isEqual(const Pool &other) const; + IClient *createClient(int id, IClientListener *listener) const; rapidjson::Value toJSON(rapidjson::Document &doc) const; std::string printableName() const; diff --git a/src/base/net/stratum/SelfSelectClient.cpp b/src/base/net/stratum/SelfSelectClient.cpp new file mode 100644 index 000000000..95a9ae957 --- /dev/null +++ b/src/base/net/stratum/SelfSelectClient.cpp @@ -0,0 +1,40 @@ +/* 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/net/stratum/SelfSelectClient.h" +#include "base/net/stratum/Client.h" + + +xmrig::SelfSelectClient::SelfSelectClient(int id, const char *agent, IClientListener *listener) : + m_listener(listener) +{ + m_client = new Client(id, agent, this); +} + + +xmrig::SelfSelectClient::~SelfSelectClient() +{ + delete m_client; +} diff --git a/src/base/net/stratum/SelfSelectClient.h b/src/base/net/stratum/SelfSelectClient.h new file mode 100644 index 000000000..b4549ebe4 --- /dev/null +++ b/src/base/net/stratum/SelfSelectClient.h @@ -0,0 +1,87 @@ +/* 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_SELFSELECTCLIENT_H +#define XMRIG_SELFSELECTCLIENT_H + + +#include "base/kernel/interfaces/IClientListener.h" +#include "base/tools/Object.h" +#include "base/kernel/interfaces/IClient.h" + + +namespace xmrig { + + +class SelfSelectClient : public IClient, public IClientListener +{ +public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(SelfSelectClient) + + SelfSelectClient(int id, const char *agent, IClientListener *listener); + ~SelfSelectClient() override; + +protected: + // IClient + bool disconnect() override { return m_client->disconnect(); } + bool hasExtension(Extension extension) const noexcept override { return m_client->hasExtension(extension); } + bool isEnabled() const override { return m_client->isEnabled(); } + bool isTLS() const override { return m_client->isTLS(); } + const char *mode() const override { return m_client->mode(); } + const char *tlsFingerprint() const override { return m_client->tlsFingerprint(); } + const char *tlsVersion() const override { return m_client->tlsVersion(); } + const Job &job() const override { return m_client->job(); } + const Pool &pool() const override { return m_client->pool(); } + const String &ip() const override { return m_client->ip(); } + int id() const override { return m_client->id(); } + int64_t submit(const JobResult &result) override { return m_client->submit(result); } + void connect() override { m_client->connect(); } + void connect(const Pool &pool) override { m_client->connect(pool); } + void deleteLater() override { m_client->deleteLater(); } + void setAlgo(const Algorithm &algo) override { m_client->setAlgo(algo); } + void setEnabled(bool enabled) override { m_client->setEnabled(enabled); } + void setPool(const Pool &pool) override { m_client->setPool(pool); } + void setQuiet(bool quiet) override { m_client->setQuiet(quiet); } + void setRetries(int retries) override { m_client->setRetries(retries); } + void setRetryPause(uint64_t ms) override { m_client->setRetryPause(ms); } + void tick(uint64_t now) override { m_client->tick(now); } + + // IClientListener + void onClose(IClient *, int failures) override { m_listener->onClose(this, failures); } + void onJobReceived(IClient *, const Job &job, const rapidjson::Value ¶ms) override { m_listener->onJobReceived(this, job, params); } + void onLogin(IClient *, rapidjson::Document &doc, rapidjson::Value ¶ms) override { m_listener->onLogin(this, doc, params); } + void onLoginSuccess(IClient *) override { m_listener->onLoginSuccess(this); } + void onResultAccepted(IClient *, const SubmitResult &result, const char *error) override { m_listener->onResultAccepted(this, result, error); } + void onVerifyAlgorithm(const IClient *, const Algorithm &algorithm, bool *ok) override { m_listener->onVerifyAlgorithm(this, algorithm, ok); } + +private: + IClient *m_client; + IClientListener *m_listener; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_SELFSELECTCLIENT_H */ diff --git a/src/base/net/stratum/strategies/FailoverStrategy.cpp b/src/base/net/stratum/strategies/FailoverStrategy.cpp index 48be2ba35..28c1ad99f 100644 --- a/src/base/net/stratum/strategies/FailoverStrategy.cpp +++ b/src/base/net/stratum/strategies/FailoverStrategy.cpp @@ -23,15 +23,10 @@ */ +#include "base/net/stratum/strategies/FailoverStrategy.h" +#include "base/kernel/interfaces/IClient.h" #include "base/kernel/interfaces/IStrategyListener.h" #include "base/kernel/Platform.h" -#include "base/net/stratum/Client.h" -#include "base/net/stratum/strategies/FailoverStrategy.h" - - -#ifdef XMRIG_FEATURE_HTTP -# include "base/net/stratum/DaemonClient.h" -#endif xmrig::FailoverStrategy::FailoverStrategy(const std::vector &pools, int retryPause, int retries, IStrategyListener *listener, bool quiet) : @@ -69,16 +64,8 @@ xmrig::FailoverStrategy::~FailoverStrategy() void xmrig::FailoverStrategy::add(const Pool &pool) { - const int id = static_cast(m_pools.size()); + IClient *client = pool.createClient(static_cast(m_pools.size()), this); -# ifdef XMRIG_FEATURE_HTTP - IClient *client = !pool.isDaemon() ? static_cast(new Client(id, Platform::userAgent(), this)) - : static_cast(new DaemonClient(id, this)); -# else - IClient *client = new Client(id, Platform::userAgent(), this); -# endif - - client->setPool(pool); client->setRetries(m_retries); client->setRetryPause(m_retryPause * 1000); client->setQuiet(m_quiet); @@ -123,8 +110,8 @@ void xmrig::FailoverStrategy::setAlgo(const Algorithm &algo) void xmrig::FailoverStrategy::stop() { - for (size_t i = 0; i < m_pools.size(); ++i) { - m_pools[i]->disconnect(); + for (auto &pool : m_pools) { + pool->disconnect(); } m_index = 0; diff --git a/src/base/net/stratum/strategies/FailoverStrategy.h b/src/base/net/stratum/strategies/FailoverStrategy.h index 283d49165..c69160ee9 100644 --- a/src/base/net/stratum/strategies/FailoverStrategy.h +++ b/src/base/net/stratum/strategies/FailoverStrategy.h @@ -32,6 +32,7 @@ #include "base/kernel/interfaces/IClientListener.h" #include "base/kernel/interfaces/IStrategy.h" #include "base/net/stratum/Pool.h" +#include "base/tools/Object.h" namespace xmrig { @@ -44,6 +45,8 @@ class IStrategyListener; class FailoverStrategy : public IStrategy, public IClientListener { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(FailoverStrategy) + FailoverStrategy(const std::vector &pool, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); FailoverStrategy(int retryPause, int retries, IStrategyListener *listener, bool quiet = false); ~FailoverStrategy() override; diff --git a/src/base/net/stratum/strategies/SinglePoolStrategy.cpp b/src/base/net/stratum/strategies/SinglePoolStrategy.cpp index c923e1c2c..a45be658f 100644 --- a/src/base/net/stratum/strategies/SinglePoolStrategy.cpp +++ b/src/base/net/stratum/strategies/SinglePoolStrategy.cpp @@ -23,33 +23,18 @@ */ +#include "base/net/stratum/strategies/SinglePoolStrategy.h" +#include "base/kernel/interfaces/IClient.h" #include "base/kernel/interfaces/IStrategyListener.h" #include "base/kernel/Platform.h" -#include "base/net/stratum/Client.h" -#include "base/net/stratum/strategies/SinglePoolStrategy.h" - - -#ifdef XMRIG_FEATURE_HTTP -# include "base/net/stratum/DaemonClient.h" -#endif +#include "base/net/stratum/Pool.h" xmrig::SinglePoolStrategy::SinglePoolStrategy(const Pool &pool, int retryPause, int retries, IStrategyListener *listener, bool quiet) : m_active(false), m_listener(listener) { -# ifdef XMRIG_FEATURE_HTTP - if (!pool.isDaemon()) { - m_client = new Client(0, Platform::userAgent(), this); - } - else { - m_client = new DaemonClient(0, this); - } -# else - m_client = new Client(0, Platform::userAgent(), this); -# endif - - m_client->setPool(pool); + m_client = pool.createClient(0, this); m_client->setRetries(retries); m_client->setRetryPause(retryPause * 1000); m_client->setQuiet(quiet); diff --git a/src/base/net/stratum/strategies/SinglePoolStrategy.h b/src/base/net/stratum/strategies/SinglePoolStrategy.h index ea808193c..f2c8b2297 100644 --- a/src/base/net/stratum/strategies/SinglePoolStrategy.h +++ b/src/base/net/stratum/strategies/SinglePoolStrategy.h @@ -28,6 +28,7 @@ #include "base/kernel/interfaces/IClientListener.h" #include "base/kernel/interfaces/IStrategy.h" +#include "base/tools/Object.h" namespace xmrig { @@ -41,6 +42,8 @@ class Pool; class SinglePoolStrategy : public IStrategy, public IClientListener { public: + XMRIG_DISABLE_COPY_MOVE_DEFAULT(SinglePoolStrategy) + SinglePoolStrategy(const Pool &pool, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); ~SinglePoolStrategy() override; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 3952d94ec..2be0af049 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -234,7 +234,7 @@ void xmrig::DonateStrategy::onTimer(const Timer *) } -xmrig::Client *xmrig::DonateStrategy::createProxy() +xmrig::IClient *xmrig::DonateStrategy::createProxy() { if (m_controller->config()->pools().proxyDonate() == Pools::PROXY_DONATE_NONE) { return nullptr; @@ -251,7 +251,7 @@ xmrig::Client *xmrig::DonateStrategy::createProxy() Pool pool(client->ip(), client->pool().port(), m_userId, client->pool().password(), 0, true, client->isTLS()); pool.setAlgo(client->pool().algorithm()); - auto proxy = new Client(-1, Platform::userAgent(), this); + IClient *proxy = new Client(-1, Platform::userAgent(), this); proxy->setPool(pool); proxy->setQuiet(true); diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 4c621f667..c249284b5 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -91,7 +91,7 @@ private: inline State state() const { return m_state; } - Client *createProxy(); + IClient *createProxy(); void idle(double min, double max); void setAlgorithms(rapidjson::Document &doc, rapidjson::Value ¶ms); void setJob(IClient *client, const Job &job);