1
0
mirror of https://github.com/xmrig/xmrig.git synced 2025-12-13 18:12:50 -05:00

Added client storage from proxy.

This commit is contained in:
XMRig
2018-03-31 18:12:52 +07:00
parent af0a6fdf20
commit 341557c34e
5 changed files with 147 additions and 47 deletions

View File

@@ -52,6 +52,7 @@
int64_t Client::m_sequence = 1;
xmrig::Storage<Client> Client::m_storage;
Client::Client(int id, const char *agent, IClientListener *listener) :
@@ -67,13 +68,17 @@ Client::Client(int id, const char *agent, IClientListener *listener) :
m_state(UnconnectedState),
m_expire(0),
m_jobs(0),
m_keepAlive(0),
m_key(0),
m_stream(nullptr),
m_socket(nullptr)
{
m_key = m_storage.add(this);
memset(m_ip, 0, sizeof(m_ip));
memset(&m_hints, 0, sizeof(m_hints));
m_resolver.data = this;
m_resolver.data = m_storage.ptr(m_key);
m_hints.ai_family = AF_UNSPEC;
m_hints.ai_socktype = SOCK_STREAM;
@@ -81,11 +86,6 @@ Client::Client(int id, const char *agent, IClientListener *listener) :
m_recvBuf.base = m_buf;
m_recvBuf.len = sizeof(m_buf);
# ifndef XMRIG_PROXY_PROJECT
m_keepAliveTimer.data = this;
uv_timer_init(uv_default_loop(), &m_keepAliveTimer);
# endif
}
@@ -121,8 +121,13 @@ void Client::deleteLater()
m_listener = nullptr;
if (!disconnect()) {
delete this;
if (state() == HostLookupState) {
uv_cancel(reinterpret_cast<uv_req_t*>(&m_resolver));
return;
}
if (!disconnect() && m_state != ClosingState) {
m_storage.remove(m_key);
}
}
@@ -139,17 +144,17 @@ void Client::setUrl(const Url *url)
void Client::tick(uint64_t now)
{
if (m_expire == 0 || now < m_expire) {
return;
}
if (m_state == ConnectedState) {
LOG_DEBUG_ERR("[%s:%u] timeout", m_url.host(), m_url.port());
close();
if (m_expire && now > m_expire) {
LOG_DEBUG_ERR("[%s:%u] timeout", m_url.host(), m_url.port());
close();
}
else if (m_keepAlive && now > m_keepAlive) {
ping();
}
}
if (m_state == ConnectingState) {
if (m_expire && now > m_expire && m_state == ConnectingState) {
connect();
}
}
@@ -157,12 +162,9 @@ void Client::tick(uint64_t now)
bool Client::disconnect()
{
# ifndef XMRIG_PROXY_PROJECT
uv_timer_stop(&m_keepAliveTimer);
# endif
m_expire = 0;
m_failures = -1;
m_keepAlive = 0;
m_expire = 0;
m_failures = -1;
return close();
}
@@ -404,10 +406,10 @@ void Client::connect(sockaddr *addr)
delete m_socket;
uv_connect_t *req = new uv_connect_t;
req->data = this;
req->data = m_storage.ptr(m_key);
m_socket = new uv_tcp_t;
m_socket->data = this;
m_socket->data = m_storage.ptr(m_key);
uv_tcp_init(uv_default_loop(), m_socket);
uv_tcp_nodelay(m_socket, 1);
@@ -567,7 +569,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap
LOG_ERR("[%s:%u] error: \"%s\", code: %d", m_url.host(), m_url.port(), message, error["code"].GetInt());
}
if (id == 1 || isCriticalError(message)) {
if (isCriticalError(message)) {
close();
}
@@ -613,18 +615,13 @@ void Client::ping()
void Client::reconnect()
{
if (!m_listener) {
delete this;
m_storage.remove(m_key);
return;
}
setState(ConnectingState);
# ifndef XMRIG_PROXY_PROJECT
if (m_url.isKeepAlive()) {
uv_timer_stop(&m_keepAliveTimer);
}
# endif
m_keepAlive = 0;
if (m_failures == -1) {
return m_listener->onClose(this, -1);
@@ -653,13 +650,9 @@ void Client::startTimeout()
{
m_expire = 0;
# ifndef XMRIG_PROXY_PROJECT
if (!m_url.isKeepAlive()) {
return;
if (m_url.keepAlive()) {
m_keepAlive = uv_now(uv_default_loop()) + (m_url.keepAlive() * 1000);
}
uv_timer_start(&m_keepAliveTimer, [](uv_timer_t *handle) { getClient(handle->data)->ping(); }, kKeepAliveTimeout, 0);
# endif
}
@@ -690,6 +683,7 @@ void Client::onConnect(uv_connect_t *req, int status)
{
auto client = getClient(req->data);
if (!client) {
delete req;
return;
}
@@ -735,6 +729,11 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
return;
}
assert(client->m_listener != nullptr);
if (!client->m_listener) {
return client->reconnect();
}
client->m_recvBufPos += nread;
char* end;
@@ -771,6 +770,11 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res)
return;
}
assert(client->m_listener != nullptr);
if (!client->m_listener) {
return client->reconnect();
}
if (status < 0) {
if (!client->m_quiet) {
LOG_ERR("[%s:%u] DNS error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status));