More flexible pools configuration.

This commit is contained in:
XMRig 2017-06-07 02:46:52 +03:00
parent 4c06d8b080
commit b8cc1136a4
8 changed files with 83 additions and 40 deletions

View file

@ -48,7 +48,6 @@ App::App(int argc, char **argv)
{ {
Console::init(); Console::init();
m_options = Options::parse(argc, argv); m_options = Options::parse(argc, argv);
m_network = new Network(m_options); m_network = new Network(m_options);
} }

View file

@ -128,7 +128,7 @@ Options::Options(int argc, char **argv) :
m_user(nullptr), m_user(nullptr),
m_algo(0), m_algo(0),
m_algoVariant(0), m_algoVariant(0),
m_donateLevel(DONATE_LEVEL), m_donateLevel(kDonateLevel),
m_maxCpuUsage(75), m_maxCpuUsage(75),
m_retries(5), m_retries(5),
m_retryPause(5), m_retryPause(5),

View file

@ -51,10 +51,11 @@ public:
static inline Options* i() { return m_self; } static inline Options* i() { return m_self; }
static Options *parse(int argc, char **argv); static Options *parse(int argc, char **argv);
inline bool isReady() const { return m_ready; } inline bool isReady() const { return m_ready; }
inline const char *pass() const { return m_pass; } inline const char *pass() const { return m_pass; }
inline const char *user() const { return m_user; } inline const char *user() const { return m_user; }
inline Url *url() const { return m_url; } inline const Url *backupUrl() const { return m_backupUrl; }
inline const Url *url() const { return m_url; }
private: private:
Options(int argc, char **argv); Options(int argc, char **argv);

View file

@ -24,6 +24,20 @@
#ifndef __DONATE_H__ #ifndef __DONATE_H__
#define __DONATE_H__ #define __DONATE_H__
#define DONATE_LEVEL 5
/*
* Dev donation.
*
* Percentage of your hashing power that you want to donate to the developer, can be 0 if you don't want to do that.
* Example of how it works for the default setting of 1:
* You miner will mine into your usual pool for 99 minutes, then switch to the developer's pool for 1 minute.
* Switching is instant, and only happens after a successful connection, so you never loose any hashes.
*
* If you plan on changing this setting to 0 please consider making a one off donation to my wallet:
* XMR: 48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD
* BTC: 1P7ujsXeX7GxQwHNnJsRMgAdNkFZmNVqJT
*/
constexpr const int kDonateLevel = 5;
#endif /* __DONATE_H__ */ #endif /* __DONATE_H__ */

View file

@ -28,9 +28,10 @@
#include "net/Url.h" #include "net/Url.h"
Client::Client(IClientListener *listener) : Client::Client(int id, IClientListener *listener) :
m_host(nullptr), m_host(nullptr),
m_listener(listener), m_listener(listener),
m_id(id),
m_retries(0), m_retries(0),
m_sequence(1), m_sequence(1),
m_recvBufPos(0), m_recvBufPos(0),
@ -59,20 +60,9 @@ Client::~Client()
} }
/** void Client::connect()
* @brief Connect to server.
*
* @param host
* @param port
*/
void Client::connect(const char *host, uint16_t port)
{ {
m_host = strdup(host); resolve(m_host);
m_port = port;
LOG_DEBUG("[%s:%u] connect", m_host, m_port);
resolve(host);
} }
@ -83,7 +73,8 @@ void Client::connect(const char *host, uint16_t port)
*/ */
void Client::connect(const Url *url) void Client::connect(const Url *url)
{ {
connect(url->host(), url->port()); setUrl(url);
resolve(m_host);
} }
@ -136,6 +127,14 @@ void Client::send(char *data)
} }
void Client::setUrl(const Url *url)
{
free(m_host);
m_host = strdup(url->host());
m_port = url->port();
}
bool Client::parseJob(const json_t *params, int *code) bool Client::parseJob(const json_t *params, int *code)
{ {
if (!json_is_object(params)) { if (!json_is_object(params)) {
@ -335,6 +334,8 @@ void Client::onClose(uv_handle_t *handle)
client->m_stream = nullptr; client->m_stream = nullptr;
client->m_socket = nullptr; client->m_socket = nullptr;
client->setState(UnconnectedState); client->setState(UnconnectedState);
LOG_NOTICE("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
} }
@ -344,6 +345,7 @@ void Client::onConnect(uv_connect_t *req, int status)
if (status < 0) { if (status < 0) {
LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_host, client->m_port, uv_strerror(status)); LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_host, client->m_port, uv_strerror(status));
free(req); free(req);
client->close();
return; return;
} }
@ -366,8 +368,7 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
LOG_ERR("[%s:%u] read error: \"%s\"", client->m_host, client->m_port, uv_strerror(nread)); LOG_ERR("[%s:%u] read error: \"%s\"", client->m_host, client->m_port, uv_strerror(nread));
} }
client->close(); return client->close();;
return;
} }
client->m_recvBufPos += nread; client->m_recvBufPos += nread;
@ -404,7 +405,7 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res)
auto client = getClient(req->data); auto client = getClient(req->data);
if (status < 0) { if (status < 0) {
LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_host, client->m_port, uv_strerror(status)); LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_host, client->m_port, uv_strerror(status));
return; return client->close();;
} }
client->connect(res->ai_addr); client->connect(res->ai_addr);

View file

@ -47,15 +47,17 @@ public:
ClosingState ClosingState
}; };
Client(IClientListener *listener); Client(int id, IClientListener *listener);
~Client(); ~Client();
void connect(const char *host, uint16_t port); void connect();
void connect(const Url *url); void connect(const Url *url);
void disconnect(); void disconnect();
void login(const char *user, const char *pass, const char *agent); void login(const char *user, const char *pass, const char *agent);
void send(char *data); void send(char *data);
void setUrl(const Url *url);
inline int id() const { return m_id; }
inline SocketState state() const { return m_state; } inline SocketState state() const { return m_state; }
private: private:
@ -82,6 +84,7 @@ private:
char *m_host; char *m_host;
char m_rpcId[64]; char m_rpcId[64];
IClientListener *m_listener; IClientListener *m_listener;
int m_id;
int64_t m_retries; int64_t m_retries;
int64_t m_sequence; int64_t m_sequence;
Job m_job; Job m_job;

View file

@ -23,30 +23,37 @@
#include <uv.h> #include <uv.h>
#include <memory>
#include "Console.h" #include "Console.h"
#include "net/Client.h" #include "net/Client.h"
#include "net/Network.h" #include "net/Network.h"
#include "net/Url.h"
#include "Options.h" #include "Options.h"
Network::Network(const Options *options) : Network::Network(const Options *options) :
m_backupPool(nullptr), m_donate(false),
m_donatePool(nullptr), m_options(options),
m_pool(nullptr), m_pool(1)
m_options(options)
{ {
m_pools.reserve(2);
m_agent = userAgent(); m_agent = userAgent();
m_pool = new Client(this);
std::unique_ptr<Url> url(new Url("donate.xmrig.com", 443));
addPool(url.get());
addPool(m_options->url());
addPool(m_options->backupUrl());
} }
Network::~Network() Network::~Network()
{ {
delete m_pool; for (auto client : m_pools) {
delete m_donatePool; delete client;
delete m_backupPool; }
free(m_agent); free(m_agent);
} }
@ -54,8 +61,7 @@ Network::~Network()
void Network::connect() void Network::connect()
{ {
m_pool->connect(m_options->url()); m_pools.at(m_pool)->connect();
// LOG_DEBUG("XX %s", m_options->url());
} }
@ -74,3 +80,16 @@ void Network::onLoginCredentialsRequired(Client *client)
void Network::onLoginSuccess(Client *client) void Network::onLoginSuccess(Client *client)
{ {
} }
void Network::addPool(const Url *url)
{
if (!url) {
return;
}
Client *client = new Client(m_pools.size(), this);
client->setUrl(url);
m_pools.push_back(client);
}

View file

@ -25,10 +25,14 @@
#define __NETWORK_H__ #define __NETWORK_H__
#include <vector>
#include "interfaces/IClientListener.h" #include "interfaces/IClientListener.h"
class Options; class Options;
class Url;
class Network : public IClientListener class Network : public IClientListener
@ -47,11 +51,13 @@ protected:
void onLoginSuccess(Client *client) override; void onLoginSuccess(Client *client) override;
private: private:
void addPool(const Url *url);
bool m_donate;
char *m_agent; char *m_agent;
Client *m_backupPool;
Client *m_donatePool;
Client *m_pool;
const Options *m_options; const Options *m_options;
int m_pool;
std::vector<Client*> m_pools;
}; };