From 1cf5ad5212b6837227a5c0ce99db0cfc3e67db8d Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Jun 2017 22:34:23 +0300 Subject: [PATCH] Implemented switch to donate pool. --- CMakeLists.txt | 2 +- src/Options.h | 1 + src/net/Client.cpp | 6 +++- src/net/Network.cpp | 71 +++++++++++++++++++++++++++++++++++++-------- src/net/Network.h | 6 ++++ src/net/Url.cpp | 7 +++++ src/net/Url.h | 1 + 7 files changed, 80 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dc3feb367..82641462b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,7 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "") set(CMAKE_BUILD_TYPE Release) endif() -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-2") #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-generate") diff --git a/src/Options.h b/src/Options.h index b2b1966df..16733ccab 100644 --- a/src/Options.h +++ b/src/Options.h @@ -57,6 +57,7 @@ public: 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 donateLevel() const { return m_donateLevel; } inline int retries() const { return m_retries; } inline int retryPause() const { return m_retryPause; } diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 38e7df25f..8fdf09e98 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -129,12 +129,16 @@ void Client::send(char *data) free(req); }); - uv_timer_start(&m_responseTimer, [](uv_timer_t* handle) { getClient(handle->data)->close(); }, kResponseTimeout, 0); + uv_timer_start(&m_responseTimer, [](uv_timer_t *handle) { getClient(handle->data)->close(); }, kResponseTimeout, 0); } void Client::setUrl(const Url *url) { + if (!url || !url->isValid()) { + return; + } + free(m_host); m_host = strdup(url->host()); m_port = url->port(); diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 692cfa7ad..d513fddae 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -22,7 +22,6 @@ */ -#include #include @@ -41,11 +40,12 @@ Network::Network(const Options *options) : m_pools.reserve(2); m_agent = userAgent(); - auto url = std::make_unique("donate.xmrig.com", 443); - - addPool(url.get()); + addPool(std::make_unique().get()); addPool(m_options->url()); addPool(m_options->backupUrl()); + + m_timer.data = this; + uv_timer_init(uv_default_loop(), &m_timer); } @@ -61,15 +61,22 @@ Network::~Network() void Network::connect() { - m_pools.at(1)->connect(); + m_pools[1]->connect(); + + if (m_options->donateLevel()) { + uv_timer_start(&m_timer, Network::onTimer, (100 - m_options->donateLevel()) * 60 * 1000, 0); + } } void Network::onClose(Client *client, int failures) { const int id = client->id(); - if (id == 0 && failures == -1) { - m_donate = false; + if (id == 0) { + if (failures == -1) { + stopDonate(); + } + return; } @@ -78,7 +85,7 @@ void Network::onClose(Client *client, int failures) } if (id == 1 && m_pools.size() > 2 && failures == m_options->retries()) { - m_pools.at(2)->connect(); + m_pools[2]->connect(); } } @@ -99,12 +106,11 @@ void Network::onLoginSuccess(Client *client) { const int id = client->id(); if (id == 0) { - m_donate = true; - return; + return startDonate(); } if (id == 2 && m_pool) { // primary pool is already active - m_pools.at(2)->disconnect(); + m_pools[2]->disconnect(); return; } @@ -112,7 +118,7 @@ void Network::onLoginSuccess(Client *client) m_pool = id; if (m_pool == 1 && m_pools.size() > 2) { // try disconnect from backup pool - m_pools.at(2)->disconnect(); + m_pools[2]->disconnect(); } } @@ -130,3 +136,44 @@ void Network::addPool(const Url *url) m_pools.push_back(client); } + + +void Network::startDonate() +{ + if (m_donate) { + return; + } + + LOG_NOTICE("dev donate started"); + + m_donate = true; +} + + +void Network::stopDonate() +{ + if (!m_donate) { + return; + } + + LOG_NOTICE("dev donate finished"); + + m_donate = false; +} + + +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", 443); + net->m_pools[0]->connect(url.get()); + + 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); +} diff --git a/src/net/Network.h b/src/net/Network.h index 89787012f..c8191a6ea 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -26,6 +26,7 @@ #include +#include #include "interfaces/IClientListener.h" @@ -53,12 +54,17 @@ protected: private: void addPool(const Url *url); + void startDonate(); + void stopDonate(); + + static void onTimer(uv_timer_t *handle); bool m_donate; char *m_agent; const Options *m_options; int m_pool; std::vector m_pools; + uv_timer_t m_timer; }; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index f54b0b92c..4e1dfd9fe 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -29,6 +29,13 @@ #include "net/Url.h" +Url::Url() : + m_host(nullptr), + m_port(3333) +{ +} + + /** * @brief Parse url. * diff --git a/src/net/Url.h b/src/net/Url.h index a0e2d48d9..7d44501d2 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -31,6 +31,7 @@ class Url { public: + Url(); Url(const char *url); Url(const char *host, uint16_t port); ~Url();