Add class Timer.
This commit is contained in:
parent
bbf0d11a51
commit
9a6a5a94b5
13 changed files with 271 additions and 73 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue