Add measure sending rate and calculate using that.
[brdnet.git] / RecvTC.pas
blob570f4537ac3fb32f0ad3946636a2e1c87531972e
1 uses MemStream,NetAddr
2 ,SysUtils,Sockets,UnixType,BaseUnix
6 var s_inet:tSocket;
8 procedure SC(fn:pointer; retval:cint);
9 begin
10 if retval < 0 then begin
11 raise eXception.Create(Format('Socket error %d operation %P',[SocketError,fn]));
12 end;
13 end;
15 procedure HandleMSG(sock:tSocket; var s:tMemoryStream; const from: tSockAddr);
16 var opcode:byte;
17 var trid:word;
18 var sec:word;
19 var sendbuf:array [1..128] of byte;
20 var r:tMemoryStream;
21 begin
22 opcode:=s.readbyte;
23 assert(opcode=6);
24 s.Read(trid,2);
25 s.Read(sec,2);
26 r.Init(@sendbuf,0,128);
27 r.WriteByte(4);
28 trid:=22; //intel :)
29 r.Write(trid,2);
30 r.Write(sec,2);
31 r.WriteWord(s.length,2);
32 SC(@fpsendto,fpsendto(s_inet,r.base,r.length,0,@from,sizeof(sockaddr_in)));
33 end;
35 procedure s_SetupInet;
36 var bind_addr:tInetSockAddr;
37 begin
38 with bind_addr do begin
39 family:=AF_INET;
40 port:=htons(3511);
41 addr:=0; {any}
42 s_inet:=fpSocket(family,SOCK_DGRAM,IPPROTO_UDP);
43 SC(@fpSocket,s_inet);
44 end;
45 SC(@fpBind,fpBind(s_inet,@bind_addr,sizeof(bind_addr)));
46 end;
48 var Terminated:boolean=false;
50 procedure SignalHandler(sig:cint);CDecl;
51 begin
52 writeln;
53 if terminated then raise eControlC.Create('CtrlC DoubleTap') ;
54 Terminated:=true;
55 writeln('Shutdown requested');
56 end;
58 procedure Loop;
59 var Buffer:array [1..4096] of byte;
60 var s:tMemoryStream;
61 var pkLen:LongInt;
62 var From:tSockAddr;
63 var FromLen:LongWord;
64 begin
65 FromLen:=sizeof(From);
66 pkLen:=fprecvfrom(s_inet,@Buffer,sizeof(Buffer),0,@from,@fromlen);
67 SC(@fprecvfrom,pkLen);
68 //writeln('size ',pkLen,' opcode ',buffer[1]);
69 s.Init(@buffer,pkLen,sizeof(buffer));
70 HandleMsg(s_inet,s,from);
71 end;
73 BEGIN
74 s_setupInet;
75 fpSignal(SigInt,@SignalHandler);
76 fpSignal(SigTerm,@SignalHandler);
77 repeat Loop until Terminated;
78 write('Standard terminate [');
79 CloseSocket(s_inet);
80 writeln(']');
81 END.