Unify network strategies with upcoming proxy.
This commit is contained in:
parent
a2f747fb0c
commit
c46c019c83
10 changed files with 110 additions and 45 deletions
|
@ -26,6 +26,7 @@
|
|||
#include "net/Client.h"
|
||||
#include "net/Job.h"
|
||||
#include "net/strategies/DonateStrategy.h"
|
||||
#include "Platform.h"
|
||||
#include "xmrig.h"
|
||||
|
||||
|
||||
|
@ -35,7 +36,7 @@ extern "C"
|
|||
}
|
||||
|
||||
|
||||
DonateStrategy::DonateStrategy(int level, const char *user, int algo, const char *agent, IStrategyListener *listener) :
|
||||
DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) :
|
||||
m_active(false),
|
||||
m_donateTime(level * 60 * 1000),
|
||||
m_idleTime((100 - level) * 60 * 1000),
|
||||
|
@ -49,7 +50,7 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, const char
|
|||
|
||||
Url *url = new Url("thanks.xmrig.com", algo == xmrig::ALGO_CRYPTONIGHT_LITE ? 5555 : 80, userId, nullptr, false, true);
|
||||
|
||||
m_client = new Client(-1, agent, this);
|
||||
m_client = new Client(-1, Platform::userAgent(), this);
|
||||
m_client->setUrl(url);
|
||||
m_client->setRetryPause(1000);
|
||||
m_client->setQuiet(true);
|
||||
|
@ -75,6 +76,11 @@ void DonateStrategy::connect()
|
|||
}
|
||||
|
||||
|
||||
void DonateStrategy::release()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void DonateStrategy::stop()
|
||||
{
|
||||
uv_timer_stop(&m_timer);
|
||||
|
@ -95,7 +101,7 @@ void DonateStrategy::onClose(Client *client, int failures)
|
|||
|
||||
void DonateStrategy::onJobReceived(Client *client, const Job &job)
|
||||
{
|
||||
m_listener->onJob(client, job);
|
||||
m_listener->onJob(this, client, job);
|
||||
}
|
||||
|
||||
|
||||
|
@ -106,13 +112,13 @@ void DonateStrategy::onLoginSuccess(Client *client)
|
|||
}
|
||||
|
||||
m_active = true;
|
||||
m_listener->onActive(client);
|
||||
m_listener->onActive(this, client);
|
||||
}
|
||||
|
||||
|
||||
void DonateStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
|
||||
{
|
||||
m_listener->onResultAccepted(client, result, error);
|
||||
m_listener->onResultAccepted(this, client, result, error);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ class Url;
|
|||
class DonateStrategy : public IStrategy, public IClientListener
|
||||
{
|
||||
public:
|
||||
DonateStrategy(int level, const char *user, int algo, const char *agent, IStrategyListener *listener);
|
||||
DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener);
|
||||
|
||||
public:
|
||||
inline bool isActive() const override { return m_active; }
|
||||
|
@ -48,6 +48,7 @@ public:
|
|||
|
||||
int64_t submit(const JobResult &result) override;
|
||||
void connect() override;
|
||||
void release() override;
|
||||
void stop() override;
|
||||
void tick(uint64_t now) override;
|
||||
|
||||
|
|
|
@ -25,17 +25,28 @@
|
|||
#include "interfaces/IStrategyListener.h"
|
||||
#include "net/Client.h"
|
||||
#include "net/strategies/FailoverStrategy.h"
|
||||
#include "Platform.h"
|
||||
|
||||
|
||||
FailoverStrategy::FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, const char *agent, IStrategyListener *listener) :
|
||||
FailoverStrategy::FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener) :
|
||||
m_release(false),
|
||||
m_retries(retries),
|
||||
m_retryPause(retryPause),
|
||||
m_active(-1),
|
||||
m_index(0),
|
||||
m_remaining(0),
|
||||
m_listener(listener)
|
||||
{
|
||||
for (const Url *url : urls) {
|
||||
add(url, agent);
|
||||
add(url);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
FailoverStrategy::~FailoverStrategy()
|
||||
{
|
||||
for (Client *client : m_pools) {
|
||||
delete client;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,13 +63,25 @@ void FailoverStrategy::connect()
|
|||
}
|
||||
|
||||
|
||||
void FailoverStrategy::release()
|
||||
{
|
||||
m_release = true;
|
||||
|
||||
for (size_t i = 0; i < m_pools.size(); ++i) {
|
||||
if (m_pools[i]->disconnect()) {
|
||||
m_remaining++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FailoverStrategy::resume()
|
||||
{
|
||||
if (!isActive()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_listener->onJob( m_pools[m_active], m_pools[m_active]->job());
|
||||
m_listener->onJob(this, m_pools[m_active], m_pools[m_active]->job());
|
||||
}
|
||||
|
||||
|
||||
|
@ -86,6 +109,14 @@ void FailoverStrategy::tick(uint64_t now)
|
|||
void FailoverStrategy::onClose(Client *client, int failures)
|
||||
{
|
||||
if (failures == -1) {
|
||||
if (m_release) {
|
||||
m_remaining--;
|
||||
|
||||
if (m_remaining == 0) {
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -107,7 +138,7 @@ void FailoverStrategy::onClose(Client *client, int failures)
|
|||
void FailoverStrategy::onJobReceived(Client *client, const Job &job)
|
||||
{
|
||||
if (m_active == client->id()) {
|
||||
m_listener->onJob(client, job);
|
||||
m_listener->onJob(this, client, job);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -128,20 +159,20 @@ void FailoverStrategy::onLoginSuccess(Client *client)
|
|||
|
||||
if (active >= 0 && active != m_active) {
|
||||
m_index = m_active = active;
|
||||
m_listener->onActive(client);
|
||||
m_listener->onActive(this, client);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FailoverStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
|
||||
{
|
||||
m_listener->onResultAccepted(client, result, error);
|
||||
m_listener->onResultAccepted(this, client, result, error);
|
||||
}
|
||||
|
||||
|
||||
void FailoverStrategy::add(const Url *url, const char *agent)
|
||||
void FailoverStrategy::add(const Url *url)
|
||||
{
|
||||
Client *client = new Client((int) m_pools.size(), agent, this);
|
||||
Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this);
|
||||
client->setUrl(url);
|
||||
client->setRetryPause(m_retryPause * 1000);
|
||||
|
||||
|
|
|
@ -40,13 +40,15 @@ class Url;
|
|||
class FailoverStrategy : public IStrategy, public IClientListener
|
||||
{
|
||||
public:
|
||||
FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, const char *agent, IStrategyListener *listener);
|
||||
FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener);
|
||||
~FailoverStrategy();
|
||||
|
||||
public:
|
||||
inline bool isActive() const override { return m_active >= 0; }
|
||||
|
||||
int64_t submit(const JobResult &result) override;
|
||||
void connect() override;
|
||||
void release() override;
|
||||
void resume() override;
|
||||
void stop() override;
|
||||
void tick(uint64_t now) override;
|
||||
|
@ -58,12 +60,14 @@ protected:
|
|||
void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override;
|
||||
|
||||
private:
|
||||
void add(const Url *url, const char *agent);
|
||||
void add(const Url *url);
|
||||
|
||||
bool m_release;
|
||||
const int m_retries;
|
||||
const int m_retryPause;
|
||||
int m_active;
|
||||
int m_index;
|
||||
int m_remaining;
|
||||
IStrategyListener *m_listener;
|
||||
std::vector<Client*> m_pools;
|
||||
};
|
||||
|
|
|
@ -25,18 +25,26 @@
|
|||
#include "interfaces/IStrategyListener.h"
|
||||
#include "net/Client.h"
|
||||
#include "net/strategies/SinglePoolStrategy.h"
|
||||
#include "Platform.h"
|
||||
|
||||
|
||||
SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, const char *agent, IStrategyListener *listener) :
|
||||
SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener) :
|
||||
m_active(false),
|
||||
m_release(false),
|
||||
m_listener(listener)
|
||||
{
|
||||
m_client = new Client(0, agent, this);
|
||||
m_client = new Client(0, Platform::userAgent(), this);
|
||||
m_client->setUrl(url);
|
||||
m_client->setRetryPause(retryPause * 1000);
|
||||
}
|
||||
|
||||
|
||||
SinglePoolStrategy::~SinglePoolStrategy()
|
||||
{
|
||||
delete m_client;
|
||||
}
|
||||
|
||||
|
||||
int64_t SinglePoolStrategy::submit(const JobResult &result)
|
||||
{
|
||||
return m_client->submit(result);
|
||||
|
@ -49,13 +57,20 @@ void SinglePoolStrategy::connect()
|
|||
}
|
||||
|
||||
|
||||
void SinglePoolStrategy::release()
|
||||
{
|
||||
m_release = true;
|
||||
m_client->disconnect();
|
||||
}
|
||||
|
||||
|
||||
void SinglePoolStrategy::resume()
|
||||
{
|
||||
if (!isActive()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_listener->onJob(m_client, m_client->job());
|
||||
m_listener->onJob(this, m_client, m_client->job());
|
||||
}
|
||||
|
||||
|
||||
|
@ -73,6 +88,11 @@ void SinglePoolStrategy::tick(uint64_t now)
|
|||
|
||||
void SinglePoolStrategy::onClose(Client *client, int failures)
|
||||
{
|
||||
if (m_release) {
|
||||
delete this;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isActive()) {
|
||||
return;
|
||||
}
|
||||
|
@ -84,18 +104,18 @@ void SinglePoolStrategy::onClose(Client *client, int failures)
|
|||
|
||||
void SinglePoolStrategy::onJobReceived(Client *client, const Job &job)
|
||||
{
|
||||
m_listener->onJob(client, job);
|
||||
m_listener->onJob(this, client, job);
|
||||
}
|
||||
|
||||
|
||||
void SinglePoolStrategy::onLoginSuccess(Client *client)
|
||||
{
|
||||
m_active = true;
|
||||
m_listener->onActive(client);
|
||||
m_listener->onActive(this, client);
|
||||
}
|
||||
|
||||
|
||||
void SinglePoolStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
|
||||
{
|
||||
m_listener->onResultAccepted(client, result, error);
|
||||
m_listener->onResultAccepted(this, client, result, error);
|
||||
}
|
||||
|
|
|
@ -37,13 +37,15 @@ class Url;
|
|||
class SinglePoolStrategy : public IStrategy, public IClientListener
|
||||
{
|
||||
public:
|
||||
SinglePoolStrategy(const Url *url, int retryPause, const char *agent, IStrategyListener *listener);
|
||||
SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener);
|
||||
~SinglePoolStrategy();
|
||||
|
||||
public:
|
||||
inline bool isActive() const override { return m_active; }
|
||||
|
||||
int64_t submit(const JobResult &result) override;
|
||||
void connect() override;
|
||||
void release() override;
|
||||
void resume() override;
|
||||
void stop() override;
|
||||
void tick(uint64_t now) override;
|
||||
|
@ -56,6 +58,7 @@ protected:
|
|||
|
||||
private:
|
||||
bool m_active;
|
||||
bool m_release;
|
||||
Client *m_client;
|
||||
IStrategyListener *m_listener;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue