moontalk/server/eventloop-server-experiment/connections.4th
Emil Williams 7721b77c93
update
2024-02-03 17:37:01 +00:00

51 lines
1.4 KiB
Forth

256 constant CONNECTION_BUFFER_SIZE
0
cell +field connection.number
cell +field connection.fd
cell +field connection.connected
cell +field connection.bufferlen
CONNECTION_BUFFER_SIZE +field connection.buffer
constant /CONNECTION
1024 10 * constant MAX_CONNECTIONS
variable last-connection -1 last-connection !
variable largest-index -1 largest-index !
MAX_CONNECTIONS /CONNECTION * constant CONNECTIONS_SIZE
create connections CONNECTIONS_SIZE allot
connections CONNECTIONS_SIZE erase
: (translate) ( index-u -- connection-addr )
/CONNECTION * connections + ;
: (untranslate) ( connection-addr -- index-u )
connections - /CONNECTION / ;
: (find-free) ( -- index-u flag )
MAX_CONNECTIONS 0 DO
I (translate) connection.connected @ invert IF
I true UNLOOP EXIT
THEN
LOOP 0 false ;
: connections.append ( connection-addr -- flag )
(find-free) IF
tuck (translate) /CONNECTION move
dup last-connection !
dup largest-index @ > IF
largest-index !
ELSE
drop
THEN
true
ELSE
2drop false
THEN ;
: connections.count ( -- count-u )
largest-index @ 1+ ;
: connections.at ( index-u -- connection-addr )
(translate) ;
: connections.last ( -- connection-addr )
last-connection @ (translate) ;
: connections.indexOf ( connection-addr -- index-u )
(untranslate) ;