From 36ef254c730b33bb166715248f2efd9cfa42adcc Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 11 Apr 2018 06:09:34 +0700 Subject: [PATCH] Rename class Url to Pool. --- CMakeLists.txt | 4 +- src/Summary.cpp | 10 ++-- src/core/CommonConfig.cpp | 45 ++++++++---------- src/core/CommonConfig.h | 8 ++-- src/core/Config.cpp | 20 ++++---- src/core/ConfigLoader.cpp | 2 +- src/core/utils/c_str.h | 8 ++-- src/net/Client.cpp | 58 +++++++++++------------ src/net/Client.h | 13 +++-- src/net/Network.cpp | 5 +- src/net/{Url.cpp => Pool.cpp} | 34 +++++++++---- src/net/{Url.h => Pool.h} | 19 +++++--- src/net/strategies/DonateStrategy.cpp | 14 +++--- src/net/strategies/DonateStrategy.h | 3 +- src/net/strategies/FailoverStrategy.cpp | 8 ++-- src/net/strategies/FailoverStrategy.h | 5 +- src/net/strategies/SinglePoolStrategy.cpp | 4 +- src/net/strategies/SinglePoolStrategy.h | 2 +- 18 files changed, 136 insertions(+), 126 deletions(-) rename src/net/{Url.cpp => Pool.cpp} (82%) rename src/net/{Url.h => Pool.h} (90%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 734913e81..5502405ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,12 +45,12 @@ set(HEADERS src/net/Job.h src/net/JobResult.h src/net/Network.h + src/net/Pool.h src/net/Storage.h src/net/strategies/DonateStrategy.h src/net/strategies/FailoverStrategy.h src/net/strategies/SinglePoolStrategy.h src/net/SubmitResult.h - src/net/Url.h src/Platform.h src/Summary.h src/version.h @@ -102,11 +102,11 @@ set(SOURCES src/net/Client.cpp src/net/Job.cpp src/net/Network.cpp + src/net/Pool.cpp src/net/strategies/DonateStrategy.cpp src/net/strategies/FailoverStrategy.cpp src/net/strategies/SinglePoolStrategy.cpp src/net/SubmitResult.cpp - src/net/Url.cpp src/Platform.cpp src/Summary.cpp src/workers/CpuThread.cpp diff --git a/src/Summary.cpp b/src/Summary.cpp index 56f3f522a..6de1cd3a8 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -32,7 +32,7 @@ #include "Cpu.h" #include "log/Log.h" #include "Mem.h" -#include "net/Url.h" +#include "net/Pool.h" #include "Summary.h" #include "version.h" @@ -112,13 +112,13 @@ static void print_threads(xmrig::Config *config) static void print_pools(xmrig::Config *config) { - const std::vector &pools = config->pools(); + const std::vector &pools = config->pools(); for (size_t i = 0; i < pools.size(); ++i) { - Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mPOOL #%d: \x1B[01;36m%s:%d" : " * POOL #%d: %s:%d", + Log::i()->text(config->isColors() ? "\x1B[01;32m * \x1B[01;37mPOOL #%d: \x1B[01;36m%s" : " * POOL #%d: %s", i + 1, - pools[i]->host(), - pools[i]->port()); + pools[i].url() + ); } # ifdef APP_DEBUG diff --git a/src/core/CommonConfig.cpp b/src/core/CommonConfig.cpp index 78eea5d90..b8af451b2 100644 --- a/src/core/CommonConfig.cpp +++ b/src/core/CommonConfig.cpp @@ -32,7 +32,7 @@ #include "core/CommonConfig.h" #include "donate.h" #include "log/Log.h" -#include "net/Url.h" +#include "net/Pool.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" @@ -79,7 +79,7 @@ xmrig::CommonConfig::CommonConfig() : m_retries(5), m_retryPause(5) { - m_pools.push_back(new Url()); + m_pools.push_back(Pool()); # ifdef XMRIG_PROXY_PROJECT m_retries = 2; @@ -90,11 +90,6 @@ xmrig::CommonConfig::CommonConfig() : xmrig::CommonConfig::~CommonConfig() { - for (Url *url : m_pools) { - delete url; - } - - m_pools.clear(); } @@ -112,8 +107,8 @@ bool xmrig::CommonConfig::adjust() m_adjusted = true; - for (Url *url : m_pools) { - url->adjust(algorithm()); + for (Pool &pool : m_pools) { + pool.adjust(algorithm()); } return true; @@ -122,7 +117,7 @@ bool xmrig::CommonConfig::adjust() bool xmrig::CommonConfig::isValid() const { - return m_pools[0]->isValid(); + return m_pools[0].isValid(); } @@ -138,12 +133,12 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable) break; case KeepAliveKey: /* --keepalive */ - m_pools.back()->setKeepAlive(enable ? Url::kKeepAliveTimeout : 0); + m_pools.back().setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); break; # ifndef XMRIG_PROXY_PROJECT case NicehashKey: /* --nicehash */ - m_pools.back()->setNicehash(enable); + m_pools.back().setNicehash(enable); break; # endif @@ -177,38 +172,36 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) break; case UserpassKey: /* --userpass */ - if (!m_pools.back()->setUserpass(arg)) { + if (!m_pools.back().setUserpass(arg)) { return false; } break; case UrlKey: /* --url */ - if (m_pools.size() > 1 || m_pools[0]->isValid()) { - Url *url = new Url(arg); - if (url->isValid()) { - m_pools.push_back(url); - } - else { - delete url; + if (m_pools.size() > 1 || m_pools[0].isValid()) { + Pool pool(arg); + + if (pool.isValid()) { + m_pools.push_back(std::move(pool)); } } else { - m_pools[0]->parse(arg); + m_pools[0].parse(arg); } - if (!m_pools.back()->isValid()) { + if (!m_pools.back().isValid()) { return false; } break; case UserKey: /* --user */ - m_pools.back()->setUser(arg); + m_pools.back().setUser(arg); break; case PasswordKey: /* --pass */ - m_pools.back()->setPassword(arg); + m_pools.back().setPassword(arg); break; case LogFileKey: /* --log-file */ @@ -325,11 +318,11 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) break; case KeepAliveKey: /* --keepalive */ - m_pools.back()->setKeepAlive(arg); + m_pools.back().setKeepAlive(arg); break; case VariantKey: /* --variant */ - m_pools.back()->setVariant(arg); + m_pools.back().setVariant(arg); break; case DonateLevelKey: /* --donate-level */ diff --git a/src/core/CommonConfig.h b/src/core/CommonConfig.h index 07ff72d74..89dbc7ed8 100644 --- a/src/core/CommonConfig.h +++ b/src/core/CommonConfig.h @@ -31,9 +31,7 @@ #include "core/utils/c_str.h" #include "interfaces/IConfig.h" #include "xmrig.h" - - -class Url; +#include "net/Pool.h" namespace xmrig { @@ -58,7 +56,7 @@ public: inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *logFile() const { return m_logFile.data(); } inline const char *userAgent() const { return m_userAgent.data(); } - inline const std::vector &pools() const { return m_pools; } + inline const std::vector &pools() const { return m_pools; } inline int apiPort() const { return m_apiPort; } inline int donateLevel() const { return m_donateLevel; } inline int printTime() const { return m_printTime; } @@ -91,7 +89,7 @@ protected: int m_printTime; int m_retries; int m_retryPause; - std::vector m_pools; + std::vector m_pools; xmrig::c_str m_apiToken; xmrig::c_str m_apiWorkerId; xmrig::c_str m_fileName; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 6bc2dc0fc..a91366608 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -30,7 +30,7 @@ #include "core/ConfigCreator.h" #include "core/ConfigLoader.h" #include "Cpu.h" -#include "net/Url.h" +#include "net/Pool.h" #include "rapidjson/document.h" #include "rapidjson/filewritestream.h" #include "rapidjson/prettywriter.h" @@ -110,22 +110,22 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const rapidjson::Value pools(rapidjson::kArrayType); - for (const Url *url : m_pools) { + for (const Pool &pool : m_pools) { rapidjson::Value obj(rapidjson::kObjectType); - obj.AddMember("url", rapidjson::StringRef(url->url()), allocator); - obj.AddMember("user", rapidjson::StringRef(url->user()), allocator); - obj.AddMember("pass", rapidjson::StringRef(url->password()), allocator); + obj.AddMember("url", rapidjson::StringRef(pool.url()), allocator); + obj.AddMember("user", rapidjson::StringRef(pool.user()), allocator); + obj.AddMember("pass", rapidjson::StringRef(pool.password()), allocator); - if (url->keepAlive() == 0 || url->keepAlive() == Url::kKeepAliveTimeout) { - obj.AddMember("keepalive", url->keepAlive() > 0, allocator); + if (pool.keepAlive() == 0 || pool.keepAlive() == Pool::kKeepAliveTimeout) { + obj.AddMember("keepalive", pool.keepAlive() > 0, allocator); } else { - obj.AddMember("keepalive", url->keepAlive(), allocator); + obj.AddMember("keepalive", pool.keepAlive(), allocator); } - obj.AddMember("nicehash", url->isNicehash(), allocator); - obj.AddMember("variant", url->variant(), allocator); + obj.AddMember("nicehash", pool.isNicehash(), allocator); + obj.AddMember("variant", pool.variant(), allocator); pools.PushBack(obj, allocator); } diff --git a/src/core/ConfigLoader.cpp b/src/core/ConfigLoader.cpp index 1dec4ecab..8c7f6546a 100644 --- a/src/core/ConfigLoader.cpp +++ b/src/core/ConfigLoader.cpp @@ -38,7 +38,7 @@ #include "core/ConfigWatcher.h" #include "interfaces/IConfig.h" #include "interfaces/IWatcherListener.h" -#include "net/Url.h" +#include "net/Pool.h" #include "Platform.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" diff --git a/src/core/utils/c_str.h b/src/core/utils/c_str.h index 393c349b7..9a11ade64 100644 --- a/src/core/utils/c_str.h +++ b/src/core/utils/c_str.h @@ -43,9 +43,11 @@ namespace xmrig { class c_str { public: - inline c_str() : m_data(nullptr) {} - inline c_str(const char *str) : m_data(nullptr) { set(str); } - inline ~c_str() { free(m_data); } + inline c_str() : m_data(nullptr) {} + inline c_str(c_str &&other) { m_data = other.m_data; other.m_data = nullptr; } + inline c_str(const c_str &other) : m_data(nullptr) { set(other.data()); } + inline c_str(const char *str) : m_data(nullptr) { set(str); } + inline ~c_str() { free(m_data); } inline void set(const char *str) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index bf64408af..5574f37f5 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -32,7 +32,6 @@ #include "interfaces/IClientListener.h" #include "log/Log.h" #include "net/Client.h" -#include "net/Url.h" #include "rapidjson/document.h" #include "rapidjson/error/en.h" #include "rapidjson/stringbuffer.h" @@ -97,7 +96,7 @@ Client::~Client() void Client::connect() { - resolve(m_url.host()); + resolve(m_pool.host()); } @@ -106,10 +105,10 @@ void Client::connect() * * @param url */ -void Client::connect(const Url *url) +void Client::connect(const Pool &url) { setUrl(url); - resolve(m_url.host()); + connect(); } @@ -132,13 +131,13 @@ void Client::deleteLater() } -void Client::setUrl(const Url *url) +void Client::setUrl(const Pool &pool) { - if (!url || !url->isValid()) { + if (!pool.isValid()) { return; } - m_url = *url; + m_pool = pool; } @@ -146,7 +145,7 @@ void Client::tick(uint64_t now) { if (m_state == ConnectedState) { if (m_expire && now > m_expire) { - LOG_DEBUG_ERR("[%s:%u] timeout", m_url.host(), m_url.port()); + LOG_DEBUG_ERR("[%s] timeout", m_pool.url()); close(); } else if (m_keepAlive && now > m_keepAlive) { @@ -266,11 +265,10 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) } # ifdef XMRIG_PROXY_PROJECT - Job job(m_id, m_url.variant()); + Job job(m_id, m_pool.variant()); job.setClientId(m_rpcId); - job.setCoin(m_url.coin()); # else - Job job(m_id, m_nicehash, m_url.algo(), m_url.variant()); + Job job(m_id, m_nicehash, m_pool.algo(), m_pool.variant()); # endif if (!job.setId(params["job_id"].GetString())) { @@ -307,7 +305,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) } if (!m_quiet) { - LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); + LOG_WARN("[%s] duplicate job received, reconnect", m_pool.url()); } close(); @@ -323,7 +321,7 @@ bool Client::parseLogin(const rapidjson::Value &result, int *code) } # ifndef XMRIG_PROXY_PROJECT - m_nicehash = m_url.isNicehash(); + m_nicehash = m_pool.isNicehash(); # endif if (result.HasMember("extensions")) { @@ -351,7 +349,7 @@ int Client::resolve(const char *host) const int r = uv_getaddrinfo(uv_default_loop(), &m_resolver, Client::onResolved, host, nullptr, &m_hints); if (r) { if (!m_quiet) { - LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_url.port(), uv_strerror(r)); + LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_pool.port(), uv_strerror(r)); } return 1; } @@ -402,7 +400,7 @@ void Client::connect(sockaddr *addr) { setState(ConnectingState); - reinterpret_cast(addr)->sin_port = htons(m_url.port()); + reinterpret_cast(addr)->sin_port = htons(m_pool.port()); delete m_socket; uv_connect_t *req = new uv_connect_t; @@ -436,9 +434,9 @@ void Client::login() doc.AddMember("method", "login", allocator); rapidjson::Value params(rapidjson::kObjectType); - params.AddMember("login", rapidjson::StringRef(m_url.user()), allocator); - params.AddMember("pass", rapidjson::StringRef(m_url.password()), allocator); - params.AddMember("agent", rapidjson::StringRef(m_agent), allocator); + params.AddMember("login", rapidjson::StringRef(m_pool.user()), allocator); + params.AddMember("pass", rapidjson::StringRef(m_pool.password()), allocator); + params.AddMember("agent", rapidjson::StringRef(m_agent), allocator); doc.AddMember("params", params, allocator); @@ -481,7 +479,7 @@ void Client::parse(char *line, size_t len) if (len < 32 || line[0] != '{') { if (!m_quiet) { - LOG_ERR("[%s:%u] JSON decode failed", m_url.host(), m_url.port()); + LOG_ERR("[%s] JSON decode failed", m_pool.url()); } return; @@ -490,7 +488,7 @@ void Client::parse(char *line, size_t len) rapidjson::Document doc; if (doc.ParseInsitu(line).HasParseError()) { if (!m_quiet) { - LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_url.host(), m_url.port(), rapidjson::GetParseError_En(doc.GetParseError())); + LOG_ERR("[%s] JSON decode failed: \"%s\"", m_pool.url(), rapidjson::GetParseError_En(doc.GetParseError())); } return; @@ -532,7 +530,7 @@ void Client::parseNotification(const char *method, const rapidjson::Value ¶m { if (error.IsObject()) { if (!m_quiet) { - LOG_ERR("[%s:%u] error: \"%s\", code: %d", m_url.host(), m_url.port(), error["message"].GetString(), error["code"].GetInt()); + LOG_ERR("[%s] error: \"%s\", code: %d", m_pool.url(), error["message"].GetString(), error["code"].GetInt()); } return; } @@ -550,7 +548,7 @@ void Client::parseNotification(const char *method, const rapidjson::Value ¶m return; } - LOG_WARN("[%s:%u] unsupported method: \"%s\"", m_url.host(), m_url.port(), method); + LOG_WARN("[%s] unsupported method: \"%s\"", m_pool.url(), method); } @@ -566,7 +564,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap m_results.erase(it); } else if (!m_quiet) { - LOG_ERR("[%s:%u] error: \"%s\", code: %d", m_url.host(), m_url.port(), message, error["code"].GetInt()); + LOG_ERR("[%s] error: \"%s\", code: %d", m_pool.url(), message, error["code"].GetInt()); } if (isCriticalError(message)) { @@ -584,7 +582,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap int code = -1; if (!parseLogin(result, &code)) { if (!m_quiet) { - LOG_ERR("[%s:%u] login error code: %d", m_url.host(), m_url.port(), code); + LOG_ERR("[%s] login error code: %d", m_pool.url(), code); } close(); @@ -650,8 +648,8 @@ void Client::startTimeout() { m_expire = 0; - if (m_url.keepAlive()) { - m_keepAlive = uv_now(uv_default_loop()) + (m_url.keepAlive() * 1000); + if (m_pool.keepAlive()) { + m_keepAlive = uv_now(uv_default_loop()) + (m_pool.keepAlive() * 1000); } } @@ -689,7 +687,7 @@ void Client::onConnect(uv_connect_t *req, int status) if (status < 0) { if (!client->m_quiet) { - LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); + LOG_ERR("[%s] connect error: \"%s\"", client->m_pool.url(), uv_strerror(status)); } delete req; @@ -717,7 +715,7 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) if (nread < 0) { if (nread != UV_EOF && !client->m_quiet) { - LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror((int) nread)); + LOG_ERR("[%s] read error: \"%s\"", client->m_pool.url(), uv_strerror((int) nread)); } client->close(); @@ -777,7 +775,7 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) if (status < 0) { if (!client->m_quiet) { - LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); + LOG_ERR("[%s] DNS error: \"%s\"", client->m_pool.url(), uv_strerror(status)); } return client->reconnect(); @@ -801,7 +799,7 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) if (ipv4.empty() && ipv6.empty()) { if (!client->m_quiet) { - LOG_ERR("[%s:%u] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", client->m_url.host(), client->m_url.port()); + LOG_ERR("[%s] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", client->m_pool.url()); } uv_freeaddrinfo(res); diff --git a/src/net/Client.h b/src/net/Client.h index e6af6503e..910c8379a 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -30,12 +30,11 @@ #include -#include "core/utils/c_str.h" #include "net/Id.h" #include "net/Job.h" #include "net/Storage.h" #include "net/SubmitResult.h" -#include "net/Url.h" +#include "net/Pool.h" #include "rapidjson/fwd.h" @@ -62,18 +61,18 @@ public: bool disconnect(); int64_t submit(const JobResult &result); void connect(); - void connect(const Url *url); + void connect(const Pool &pool); void deleteLater(); - void setUrl(const Url *url); + void setUrl(const Pool &pool); void tick(uint64_t now); inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; } - inline const char *host() const { return m_url.host(); } + inline const char *host() const { return m_pool.host(); } inline const char *ip() const { return m_ip; } inline const Job &job() const { return m_job; } inline int id() const { return m_id; } inline SocketState state() const { return m_state; } - inline uint16_t port() const { return m_url.port(); } + inline uint16_t port() const { return m_pool.port(); } inline void setQuiet(bool quiet) { m_quiet = quiet; } inline void setRetryPause(int ms) { m_retryPause = ms; } @@ -118,6 +117,7 @@ private: int m_retryPause; int64_t m_failures; Job m_job; + Pool m_pool; size_t m_recvBufPos; SocketState m_state; std::map m_results; @@ -125,7 +125,6 @@ private: uint64_t m_jobs; uint64_t m_keepAlive; uintptr_t m_key; - Url m_url; uv_buf_t m_recvBuf; uv_getaddrinfo_t m_resolver; uv_stream_t *m_stream; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index a8e60efac..98ce7e8d6 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -38,7 +38,6 @@ #include "net/strategies/FailoverStrategy.h" #include "net/strategies/SinglePoolStrategy.h" #include "net/SubmitResult.h" -#include "net/Url.h" #include "workers/Workers.h" #include "core/Controller.h" #include "core/Config.h" @@ -52,7 +51,7 @@ Network::Network(xmrig::Controller *controller) : Workers::setListener(this); - const std::vector &pools = controller->config()->pools(); + const std::vector &pools = controller->config()->pools(); if (pools.size() > 1) { m_strategy = new FailoverStrategy(pools, controller->config()->retryPause(), controller->config()->retries(), this); @@ -62,7 +61,7 @@ Network::Network(xmrig::Controller *controller) : } if (controller->config()->donateLevel() > 0) { - m_donate = new DonateStrategy(controller->config()->donateLevel(), controller->config()->pools().front()->user(), controller->config()->algorithm(), this); + m_donate = new DonateStrategy(controller->config()->donateLevel(), controller->config()->pools().front().user(), controller->config()->algorithm(), this); } m_timer.data = this; diff --git a/src/net/Url.cpp b/src/net/Pool.cpp similarity index 82% rename from src/net/Url.cpp rename to src/net/Pool.cpp index c2094d15a..ff7d461ad 100644 --- a/src/net/Url.cpp +++ b/src/net/Pool.cpp @@ -28,7 +28,7 @@ #include -#include "net/Url.h" +#include "net/Pool.h" #ifdef _MSC_VER @@ -36,7 +36,7 @@ #endif -Url::Url() : +Pool::Pool() : m_nicehash(false), m_keepAlive(0), m_port(kDefaultPort), @@ -57,7 +57,7 @@ Url::Url() : * * @param url */ -Url::Url(const char *url) : +Pool::Pool(const char *url) : m_nicehash(false), m_keepAlive(0), m_port(kDefaultPort), @@ -69,7 +69,7 @@ Url::Url(const char *url) : } -Url::Url(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, xmrig::Variant variant) : +Pool::Pool(const char *host, uint16_t port, const char *user, const char *password, int keepAlive, bool nicehash, xmrig::Variant variant) : m_nicehash(nicehash), m_keepAlive(keepAlive), m_port(port), @@ -89,7 +89,7 @@ Url::Url(const char *host, uint16_t port, const char *user, const char *password } -bool Url::parse(const char *url) +bool Pool::parse(const char *url) { assert(url != nullptr); @@ -114,7 +114,7 @@ bool Url::parse(const char *url) const char *port = strchr(base, ':'); if (!port) { - m_host = strdup(base); + m_host = base; return false; } @@ -129,7 +129,7 @@ bool Url::parse(const char *url) } -bool Url::setUserpass(const char *userpass) +bool Pool::setUserpass(const char *userpass) { const char *p = strchr(userpass, ':'); if (!p) { @@ -146,7 +146,7 @@ bool Url::setUserpass(const char *userpass) } -void Url::adjust(xmrig::Algo algo) +void Pool::adjust(xmrig::Algo algo) { if (!isValid()) { return; @@ -165,7 +165,7 @@ void Url::adjust(xmrig::Algo algo) } -void Url::setVariant(int variant) +void Pool::setVariant(int variant) { switch (variant) { case xmrig::VARIANT_AUTO: @@ -181,7 +181,21 @@ void Url::setVariant(int variant) } -bool Url::parseIPv6(const char *addr) +bool Pool::isEqual(const Pool &other) const +{ + return (m_nicehash == other.m_nicehash + && m_keepAlive == other.m_keepAlive + && m_port == other.m_port + && m_algo == other.m_algo + && m_host == other.m_host + && m_password == other.m_password + && m_url == other.m_url + && m_user == other.m_user + && m_variant == other.m_variant); +} + + +bool Pool::parseIPv6(const char *addr) { const char *end = strchr(addr, ']'); if (!end) { diff --git a/src/net/Url.h b/src/net/Pool.h similarity index 90% rename from src/net/Url.h rename to src/net/Pool.h index 7a4611327..5a2c35297 100644 --- a/src/net/Url.h +++ b/src/net/Pool.h @@ -21,8 +21,8 @@ * along with this program. If not, see . */ -#ifndef __URL_H__ -#define __URL_H__ +#ifndef __POOL_H__ +#define __POOL_H__ #include @@ -32,7 +32,7 @@ #include "xmrig.h" -class Url +class Pool { public: constexpr static const char *kDefaultPassword = "x"; @@ -40,9 +40,9 @@ public: constexpr static uint16_t kDefaultPort = 3333; constexpr static int kKeepAliveTimeout = 60; - Url(); - Url(const char *url); - Url(const char *host, + Pool(); + Pool(const char *url); + Pool(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, @@ -66,11 +66,16 @@ public: inline xmrig::Algo algo() const { return m_algo; } inline xmrig::Variant variant() const { return m_variant; } + inline bool operator!=(const Pool &other) const { return !isEqual(other); } + inline bool operator==(const Pool &other) const { return isEqual(other); } + bool parse(const char *url); bool setUserpass(const char *userpass); void adjust(xmrig::Algo algo); void setVariant(int variant); + bool isEqual(const Pool &other) const; + private: bool parseIPv6(const char *addr); @@ -85,4 +90,4 @@ private: xmrig::Variant m_variant; }; -#endif /* __URL_H__ */ +#endif /* __POOL_H__ */ diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index f0ec5bde7..311ca4241 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -60,17 +60,17 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL Job::toHex(hash, 32, userId); if (algo == xmrig::CRYPTONIGHT) { - m_pools.push_back(new Url(kDonatePool1, 6666, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool1, 80, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool2, 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); + m_pools.push_back(Pool(kDonatePool1, 6666, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool1, 80, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool2, 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false)); } else if (algo == xmrig::CRYPTONIGHT_HEAVY) { - m_pools.push_back(new Url(kDonatePool1, 8888, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool1, 8889, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool1, 8888, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool1, 8889, userId, nullptr, false, true)); } else { - m_pools.push_back(new Url(kDonatePool1, 5555, userId, nullptr, false, true)); - m_pools.push_back(new Url(kDonatePool1, 7777, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool1, 5555, userId, nullptr, false, true)); + m_pools.push_back(Pool(kDonatePool1, 7777, userId, nullptr, false, true)); } m_strategy = new FailoverStrategy(m_pools, 1, 1, this, true); diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 4ef299587..cad360b18 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -32,6 +32,7 @@ #include "interfaces/IClientListener.h" #include "interfaces/IStrategy.h" #include "interfaces/IStrategyListener.h" +#include "net/Pool.h" class Client; @@ -71,7 +72,7 @@ private: const int m_idleTime; IStrategy *m_strategy; IStrategyListener *m_listener; - std::vector m_pools; + std::vector m_pools; uv_timer_t m_timer; }; diff --git a/src/net/strategies/FailoverStrategy.cpp b/src/net/strategies/FailoverStrategy.cpp index dbfeb3114..7a581407c 100644 --- a/src/net/strategies/FailoverStrategy.cpp +++ b/src/net/strategies/FailoverStrategy.cpp @@ -28,7 +28,7 @@ #include "Platform.h" -FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : +FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) : m_quiet(quiet), m_retries(retries), m_retryPause(retryPause), @@ -36,7 +36,7 @@ FailoverStrategy::FailoverStrategy(const std::vector &urls, int retryPause m_index(0), m_listener(listener) { - for (const Url *url : urls) { + for (const Pool &url : urls) { add(url); } } @@ -153,10 +153,10 @@ void FailoverStrategy::onResultAccepted(Client *client, const SubmitResult &resu } -void FailoverStrategy::add(const Url *url) +void FailoverStrategy::add(const Pool &pool) { Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this); - client->setUrl(url); + client->setUrl(pool); client->setRetryPause(m_retryPause * 1000); client->setQuiet(m_quiet); diff --git a/src/net/strategies/FailoverStrategy.h b/src/net/strategies/FailoverStrategy.h index 8ad767ca7..a48496fbb 100644 --- a/src/net/strategies/FailoverStrategy.h +++ b/src/net/strategies/FailoverStrategy.h @@ -30,6 +30,7 @@ #include "interfaces/IClientListener.h" #include "interfaces/IStrategy.h" +#include "net/Pool.h" class Client; @@ -40,7 +41,7 @@ class Url; class FailoverStrategy : public IStrategy, public IClientListener { public: - FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); + FailoverStrategy(const std::vector &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet = false); ~FailoverStrategy(); public: @@ -59,7 +60,7 @@ protected: void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; private: - void add(const Url *url); + void add(const Pool &pool); const bool m_quiet; const int m_retries; diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index fc7f209e0..42a60b30f 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -28,12 +28,12 @@ #include "Platform.h" -SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet) : +SinglePoolStrategy::SinglePoolStrategy(const Pool &pool, int retryPause, IStrategyListener *listener, bool quiet) : m_active(false), m_listener(listener) { m_client = new Client(0, Platform::userAgent(), this); - m_client->setUrl(url); + m_client->setUrl(pool); m_client->setRetryPause(retryPause * 1000); m_client->setQuiet(quiet); } diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h index d5682cf73..ce3d0f7f4 100644 --- a/src/net/strategies/SinglePoolStrategy.h +++ b/src/net/strategies/SinglePoolStrategy.h @@ -37,7 +37,7 @@ class Url; class SinglePoolStrategy : public IStrategy, public IClientListener { public: - SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet = false); + SinglePoolStrategy(const Pool &pool, int retryPause, IStrategyListener *listener, bool quiet = false); ~SinglePoolStrategy(); public: