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

@ -28,9 +28,10 @@
#include "net/Url.h"
Client::Client(IClientListener *listener) :
Client::Client(int id, IClientListener *listener) :
m_host(nullptr),
m_listener(listener),
m_id(id),
m_retries(0),
m_sequence(1),
m_recvBufPos(0),
@ -59,20 +60,9 @@ Client::~Client()
}
/**
* @brief Connect to server.
*
* @param host
* @param port
*/
void Client::connect(const char *host, uint16_t port)
void Client::connect()
{
m_host = strdup(host);
m_port = port;
LOG_DEBUG("[%s:%u] connect", m_host, m_port);
resolve(host);
resolve(m_host);
}
@ -83,7 +73,8 @@ void Client::connect(const char *host, uint16_t port)
*/
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)
{
if (!json_is_object(params)) {
@ -335,6 +334,8 @@ void Client::onClose(uv_handle_t *handle)
client->m_stream = nullptr;
client->m_socket = nullptr;
client->setState(UnconnectedState);
LOG_NOTICE("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
@ -344,6 +345,7 @@ void Client::onConnect(uv_connect_t *req, int status)
if (status < 0) {
LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_host, client->m_port, uv_strerror(status));
free(req);
client->close();
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));
}
client->close();
return;
return client->close();;
}
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);
if (status < 0) {
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);

View file

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

View file

@ -23,30 +23,37 @@
#include <uv.h>
#include <memory>
#include "Console.h"
#include "net/Client.h"
#include "net/Network.h"
#include "net/Url.h"
#include "Options.h"
Network::Network(const Options *options) :
m_backupPool(nullptr),
m_donatePool(nullptr),
m_pool(nullptr),
m_options(options)
m_donate(false),
m_options(options),
m_pool(1)
{
m_pools.reserve(2);
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()
{
delete m_pool;
delete m_donatePool;
delete m_backupPool;
for (auto client : m_pools) {
delete client;
}
free(m_agent);
}
@ -54,8 +61,7 @@ Network::~Network()
void Network::connect()
{
m_pool->connect(m_options->url());
// LOG_DEBUG("XX %s", m_options->url());
m_pools.at(m_pool)->connect();
}
@ -74,3 +80,16 @@ void Network::onLoginCredentialsRequired(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__
#include <vector>
#include "interfaces/IClientListener.h"
class Options;
class Url;
class Network : public IClientListener
@ -47,11 +51,13 @@ protected:
void onLoginSuccess(Client *client) override;
private:
void addPool(const Url *url);
bool m_donate;
char *m_agent;
Client *m_backupPool;
Client *m_donatePool;
Client *m_pool;
const Options *m_options;
int m_pool;
std::vector<Client*> m_pools;
};