mirror of
https://github.com/xmrig/xmrig.git
synced 2024-11-17 16:27:44 +00:00
Use Url inside Client. Remove onLoginCredentialsRequired from IClientListener interface.
This commit is contained in:
parent
c0dcfc2a97
commit
b2d26eb019
7 changed files with 66 additions and 71 deletions
|
@ -36,7 +36,6 @@ public:
|
|||
|
||||
virtual void onClose(Client *client, int failures) = 0;
|
||||
virtual void onJobReceived(Client *client, const Job &job) = 0;
|
||||
virtual void onLoginCredentialsRequired(Client *client) = 0;
|
||||
virtual void onLoginSuccess(Client *client) = 0;
|
||||
};
|
||||
|
||||
|
|
|
@ -37,9 +37,8 @@
|
|||
#endif
|
||||
|
||||
|
||||
Client::Client(int id, IClientListener *listener) :
|
||||
m_keepAlive(false),
|
||||
m_host(nullptr),
|
||||
Client::Client(int id, const char *agent, IClientListener *listener) :
|
||||
m_agent(agent),
|
||||
m_listener(listener),
|
||||
m_id(id),
|
||||
m_retryPause(5000),
|
||||
|
@ -47,7 +46,6 @@ Client::Client(int id, IClientListener *listener) :
|
|||
m_sequence(1),
|
||||
m_recvBufPos(0),
|
||||
m_state(UnconnectedState),
|
||||
m_port(0),
|
||||
m_stream(nullptr),
|
||||
m_socket(nullptr)
|
||||
{
|
||||
|
@ -72,13 +70,12 @@ Client::~Client()
|
|||
{
|
||||
free(m_recvBuf.base);
|
||||
free(m_socket);
|
||||
free(m_host);
|
||||
}
|
||||
|
||||
|
||||
void Client::connect()
|
||||
{
|
||||
resolve(m_host);
|
||||
resolve(m_url.host());
|
||||
}
|
||||
|
||||
|
||||
|
@ -90,7 +87,7 @@ void Client::connect()
|
|||
void Client::connect(const Url *url)
|
||||
{
|
||||
setUrl(url);
|
||||
resolve(m_host);
|
||||
resolve(m_url.host());
|
||||
}
|
||||
|
||||
|
||||
|
@ -102,18 +99,6 @@ void Client::disconnect()
|
|||
}
|
||||
|
||||
|
||||
void Client::login(const char *user, const char *pass, const char *agent)
|
||||
{
|
||||
m_sequence = 1;
|
||||
|
||||
const size_t size = 96 + strlen(user) + strlen(pass) + strlen(agent);
|
||||
char *req = static_cast<char*>(malloc(size));
|
||||
snprintf(req, size, "{\"id\":%llu,\"jsonrpc\":\"2.0\",\"method\":\"login\",\"params\":{\"login\":\"%s\",\"pass\":\"%s\",\"agent\":\"%s\"}}\n", m_sequence, user, pass, agent);
|
||||
|
||||
send(req);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Send raw data to server.
|
||||
*
|
||||
|
@ -121,9 +106,9 @@ void Client::login(const char *user, const char *pass, const char *agent)
|
|||
*/
|
||||
void Client::send(char *data)
|
||||
{
|
||||
LOG_DEBUG("[%s:%u] send (%d bytes): \"%s\"", m_host, m_port, strlen(data), data);
|
||||
LOG_DEBUG("[%s:%u] send (%d bytes): \"%s\"", m_url.host(), m_url.port(), strlen(data), data);
|
||||
if (state() != ConnectedState) {
|
||||
LOG_DEBUG_ERR("[%s:%u] send failed, invalid state: %d", m_host, m_port, m_state);
|
||||
LOG_DEBUG_ERR("[%s:%u] send failed, invalid state: %d", m_url.host(), m_url.port(), m_state);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -148,9 +133,7 @@ void Client::setUrl(const Url *url)
|
|||
return;
|
||||
}
|
||||
|
||||
free(m_host);
|
||||
m_host = strdup(url->host());
|
||||
m_port = url->port();
|
||||
m_url = url;
|
||||
}
|
||||
|
||||
|
||||
|
@ -199,7 +182,7 @@ bool Client::parseJob(const json_t *params, int *code)
|
|||
job.setPoolId(m_id);
|
||||
m_job = std::move(job);
|
||||
|
||||
LOG_DEBUG("[%s:%u] job: \"%s\", diff: %lld", m_host, m_port, job.id(), job.diff());
|
||||
LOG_DEBUG("[%s:%u] job: \"%s\", diff: %lld", m_url.host(), m_url.port(), job.id(), job.diff());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -227,7 +210,7 @@ int Client::resolve(const char *host)
|
|||
|
||||
const int r = uv_getaddrinfo(uv_default_loop(), &m_resolver, Client::onResolved, host, NULL, &m_hints);
|
||||
if (r) {
|
||||
LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_port, uv_strerror(r));
|
||||
LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_url.port(), uv_strerror(r));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -250,7 +233,7 @@ void Client::connect(struct sockaddr *addr)
|
|||
{
|
||||
setState(ConnectingState);
|
||||
|
||||
reinterpret_cast<struct sockaddr_in*>(addr)->sin_port = htons(m_port);
|
||||
reinterpret_cast<struct sockaddr_in*>(addr)->sin_port = htons(m_url.port());
|
||||
free(m_socket);
|
||||
|
||||
uv_connect_t *req = (uv_connect_t*) malloc(sizeof(uv_connect_t));
|
||||
|
@ -270,19 +253,31 @@ void Client::connect(struct sockaddr *addr)
|
|||
}
|
||||
|
||||
|
||||
void Client::login()
|
||||
{
|
||||
m_sequence = 1;
|
||||
|
||||
const size_t size = 96 + strlen(m_url.user()) + strlen(m_url.password()) + strlen(m_agent);
|
||||
char *req = static_cast<char*>(malloc(size));
|
||||
snprintf(req, size, "{\"id\":%llu,\"jsonrpc\":\"2.0\",\"method\":\"login\",\"params\":{\"login\":\"%s\",\"pass\":\"%s\",\"agent\":\"%s\"}}\n", m_sequence, m_url.user(), m_url.password(), m_agent);
|
||||
|
||||
send(req);
|
||||
}
|
||||
|
||||
|
||||
void Client::parse(char *line, size_t len)
|
||||
{
|
||||
startTimeout();
|
||||
|
||||
line[len - 1] = '\0';
|
||||
|
||||
LOG_DEBUG("[%s:%u] received (%d bytes): \"%s\"", m_host, m_port, len, line);
|
||||
LOG_DEBUG("[%s:%u] received (%d bytes): \"%s\"", m_url.host(), m_url.port(), len, line);
|
||||
|
||||
json_error_t err;
|
||||
json_t *val = json_loads(line, 0, &err);
|
||||
|
||||
if (!val) {
|
||||
LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_host, m_port, err.text);
|
||||
LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_url.host(), m_url.port(), err.text);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -301,7 +296,7 @@ void Client::parse(char *line, size_t len)
|
|||
void Client::parseNotification(const char *method, const json_t *params, const json_t *error)
|
||||
{
|
||||
if (json_is_object(error)) {
|
||||
LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_host, m_port, json_string_value(json_object_get(error, "message")), json_integer_value(json_object_get(error, "code")));
|
||||
LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), json_string_value(json_object_get(error, "message")), json_integer_value(json_object_get(error, "code")));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -318,7 +313,7 @@ void Client::parseNotification(const char *method, const json_t *params, const j
|
|||
return;
|
||||
}
|
||||
|
||||
LOG_WARN("[%s:%u] unsupported method: \"%s\"", m_host, m_port, method);
|
||||
LOG_WARN("[%s:%u] unsupported method: \"%s\"", m_url.host(), m_url.port(), method);
|
||||
}
|
||||
|
||||
|
||||
|
@ -326,7 +321,7 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error
|
|||
{
|
||||
if (json_is_object(error)) {
|
||||
const char *message = json_string_value(json_object_get(error, "message"));
|
||||
LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_host, m_port, message, json_integer_value(json_object_get(error, "code")));
|
||||
LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), message, json_integer_value(json_object_get(error, "code")));
|
||||
|
||||
if (id == 1 || (message && strncasecmp(message, "Unauthenticated", 15) == 0)) {
|
||||
close();
|
||||
|
@ -342,7 +337,7 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error
|
|||
if (id == 1) {
|
||||
int code = -1;
|
||||
if (!parseLogin(result, &code)) {
|
||||
LOG_ERR("[%s:%u] login error code: %d", m_host, m_port, code);
|
||||
LOG_ERR("[%s:%u] login error code: %d", m_url.host(), m_url.port(), code);
|
||||
return close();
|
||||
}
|
||||
|
||||
|
@ -366,7 +361,7 @@ void Client::ping()
|
|||
void Client::reconnect()
|
||||
{
|
||||
uv_timer_stop(&m_responseTimer);
|
||||
if (m_keepAlive) {
|
||||
if (m_url.isKeepAlive()) {
|
||||
uv_timer_stop(&m_keepAliveTimer);
|
||||
}
|
||||
|
||||
|
@ -383,7 +378,7 @@ void Client::reconnect()
|
|||
|
||||
void Client::setState(SocketState state)
|
||||
{
|
||||
LOG_DEBUG("[%s:%u] state: %d", m_host, m_port, state);
|
||||
LOG_DEBUG("[%s:%u] state: %d", m_url.host(), m_url.port(), state);
|
||||
|
||||
if (m_state == state) {
|
||||
return;
|
||||
|
@ -396,7 +391,7 @@ void Client::setState(SocketState state)
|
|||
void Client::startTimeout()
|
||||
{
|
||||
uv_timer_stop(&m_responseTimer);
|
||||
if (!m_keepAlive) {
|
||||
if (!m_url.isKeepAlive()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -431,7 +426,7 @@ void Client::onConnect(uv_connect_t *req, int status)
|
|||
{
|
||||
auto client = getClient(req->data);
|
||||
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_url.host(), client->m_url.port(), uv_strerror(status));
|
||||
free(req);
|
||||
client->close();
|
||||
return;
|
||||
|
@ -444,7 +439,7 @@ void Client::onConnect(uv_connect_t *req, int status)
|
|||
uv_read_start(client->m_stream, Client::onAllocBuffer, Client::onRead);
|
||||
free(req);
|
||||
|
||||
client->m_listener->onLoginCredentialsRequired(client);
|
||||
client->login();
|
||||
}
|
||||
|
||||
|
||||
|
@ -453,7 +448,7 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
|
|||
auto client = getClient(stream->data);
|
||||
if (nread < 0) {
|
||||
if (nread != UV_EOF) {
|
||||
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_url.host(), client->m_url.port(), uv_strerror(nread));
|
||||
}
|
||||
|
||||
return client->close();;
|
||||
|
@ -492,7 +487,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));
|
||||
LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status));
|
||||
return client->reconnect();;
|
||||
}
|
||||
|
||||
|
|
|
@ -30,11 +30,11 @@
|
|||
|
||||
|
||||
#include "net/Job.h"
|
||||
#include "net/Url.h"
|
||||
|
||||
|
||||
class IClientListener;
|
||||
class JobResult;
|
||||
class Url;
|
||||
|
||||
|
||||
class Client
|
||||
|
@ -51,24 +51,21 @@ public:
|
|||
constexpr static int kResponseTimeout = 15 * 1000;
|
||||
constexpr static int kKeepAliveTimeout = 60 * 1000;
|
||||
|
||||
Client(int id, IClientListener *listener);
|
||||
Client(int id, const char *agent, IClientListener *listener);
|
||||
~Client();
|
||||
|
||||
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);
|
||||
void submit(const JobResult &result);
|
||||
|
||||
inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; }
|
||||
inline const char *host() const { return m_host; }
|
||||
inline const char *host() const { return m_url.host(); }
|
||||
inline const Job &job() const { return m_job; }
|
||||
inline int id() const { return m_id; }
|
||||
inline SocketState state() const { return m_state; }
|
||||
inline uint16_t port() const { return m_port; }
|
||||
inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; }
|
||||
inline uint16_t port() const { return m_url.port(); }
|
||||
inline void setRetryPause(int ms) { m_retryPause = ms; }
|
||||
|
||||
private:
|
||||
|
@ -79,6 +76,7 @@ private:
|
|||
int resolve(const char *host);
|
||||
void close();
|
||||
void connect(struct sockaddr *addr);
|
||||
void login();
|
||||
void parse(char *line, size_t len);
|
||||
void parseNotification(const char *method, const json_t *params, const json_t *error);
|
||||
void parseResponse(int64_t id, const json_t *result, const json_t *error);
|
||||
|
@ -95,9 +93,8 @@ private:
|
|||
|
||||
static Client *getClient(void *data);
|
||||
|
||||
bool m_keepAlive;
|
||||
char *m_host;
|
||||
char m_rpcId[64];
|
||||
const char *m_agent;
|
||||
IClientListener *m_listener;
|
||||
int m_id;
|
||||
int m_retryPause;
|
||||
|
@ -107,7 +104,7 @@ private:
|
|||
size_t m_recvBufPos;
|
||||
SocketState m_state;
|
||||
struct addrinfo m_hints;
|
||||
uint16_t m_port;
|
||||
Url m_url;
|
||||
uv_buf_t m_recvBuf;
|
||||
uv_getaddrinfo_t m_resolver;
|
||||
uv_stream_t *m_stream;
|
||||
|
|
|
@ -118,12 +118,6 @@ void Network::onJobResult(const JobResult &result)
|
|||
}
|
||||
|
||||
|
||||
void Network::onLoginCredentialsRequired(Client *client)
|
||||
{
|
||||
client->login(m_options->pools().front()->password(), m_options->pools().front()->password(), m_agent);
|
||||
}
|
||||
|
||||
|
||||
void Network::onLoginSuccess(Client *client)
|
||||
{
|
||||
const int id = client->id();
|
||||
|
@ -151,7 +145,7 @@ void Network::addPool(const Url *url)
|
|||
return;
|
||||
}
|
||||
|
||||
Client *client = new Client(m_pools.size(), this);
|
||||
Client *client = new Client(m_pools.size(), m_agent, this);
|
||||
client->setUrl(url);
|
||||
client->setRetryPause(m_options->retryPause() * 1000);
|
||||
// client->setKeepAlive(m_options->keepAlive());
|
||||
|
|
|
@ -51,7 +51,6 @@ protected:
|
|||
void onClose(Client *client, int failures) override;
|
||||
void onJobReceived(Client *client, const Job &job) override;
|
||||
void onJobResult(const JobResult &result) override;
|
||||
void onLoginCredentialsRequired(Client *client) override;
|
||||
void onLoginSuccess(Client *client) override;
|
||||
|
||||
private:
|
||||
|
|
|
@ -144,23 +144,31 @@ bool Url::setUserpass(const char *userpass)
|
|||
}
|
||||
|
||||
|
||||
void Url::setPassword(const char *password, bool force)
|
||||
void Url::setPassword(const char *password)
|
||||
{
|
||||
if (m_password != nullptr && !force) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(m_password);
|
||||
m_password = strdup(password);
|
||||
}
|
||||
|
||||
|
||||
void Url::setUser(const char *user, bool force)
|
||||
void Url::setUser(const char *user)
|
||||
{
|
||||
if (m_user != nullptr && !force) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(m_user);
|
||||
m_user = strdup(user);
|
||||
}
|
||||
|
||||
|
||||
Url &Url::operator=(const Url *other)
|
||||
{
|
||||
m_keepAlive = other->m_keepAlive;
|
||||
m_nicehash = other->m_nicehash;
|
||||
m_port = other->m_port;
|
||||
|
||||
free(m_host);
|
||||
m_host = strdup(other->m_host);
|
||||
|
||||
setPassword(other->m_password);
|
||||
setUser(other->m_user);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
|
|
@ -52,8 +52,11 @@ public:
|
|||
bool isNicehash() const;
|
||||
bool parse(const char *url);
|
||||
bool setUserpass(const char *userpass);
|
||||
void setPassword(const char *password, bool force = true);
|
||||
void setUser(const char *user, bool force = true);
|
||||
void setPassword(const char *password);
|
||||
void setUser(const char *user);
|
||||
|
||||
Url &operator=(const Url *other);
|
||||
|
||||
private:
|
||||
bool m_keepAlive;
|
||||
bool m_nicehash;
|
||||
|
|
Loading…
Reference in a new issue