diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 376d7ff37..28d89cc43 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -211,6 +211,7 @@ SET(common_headers xml_parser.h zone_numbers.h event/event_loop.h + event/idle.h event/task.h event/timer.h json/json.h @@ -271,6 +272,7 @@ SET(common_headers SOURCE_GROUP(Event FILES event/event_loop.h + event/idle.h event/timer.h event/task.h ) diff --git a/common/event/event_loop.h b/common/event/event_loop.h index 268f78c4d..6d0c8fcca 100644 --- a/common/event/event_loop.h +++ b/common/event/event_loop.h @@ -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; }; } diff --git a/common/event/task.h b/common/event/task.h index ae19700a2..4ac230496 100644 --- a/common/event/task.h +++ b/common/event/task.h @@ -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; }; } diff --git a/common/event/timer.h b/common/event/timer.h index e71459630..79eaae307 100644 --- a/common/event/timer.h +++ b/common/event/timer.h @@ -6,16 +6,29 @@ namespace EQ { class Timer { public: - Timer(std::function cb) + Timer(EventLoop &loop, std::function cb) : _loop(loop) { - m_timer = nullptr; - m_cb = cb; + _timer = nullptr; + _cb = cb; } - Timer(uint64_t duration_ms, bool repeats, std::function cb) + Timer(EventLoop &loop, uint64_t duration_ms, bool repeats, std::function cb) : _loop(loop) { - m_timer = nullptr; - m_cb = cb; + _timer = nullptr; + _cb = cb; + Start(duration_ms, repeats); + } + + Timer(std::function cb) : _loop(EventLoop::GetDefault()) + { + _timer = nullptr; + _cb = cb; + } + + Timer(uint64_t duration_ms, bool repeats, std::function 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 m_cb; + EventLoop &_loop; + uv_timer_t *_timer; + std::function _cb; }; } diff --git a/common/net/daybreak_connection.cpp b/common/net/daybreak_connection.cpp index b10203854..5c44851cc 100644 --- a/common/net/daybreak_connection.cpp +++ b/common/net/daybreak_connection.cpp @@ -13,7 +13,7 @@ EQ::Net::DaybreakConnectionManager::DaybreakConnectionManager() memset(&m_timer, 0, sizeof(uv_timer_t)); memset(&m_socket, 0, sizeof(uv_udp_t)); - Attach(EQ::EventLoop::Get().Handle()); + Attach(EQ::EventLoop::GetDefault().Handle()); } EQ::Net::DaybreakConnectionManager::DaybreakConnectionManager(const DaybreakConnectionManagerOptions &opts) @@ -23,7 +23,7 @@ EQ::Net::DaybreakConnectionManager::DaybreakConnectionManager(const DaybreakConn memset(&m_timer, 0, sizeof(uv_timer_t)); memset(&m_socket, 0, sizeof(uv_udp_t)); - Attach(EQ::EventLoop::Get().Handle()); + Attach(EQ::EventLoop::GetDefault().Handle()); } EQ::Net::DaybreakConnectionManager::~DaybreakConnectionManager() diff --git a/common/net/dns.h b/common/net/dns.h index d43f41566..e105e40b4 100644 --- a/common/net/dns.h +++ b/common/net/dns.h @@ -21,7 +21,7 @@ namespace EQ hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; - auto loop = EQ::EventLoop::Get().Handle(); + auto loop = EQ::EventLoop::GetDefault().Handle(); uv_getaddrinfo_t *resolver = new uv_getaddrinfo_t(); memset(resolver, 0, sizeof(uv_getaddrinfo_t)); auto port_str = std::to_string(port); diff --git a/common/net/tcp_connection.cpp b/common/net/tcp_connection.cpp index 03f8466de..fc162d0b5 100644 --- a/common/net/tcp_connection.cpp +++ b/common/net/tcp_connection.cpp @@ -23,7 +23,7 @@ void EQ::Net::TCPConnection::Connect(const std::string &addr, int port, bool ipv std::function)> cb; }; - auto loop = EQ::EventLoop::Get().Handle(); + auto loop = EQ::EventLoop::GetDefault().Handle(); uv_tcp_t *socket = new uv_tcp_t; memset(socket, 0, sizeof(uv_tcp_t)); uv_tcp_init(loop, socket); diff --git a/common/net/tcp_server.cpp b/common/net/tcp_server.cpp index ed68d54dc..f6fd1a211 100644 --- a/common/net/tcp_server.cpp +++ b/common/net/tcp_server.cpp @@ -32,7 +32,7 @@ void EQ::Net::TCPServer::Listen(const std::string &addr, int port, bool ipv6, st m_on_new_connection = cb; - auto loop = EQ::EventLoop::Get().Handle(); + auto loop = EQ::EventLoop::GetDefault().Handle(); m_socket = new uv_tcp_t; memset(m_socket, 0, sizeof(uv_tcp_t)); uv_tcp_init(loop, m_socket); @@ -53,7 +53,7 @@ void EQ::Net::TCPServer::Listen(const std::string &addr, int port, bool ipv6, st return; } - auto loop = EQ::EventLoop::Get().Handle(); + auto loop = EQ::EventLoop::GetDefault().Handle(); uv_tcp_t *client = new uv_tcp_t; memset(client, 0, sizeof(uv_tcp_t)); uv_tcp_init(loop, client); diff --git a/eqlaunch/eqlaunch.cpp b/eqlaunch/eqlaunch.cpp index ef8e5603f..975039827 100644 --- a/eqlaunch/eqlaunch.cpp +++ b/eqlaunch/eqlaunch.cpp @@ -136,7 +136,7 @@ int main(int argc, char *argv[]) { zones.erase(rem); } - EQ::EventLoop::Get().Process(); + EQ::EventLoop::GetDefault().Process(); Sleep(5); } diff --git a/loginserver/main.cpp b/loginserver/main.cpp index fde9e549f..33bad4e84 100644 --- a/loginserver/main.cpp +++ b/loginserver/main.cpp @@ -179,7 +179,7 @@ int main() while (run_server) { Timer::SetCurrentTime(); server.client_manager->Process(); - EQ::EventLoop::Get().Process(); + EQ::EventLoop::GetDefault().Process(); Sleep(5); } diff --git a/queryserv/queryserv.cpp b/queryserv/queryserv.cpp index 326b80b3a..70ff5d8ee 100644 --- a/queryserv/queryserv.cpp +++ b/queryserv/queryserv.cpp @@ -99,7 +99,7 @@ int main() { if(LFGuildExpireTimer.Check()) lfguildmanager.ExpireEntries(); - EQ::EventLoop::Get().Process(); + EQ::EventLoop::GetDefault().Process(); Sleep(5); } LogSys.CloseFileLogs(); diff --git a/ucs/ucs.cpp b/ucs/ucs.cpp index 60a7b727b..fb552bc0a 100644 --- a/ucs/ucs.cpp +++ b/ucs/ucs.cpp @@ -153,7 +153,7 @@ int main() { if(ChannelListProcessTimer.Check()) ChannelList->Process(); - EQ::EventLoop::Get().Process(); + EQ::EventLoop::GetDefault().Process(); Sleep(5); } diff --git a/world/net.cpp b/world/net.cpp index a192cce50..b57a0cefe 100644 --- a/world/net.cpp +++ b/world/net.cpp @@ -587,7 +587,7 @@ int main(int argc, char** argv) { UpdateWindowTitle(window_title); } - EQ::EventLoop::Get().Process(); + EQ::EventLoop::GetDefault().Process(); Sleep(5); } Log(Logs::General, Logs::World_Server, "World main loop completed."); diff --git a/zone/net.cpp b/zone/net.cpp index 18d2253f1..4d72e1bae 100644 --- a/zone/net.cpp +++ b/zone/net.cpp @@ -586,7 +586,7 @@ int main(int argc, char** argv) { while (RunLoops) { bool previous_loaded = is_zone_loaded && numclients > 0; - EQ::EventLoop::Get().Process(); + EQ::EventLoop::GetDefault().Process(); bool current_loaded = is_zone_loaded && numclients > 0; if (previous_loaded && !current_loaded) {