@prog lib-connects
1 99999 d
1 i
( ***** Misc Connect routines -- CONN *****
All funcs return descriptor numbers.  To get a connection number
from a descriptor number, use DESCRCON.
)
  
: findconn (a d -- i)
  descriptors dup 2 + rotate 0
  begin
    3 pick 0 > while
    rot 1 - -3 rotate 4 rotate
    over if
      over over 5 pick execute if swap then pop
    else
      swap pop
    then
  repeat
  -3 rotate pop pop
;

  
: leastidle-comp descrcon conidle swap descrcon conidle < ;
: leastidle
  (returns the number of the least idle connection for the given player)
  'leastidle-comp swap findconn
;


: mostidle-comp descrcon conidle swap descrcon conidle > ;
: mostidle
  (returns the number of the most idle connection for the given player)
  'mostidle-comp swap findconn
;

: firstconn (d -- i)
  descriptors
  begin
    dup 1 > while
    swap pop 1 -
  repeat
  pop
;

: lastconn (d -- i)
  descriptors 1 - swap over 2 + -1 * rotate
  begin dup while 1 - swap pop repeat pop
;

public findconn
public leastidle
public mostidle
public firstconn
public lastconn
.
c
q
@register lib-connects=lib/connects
@register #me lib-connects=tmp/prog1
@set $tmp/prog1=L
@set $tmp/prog1=3

@set $tmp/prog1=/_defs/conn-findconn:"$lib/connects" match "findconn" call
@set $tmp/prog1=/_defs/conn-leastidle:"$lib/connects" match "leastidle" call
@set $tmp/prog1=/_defs/conn-mostidle:"$lib/connects" match "mostidle" call
@set $tmp/prog1=/_defs/conn-firstconn:"$lib/connects" match "firstconn" call
@set $tmp/prog1=/_defs/conn-lastconn:"$lib/connects" match "lastconn" call

@set $tmp/prog1=/_docs:@list $lib/connects=1-29
