-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjTrans.pas
448 lines (423 loc) · 10.3 KB
/
ObjTrans.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
UNIT ObjTrans;
{
Object Transfer Client
}
INTERFACE
uses ObjectModel,Classes,Store,ServerLoop;
type
tAggr_ptr=^tAggr;
tSeg=object
first,after:LongWord;
end;
tSegItem=object(tSeg)
next:^tSegItem;
end;
tJob=object
public {writable}
Weight:0..127;
Callback:procedure of object;
public {read-only}
Received:LongWord;
Total:LongWord;
error:integer;{0progress,1done,-2ioerror,-3timeout,-4full,OTinfo}
dataf:tStream; {do not touch while active}
FID:tFID;
aggr:tAggr_ptr;
procedure Init;
procedure Start;
procedure Done;
procedure MakeRequest(diag:byte);
private
ch:byte;
FirstSeg:^tSegItem;
RetryCounter:word;
DgrCntSinceReq:LongWord;
procedure OnData(msg:tMemoryStream);
procedure OnInfo(icode:byte; ifilelen:LongWord; ireqlen:LongWord; msg:tMemoryStream);
procedure OnTimeout; {nothing is flowing thru aggr or afterrequest check}
end;
tAggr=object
Remote:tNetAddr;
refc: byte;
distance: longint;
max_channels{highest channel number},
max_segments{one less then real max}: byte;
procedure ResetIdle;
private
channel: array [1..16] of ^tJob;
ncpt, idletick: byte;
MarkValue: byte;
LargestDgr: Word;
ByteCnt:LongWord;
DgrCnt:LongWord;
DgrCntCheck:LongWord;
StartT:tMTime;
ReadyForData:boolean;
procedure Init;
procedure Add(var job: tJob);
procedure Del(var job: tJob);
procedure OnInfo(msg:tSMsg);
procedure OnData(msg:tSMsg);
procedure Periodic;
end;
function GetAggr(const iRemote: tNetAddr; create: boolean): tAggr_ptr;
IMPLEMENTATION
uses opcode,SysUtils;
var Aggrs: array [0..511] of ^tAggr;
function compareAggr(a: pointer; key: pointer): ShortInt;
begin
if assigned(a)
then result:=CompareByte(tAggr(a^).Remote, tNetAddr(key^), sizeof(tNetAddr))
else result:=1; {outside of array, go left}
end;
function GetAggr(const iRemote: tNetAddr; create: boolean): tAggr_ptr;
var i:longword;
begin
i:=FindIndex(@Aggrs[0], high(Aggrs)+1, @iremote, @compareAggr);
if i<=high(Aggrs) then begin
if assigned(Aggrs[i]) and (Aggrs[i]^.Remote=iRemote)
then result:=Aggrs[i]
else if create
and( (Aggrs[i]=nil) or PtrListShiftRight(@Aggrs[0], High(Aggrs), i) )then begin
New(Aggrs[i]);
Aggrs[i]^.Remote:=iRemote;
Aggrs[i]^.Init;
result:=Aggrs[i];
end else result:=nil;
end else result:=nil;
end;
procedure tJob.Init;
begin
Weight:=32;
Callback:=nil;
Received:=0;
Total:=$FFFFFFFF;
RetryCounter:=0;
FirstSeg:=nil;
Aggr:=nil;
ch:=0;
error:=1;
end;
procedure tJob.Start;
begin
Assert(assigned(callback));
Assert(assigned(aggr));
assert(ch=0);
{set fid and open temporary data file}
{attach to connection}
aggr^.Add(self);
if ch=0 then begin
error:=-4;
end else begin
{make the request}
error:=0;
MakeRequest(1);
end;
end;
procedure tJob.Done;
var cp,dp:^tSegItem;
begin
UnShedule(@OnTimeout);
if assigned(aggr) then aggr^.Del(self);
cp:=FirstSeg;
while assigned(cp) do begin
dp:=cp;
cp:=cp^.next;
Dispose(dp);
end;
end;
{Strategy for fixing holes without waiting for DONE message:
* stuff all small segments to LSEG
* put large one at end
* when datagrams from the large arrive, Repeat
}
procedure tJob.MakeRequest(diag:byte);
var s:tMemoryStream;
var b,l,ReqLen:LongWord;
var ReqCnt:byte;
var seg:^tSegItem;
const ReqLenLim=5000000;
begin
write('ObjTrans.',string(aggr^.remote),'#',ch,'.MakeRequest',diag);
seg:=nil;
ReqLen:=0;
ReqCnt:=0;
{$hint derive request size from connection datagram size}
s:=tMemoryStream.Create;
s.w1(opcode.otRequest);
s.w1(ch);
s.w1(Weight);
s.w1(0{flags});
s.Write(FID,24);
repeat
if seg=nil then begin
b:=0;
seg:=FirstSeg;
if assigned(seg) and (seg^.first=0) then begin
b:=seg^.after;
seg:=seg^.next;
end;
end else begin
b:=seg^.after;
seg:=seg^.next;
end;
if assigned(seg) then l:=seg^.first-b else l:=Total-b;
if l=0 then break;
if (ReqLen+l)>ReqLenLim then l:=ReqLenLim-ReqLen;
write(' ',b,'+',l);
s.w1(0);
s.w4(b);
s.w4(l);
inc(ReqLen,l);
inc(ReqCnt);
until (ReqCnt>=40)or(ReqLen>=ReqLenLim)or(ReqCnt>aggr^.max_segments);
if ReqLen=0 then begin
writeln(' done');
s.Free;
error:=1;
Callback;
end else begin
writeln(' send ',s.size);
SendMessage(s.memory^,s.size,aggr^.Remote);
s.free;
UnShedule(@OnTimeout);
Shedule(750,@OnTimeout);
DgrCntSinceReq:=0;
//HighestRequestBase:=b;
end;
end;
procedure tJob.OnInfo(icode:byte; ifilelen:LongWord; ireqlen:LongWord; msg:tMemoryStream);
begin
if (icode=0) or (icode=1) then begin
Total:=ifilelen;
{...:=}{ireqlen};
if ireqlen=0 then begin
{$hint some rate limiting}
MakeRequest(3);
end;
end else begin
error:=icode;
Callback;
end
end;
procedure tJob.OnData(msg:tMemoryStream);
var hiOfs:byte;
var Offset:LongWord;
var dtlen:word;
procedure SetSegment(first,after:LongWord);
var p:^pointer;
var c,k:^tSegItem;
begin
p:=@FirstSeg; c:=p^; k:=nil;
while assigned(c) do begin
{merge cur to new}
if (c^.first<first)and(c^.after>=first) then first:=c^.first;
if (c^.after>after)and(c^.first<=after) then after:=c^.after;
{remove cur if fully contained in new}
if (first<=c^.first)and(after>=c^.after) then begin
p^:=c^.next;
if assigned(k) then dispose(c) else k:=c;
c:=p^; continue;
end;
p:=@c^.next; c:=p^;
if first>=after then begin
if assigned(k) then dispose(k);
exit end;
end;
{merge completed, insert new segment}
if not assigned(k) then new(k);
k^.first:=first;
k^.after:=after;
p:=@FirstSeg; c:=p^; while assigned(c) and (c^.first<first)
do begin p:=@c^.next; c:=p^ end;
k^.next:=c;
p^:=k;
end;
begin
hiOfs:=msg.r1;
assert(hiOfs=0{!!!});
Offset:=msg.r4;
dtlen:=msg.left;
dataf.Position:=Offset;
dataf.Write((msg.memory+msg.position)^,dtlen);
{$hint iocheck todo}
SetSegment(Offset,Offset+dtlen);
RetryCounter:=0;
Inc(DgrCntSinceReq);
Inc(Received,dtLen);
end;
procedure tJob.OnTimeout;
{called by aggr or resheduled in MakeRequest}
begin
if DgrCntSinceReq=0 then begin
if RetryCounter>=13 then begin
error:=-3;
Callback;
end else begin
MakeRequest(4);
Inc(RetryCounter);
end;
end
else DgrCntSinceReq:=0; {next time aggr calls its probably serious}
end;
procedure tAggr.OnInfo(msg:tSMsg);
var rr:packed record op,chn,flg,code:byte end;
var flenhi:byte;
var flen,rlen:LongWord;
var s:tMemoryStream absolute msg.st;
var debugmsg:string[127];
begin
{common things}
s.Read(rr,4);
{handle debug info}
if rr.code=2 then begin
SetLength(debugmsg,s.Left);
s.Read(debugmsg[1],length(debugmsg));
writeln('ObjTrans.',string(remote),'#',rr.chn,'.ServerDebug: '#10+debugmsg);
EXIT end;
max_channels:=s.r1;
max_segments:=s.r1;
distance:=s.r1-msg.ttl;
if rr.chn>0 then begin
flenhi:=s.r1;
flen:=s.r4;
rlen:=s.r4;
assert(flenhi=0{!!!});
if (rr.chn<=high(channel)) and assigned(channel[rr.chn]) then begin
channel[rr.chn]^.OnInfo(rr.code,flen,rlen,s);
end {else invalid info...}
end else begin
{info/error to all channels...}
end;
end;
procedure tAggr.OnData(msg:tSMsg);
var op,chn,mark:byte;
var rate:single;
var s:tMemoryStream absolute msg.st;
begin
{common things}
op:=s.r1;
mark:=s.r1;
chn:=s.r1;
{handle invalid channel}
if chn=0 then exit;
if (chn>high(channel)) or (not assigned(channel[chn])) then begin
s.clear;
s.w1(opcode.otStop);
s.w1(chn);
SendMessage(s.memory^,s.size,Remote);
exit end;
{measurements}
if (DgrCnt=0)or(MarkValue<>mark) then begin
{single delayed packet can mess up this}
StartT:=mNow;
ByteCnt:=0;
DgrCnt:=0;
MarkValue:=mark;
LargestDgr:=0;
end;
Inc(ByteCnt,s.Size);
Inc(DgrCnt);
Inc(DgrCntCheck);
if LargestDgr<s.Size then LargestDgr:=s.Size;
{give channel data up}
channel[chn]^.OnData(s);
{send report}
if (op=otDataSync) or ((DgrCnt>=3) and ((mNow-StartT)>=100)) then begin
s.Clear;
s.w1(otReport);
s.w1(MarkValue);
s.w1(0{flags});
s.w1(0{TTL});
rate:=(ByteCnt/(mNow-StartT))*16;
s.w4(round(rate));
s.w2(LargestDgr);
StartT:=mNow;
ByteCnt:=1;
DgrCnt:=0;
SendMessage(s.memory^,s.size,Remote);
end;
end;
procedure tAggr.Init;
var i:integer;
begin
refc:=0; ncpt:=0;
ReadyForData:=true;
Shedule(915,@Periodic);
for i:=1 to high(channel) do channel[i]:=nil;
ByteCnt:=0;
DgrCnt:=0;
DgrCntCheck:=0;
StartT:=0;
LargestDgr:=32;
Distance:=65535;
max_channels:=32;
max_segments:=15;
MarkValue:=255;
end;
procedure tAggr.Add(var job: tJob);
var i:integer;
begin
Assert(job.ch=0);
i:=ncpt;
repeat
if i=high(channel) then i:=1 else i:=i+1;
if channel[i]=nil then begin
job.ch:=i; ncpt:=i;
channel[job.ch]:=@job;
inc(refc);
idletick:=0;
break;
end;
until i=ncpt;
end;
procedure tAggr.Del(var job: tJob);
begin
Assert(job.ch>0);
Assert(assigned(channel[job.ch]));
dec(refc);
channel[job.ch]:=nil;
job.ch:=0;
end;
procedure tAggr.ResetIdle;
begin
if (RefC=0) and (idletick>0) then idletick:=1;
end;
procedure tAggr.Periodic;
var i:integer;
begin
if refc>0 then begin
{check DgrCntCheck and issue Timeout}
if DgrCntCheck=0 then begin
for i:=1 to high(channel) do if assigned(channel[i])
then channel[i]^.OnTimeout;
end else DgrCntCheck:=0;
end else begin
{check idle time and delete self}
if idletick>17 then begin
writeln('ObjTrans.tAggr.Periodic.IdleDeleteSelf ',string(remote));
i:=FindIndex(@Aggrs[0], high(Aggrs)+1, @Remote, @compareAggr);
writeln('ObjTrans.tAggr.Periodic.IdleDeleteSelf ',i);
Aggrs[i]:=nil;
ReadyForData:=false;
FreeMem(@self,sizeof(self));
EXIT end;
inc(idletick);
end;
Shedule(700,@Periodic);
end;
procedure DataHandler(msg:tSMsg);
var aggr:^tAggr;
begin
aggr:=GetAggr(Msg.Source, False);
if assigned(aggr) and (aggr^.ReadyForData) then begin
if byte(msg.Data^)=opcode.otInfo
then aggr^.OnInfo(Msg)
else aggr^.OnData(Msg);
end;
end;
BEGIN
SetupOpcode(opcode.otData,@DataHandler);
SetupOpcode(opcode.otDataSync,@DataHandler);
SetupOpcode(opcode.otInfo,@DataHandler);
END.