From 6f4ed5f66d1956a3936979b1015e72f8b374e8e3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 26 Mar 2018 11:47:01 +0700 Subject: [PATCH] #478 Fixed totally broken reconnect. --- cmake/flags.cmake | 4 ++++ src/App_unix.cpp | 6 ++++-- src/net/Client.cpp | 49 +++++++++++++++++++++++++++++++++------------- src/net/Client.h | 1 + 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/cmake/flags.cmake b/cmake/flags.cmake index 488f12360..13e505646 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -6,6 +6,10 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "") set(CMAKE_BUILD_TYPE Release) endif() +if (CMAKE_BUILD_TYPE STREQUAL "Release") + add_definitions(/DNDEBUG) +endif() + if (CMAKE_CXX_COMPILER_ID MATCHES GNU) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-strict-aliasing") diff --git a/src/App_unix.cpp b/src/App_unix.cpp index 669572085..674a53e6d 100644 --- a/src/App_unix.cpp +++ b/src/App_unix.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,6 +36,8 @@ void App::background() { + signal(SIGPIPE, SIG_IGN); + if (m_options->affinity() != -1L) { Cpu::setAffinity(-1, m_options->affinity()); } diff --git a/src/net/Client.cpp b/src/net/Client.cpp index b4506e44b..c8a93f24c 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -21,6 +21,7 @@ * along with this program. If not, see . */ +#include #include #include #include @@ -204,16 +205,30 @@ bool Client::close() setState(ClosingState); - uv_read_stop(reinterpret_cast(m_socket)); + uv_stream_t *stream = reinterpret_cast(m_socket); - uv_shutdown(new uv_shutdown_t, reinterpret_cast(m_socket), [](uv_shutdown_t* req, int status) { + if (uv_is_readable(stream) == 1) { + uv_read_stop(stream); + } - if (uv_is_closing(reinterpret_cast(req->handle)) == 0) { - uv_close(reinterpret_cast(req->handle), Client::onClose); + if (uv_is_writable(stream) == 1) { + const int rc = uv_shutdown(new uv_shutdown_t, stream, [](uv_shutdown_t* req, int status) { + if (uv_is_closing(reinterpret_cast(req->handle)) == 0) { + uv_close(reinterpret_cast(req->handle), Client::onClose); + } + + delete req; + }); + + assert(rc == 0); + + if (rc != 0) { + onClose(); } - - delete req; - }); + } + else { + uv_close(reinterpret_cast(m_socket), Client::onClose); + } return true; } @@ -442,6 +457,18 @@ void Client::login() } +void Client::onClose() +{ + delete m_socket; + + m_stream = nullptr; + m_socket = nullptr; + setState(UnconnectedState); + + reconnect(); +} + + void Client::parse(char *line, size_t len) { startTimeout(); @@ -655,13 +682,7 @@ void Client::onClose(uv_handle_t *handle) return; } - delete client->m_socket; - - client->m_stream = nullptr; - client->m_socket = nullptr; - client->setState(UnconnectedState); - - client->reconnect(); + client->onClose(); } diff --git a/src/net/Client.h b/src/net/Client.h index cf4261193..fff7a1560 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -87,6 +87,7 @@ private: void connect(const std::vector &ipv4, const std::vector &ipv6); void connect(sockaddr *addr); void login(); + void onClose(); void parse(char *line, size_t len); void parseExtensions(const rapidjson::Value &value); void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error);