Implemented switch to donate pool.

This commit is contained in:
XMRig 2017-06-07 22:34:23 +03:00
parent c31ea00399
commit 1cf5ad5212
7 changed files with 80 additions and 14 deletions

View file

@ -22,7 +22,6 @@
*/
#include <uv.h>
#include <memory>
@ -41,11 +40,12 @@ Network::Network(const Options *options) :
m_pools.reserve(2);
m_agent = userAgent();
auto url = std::make_unique<Url>("donate.xmrig.com", 443);
addPool(url.get());
addPool(std::make_unique<Url>().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<Network*>(handle->data);
if (!net->m_donate) {
auto url = std::make_unique<Url>("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);
}