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