Decoupling eventloop singleton from the event loop (for now not complete but later we will)

This commit is contained in:
KimLS 2019-06-24 00:15:02 -07:00
parent 5c7ab59fd3
commit 0e0027ca20
14 changed files with 82 additions and 61 deletions

View File

@ -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
)

View File

@ -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;
};
}

View File

@ -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;
};
}

View File

@ -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;
};
}

View File

@ -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()

View File

@ -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);

View File

@ -23,7 +23,7 @@ void EQ::Net::TCPConnection::Connect(const std::string &addr, int port, bool ipv
std::function<void(std::shared_ptr<EQ::Net::TCPConnection>)> 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);

View File

@ -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);

View File

@ -136,7 +136,7 @@ int main(int argc, char *argv[]) {
zones.erase(rem);
}
EQ::EventLoop::Get().Process();
EQ::EventLoop::GetDefault().Process();
Sleep(5);
}

View File

@ -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);
}

View File

@ -99,7 +99,7 @@ int main() {
if(LFGuildExpireTimer.Check())
lfguildmanager.ExpireEntries();
EQ::EventLoop::Get().Process();
EQ::EventLoop::GetDefault().Process();
Sleep(5);
}
LogSys.CloseFileLogs();

View File

@ -153,7 +153,7 @@ int main() {
if(ChannelListProcessTimer.Check())
ChannelList->Process();
EQ::EventLoop::Get().Process();
EQ::EventLoop::GetDefault().Process();
Sleep(5);
}

View File

@ -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.");

View File

@ -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) {