2 {Upload Manager for brodnetd}
5 USES Chat
,TC
,opcode
,ServerLoop
,MemStream
,NetAddr
;
19 isOpen
,Active
:boolean;
21 oinfo
:tStoreObjectInfo
;
22 procedure Init(ag
:tAggr_ptr
; var nchat
:tChat
; msg
: tSMsg
);
23 procedure OnMsg(msg
:tSMsg
; data
:boolean);
25 procedure DoGET(const fid
:tfid
; base
,limit
:LongWord
);
26 procedure DoSEG(base
,limit
:LongWord
);
30 procedure ChatTimeout(willwait
:LongWord
);
41 procedure Init(const source
:tNetAddr
);
50 GET(filehash:20; baseHi:word2; base:word4; limit:word4);
51 SEG(baseHi:word2; base:word4; limit:word4);
53 INFO(sizeHi:word2; size:word4; final:byte; seglen:word4);
58 procedure tPrv
.DoGET(const fid
:tfid
; base
,limit
:LongWord
);
59 var err
:tmemorystream
;
61 if isOpen
then oinfo
.Close
;
62 if Active
then Stop
; //opt
65 {if not oinfo.final then begin
69 if oinfo
.rc
>0 then begin
70 ch
^.StreamInit(err
,3);
71 err
.WriteByte(upFAIL
);
72 if oinfo
.rc
=1 then err
.WriteByte(upErrNotFound
)
73 else begin err
.WriteByte(upErrIO
); err
.WriteByte(oinfo
.rc
) end;
81 procedure tPrv
.DoSEG(base
,limit
:LongWord
);
82 var err
:tmemorystream
;
85 ch
^.StreamInit(err
,12);
87 if oinfo
.rc
>0 then begin
88 err
.WriteByte(upFAIL
);
89 err
.WriteByte(upErrIO
);
90 err
.WriteByte(oinfo
.rc
);
94 err
.WriteByte(upINFO
);
96 err
.WriteWord(oinfo
.length
,4);
98 if oinfo
.seglen
<seglen
then seglen
:=oinfo
.seglen
;
100 then err
.WriteByte(1)
101 else err
.WriteByte(0);
102 err
.WriteWord(seglen
,4);
104 if not Active
then Start
;
106 ch
^.StreamInit(err
,2);
107 err
.WriteByte(upFAIL
);
108 err
.WriteByte(upErrSegNoGet
);
113 procedure tPrv
.Start
;
119 writeln('upmgr: Startig transfer');
120 if not assigned(aggr
^.prv
) then begin
126 next
:=aggr
^.prv
^.next
;
138 if prev
<>@self
then begin
142 if aggr
^.prv
=@self
then aggr
^.prv
:=next
;
144 Shedule(20000,@Close
);
145 writeln('upmgr: Stop');
151 var buf
:array [1..2048] of byte;
153 //writeln('upmgr: CONT! ',chan);
154 Assert(Active
and isOpen
);
155 sz
:=aggr
^.tcs
.MaxSize(sizeof(buf
))-1;
156 if sz
>SegLen
then sz
:=SegLen
;
157 //s.Init(GetMem(sz),0,sz);
158 Assert((sz
+1)<=sizeof(buf
));
159 s
.Init(@buf
,0,sizeof(buf
)); aggr
^.tcs
.WriteHeaders(s
);
161 Assert(sz
<=s
.WrBufLen
);
162 oinfo
.ReadAhead(sz
,s
.WrBuf
); //todo
164 Assert(oinfo
.rc
=0); //todo
167 //FreeMem(s.base,s.size);
170 if SegLen
=0 then begin
176 if (wcur
=0) then begin
182 procedure tPrv
.DoClose
;
188 procedure tPrv
.OnMsg(msg
:tSMsg
; data
:boolean);
193 var err
:tmemorystream
;
196 if not data
then exit
; //todo
197 Assert(not(aggr
^.rekt
and active
));
198 if aggr
^.rekt
then exit
;
199 if msg
.stream
.RdBufLen
<1 then goto malformed
;
200 op
:=msg
.stream
.ReadByte
;
201 writeln('upmgr: opcode=',op
,' sz=',msg
.stream
.RdBufLen
);
205 if msg
.stream
.RdBufLen
<>30 then goto malformed
;
206 msg
.stream
.Read(hash
,20);
207 if msg
.stream
.ReadWord(2)>0 then goto malformed
;
208 base
:=msg
.stream
.ReadWord(4);
209 limit
:=msg
.stream
.ReadWord(4);
210 DoGet(hash
,base
,limit
);
213 if msg
.stream
.RdBufLen
<10 then goto malformed
;
214 if msg
.stream
.ReadWord(2)>0 then goto malformed
;
215 base
:=msg
.stream
.ReadWord(4);
216 limit
:=msg
.stream
.ReadWord(4);
222 ch
^.StreamInit(err
,2);
223 err
.WriteByte(upFAIL
);
224 err
.WriteByte(upErrMalformed
);
226 writeln('upmgr: malformed request stage=1');
229 {######Timeouts and Shit#######}
230 procedure tPrv
.ChatTimeout(willwait
:LongWord
);
232 if WillWait
<8000 then exit
;
233 writeln('upmgr: Chat timeout');
236 procedure tPrv
.Close
;
237 var err
:tMemoryStream
;
239 assert(assigned(ch
));
240 ch
^.StreamInit(err
,1);
241 err
.WriteByte(upClose
);
242 try ch
^.Send(err
); except end;
244 if isOpen
then oinfo
.Close
;
250 FreeMem(@self
,sizeof(self
));
253 procedure tPrv
.Init(ag
:tAggr_ptr
; var nchat
:tChat
; msg
: tSMsg
);
256 ch
^.Callback
:=@OnMsg
;
257 ch
^.TMHook
:=@ChatTimeout
;
261 chan
:=msg
.stream
.readbyte
;
262 writeln('upmgr: prv init chan=',chan
);
265 isOpen
:=false; Active
:=false;
267 Shedule(5000,@Close
);
271 procedure tAggr
.Init(const source
:tNetAddr
);
273 writeln('upmgr: init');
277 if assigned(Peers
) then Peers
^.prev
:=@self
;
282 tcs
.OnTimeout
:=@TCTimeout
;
287 procedure tAggr
.TCTimeout
;
290 writeln('upmgr: TCTimeout');
291 while assigned(prv
) do begin
300 procedure tAggr
.Cont
;
302 if not assigned(prv
) then exit
;
305 procedure tAggr
.UnRef
;
309 writeln('upmgr: aggr unrefd');
312 FreeMem(@self
,sizeof(self
));
315 procedure tAggr
.Done
;
318 writeln('upmgr: aggr close');
321 if assigned(prev
) then prev
^.next
:=next
else Peers
:=next
;
322 if assigned(next
) then next
^.prev
:=prev
;
325 function FindAggr({const} addr
:tNetAddr
): tAggr_ptr
;
328 while assigned(result
) do begin
329 if assigned(result
^.next
) then assert(result
^.next
^.prev
=result
);
330 if result
^.tcs
.remote
=addr
then exit
;
331 result
:=result
^.next
;
335 procedure ChatHandler(var nchat
:tChat
; msg
:tSMsg
);
341 writeln('upmgr: ChatHandler');
342 msg
.stream
.skip({the initcode}1);
343 if msg
.stream
.RdBufLen
<2 then begin
344 writeln('upmgr: malformed init');
345 nchat
.StreamInit(s
,16);
347 s
.writebyte(upErrMalformed
);
350 writeln('upmgr: malformed request stage=0');
353 ag
:=FindAggr(msg
.source
^);
354 if assigned(ag
) then begin
356 if ag
^.Cnt
>=cMax
then begin
357 nchat
.StreamInit(s
,16);
359 s
.WriteByte(upErrHiChan
);
361 s
.WriteByte(ag
^.Cnt
);
367 ag
^.init(msg
.source
^);
370 pr
^.Init(ag
,nchat
,msg
);
374 SetChatHandler(opcode
.upFileServer
,@ChatHandler
);