Add class Timer.

This commit is contained in:
XMRig 2019-03-18 02:36:17 +07:00
parent bbf0d11a51
commit 9a6a5a94b5
13 changed files with 271 additions and 73 deletions

View file

@ -31,14 +31,12 @@
#include "base/kernel/interfaces/ISignalListener.h"
class Httpd;
namespace xmrig {
class Console;
class Controller;
class Httpd;
class Network;
class Process;
class Signals;

View file

@ -11,6 +11,7 @@ set(HEADERS_BASE
src/base/kernel/interfaces/ISignalListener.h
src/base/kernel/interfaces/IStrategy.h
src/base/kernel/interfaces/IStrategyListener.h
src/base/kernel/interfaces/ITimerListener.h
src/base/kernel/interfaces/IWatcherListener.h
src/base/kernel/Process.h
src/base/kernel/Signals.h
@ -30,6 +31,7 @@ set(HEADERS_BASE
src/base/tools/Chrono.h
src/base/tools/Handle.h
src/base/tools/String.h
src/base/tools/Timer.h
)
set(SOURCES_BASE
@ -50,6 +52,7 @@ set(SOURCES_BASE
src/base/tools/Arguments.cpp
src/base/tools/Buffer.cpp
src/base/tools/String.cpp
src/base/tools/Timer.cpp
)

View file

@ -29,37 +29,31 @@
#include "base/kernel/interfaces/IWatcherListener.h"
#include "base/io/Watcher.h"
#include "base/tools/Handle.h"
#include "base/tools/Timer.h"
xmrig::Watcher::Watcher(const String &path, IWatcherListener *listener) :
m_listener(listener),
m_path(path)
{
m_timer = new Timer(this);
m_fsEvent = new uv_fs_event_t;
m_fsEvent->data = this;
uv_fs_event_init(uv_default_loop(), m_fsEvent);
m_timer = new uv_timer_t;
uv_timer_init(uv_default_loop(), m_timer);
m_fsEvent->data = m_timer->data = this;
start();
}
xmrig::Watcher::~Watcher()
{
Handle::close(m_timer);
delete m_timer;
Handle::close(m_fsEvent);
}
void xmrig::Watcher::onTimer(uv_timer_t *handle)
{
static_cast<Watcher *>(handle->data)->reload();
}
void xmrig::Watcher::onFsEvent(uv_fs_event_t *handle, const char *filename, int, int)
{
if (!filename) {
@ -72,8 +66,8 @@ void xmrig::Watcher::onFsEvent(uv_fs_event_t *handle, const char *filename, int,
void xmrig::Watcher::queueUpdate()
{
uv_timer_stop(m_timer);
uv_timer_start(m_timer, xmrig::Watcher::onTimer, kDelay, 0);
m_timer->stop();
m_timer->start(kDelay, 0);
}

View file

@ -26,30 +26,33 @@
#define XMRIG_WATCHER_H
#include "base/kernel/interfaces/ITimerListener.h"
#include "base/tools/String.h"
typedef struct uv_fs_event_s uv_fs_event_t;
typedef struct uv_timer_s uv_timer_t;
namespace xmrig {
class IWatcherListener;
class Timer;
class Watcher
class Watcher : public ITimerListener
{
public:
Watcher(const String &path, IWatcherListener *listener);
~Watcher();
~Watcher() override;
protected:
inline void onTimer(const Timer *) override { reload(); }
private:
constexpr static int kDelay = 500;
static void onFsEvent(uv_fs_event_t *handle, const char *filename, int events, int status);
static void onTimer(uv_timer_t *handle);
void queueUpdate();
void reload();
@ -57,11 +60,12 @@ private:
IWatcherListener *m_listener;
String m_path;
Timer *m_timer;
uv_fs_event_t *m_fsEvent;
uv_timer_t *m_timer;
};
} /* namespace xmrig */
#endif /* XMRIG_WATCHER_H */

View file

@ -0,0 +1,47 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_ITIMERLISTENER_H
#define XMRIG_ITIMERLISTENER_H
namespace xmrig {
class Timer;
class ITimerListener
{
public:
virtual ~ITimerListener() = default;
virtual void onTimer(const Timer *timer) = 0;
};
} /* namespace xmrig */
#endif // XMRIG_ITIMERLISTENER_H

View file

@ -5,6 +5,7 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify

View file

@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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

91
src/base/tools/Timer.cpp Normal file
View file

@ -0,0 +1,91 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base/kernel/interfaces/ITimerListener.h"
#include "base/tools/Handle.h"
#include "base/tools/Timer.h"
xmrig::Timer::Timer(ITimerListener *listener) :
m_listener(listener),
m_timer(nullptr)
{
init();
}
xmrig::Timer::Timer(ITimerListener *listener, uint64_t timeout, uint64_t repeat) :
m_listener(listener),
m_timer(nullptr)
{
init();
start(timeout, repeat);
}
xmrig::Timer::~Timer()
{
Handle::close(m_timer);
}
uint64_t xmrig::Timer::repeat() const
{
return uv_timer_get_repeat(m_timer);
}
void xmrig::Timer::setRepeat(uint64_t repeat)
{
uv_timer_set_repeat(m_timer, repeat);
}
void xmrig::Timer::start(uint64_t timeout, uint64_t repeat)
{
uv_timer_start(m_timer, onTimer, timeout, repeat);
}
void xmrig::Timer::stop()
{
uv_timer_stop(m_timer);
}
void xmrig::Timer::init()
{
m_timer = new uv_timer_t;
m_timer->data = this;
uv_timer_init(uv_default_loop(), m_timer);
}
void xmrig::Timer::onTimer(uv_timer_t *handle)
{
const Timer *timer = static_cast<Timer *>(handle->data);
timer->m_listener->onTimer(timer);
}

66
src/base/tools/Timer.h Normal file
View file

@ -0,0 +1,66 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_TIMER_H
#define XMRIG_TIMER_H
#include <stdint.h>
typedef struct uv_timer_s uv_timer_t;
namespace xmrig {
class ITimerListener;
class Timer
{
public:
Timer(ITimerListener *listener);
Timer(ITimerListener *listener, uint64_t timeout, uint64_t repeat);
~Timer();
uint64_t repeat() const;
void setRepeat(uint64_t repeat);
void start(uint64_t timeout, uint64_t repeat);
void stop();
private:
void init();
static void onTimer(uv_timer_t *handle);
ITimerListener *m_listener;
uv_timer_t *m_timer;
};
} /* namespace xmrig */
#endif /* XMRIG_TIMER_H */

View file

@ -29,13 +29,14 @@
#include "api/Api.h"
#include "base/tools/Handle.h"
#include "base/tools/Timer.h"
#include "common/api/Httpd.h"
#include "common/api/HttpReply.h"
#include "common/api/HttpRequest.h"
#include "common/log/Log.h"
Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) :
xmrig::Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) :
m_idle(true),
m_IPv6(IPv6),
m_restricted(restricted),
@ -43,13 +44,11 @@ Httpd::Httpd(int port, const char *accessToken, bool IPv6, bool restricted) :
m_port(port),
m_daemon(nullptr)
{
m_timer = new uv_timer_t;
uv_timer_init(uv_default_loop(), m_timer);
m_timer->data = this;
m_timer = new Timer(this);
}
Httpd::~Httpd()
xmrig::Httpd::~Httpd()
{
stop();
@ -61,7 +60,7 @@ Httpd::~Httpd()
}
bool Httpd::start()
bool xmrig::Httpd::start()
{
if (!m_port) {
return false;
@ -85,23 +84,23 @@ bool Httpd::start()
}
# if MHD_VERSION >= 0x00093900
uv_timer_start(m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval);
m_timer->start(kIdleInterval, kIdleInterval);
# else
uv_timer_start(m_timer, Httpd::onTimer, kActiveInterval, kActiveInterval);
m_timer->start(kActiveInterval, kActiveInterval);
# endif
return true;
}
void Httpd::stop()
void xmrig::Httpd::stop()
{
xmrig::Handle::close(m_timer);
delete m_timer;
m_timer = nullptr;
}
int Httpd::process(xmrig::HttpRequest &req)
int xmrig::Httpd::process(HttpRequest &req)
{
xmrig::HttpReply reply;
if (!req.process(m_accessToken, m_restricted, reply)) {
@ -118,27 +117,27 @@ int Httpd::process(xmrig::HttpRequest &req)
}
void Httpd::run()
void xmrig::Httpd::run()
{
MHD_run(m_daemon);
# if MHD_VERSION >= 0x00093900
const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS);
if (m_idle && info->num_connections) {
uv_timer_set_repeat(m_timer, kActiveInterval);
m_timer->setRepeat(kActiveInterval);
m_idle = false;
}
else if (!m_idle && !info->num_connections) {
uv_timer_set_repeat(m_timer, kIdleInterval);
m_timer->setRepeat(kIdleInterval);
m_idle = true;
}
# endif
}
int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *uploadData, size_t *uploadSize, void **con_cls)
int xmrig::Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *, const char *uploadData, size_t *uploadSize, void **con_cls)
{
xmrig::HttpRequest req(connection, url, method, uploadData, uploadSize, con_cls);
HttpRequest req(connection, url, method, uploadData, uploadSize, con_cls);
if (req.method() == xmrig::HttpRequest::Options) {
return req.end(MHD_HTTP_OK, nullptr);
@ -150,9 +149,3 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url
return static_cast<Httpd*>(cls)->process(req);
}
void Httpd::onTimer(uv_timer_t *handle)
{
static_cast<Httpd*>(handle->data)->run();
}

View file

@ -26,7 +26,10 @@
#define XMRIG_HTTPD_H
#include <uv.h>
#include <stddef.h>
#include "base/kernel/interfaces/ITimerListener.h"
struct MHD_Connection;
@ -34,31 +37,33 @@ struct MHD_Daemon;
struct MHD_Response;
class UploadCtx;
namespace xmrig {
class HttpRequest;
}
class Httpd
class HttpRequest;
class Timer;
class Httpd : public ITimerListener
{
public:
Httpd(int port, const char *accessToken, bool IPv6, bool restricted);
~Httpd();
~Httpd() override;
bool start();
void stop();
protected:
void onTimer(const Timer *) override { run(); }
private:
constexpr static const int kIdleInterval = 200;
constexpr static const int kActiveInterval = 25;
int process(xmrig::HttpRequest &req);
int process(HttpRequest &req);
void run();
static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *uploadData, size_t *uploadSize, void **con_cls);
static void onTimer(uv_timer_t *handle);
bool m_idle;
bool m_IPv6;
@ -66,7 +71,11 @@ private:
const char *m_accessToken;
const int m_port;
MHD_Daemon *m_daemon;
uv_timer_t *m_timer;
Timer *m_timer;
};
} /* namespace xmrig */
#endif /* XMRIG_HTTPD_H */

View file

@ -35,7 +35,7 @@
#include "base/net/stratum/Client.h"
#include "base/net/stratum/SubmitResult.h"
#include "base/tools/Chrono.h"
#include "base/tools/Handle.h"
#include "base/tools/Timer.h"
#include "common/log/Log.h"
#include "core/Config.h"
#include "core/Controller.h"
@ -58,16 +58,13 @@ xmrig::Network::Network(Controller *controller) :
m_donate = new DonateStrategy(controller->config()->donateLevel(), pools.data().front().user(), controller->config()->algorithm().algo(), this);
}
m_timer = new uv_timer_t;
m_timer->data = this;
uv_timer_init(uv_default_loop(), m_timer);
uv_timer_start(m_timer, Network::onTick, kTickInterval, kTickInterval);
m_timer = new Timer(this, kTickInterval, kTickInterval);
}
xmrig::Network::~Network()
{
Handle::close(m_timer);
delete m_timer;
if (m_donate) {
delete m_donate;
@ -215,9 +212,3 @@ void xmrig::Network::tick()
Api::tick(m_state);
# endif
}
void xmrig::Network::onTick(uv_timer_t *handle)
{
static_cast<Network*>(handle->data)->tick();
}

View file

@ -27,11 +27,11 @@
#include <vector>
#include <uv.h>
#include "api/NetworkState.h"
#include "base/kernel/interfaces/IStrategyListener.h"
#include "base/kernel/interfaces/ITimerListener.h"
#include "common/interfaces/IControllerListener.h"
#include "interfaces/IJobResultListener.h"
@ -43,7 +43,7 @@ class Controller;
class IStrategy;
class Network : public IJobResultListener, public IStrategyListener, public IControllerListener
class Network : public IJobResultListener, public IStrategyListener, public IControllerListener, public ITimerListener
{
public:
Network(Controller *controller);
@ -52,6 +52,8 @@ public:
void connect();
protected:
inline void onTimer(const Timer *) override { tick(); }
void onActive(IStrategy *strategy, Client *client) override;
void onConfigChanged(Config *config, Config *previousConfig) override;
void onJob(IStrategy *strategy, Client *client, const Job &job) override;
@ -66,12 +68,10 @@ private:
void setJob(Client *client, const Job &job, bool donate);
void tick();
static void onTick(uv_timer_t *handle);
IStrategy *m_donate;
IStrategy *m_strategy;
NetworkState m_state;
uv_timer_t *m_timer;
Timer *m_timer;
};