From 2e738509bb782c46ac034f08cd0628bf31a4af13 Mon Sep 17 00:00:00 2001 From: Tony Butler Date: Thu, 30 Dec 2021 05:39:12 -0700 Subject: [PATCH] Add support for client.reconnect method --- src/base/net/stratum/Client.cpp | 39 ++++++++++++++++++++++++++++++++- src/base/net/stratum/Client.h | 1 + src/base/net/stratum/Pool.h | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index db6ceca38..31f72b963 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #ifdef XMRIG_FEATURE_TLS @@ -683,12 +684,48 @@ void xmrig::Client::parse(char *line, size_t len) const auto &id = Json::getValue(doc, "id"); const auto &error = Json::getValue(doc, "error"); + const char *method = Json::getString(doc, "method"); + + if (method && strcmp(method, "client.reconnect") == 0) { + const auto ¶ms = Json::getValue(doc, "params"); + if (!params.IsArray()) { + LOG_ERR("%s " RED("invalid client.reconnect notification: params is not an array"), tag()); + return; + } + + auto arr = params.GetArray(); + + if (arr.Empty()) { + LOG_ERR("%s " RED("invalid client.reconnect notification: params array is empty"), tag()); + return; + } + + if (arr.Size() != 2) { + LOG_ERR("%s " RED("invalid client.reconnect notification: params array has wrong size"), tag()); + return; + } + + if (!arr[0].IsString()) { + LOG_ERR("%s " RED("invalid client.reconnect notification: host is not a string"), tag()); + return; + } + + if (!arr[1].IsString()) { + LOG_ERR("%s " RED("invalid client.reconnect notification: port is not a string"), tag()); + return; + } + + std::stringstream s; + s << arr[0].GetString() << ":" << arr[1].GetString(); + LOG_WARN("%s " YELLOW("client.reconnect to %s"), tag(), s.str().c_str()); + setPoolUrl(s.str().c_str()); + return reconnect(); + } if (id.IsInt64()) { return parseResponse(id.GetInt64(), Json::getValue(doc, "result"), error); } - const char *method = Json::getString(doc, "method"); if (!method) { return; } diff --git a/src/base/net/stratum/Client.h b/src/base/net/stratum/Client.h index e1f5d7562..300db138a 100644 --- a/src/base/net/stratum/Client.h +++ b/src/base/net/stratum/Client.h @@ -84,6 +84,7 @@ protected: inline const char *url() const { return m_pool.url(); } inline const String &rpcId() const { return m_rpcId; } inline void setRpcId(const char *id) { m_rpcId = id; } + inline void setPoolUrl(const char *url) { m_pool.setUrl(url); } virtual bool parseLogin(const rapidjson::Value &result, int *code); virtual void login(); diff --git a/src/base/net/stratum/Pool.h b/src/base/net/stratum/Pool.h index 865d3db56..78684510f 100644 --- a/src/base/net/stratum/Pool.h +++ b/src/base/net/stratum/Pool.h @@ -111,6 +111,7 @@ public: inline int zmq_port() const { return m_zmqPort; } inline uint64_t pollInterval() const { return m_pollInterval; } inline void setAlgo(const Algorithm &algorithm) { m_algorithm = algorithm; } + inline void setUrl(const char *url) { m_url = Url(url); } inline void setPassword(const String &password) { m_password = password; } inline void setProxy(const ProxyUrl &proxy) { m_proxy = proxy; } inline void setRigId(const String &rigId) { m_rigId = rigId; }