moontalk/server/eventloop-server-experiment/connections.4th

54 lines
1.5 KiB
Plaintext
Raw Normal View History

2024-02-17 13:26:14 -05:00
require util.4th
2024-02-03 12:37:01 -05:00
256 constant CONNECTION_BUFFER_SIZE
0
cell +field connection.number
2024-02-17 13:26:14 -05:00
cell +field connection.admin
2024-02-03 12:37:01 -05:00
cell +field connection.fd
cell +field connection.connected
2024-02-17 13:26:14 -05:00
cell +field connection.sendcount
cell +field connection.circuitid
2024-02-03 12:37:01 -05:00
cell +field connection.bufferlen
CONNECTION_BUFFER_SIZE +field connection.buffer
constant /CONNECTION
2024-02-17 13:26:14 -05:00
32 constant MAX_CONNECTIONS
2024-02-03 12:37:01 -05:00
2024-02-17 13:26:14 -05:00
-1 variable! last-connection
-1 variable! largest-index
2024-02-03 12:37:01 -05:00
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) ;