fuck
[brdnet.git] / UploadTC.pas
blobfa3b986f12ed6768c5324d715217143fff19d2a5
1 {include file}
2 procedure tAggr.CalcRates(rxRate:Single);
3 var txRate:Single;
4 var RateFill:Single;
5 var pMark:byte;
6 const limRateIF=1;
7 begin
8 EnterCriticalSection(thr.crit);
9 pMark:=thr.mark1;
10 if thr.MarkTime=0 then thr.MarkTime:=1;
11 if thr.MarkData=0
12 then begin
13 writeln('RESET INITIAL');
14 thr.Mark1:=random(255)+1; thr.MarkData:=0; thr.MarkTime:=0; txRate:=rxRate; thr.Rate:=4096;
15 end else txRate:=thr.MarkData/(thr.MarkTime/1000);
16 if rxRate=0 then rxRate:=1;
17 if txRate=0 then txRate:=1;
18 RateFill:=rxRate/txRate;
19 write('speed: ',(rxRate/1024):8:2,'kB/s (',(RateFill*100):3:0,'% of ',txRate/1024:8:2,'), ');
20 if RateFill<0.90 then begin
21 write('limit');
22 if RateFill<0.5 then thr.size1:=round(thr.size1*0.75);
23 thr.Rate:=rxRate;
24 RateIF:=RateIF*0.1*RateFill;
25 end else
26 if (txRate/thr.Rate)>0.7 then begin
27 write('pass');
28 thr.Rate:=1+txRate*(RateIF+1);
29 if thr.Rate>limRate then thr.Rate:=limRate
30 else RateIF:=RateIF+0.1;
31 if RateIF>limRateIF then RateIF:=LimRateIF;
32 end else write('3hard');
33 repeat thr.mark1:=Random(255)+1 until (thr.mark1<>pMark);
34 thr.MarkData:=0;
35 thr.MarkTime:=0;
36 if thr.size1<120 then thr.size1:=128;
37 {no ack to size inc packet, back up}
38 sizeIF:=sizeIF/8;
39 {but at least 1 byte increase}
40 if (thr.size1*SizeIF)<1 then SizeIF:=1/thr.Size1;
41 {freq...}
42 if (thr.Size1/thr.Rate)>0.11 then begin thr.size1:=100; thr.rate:=4096; end;
43 write(', if=',RateIF:6:4);
44 write(', size=',thr.size1:5,'+',SizeIF:6:4);
45 {request ack, also triggers MTU discovery}
46 thr.size2:=thr.size1;
47 thr.size1:=thr.size2-1;
48 thr.mark2:=thr.Mark1;
49 writeln;
50 LeaveCriticalSection(thr.crit);
51 end;
53 procedure tAggr.OnCont(msg:tSMsg);
54 var op,rmark:byte;
55 var rRate:LongWord;
56 begin
57 op:=msg.stream.readbyte;
58 assert(op=opcode.tccont);
59 rmark:=msg.stream.readbyte;
60 if rmark=thr.mark1 then begin
61 inc(acks);
62 timeout:=0;
63 rrate:=msg.stream.readword(4);
64 CalcRates(rRate*64);
65 end;
66 end;
68 procedure tAggr.OnAck(msg:tSMsg);
69 var op,rmark:byte;
70 var rSize:LongWord;
71 const limSizeIF=1;
72 begin
73 op:=msg.stream.readbyte;
74 assert(op=opcode.tceack);
75 rmark:=msg.stream.readbyte;
76 rsize:=msg.stream.readword(2);
77 if (rmark<>thr.mark2)and(rmark<>thr.mark1) then exit;
78 inc(acks);
79 Timeout:=0;
80 {do nothing if timeout recovery or not increase}
81 if (rsize<=thr.size1)or(timeout>0) then exit;
82 EnterCriticalSection(thr.crit);
83 {try to maintain frequency}
84 if (rSize/thr.Rate)<0.11
85 then thr.size1:=rSize {use the new size as main size}
86 else sizeIF:=0;
87 {increase increase fastor}
88 SizeIF:=SizeIF*2; if SizeIF>limSizeIF then SizeIF:=limSizeIF;
89 assert(thr.size2=0);
90 {calc new packet size}
91 thr.size2:=round(thr.Size1*(1+SizeIF));
92 {do nothing if they are equal}
93 if thr.size1=thr.size2 then thr.size2:=0
94 {else writeln('Set size2: ',thr.size2,' ',thr.size1)};
95 thr.mark2:=thr.mark1;
96 LeaveCriticalSection(thr.crit);
97 end;