require 'cs.pl' ;

$rc = &rCsInit() ;
die unless ($rc == 0) ;
$file = &CsIoOpen("udp///99",4,0x4003) ;
die unless ($file != 0) ;
&set_blocking($file,1) ;
&set_immediate($file,1) ;	# datagrams - not stream
$packet_size = 1024 ;
$data = pack("SC$packet_size",0) ;       # presize the buffer
for (;;)  {
    $rc = &rCsIoRead($buf,1,512,$nb,$file) ;
    die if ($rc != 0) ;
    $sample_size = unpack("S",$buf) ;
#    print "Server: sample size = $sample_size\n" ;
    $seq = 0 ;
    for ($i=0 ; $i<$sample_size ; $i+=$packet_size)  {
#        print "Server: sending #$seq\n" ;
        substr($data,0,2) = pack("S",$seq++) ;
        for ($z=0 ; $z<$packet_size ; $z++)  {
# triangle ascending...
            substr($data,$z+2,1) = pack("C",$z+$base) ;
# random height
#            substr($data,$z+2,1) = pack("C",256*rand) ;
        }
        $rc = &rCsIoWrite($data,1,length($data),$nb,$file) ;
        die if ($rc != 0) ;
        &rCsSetTimer(10,1,10) ;
        &rCsWaitEventsMask(1<<10) ;
    }
    $base++ ;
}
