From 548fbb9f711b2817ad2386f9c4b8020ba3a28ec5 Mon Sep 17 00:00:00 2001 From: Tony Butler Date: Wed, 22 Dec 2021 05:17:08 -0700 Subject: [PATCH 1/2] Add API rebind polling --- src/base/api/Api.cpp | 14 ++++++++++++++ src/base/api/Api.h | 2 ++ src/base/api/Httpd.h | 1 + src/net/Network.cpp | 3 +++ 4 files changed, 20 insertions(+) diff --git a/src/base/api/Api.cpp b/src/base/api/Api.cpp index 54666efd..a7246b9f 100644 --- a/src/base/api/Api.cpp +++ b/src/base/api/Api.cpp @@ -123,6 +123,20 @@ void xmrig::Api::stop() } +void xmrig::Api::tick() +{ +# ifdef XMRIG_FEATURE_HTTP + if (!m_base->config()->http().isEnabled() || m_httpd->isBound()) { + return; + } + if (++m_ticks % 10 == 0) { + m_ticks = 0; + m_httpd->start(); + } +# endif +} + + void xmrig::Api::onConfigChanged(Config *config, Config *previousConfig) { if (config->apiId() != previousConfig->apiId()) { diff --git a/src/base/api/Api.h b/src/base/api/Api.h index c56e29ee..685b56ff 100644 --- a/src/base/api/Api.h +++ b/src/base/api/Api.h @@ -54,6 +54,7 @@ public: void request(const HttpData &req); void start(); void stop(); + void tick(); protected: void onConfigChanged(Config *config, Config *previousConfig) override; @@ -67,6 +68,7 @@ private: char m_id[32]{}; String m_workerId; const uint64_t m_timestamp; + uint8_t m_ticks; Httpd *m_httpd = nullptr; std::vector m_listeners; }; diff --git a/src/base/api/Httpd.h b/src/base/api/Httpd.h index 188646f5..fe8cb914 100644 --- a/src/base/api/Httpd.h +++ b/src/base/api/Httpd.h @@ -48,6 +48,7 @@ public: bool start(); void stop(); + inline const bool isBound() const { return m_server != nullptr; } protected: void onConfigChanged(Config *config, Config *previousConfig) override; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index a3566242..841dd316 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -308,6 +308,9 @@ void xmrig::Network::tick() if (m_donate) { m_donate->tick(now); } +#ifdef XMRIG_FEATURE_API + m_controller->api()->tick(); +#endif } From 7d7f30701f0e22a0260ec6059a7d2447eb5f4c10 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Jun 2023 20:48:56 +0700 Subject: [PATCH 2/2] Code cleanup. --- src/base/api/Api.cpp | 7 ++++--- src/base/api/Api.h | 15 +++++++-------- src/base/api/Httpd.h | 18 +++++++----------- src/net/Network.cpp | 5 +++-- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/base/api/Api.cpp b/src/base/api/Api.cpp index a7246b9f..ea78c35e 100644 --- a/src/base/api/Api.cpp +++ b/src/base/api/Api.cpp @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 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 @@ -126,9 +126,10 @@ void xmrig::Api::stop() void xmrig::Api::tick() { # ifdef XMRIG_FEATURE_HTTP - if (!m_base->config()->http().isEnabled() || m_httpd->isBound()) { + if (m_httpd->isBound() || !m_base->config()->http().isEnabled()) { return; } + if (++m_ticks % 10 == 0) { m_ticks = 0; m_httpd->start(); diff --git a/src/base/api/Api.h b/src/base/api/Api.h index 685b56ff..7c686ff4 100644 --- a/src/base/api/Api.h +++ b/src/base/api/Api.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 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 @@ -21,7 +21,6 @@ #include -#include #include "base/kernel/interfaces/IBaseListener.h" @@ -44,7 +43,7 @@ class Api : public IBaseListener public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(Api) - Api(Base *base); + explicit Api(Base *base); ~Api() override; inline const char *id() const { return m_id; } @@ -66,15 +65,15 @@ private: Base *m_base; char m_id[32]{}; - String m_workerId; const uint64_t m_timestamp; - uint8_t m_ticks; - Httpd *m_httpd = nullptr; + Httpd *m_httpd = nullptr; std::vector m_listeners; + String m_workerId; + uint8_t m_ticks = 0; }; } // namespace xmrig -#endif /* XMRIG_API_H */ +#endif // XMRIG_API_H diff --git a/src/base/api/Httpd.h b/src/base/api/Httpd.h index fe8cb914..e00c8eb5 100644 --- a/src/base/api/Httpd.h +++ b/src/base/api/Httpd.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2023 SChernykh + * Copyright (c) 2016-2023 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 @@ -22,11 +22,6 @@ #include "base/kernel/interfaces/IBaseListener.h" #include "base/net/http/HttpListener.h" -#include "base/tools/Object.h" - - -#include -#include namespace xmrig { @@ -43,12 +38,13 @@ class Httpd : public IBaseListener, public IHttpListener public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(Httpd) - Httpd(Base *base); + explicit Httpd(Base *base); ~Httpd() override; + inline bool isBound() const { return m_server != nullptr; } + bool start(); void stop(); - inline const bool isBound() const { return m_server != nullptr; } protected: void onConfigChanged(Config *config, Config *previousConfig) override; @@ -70,7 +66,7 @@ private: }; -} /* namespace xmrig */ +} // namespace xmrig -#endif /* XMRIG_HTTPD_H */ +#endif // XMRIG_HTTPD_H diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 841dd316..0ca1914a 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -308,9 +308,10 @@ void xmrig::Network::tick() if (m_donate) { m_donate->tick(now); } -#ifdef XMRIG_FEATURE_API + +# ifdef XMRIG_FEATURE_API m_controller->api()->tick(); -#endif +# endif }