void ServiceTimers(void) { MSG msg; INT64 ms; StartupComplete(); /* for the interface to report no errors on startup */ InterfaceUpdate(); lprintf("Status: %i accounts\n",GetNextAccountID()); lprintf("-------------------------------------------------------------------------------------\n"); dprintf("-------------------------------------------------------------------------------------\n"); eprintf("-------------------------------------------------------------------------------------\n"); in_main_loop = True; SetWindowText(hwndMain, ConfigStr(CONSOLE_CAPTION)); AsyncSocketStart(); for(;;) { if (timers == NULL) ms = 500; else { ms = timers->time - GetMilliCount(); if (ms <= 0) ms = 0; if (ms > 500) ms = 500; } if (MsgWaitForMultipleObjects(0,NULL,0,(DWORD)ms,QS_ALLINPUT) == WAIT_OBJECT_0) { while (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if (msg.message == WM_QUIT) { lprintf("ServiceTimers shutting down the server\n"); return; } switch (msg.message) { case WM_BLAK_MAIN_READ : EnterServerLock(); PollSession(msg.lParam); TimerActivate(); LeaveServerLock(); break; case WM_BLAK_MAIN_RECALIBRATE : /* new soonest timer, so we should recalculate our time left... so we just need to restart the loop! */ break; case WM_BLAK_MAIN_DELETE_ACCOUNT : EnterServerLock(); DeleteAccountAndAssociatedUsersByID(msg.lParam); LeaveServerLock(); break; case WM_BLAK_MAIN_VERIFIED_LOGIN : EnterServerLock(); VerifiedLoginSession(msg.lParam); LeaveServerLock(); break; case WM_BLAK_MAIN_LOAD_GAME : EnterServerLock(); LoadFromKod(msg.lParam); LeaveServerLock(); break; default : dprintf("ServiceTimers got unknown message %i\n",msg.message); break; } } } else { /* a Blakod timer is ready to go */ EnterServerLock(); PollSessions(); /* really just need to check session timers */ TimerActivate(); LeaveServerLock(); } } }
void RunMainLoop(void) { INT64 ms; const uint32_t num_notify_events = 500; struct epoll_event notify_events[num_notify_events]; int i; signal(SIGPIPE, SIG_IGN); while (!GetQuit()) { ms = GetMainLoopWaitTime(); // wait up to ms, dispatch any socket events int val = epoll_wait(fd_epoll, notify_events, num_notify_events, ms); if (val == -1) { eprintf("RunMainLoop error on epoll_wait %s\n", GetLastErrorStr()); } //printf("got events %i %lu\n", val, ms); for (i=0;i<val;i++) { if (notify_events[i].events == 0) continue; if (IsAcceptingSocket(notify_events[i].data.fd)) { if (notify_events[i].events & ~EPOLLIN) { eprintf("RunMainLoop error on accepting socket %i\n",notify_events[i].data.fd); } else { AsyncSocketAccept(notify_events[i].data.fd,FD_ACCEPT,0,GetAcceptingSocketConnectionType(notify_events[i].data.fd)); } } else { if (notify_events[i].events & ~(EPOLLIN | EPOLLOUT)) { // this means there was an error AsyncSocketSelect(notify_events[i].data.fd,0,1); } else { if (notify_events[i].events & EPOLLIN) { AsyncSocketSelect(notify_events[i].data.fd,FD_READ,0); } if (notify_events[i].events & EPOLLOUT) { AsyncSocketSelect(notify_events[i].data.fd,FD_WRITE,0); } } } } EnterServerLock(); PollSessions(); /* really just need to check session timers */ TimerActivate(); LeaveServerLock(); } close(fd_epoll); }