Fixed reconnecting in case of errors

This commit is contained in:
BenDroid 2018-03-30 21:57:41 +02:00
parent 93ef680a21
commit e81aaa8280
4 changed files with 49 additions and 12 deletions

View file

@ -46,7 +46,7 @@ struct net_s {
void (*conn_cb)(net_t*); void (*conn_cb)(net_t*);
void (*read_cb)(net_t*, size_t, char*); void (*read_cb)(net_t*, size_t, char*);
void (*error_cb)(net_t*, int, char*); void (*error_cb)(net_t*, int, char*);
void (*close_cb)(uv_handle_t*); void (*close_cb)(net_t*);
}; };
/* /*

View file

@ -40,6 +40,17 @@ net_connect(net_t * net) {
return NET_OK; return NET_OK;
} }
void
net_close_cb(uv_handle_t *handle) {
net_t * net = (net_t*) handle->data;
if (net) {
if (net->close_cb) {
net->close_cb(net);
}
}
}
int int
net_close(net_t * net, void (*cb)(uv_handle_t*)) { net_close(net_t * net, void (*cb)(uv_handle_t*)) {
int r = net->connected; int r = net->connected;
@ -57,13 +68,17 @@ net_close(net_t * net, void (*cb)(uv_handle_t*)) {
uv_read_stop((uv_stream_t*)net->handle); uv_read_stop((uv_stream_t*)net->handle);
} }
uv_close((uv_handle_t*)net->handle, cb); uv_close((uv_handle_t *) net->handle, net_close_cb);
#ifndef XMRIG_NO_TLS #ifndef XMRIG_NO_TLS
if (net->use_ssl) { if (net->use_ssl) {
tls_free(net->tls); tls_free(net->tls);
} }
#endif #endif
} else{
if (net->close_cb) {
net->close_cb(net);
}
} }
return r; return r;
@ -71,9 +86,18 @@ net_close(net_t * net, void (*cb)(uv_handle_t*)) {
int int
net_free(net_t * net) { net_free(net_t * net) {
net_close(net, NULL); if (net->conn != NULL) {
free(net->conn);
net->conn = NULL;
}
if (net->resolver != NULL) {
free(net->resolver); free(net->resolver);
net->resolver = NULL;
}
if (net != NULL) {
free(net); free(net);
net = NULL;
}
return NET_OK; return NET_OK;
} }

View file

@ -295,13 +295,9 @@ void Client::close()
LOG_DEBUG("Client::close"); LOG_DEBUG("Client::close");
if (m_net) { if (m_net) {
auto client = getClient(m_net->data); net_close(m_net, nullptr);
} else {
net_free(m_net); reconnect();
m_net = nullptr;
client->reconnect();
} }
} }
@ -314,6 +310,7 @@ void Client::connect()
m_net->data = this; m_net->data = this;
m_net->conn_cb = Client::onConnect; m_net->conn_cb = Client::onConnect;
m_net->read_cb = Client::onRead; m_net->read_cb = Client::onRead;
m_net->close_cb = Client::onClose;
m_net->error_cb = Client::onError; m_net->error_cb = Client::onError;
#ifndef XMRIG_NO_TLS #ifndef XMRIG_NO_TLS
@ -388,6 +385,21 @@ void Client::onError(net_t *net, int err, char *errStr)
} }
} }
void Client::onClose(net_t *net)
{
LOG_DEBUG("Client::onClose");
if (net) {
auto client = getClient(net->data);
net_free(net);
client->m_net = nullptr;
client->reconnect();
}
}
void Client::login() void Client::login()
{ {
LOG_DEBUG("Client::login"); LOG_DEBUG("Client::login");

View file

@ -96,6 +96,7 @@ private:
static void onRead(net_t *net, size_t read, char *buf); static void onRead(net_t *net, size_t read, char *buf);
static void onConnect(net_t *net); static void onConnect(net_t *net);
static void onError(net_t *net, int err, char *errStr); static void onError(net_t *net, int err, char *errStr);
static void onClose(net_t *net);
static inline Client *getClient(void *data) { return static_cast<Client*>(data); } static inline Client *getClient(void *data) { return static_cast<Client*>(data); }