From 952017ae7a3164d76b12af3b1b121a11915d9216 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 26 Jun 2017 21:13:05 +0300 Subject: [PATCH] Initial multiple pools support [1/2]. --- src/Options.cpp | 73 ++++++++++++++---------------- src/Options.h | 44 +++++++++---------- src/Summary.cpp | 19 ++++---- src/net/Network.cpp | 6 +-- src/net/Url.cpp | 105 +++++++++++++++++++++++++++++++------------- src/net/Url.h | 26 ++++++++--- 6 files changed, 162 insertions(+), 111 deletions(-) diff --git a/src/Options.cpp b/src/Options.cpp index 5929d062..3c636cec 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -150,7 +150,6 @@ Options::Options(int argc, char **argv) : m_syslog(false), m_logFile(nullptr), m_pass(nullptr), - m_user(nullptr), m_algo(0), m_algoVariant(0), m_donateLevel(kDonateLevel), @@ -159,10 +158,10 @@ Options::Options(int argc, char **argv) : m_retries(5), m_retryPause(5), m_threads(0), - m_affinity(-1L), - m_backupUrl(nullptr), - m_url(nullptr) + m_affinity(-1L) { + m_pools.push_back(new Url()); + int key; while (1) { @@ -181,18 +180,18 @@ Options::Options(int argc, char **argv) : return; } - if (!m_url) { + if (!m_pools[0]->isValid()) { fprintf(stderr, "No pool URL supplied. Exiting."); return; } - if (!m_nicehash && m_url->isNicehash()) { - m_nicehash = true; - } +// if (!m_nicehash && m_url->isNicehash()) { +// m_nicehash = true; +// } - if (!m_user) { - m_user = strdup("x"); - } +// if (!m_user) { +// m_user = strdup("x"); +// } if (!m_pass) { m_pass = strdup("x"); @@ -219,10 +218,6 @@ Options::Options(int argc, char **argv) : Options::~Options() { - delete m_url; - delete m_backupUrl; - - free(m_user); free(m_pass); } @@ -232,7 +227,6 @@ bool Options::parseArg(int key, char *arg) char *p; int v; uint64_t ul; - Url *url; switch (key) { case 'a': /* --algo */ @@ -248,29 +242,26 @@ bool Options::parseArg(int key, char *arg) break; case 'o': /* --url */ - url = parseUrl(arg); - if (url) { - free(m_url); - m_url = url; + if (m_pools[0]->isValid()) { + Url *url = new Url(arg); + if (url->isValid()) { + m_pools.push_back(url); + } + else { + delete url; + } } - break; - - case 'b': /* --backup-url */ - url = parseUrl(arg); - if (url) { - free(m_backupUrl); - m_backupUrl = url; + else { + m_pools[0]->parse(arg); } break; case 'u': /* --user */ - free(m_user); - m_user = strdup(arg); + m_pools.back()->setUser(arg); break; case 'p': /* --pass */ - free(m_pass); - m_pass = strdup(arg); + m_pools.back()->setPassword(arg); break; case 'l': /* --log-file */ @@ -485,18 +476,18 @@ bool Options::setAlgo(const char *algo) bool Options::setUserpass(const char *userpass) { - const char *p = strchr(userpass, ':'); - if (!p) { - showUsage(1); - return false; - } +// const char *p = strchr(userpass, ':'); +// if (!p) { +// showUsage(1); +// return false; +// } - free(m_user); - free(m_pass); +//// free(m_user); +// free(m_pass); - m_user = static_cast(calloc(p - userpass + 1, 1)); - strncpy(m_user, userpass, p - userpass); - m_pass = strdup(p + 1); +//// m_user = static_cast(calloc(p - userpass + 1, 1)); +// strncpy(m_user, userpass, p - userpass); +// m_pass = strdup(p + 1); return true; } diff --git a/src/Options.h b/src/Options.h index 907819b7..e2e6c9f9 100644 --- a/src/Options.h +++ b/src/Options.h @@ -25,6 +25,7 @@ #define __OPTIONS_H__ +#include #include @@ -51,26 +52,24 @@ public: static inline Options* i() { return m_self; } static Options *parse(int argc, char **argv); - inline bool background() const { return m_background; } - inline bool colors() const { return m_colors; } - inline bool doubleHash() const { return m_doubleHash; } - inline bool isReady() const { return m_ready; } - inline bool keepAlive() const { return m_keepAlive; } - inline bool nicehash() const { return m_nicehash; } - inline bool syslog() const { return m_syslog; } - inline const char *logFile() const { return m_logFile; } - inline const char *pass() const { return m_pass; } - inline const char *user() const { return m_user; } - inline const Url *backupUrl() const { return m_backupUrl; } - inline const Url *url() const { return m_url; } - inline int algo() const { return m_algo; } - inline int algoVariant() const { return m_algoVariant; } - inline int donateLevel() const { return m_donateLevel; } - inline int printTime() const { return m_printTime; } - inline int retries() const { return m_retries; } - inline int retryPause() const { return m_retryPause; } - inline int threads() const { return m_threads; } - inline int64_t affinity() const { return m_affinity; } + inline bool background() const { return m_background; } + inline bool colors() const { return m_colors; } + inline bool doubleHash() const { return m_doubleHash; } + inline bool isReady() const { return m_ready; } + inline bool keepAlive() const { return m_keepAlive; } + inline bool nicehash() const { return m_nicehash; } + inline bool syslog() const { return m_syslog; } + inline const char *logFile() const { return m_logFile; } + inline const char *pass() const { return m_pass; } + inline const std::vector &pools() const { return m_pools; } + inline int algo() const { return m_algo; } + inline int algoVariant() const { return m_algoVariant; } + inline int donateLevel() const { return m_donateLevel; } + inline int printTime() const { return m_printTime; } + inline int retries() const { return m_retries; } + inline int retryPause() const { return m_retryPause; } + inline int threads() const { return m_threads; } + inline int64_t affinity() const { return m_affinity; } const char *algoName() const; @@ -103,7 +102,7 @@ private: bool m_syslog; char *m_logFile; char *m_pass; - char *m_user; +// char *m_user; int m_algo; int m_algoVariant; int m_donateLevel; @@ -113,8 +112,7 @@ private: int m_retryPause; int m_threads; int64_t m_affinity; - Url *m_backupUrl; - Url *m_url; + std::vector m_pools; }; #endif /* __OPTIONS_H__ */ diff --git a/src/Summary.cpp b/src/Summary.cpp index a243d9de..54630e8c 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -111,17 +111,20 @@ static void print_threads() static void print_pools() { - Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mPOOL #1: \x1B[01;36m%s:%d" : " * POOL #1: %s:%d", - Options::i()->url()->host(), - Options::i()->url()->port()); + const std::vector &pools = Options::i()->pools(); - if (!Options::i()->backupUrl()) { - return; + for (size_t i = 0; i < pools.size(); ++i) { + Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mPOOL #%d: \x1B[01;36m%s:%d" : " * POOL #%d: %s:%d", + i + 1, + pools[i]->host(), + pools[i]->port()); } - Log::i()->text(Options::i()->colors() ? "\x1B[01;32m * \x1B[01;37mPOOL #2: \x1B[01;36m%s:%d" : " * POOL #2: %s:%d", - Options::i()->backupUrl()->host(), - Options::i()->backupUrl()->port()); +# ifdef APP_DEBUG + for (size_t i = 0; i < pools.size(); ++i) { + Log::i()->text("%s:%d, user: %s, pass: %s, ka: %d, nicehash: %d", pools[i]->host(), pools[i]->port(), pools[i]->user(), pools[i]->password(), pools[i]->isKeepAlive(), pools[i]->isNicehash()); + } +# endif } diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 8de785bd..903ed826 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -45,8 +45,8 @@ Network::Network(const Options *options) : m_agent = userAgent(); addPool(std::make_unique().get()); - addPool(m_options->url()); - addPool(m_options->backupUrl()); +// addPool(m_options->url()); +// addPool(m_options->backupUrl()); m_timer.data = this; uv_timer_init(uv_default_loop(), &m_timer); @@ -120,7 +120,7 @@ void Network::onJobResult(const JobResult &result) void Network::onLoginCredentialsRequired(Client *client) { - client->login(m_options->user(), m_options->pass(), m_agent); +// client->login(m_options->user(), m_options->pass(), m_agent); } diff --git a/src/net/Url.cpp b/src/net/Url.cpp index d8264c20..96376d05 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -35,8 +35,12 @@ Url::Url() : + m_keepAlive(false), + m_nicehash(false), m_host(nullptr), - m_port(3333) + m_password(nullptr), + m_user(nullptr), + m_port(kDefaultPort) { } @@ -53,40 +57,22 @@ Url::Url() : * @param url */ Url::Url(const char *url) : + m_keepAlive(false), + m_nicehash(false), m_host(nullptr), - m_port(3333) + m_password(nullptr), + m_user(nullptr), + m_port(kDefaultPort) { - const char *p = strstr(url, "://"); - const char *base = url; - - if (p) { - if (strncasecmp(url, "stratum+tcp://", 14)) { - return; - } - - base = url + 14; - } - - if (!strlen(base) || *base == '/') { - return; - } - - const char *port = strchr(base, ':'); - if (!port) { - m_host = strdup(base); - return; - } - - const size_t size = port++ - base + 1; - m_host = static_cast(malloc(size)); - memcpy(m_host, base, size - 1); - m_host[size - 1] = '\0'; - - m_port = strtol(port, nullptr, 10); + parse(url); } -Url::Url(const char *host, uint16_t port) : +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_port(port) { m_host = strdup(host); @@ -96,10 +82,67 @@ Url::Url(const char *host, uint16_t port) : Url::~Url() { free(m_host); + free(m_password); + free(m_user); } bool Url::isNicehash() const { - return isValid() && strstr(m_host, ".nicehash.com"); + return isValid() && (m_nicehash || strstr(m_host, ".nicehash.com")); +} + + +bool Url::parse(const char *url) +{ + const char *p = strstr(url, "://"); + const char *base = url; + + if (p) { + if (strncasecmp(url, "stratum+tcp://", 14)) { + return false; + } + + base = url + 14; + } + + if (!strlen(base) || *base == '/') { + return false; + } + + const char *port = strchr(base, ':'); + if (!port) { + m_host = strdup(base); + return false; + } + + const size_t size = port++ - base + 1; + m_host = static_cast(malloc(size)); + memcpy(m_host, base, size - 1); + m_host[size - 1] = '\0'; + + m_port = strtol(port, nullptr, 10); + return true; +} + + +void Url::setPassword(const char *password, bool force) +{ + if (m_password != nullptr && !force) { + return; + } + + free(m_password); + m_password = strdup(password); +} + + +void Url::setUser(const char *user, bool force) +{ + if (m_user != nullptr && !force) { + return; + } + + free(m_user); + m_user = strdup(user); } diff --git a/src/net/Url.h b/src/net/Url.h index 7d44501d..bac2a564 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -31,19 +31,35 @@ class Url { public: + constexpr static const char *kDefaultPassword = "x"; + constexpr static const char *kDefaultUser = "x"; + constexpr static uint16_t kDefaultPort = 3333; + Url(); Url(const char *url); - Url(const char *host, uint16_t port); + Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false ); ~Url(); - bool isNicehash() const; + inline bool isKeepAlive() const { return m_keepAlive; } + inline bool isValid() const { return m_host && m_port > 0; } + inline const char *host() const { return m_host; } + inline const char *password() const { return m_password ? m_password : kDefaultPassword; } + inline const char *user() const { return m_user ? m_user : kDefaultUser; } + inline uint16_t port() const { return m_port; } + inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; } + inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } - inline bool isValid() const { return m_host && m_port > 0; } - inline const char *host() const { return m_host; } - inline uint16_t port() const { return m_port; } + bool isNicehash() const; + bool parse(const char *url); + void setPassword(const char *password, bool force = true); + void setUser(const char *user, bool force = true); private: + bool m_keepAlive; + bool m_nicehash; char *m_host; + char *m_password; + char *m_user; uint16_t m_port; };