moontalk/server/eventloop-server-experiment/main.4th
2024-02-17 18:26:14 +00:00

57 lines
1.1 KiB
Forth

require util.4th
require torcontrol-constants.4th
require configuration.4th
require stdout-hook.4th
require check-gforth.4th
require eventloop.4th
require event-constants.4th
require server.4th
\ TODO: integrate generic timed event handling into the event loop?
0 variable! clcounter
: handle-command-line? ( -- flag )
clcounter @ 10 >= dup IF
0 clcounter !
ELSE
1 clcounter +!
THEN ;
0 variable! dostimer
: handle-dos? ( -- flag )
0 time dup dostimer @ > IF
DOS_UPDATE_INTERVAL + dostimer !
true
ELSE
drop false
THEN ;
: custom-eventloop ( -- )
BEGIN
handle-command-line? IF
0 EVENT_COMMANDLINE events.enqueue
THEN
handle-dos? IF
dos-update
THEN
eventloop.has-events? IF
eventloop.dispatch
ELSE
server-idle? IF
10 ms false server-idle!
THEN
0 0 events.enqueue eventloop.dispatch
THEN
AGAIN ;
: main ( -- )
logger.open
['] custom-eventloop catch close-server throw
logger.close ;
\ : main ( -- )
\ logger.open
\ custom-eventloop close-server
\ logger.close ;
main