diff --git a/src/Console.h b/src/Console.h index a02e37612..73e047061 100644 --- a/src/Console.h +++ b/src/Console.h @@ -71,11 +71,13 @@ private: #define LOG_INFO(x, ...) Console::i()->message(Console::INFO, x, ##__VA_ARGS__) #ifdef APP_DEBUG -# define LOG_DEBUG(x, ...) Console::i()->message(Console::DEBUG, x, ##__VA_ARGS__) -# define LOG_DEBUG_ERR(x, ...) Console::i()->message(Console::ERR, x, ##__VA_ARGS__) +# define LOG_DEBUG(x, ...) Console::i()->message(Console::DEBUG, x, ##__VA_ARGS__) +# define LOG_DEBUG_ERR(x, ...) Console::i()->message(Console::ERR, x, ##__VA_ARGS__) +# define LOG_DEBUG_WARN(x, ...) Console::i()->message(Console::WARNING, x, ##__VA_ARGS__) #else # define LOG_DEBUG(x, ...) # define LOG_DEBUG_ERR(x, ...) +# define LOG_DEBUG_WARN(x, ...) #endif #endif /* __CONSOLE_H__ */ diff --git a/src/Options.cpp b/src/Options.cpp index 2df634413..c09de032f 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -131,7 +131,7 @@ Options::Options(int argc, char **argv) : m_donateLevel(kDonateLevel), m_maxCpuUsage(75), m_retries(5), - m_retryPause(2), + m_retryPause(5), m_threads(0), m_affinity(-1L), m_backupUrl(nullptr), diff --git a/src/Options.h b/src/Options.h index 227659f79..b2b1966df 100644 --- a/src/Options.h +++ b/src/Options.h @@ -57,6 +57,7 @@ public: inline const char *user() const { return m_user; } inline const Url *backupUrl() const { return m_backupUrl; } inline const Url *url() const { return m_url; } + inline int retries() const { return m_retries; } inline int retryPause() const { return m_retryPause; } private: diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 72f179c26..38e7df25f 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -33,7 +33,7 @@ Client::Client(int id, IClientListener *listener) : m_host(nullptr), m_listener(listener), m_id(id), - m_retryPause(2000), + m_retryPause(5000), m_failures(0), m_sequence(1), m_recvBufPos(0), diff --git a/src/net/Client.h b/src/net/Client.h index 62bb41966..6110488d0 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -60,8 +60,10 @@ public: void send(char *data); void setUrl(const Url *url); + inline const char *host() const { return m_host; } 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 void setRetryPause(int ms) { m_retryPause = ms; } diff --git a/src/net/Network.cpp b/src/net/Network.cpp index a69341674..692cfa7ad 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -36,7 +36,7 @@ Network::Network(const Options *options) : m_donate(false), m_options(options), - m_pool(1) + m_pool(0) { m_pools.reserve(2); m_agent = userAgent(); @@ -61,13 +61,25 @@ Network::~Network() void Network::connect() { - m_pools.at(m_pool)->connect(); + m_pools.at(1)->connect(); } void Network::onClose(Client *client, int failures) { - LOG_DEBUG("CLOSE %d %d", client->id(), failures); + const int id = client->id(); + if (id == 0 && failures == -1) { + m_donate = false; + return; + } + + if (m_pool == id) { + m_pool = 0; + } + + if (id == 1 && m_pools.size() > 2 && failures == m_options->retries()) { + m_pools.at(2)->connect(); + } } @@ -85,6 +97,23 @@ void Network::onLoginCredentialsRequired(Client *client) void Network::onLoginSuccess(Client *client) { + const int id = client->id(); + if (id == 0) { + m_donate = true; + return; + } + + if (id == 2 && m_pool) { // primary pool is already active + m_pools.at(2)->disconnect(); + return; + } + + LOG_NOTICE("use pool: \"%s:%d\"", client->host(), client->port()); + m_pool = id; + + if (m_pool == 1 && m_pools.size() > 2) { // try disconnect from backup pool + m_pools.at(2)->disconnect(); + } }