2 ,SysUtils
,Sockets
,UnixType
,BaseUnix
8 procedure SC(fn
:pointer; retval
:cint
);
10 if retval
< 0 then begin
11 raise eXception
.Create(Format('Socket error %d operation %P',[SocketError
,fn
]));
20 {opcode 4=data, 8=data-no-report, 6=data-immediate-ack}
21 {opcode 5=cont, 7=ack}
23 procedure HandleMSG(sock
:tSocket
; var s
:tMemoryStream
; const from
: tSockAddr
);
26 var sendbuf
:array [1..128] of byte;
29 var rate
:DWord
; {BytesPerSecond shr 6 (=64)}
31 r
.Init(@sendbuf
,0,128);
36 if mark
<>PrvMark
then begin
37 if mark
<>CurMark
then begin
43 end else begin Inc(Total
,s
.length
); Inc(DgCnt
) end;
50 r
.WriteWord(s
.length
,2);
51 SC(@fpsendto
,fpsendto(s_inet
,r
.base
,r
.length
,0,@from
,sizeof(sockaddr_in
)));
55 if (now
-Startt
)<(0.4/SecsPerDay
) then exit
;
56 rateR
:=Total
/((now
-Startt
)*SecsPerDay
);
57 writeln('Rate: ',(rateR
/1024):7:1);
58 rate
:=round(rateR
/64);
64 SC(@fpsendto
,fpsendto(s_inet
,r
.base
,r
.length
,0,@from
,sizeof(sockaddr_in
)));
67 procedure s_SetupInet
;
68 var bind_addr
:tInetSockAddr
;
70 with bind_addr
do begin
74 s_inet
:=fpSocket(family
,SOCK_DGRAM
,IPPROTO_UDP
);
77 SC(@fpBind
,fpBind(s_inet
,@bind_addr
,sizeof(bind_addr
)));
80 var Terminated
:boolean=false;
82 procedure SignalHandler(sig
:cint
);CDecl;
85 if terminated
then raise eControlC
.Create('CtrlC DoubleTap') ;
87 writeln('Shutdown requested');
91 var Buffer
:array [1..4096] of byte;
97 FromLen
:=sizeof(From
);
98 pkLen
:=fprecvfrom(s_inet
,@Buffer
,sizeof(Buffer
),0,@from
,@fromlen
);
99 SC(@fprecvfrom
,pkLen
);
100 //writeln('size ',pkLen,' opcode ',buffer[1]);
101 s
.Init(@buffer
,pkLen
,sizeof(buffer
));
102 HandleMsg(s_inet
,s
,from
);
107 fpSignal(SigInt
,@SignalHandler
);
108 fpSignal(SigTerm
,@SignalHandler
);
109 repeat Loop
until Terminated
;
110 write('Standard terminate [');