mirror of
https://github.com/EQEmu/Server.git
synced 2026-04-10 20:22:41 +00:00
Decoupling eventloop singleton from the event loop (for now not complete but later we will)
This commit is contained in:
parent
5c7ab59fd3
commit
0e0027ca20
@ -211,6 +211,7 @@ SET(common_headers
|
|||||||
xml_parser.h
|
xml_parser.h
|
||||||
zone_numbers.h
|
zone_numbers.h
|
||||||
event/event_loop.h
|
event/event_loop.h
|
||||||
|
event/idle.h
|
||||||
event/task.h
|
event/task.h
|
||||||
event/timer.h
|
event/timer.h
|
||||||
json/json.h
|
json/json.h
|
||||||
@ -271,6 +272,7 @@ SET(common_headers
|
|||||||
|
|
||||||
SOURCE_GROUP(Event FILES
|
SOURCE_GROUP(Event FILES
|
||||||
event/event_loop.h
|
event/event_loop.h
|
||||||
|
event/idle.h
|
||||||
event/timer.h
|
event/timer.h
|
||||||
event/task.h
|
event/task.h
|
||||||
)
|
)
|
||||||
|
|||||||
@ -8,15 +8,23 @@ namespace EQ
|
|||||||
class EventLoop
|
class EventLoop
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static EventLoop &Get() {
|
EventLoop() {
|
||||||
static thread_local EventLoop inst;
|
memset(&m_loop, 0, sizeof(uv_loop_t));
|
||||||
return inst;
|
uv_loop_init(&m_loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
~EventLoop() {
|
~EventLoop() {
|
||||||
uv_loop_close(&m_loop);
|
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() {
|
void Process() {
|
||||||
uv_run(&m_loop, UV_RUN_NOWAIT);
|
uv_run(&m_loop, UV_RUN_NOWAIT);
|
||||||
}
|
}
|
||||||
@ -24,14 +32,6 @@ namespace EQ
|
|||||||
uv_loop_t* Handle() { return &m_loop; }
|
uv_loop_t* Handle() { return &m_loop; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EventLoop() {
|
|
||||||
memset(&m_loop, 0, sizeof(uv_loop_t));
|
|
||||||
uv_loop_init(&m_loop);
|
|
||||||
}
|
|
||||||
|
|
||||||
EventLoop(const EventLoop&);
|
|
||||||
EventLoop& operator=(const EventLoop&);
|
|
||||||
|
|
||||||
uv_loop_t m_loop;
|
uv_loop_t m_loop;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,8 +24,12 @@ namespace EQ {
|
|||||||
std::exception error;
|
std::exception error;
|
||||||
};
|
};
|
||||||
|
|
||||||
Task(TaskFn fn) {
|
Task(EventLoop &loop, TaskFn fn) : _loop(loop) {
|
||||||
m_fn = fn;
|
_fn = fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
Task(TaskFn fn) : _loop(EventLoop::GetDefault()) {
|
||||||
|
_fn = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
~Task() {
|
~Task() {
|
||||||
@ -33,34 +37,34 @@ namespace EQ {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Task& Then(ResolveFn fn) {
|
Task& Then(ResolveFn fn) {
|
||||||
m_then = fn;
|
_then = fn;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Task& Catch(RejectFn fn) {
|
Task& Catch(RejectFn fn) {
|
||||||
m_catch = fn;
|
_catch = fn;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Task& Finally(FinallyFn fn) {
|
Task& Finally(FinallyFn fn) {
|
||||||
m_finally = fn;
|
_fin = fn;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Run() {
|
void Run() {
|
||||||
uv_work_t *m_work = new uv_work_t;
|
uv_work_t *work = new uv_work_t;
|
||||||
memset(m_work, 0, sizeof(uv_work_t));
|
memset(work, 0, sizeof(uv_work_t));
|
||||||
TaskBaton *baton = new TaskBaton();
|
TaskBaton *baton = new TaskBaton();
|
||||||
baton->fn = m_fn;
|
baton->fn = _fn;
|
||||||
baton->on_then = m_then;
|
baton->on_then = _then;
|
||||||
baton->on_catch = m_catch;
|
baton->on_catch = _catch;
|
||||||
baton->on_finally = m_finally;
|
baton->on_finally = _fin;
|
||||||
baton->has_result = false;
|
baton->has_result = false;
|
||||||
baton->has_error = 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;
|
TaskBaton *baton = (TaskBaton*)req->data;
|
||||||
|
|
||||||
baton->fn([baton](const EQEmu::Any& result) {
|
baton->fn([baton](const EQEmu::Any& result) {
|
||||||
@ -92,9 +96,10 @@ namespace EQ {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TaskFn m_fn;
|
TaskFn _fn;
|
||||||
ResolveFn m_then;
|
ResolveFn _then;
|
||||||
RejectFn m_catch;
|
RejectFn _catch;
|
||||||
FinallyFn m_finally;
|
FinallyFn _fin;
|
||||||
|
EventLoop &_loop;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,16 +6,29 @@ namespace EQ {
|
|||||||
class Timer
|
class Timer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Timer(std::function<void(Timer *)> cb)
|
Timer(EventLoop &loop, std::function<void(Timer *)> cb) : _loop(loop)
|
||||||
{
|
{
|
||||||
m_timer = nullptr;
|
_timer = nullptr;
|
||||||
m_cb = cb;
|
_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;
|
_timer = nullptr;
|
||||||
m_cb = cb;
|
_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);
|
Start(duration_ms, repeats);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,21 +38,21 @@ namespace EQ {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Start(uint64_t duration_ms, bool repeats) {
|
void Start(uint64_t duration_ms, bool repeats) {
|
||||||
auto loop = EventLoop::Get().Handle();
|
auto loop = EventLoop::GetDefault().Handle();
|
||||||
if (!m_timer) {
|
if (!_timer) {
|
||||||
m_timer = new uv_timer_t;
|
_timer = new uv_timer_t;
|
||||||
memset(m_timer, 0, sizeof(uv_timer_t));
|
memset(_timer, 0, sizeof(uv_timer_t));
|
||||||
uv_timer_init(loop, m_timer);
|
uv_timer_init(loop, _timer);
|
||||||
m_timer->data = this;
|
_timer->data = this;
|
||||||
|
|
||||||
if (repeats) {
|
if (repeats) {
|
||||||
uv_timer_start(m_timer, [](uv_timer_t *handle) {
|
uv_timer_start(_timer, [](uv_timer_t *handle) {
|
||||||
Timer *t = (Timer*)handle->data;
|
Timer *t = (Timer*)handle->data;
|
||||||
t->Execute();
|
t->Execute();
|
||||||
}, duration_ms, duration_ms);
|
}, duration_ms, duration_ms);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
uv_timer_start(m_timer, [](uv_timer_t *handle) {
|
uv_timer_start(_timer, [](uv_timer_t *handle) {
|
||||||
Timer *t = (Timer*)handle->data;
|
Timer *t = (Timer*)handle->data;
|
||||||
t->Stop();
|
t->Stop();
|
||||||
t->Execute();
|
t->Execute();
|
||||||
@ -49,19 +62,20 @@ namespace EQ {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Stop() {
|
void Stop() {
|
||||||
if (m_timer) {
|
if (_timer) {
|
||||||
uv_close((uv_handle_t*)m_timer, [](uv_handle_t* handle) {
|
uv_close((uv_handle_t*)_timer, [](uv_handle_t* handle) {
|
||||||
delete handle;
|
delete handle;
|
||||||
});
|
});
|
||||||
m_timer = nullptr;
|
_timer = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
void Execute() {
|
void Execute() {
|
||||||
m_cb(this);
|
_cb(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
uv_timer_t *m_timer;
|
EventLoop &_loop;
|
||||||
std::function<void(Timer*)> m_cb;
|
uv_timer_t *_timer;
|
||||||
|
std::function<void(Timer*)> _cb;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,7 @@ EQ::Net::DaybreakConnectionManager::DaybreakConnectionManager()
|
|||||||
memset(&m_timer, 0, sizeof(uv_timer_t));
|
memset(&m_timer, 0, sizeof(uv_timer_t));
|
||||||
memset(&m_socket, 0, sizeof(uv_udp_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)
|
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_timer, 0, sizeof(uv_timer_t));
|
||||||
memset(&m_socket, 0, sizeof(uv_udp_t));
|
memset(&m_socket, 0, sizeof(uv_udp_t));
|
||||||
|
|
||||||
Attach(EQ::EventLoop::Get().Handle());
|
Attach(EQ::EventLoop::GetDefault().Handle());
|
||||||
}
|
}
|
||||||
|
|
||||||
EQ::Net::DaybreakConnectionManager::~DaybreakConnectionManager()
|
EQ::Net::DaybreakConnectionManager::~DaybreakConnectionManager()
|
||||||
|
|||||||
@ -21,7 +21,7 @@ namespace EQ
|
|||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
hints.ai_protocol = IPPROTO_TCP;
|
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();
|
uv_getaddrinfo_t *resolver = new uv_getaddrinfo_t();
|
||||||
memset(resolver, 0, sizeof(uv_getaddrinfo_t));
|
memset(resolver, 0, sizeof(uv_getaddrinfo_t));
|
||||||
auto port_str = std::to_string(port);
|
auto port_str = std::to_string(port);
|
||||||
|
|||||||
@ -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;
|
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;
|
uv_tcp_t *socket = new uv_tcp_t;
|
||||||
memset(socket, 0, sizeof(uv_tcp_t));
|
memset(socket, 0, sizeof(uv_tcp_t));
|
||||||
uv_tcp_init(loop, socket);
|
uv_tcp_init(loop, socket);
|
||||||
|
|||||||
@ -32,7 +32,7 @@ void EQ::Net::TCPServer::Listen(const std::string &addr, int port, bool ipv6, st
|
|||||||
|
|
||||||
m_on_new_connection = cb;
|
m_on_new_connection = cb;
|
||||||
|
|
||||||
auto loop = EQ::EventLoop::Get().Handle();
|
auto loop = EQ::EventLoop::GetDefault().Handle();
|
||||||
m_socket = new uv_tcp_t;
|
m_socket = new uv_tcp_t;
|
||||||
memset(m_socket, 0, sizeof(uv_tcp_t));
|
memset(m_socket, 0, sizeof(uv_tcp_t));
|
||||||
uv_tcp_init(loop, m_socket);
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto loop = EQ::EventLoop::Get().Handle();
|
auto loop = EQ::EventLoop::GetDefault().Handle();
|
||||||
uv_tcp_t *client = new uv_tcp_t;
|
uv_tcp_t *client = new uv_tcp_t;
|
||||||
memset(client, 0, sizeof(uv_tcp_t));
|
memset(client, 0, sizeof(uv_tcp_t));
|
||||||
uv_tcp_init(loop, client);
|
uv_tcp_init(loop, client);
|
||||||
|
|||||||
@ -136,7 +136,7 @@ int main(int argc, char *argv[]) {
|
|||||||
zones.erase(rem);
|
zones.erase(rem);
|
||||||
}
|
}
|
||||||
|
|
||||||
EQ::EventLoop::Get().Process();
|
EQ::EventLoop::GetDefault().Process();
|
||||||
Sleep(5);
|
Sleep(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -179,7 +179,7 @@ int main()
|
|||||||
while (run_server) {
|
while (run_server) {
|
||||||
Timer::SetCurrentTime();
|
Timer::SetCurrentTime();
|
||||||
server.client_manager->Process();
|
server.client_manager->Process();
|
||||||
EQ::EventLoop::Get().Process();
|
EQ::EventLoop::GetDefault().Process();
|
||||||
Sleep(5);
|
Sleep(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -99,7 +99,7 @@ int main() {
|
|||||||
if(LFGuildExpireTimer.Check())
|
if(LFGuildExpireTimer.Check())
|
||||||
lfguildmanager.ExpireEntries();
|
lfguildmanager.ExpireEntries();
|
||||||
|
|
||||||
EQ::EventLoop::Get().Process();
|
EQ::EventLoop::GetDefault().Process();
|
||||||
Sleep(5);
|
Sleep(5);
|
||||||
}
|
}
|
||||||
LogSys.CloseFileLogs();
|
LogSys.CloseFileLogs();
|
||||||
|
|||||||
@ -153,7 +153,7 @@ int main() {
|
|||||||
if(ChannelListProcessTimer.Check())
|
if(ChannelListProcessTimer.Check())
|
||||||
ChannelList->Process();
|
ChannelList->Process();
|
||||||
|
|
||||||
EQ::EventLoop::Get().Process();
|
EQ::EventLoop::GetDefault().Process();
|
||||||
|
|
||||||
Sleep(5);
|
Sleep(5);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -587,7 +587,7 @@ int main(int argc, char** argv) {
|
|||||||
UpdateWindowTitle(window_title);
|
UpdateWindowTitle(window_title);
|
||||||
}
|
}
|
||||||
|
|
||||||
EQ::EventLoop::Get().Process();
|
EQ::EventLoop::GetDefault().Process();
|
||||||
Sleep(5);
|
Sleep(5);
|
||||||
}
|
}
|
||||||
Log(Logs::General, Logs::World_Server, "World main loop completed.");
|
Log(Logs::General, Logs::World_Server, "World main loop completed.");
|
||||||
|
|||||||
@ -586,7 +586,7 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
while (RunLoops) {
|
while (RunLoops) {
|
||||||
bool previous_loaded = is_zone_loaded && numclients > 0;
|
bool previous_loaded = is_zone_loaded && numclients > 0;
|
||||||
EQ::EventLoop::Get().Process();
|
EQ::EventLoop::GetDefault().Process();
|
||||||
|
|
||||||
bool current_loaded = is_zone_loaded && numclients > 0;
|
bool current_loaded = is_zone_loaded && numclients > 0;
|
||||||
if (previous_loaded && !current_loaded) {
|
if (previous_loaded && !current_loaded) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user