mirror of
https://github.com/EQEmu/Server.git
synced 2026-06-16 09:48:22 +00:00
Decoupling eventloop singleton from the event loop (for now not complete but later we will)
This commit is contained in:
+12
-12
@@ -8,30 +8,30 @@ namespace EQ
|
||||
class EventLoop
|
||||
{
|
||||
public:
|
||||
static EventLoop &Get() {
|
||||
static thread_local EventLoop inst;
|
||||
return inst;
|
||||
EventLoop() {
|
||||
memset(&m_loop, 0, sizeof(uv_loop_t));
|
||||
uv_loop_init(&m_loop);
|
||||
}
|
||||
|
||||
~EventLoop() {
|
||||
uv_loop_close(&m_loop);
|
||||
}
|
||||
|
||||
EventLoop(const EventLoop&) = delete;
|
||||
EventLoop& operator=(const EventLoop&) = delete;
|
||||
|
||||
static EventLoop &GetDefault() {
|
||||
static thread_local EventLoop inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
void Process() {
|
||||
uv_run(&m_loop, UV_RUN_NOWAIT);
|
||||
}
|
||||
|
||||
uv_loop_t* Handle() { return &m_loop; }
|
||||
|
||||
private:
|
||||
EventLoop() {
|
||||
memset(&m_loop, 0, sizeof(uv_loop_t));
|
||||
uv_loop_init(&m_loop);
|
||||
}
|
||||
|
||||
EventLoop(const EventLoop&);
|
||||
EventLoop& operator=(const EventLoop&);
|
||||
|
||||
private:
|
||||
uv_loop_t m_loop;
|
||||
};
|
||||
}
|
||||
|
||||
+22
-17
@@ -24,8 +24,12 @@ namespace EQ {
|
||||
std::exception error;
|
||||
};
|
||||
|
||||
Task(TaskFn fn) {
|
||||
m_fn = fn;
|
||||
Task(EventLoop &loop, TaskFn fn) : _loop(loop) {
|
||||
_fn = fn;
|
||||
}
|
||||
|
||||
Task(TaskFn fn) : _loop(EventLoop::GetDefault()) {
|
||||
_fn = fn;
|
||||
}
|
||||
|
||||
~Task() {
|
||||
@@ -33,34 +37,34 @@ namespace EQ {
|
||||
}
|
||||
|
||||
Task& Then(ResolveFn fn) {
|
||||
m_then = fn;
|
||||
_then = fn;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Task& Catch(RejectFn fn) {
|
||||
m_catch = fn;
|
||||
_catch = fn;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Task& Finally(FinallyFn fn) {
|
||||
m_finally = fn;
|
||||
_fin = fn;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Run() {
|
||||
uv_work_t *m_work = new uv_work_t;
|
||||
memset(m_work, 0, sizeof(uv_work_t));
|
||||
uv_work_t *work = new uv_work_t;
|
||||
memset(work, 0, sizeof(uv_work_t));
|
||||
TaskBaton *baton = new TaskBaton();
|
||||
baton->fn = m_fn;
|
||||
baton->on_then = m_then;
|
||||
baton->on_catch = m_catch;
|
||||
baton->on_finally = m_finally;
|
||||
baton->fn = _fn;
|
||||
baton->on_then = _then;
|
||||
baton->on_catch = _catch;
|
||||
baton->on_finally = _fin;
|
||||
baton->has_result = false;
|
||||
baton->has_error = false;
|
||||
|
||||
m_work->data = baton;
|
||||
work->data = baton;
|
||||
|
||||
uv_queue_work(EventLoop::Get().Handle(), m_work, [](uv_work_t* req) {
|
||||
uv_queue_work(_loop.Handle(), work, [](uv_work_t* req) {
|
||||
TaskBaton *baton = (TaskBaton*)req->data;
|
||||
|
||||
baton->fn([baton](const EQEmu::Any& result) {
|
||||
@@ -92,9 +96,10 @@ namespace EQ {
|
||||
}
|
||||
|
||||
private:
|
||||
TaskFn m_fn;
|
||||
ResolveFn m_then;
|
||||
RejectFn m_catch;
|
||||
FinallyFn m_finally;
|
||||
TaskFn _fn;
|
||||
ResolveFn _then;
|
||||
RejectFn _catch;
|
||||
FinallyFn _fin;
|
||||
EventLoop &_loop;
|
||||
};
|
||||
}
|
||||
|
||||
+34
-20
@@ -6,16 +6,29 @@ namespace EQ {
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
Timer(std::function<void(Timer *)> cb)
|
||||
Timer(EventLoop &loop, std::function<void(Timer *)> cb) : _loop(loop)
|
||||
{
|
||||
m_timer = nullptr;
|
||||
m_cb = cb;
|
||||
_timer = nullptr;
|
||||
_cb = cb;
|
||||
}
|
||||
|
||||
Timer(uint64_t duration_ms, bool repeats, std::function<void(Timer *)> cb)
|
||||
Timer(EventLoop &loop, uint64_t duration_ms, bool repeats, std::function<void(Timer *)> cb) : _loop(loop)
|
||||
{
|
||||
m_timer = nullptr;
|
||||
m_cb = cb;
|
||||
_timer = nullptr;
|
||||
_cb = cb;
|
||||
Start(duration_ms, repeats);
|
||||
}
|
||||
|
||||
Timer(std::function<void(Timer *)> cb) : _loop(EventLoop::GetDefault())
|
||||
{
|
||||
_timer = nullptr;
|
||||
_cb = cb;
|
||||
}
|
||||
|
||||
Timer(uint64_t duration_ms, bool repeats, std::function<void(Timer *)> cb) : _loop(EventLoop::GetDefault())
|
||||
{
|
||||
_timer = nullptr;
|
||||
_cb = cb;
|
||||
Start(duration_ms, repeats);
|
||||
}
|
||||
|
||||
@@ -25,21 +38,21 @@ namespace EQ {
|
||||
}
|
||||
|
||||
void Start(uint64_t duration_ms, bool repeats) {
|
||||
auto loop = EventLoop::Get().Handle();
|
||||
if (!m_timer) {
|
||||
m_timer = new uv_timer_t;
|
||||
memset(m_timer, 0, sizeof(uv_timer_t));
|
||||
uv_timer_init(loop, m_timer);
|
||||
m_timer->data = this;
|
||||
auto loop = EventLoop::GetDefault().Handle();
|
||||
if (!_timer) {
|
||||
_timer = new uv_timer_t;
|
||||
memset(_timer, 0, sizeof(uv_timer_t));
|
||||
uv_timer_init(loop, _timer);
|
||||
_timer->data = this;
|
||||
|
||||
if (repeats) {
|
||||
uv_timer_start(m_timer, [](uv_timer_t *handle) {
|
||||
uv_timer_start(_timer, [](uv_timer_t *handle) {
|
||||
Timer *t = (Timer*)handle->data;
|
||||
t->Execute();
|
||||
}, duration_ms, duration_ms);
|
||||
}
|
||||
else {
|
||||
uv_timer_start(m_timer, [](uv_timer_t *handle) {
|
||||
uv_timer_start(_timer, [](uv_timer_t *handle) {
|
||||
Timer *t = (Timer*)handle->data;
|
||||
t->Stop();
|
||||
t->Execute();
|
||||
@@ -49,19 +62,20 @@ namespace EQ {
|
||||
}
|
||||
|
||||
void Stop() {
|
||||
if (m_timer) {
|
||||
uv_close((uv_handle_t*)m_timer, [](uv_handle_t* handle) {
|
||||
if (_timer) {
|
||||
uv_close((uv_handle_t*)_timer, [](uv_handle_t* handle) {
|
||||
delete handle;
|
||||
});
|
||||
m_timer = nullptr;
|
||||
_timer = nullptr;
|
||||
}
|
||||
}
|
||||
private:
|
||||
void Execute() {
|
||||
m_cb(this);
|
||||
_cb(this);
|
||||
}
|
||||
|
||||
uv_timer_t *m_timer;
|
||||
std::function<void(Timer*)> m_cb;
|
||||
EventLoop &_loop;
|
||||
uv_timer_t *_timer;
|
||||
std::function<void(Timer*)> _cb;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user