3 Implement two-way realiable acked lock-step protocol
6 uses NetAddr
,ServerLoop
,MemStream
;
16 callback
: procedure(msg
:tSMsg
; data
:boolean) of object; {client must maintain active chats}
17 TMhook
: procedure(willwait
:LongWord
) of object;
18 DisposeHook
: procedure of object; {called instead of freeing self}
19 procedure Init(const iremote
:tNetAddr
);
20 procedure AddHeaders(var s
:tMemoryStream
);
21 procedure StreamInit(var s
:tMemoryStream
; l
:word);
22 procedure Send(s
:tMemoryStream
);
23 {the stream can be invalidated, but the buffer must not be modified or freed}
27 txPk
:pointer; txLen
:word; {last sent, not acked msg}
29 procedure InitFrom(const iremote
:tNetAddr
; iopcode
:byte);
32 procedure OnReply(msg
:tSMsg
);
35 type tChatHandler
=procedure(var nchat
:tChat
; msg
:tSMsg
);
36 procedure SetChatHandler(initcode
:byte; handler
:tChatHandler
);
38 { download manager create FileRequest
39 File Request open chat session to server
40 upmgr accepts chat and send reply
41 FileRequest acks, chat is then closed after TimeWait
42 upmgr starts TC transfer
43 transfer finished, upmgr send new Chat to FileRequest
44 FileRequest acks, chat is closed on both ends
45 FileRequest can open new chat if blocks are missing
47 => chat msgs must be created with New, disposed by Chat
48 => there is TimeWait, no references are to the Chat, except Sheduler, it Disposes itself.
51 { Chats are the HiMsg. Use hash table from ServerLoop, works for HiMsg too. }
55 procedure tChat
.Init(const iremote
:tNetAddr
);
58 opcode
:=128+Random(128);
59 while ServerLoop
.IsMsgHandled(opcode
,remote
) do inc(opcode
);
60 InitFrom(remote
,opcode
);
62 procedure tChat
.InitFrom(const iremote
:tNetAddr
; iopcode
:byte);
66 SetMsgHandler(opcode
,remote
,@OnReply
);
69 rxAcked
:=true; {to not ack pk 0}
76 RTT
:=200; {a default for timeouts}
86 procedure tCHat
.AddHeaders(var s
:tMemoryStream
);
88 procedure tChat
.StreamInit(var s
:tMemoryStream
; l
:word);
90 s
.Init(GetMem(l
+5),0,l
+5);
94 procedure tChat
.Send(s
:tMemoryStream
);
97 //assert(assigned(callback));
101 s
.WriteWord(txSeq
,2);
102 if not rxAcked
then begin
103 s
.WriteWord(rxSeq
,2);
105 end else s
.WriteWord(0,2);
108 ServerLoop
.SendMessage(txPk
^,txLen
,remote
);
109 ServerLoop
.Shedule(RTT
*2,@Resend
);
116 if not rxAcked
then begin
117 s
.Init(GetMem(5),0,5);
120 s
.WriteWord(rxSeq
,2);
121 ServerLoop
.SendMessage(s
.base
^,s
.length
,remote
);
122 FreeMem(s
.base
,s
.length
);
127 procedure tChat
.Close
;
132 //writeln('Chat: closing');
133 if txLen
=0 {no packets in flight} then begin
134 Shedule(15000{todo},@Done
); {wait for something lost}
135 callback
:=nil; {avoid calling}
140 procedure tChat
.Done
;
142 if txLen
>0 then FreeMem(txPk
,txLen
);
143 SetMsgHandler(opcode
,remote
,nil);
144 if assigned(DisposeHook
) then DisposeHook
145 else FreeMem(@self
,sizeof(self
));
146 //writeln('Chat: closed');
149 procedure tChat
.Resend
;
150 {timeout waiting for ack}
152 {resend and reshedule}
153 if txLen
=0 then exit
;
155 if RTT
<1 then RTT
:=2;
157 if assigned(TMhook
) and (not closed
) then begin
160 Done
; {if hook decided to close then abort}
164 if closed
and (RTT
<400) then RTT
:=400;
165 if (RTT
>=5000) and closed
then begin
168 {finally resend the msg}
169 //writeln('Chat: retry');
170 ServerLoop
.SendMessage(txPk
^,txLen
,remote
);
171 ServerLoop
.Shedule(RTT
,@Resend
);
175 procedure tChat
.OnReply(msg
:tSMsg
);
179 msg
.stream
.skip(1{opcode});
180 seq
:=msg
.stream
.ReadWord(2);
181 aseq
:=msg
.stream
.ReadWord(2);
182 if aseq
>0 then {ack of our msg} begin
183 if (aseq
=txSeq
)and(txLen
>0) {it is current} then begin
184 if txTime
>0 then RTT
:=Round((Now
-txTime
)*MsecsPerDay
);
187 if Closed
then Shedule(5,@Done
);
190 if assigned(callback
) then callback(msg
,false);
191 end else {write(' old-ack')it is ack of old data, do nothing};
193 if seq
>0 then {some data} begin
194 if seq
<=rxSeq
then {remote didnt get our ack} begin
195 s
.Init(GetMem(5),0,5);
198 s
.WriteWord(rxSeq
,2);
199 ServerLoop
.SendMessage(s
.base
^,s
.length
,remote
);
200 FreeMem(s
.base
,s
.length
);
201 if seq
=rxSeq
then rxacked
:=true;
206 if assigned(callback
) then callback(msg
,true);
211 var ChatHandlers
: array [1..32] of tChatHandler
;
213 procedure SetChatHandler(initcode
:byte; handler
:tChatHandler
);
215 assert(ChatHandlers
[initcode
]=nil);
216 ChatHandlers
[initcode
]:=handler
;
219 procedure OnHiMsg(msg
:tSMsg
);
220 {new chat was received!}
223 var hnd
:tChatHandler
;
227 opcode
:=msg
.stream
.ReadByte
;
228 assert(not IsMsgHandled(opcode
,msg
.source
^));
229 seq
:=msg
.stream
.ReadWord(2);
230 aseq
:=msg
.stream
.ReadWord(2);
231 if (seq
<>1)or(aseq
>0) then exit
; {invalid initial state}
232 ix
:=msg
.stream
.ReadByte
;
233 if (ix
<1)or(ix
>high(ChatHandlers
)) then exit
;
234 hnd
:=ChatHandlers
[ix
];
235 if not assigned(hnd
) then raise eXception
.Create('No handler for initcode '+IntToStr(ix
));
236 msg
.stream
.seek(msg
.stream
.position
-1);{unskip the initcode}
237 nchat
:=GetMem(sizeof(tChat
));
238 nchat
^.InitFrom(msg
.Source
^,opcode
);
239 nchat
^.rxacked
:=false;
245 FillChar(ChatHandlers
,sizeof(chathandlers
),0);
246 ServerLoop
.SetHiMsgHandler(@OnHiMsg
);