UnShedule all duplicate entries.
[brdnet.git] / TestChat.pas
blob85f611f44fc341149ffa0003e5d816210f39f40b
1 unit TestChat;
3 INTERFACE
4 IMPLEMENTATION
5 USES ServerLoop,Chat,SysUtils,MemStream,NetAddr;
7 type t=object
8 ch: tChat;
9 //procedure UserInput
10 procedure Reply(msg:tSMsg; data:boolean);
11 procedure Rekt;
12 procedure Timeout(willwait:LongWord);
13 procedure HardTimeout;
14 end;
16 procedure t.Reply(msg:tSMsg; data:boolean);
17 var d:string;
18 begin
19 write('TestChat: ');
20 if data then begin
21 setlength(d,msg.stream.RDBufLen);
22 msg.stream.read(d[1],msg.stream.RDBufLen);
23 writeln('reply '+d);
24 end else writeln('ack');
25 end;
27 procedure t.Rekt;
28 begin
29 writeln('TestChat: rekt');
30 FreeMem(@self,sizeof(self));
31 end;
33 procedure t.Timeout(willwait:LongWord);
34 begin
35 if willwait>=16000 then begin
36 writeln('TestChat: timeout, give up');
37 ch.Close;
38 end else
39 writeln('TestChat: resend willwait='+IntToStr(willwait));
40 end;
42 procedure t.HardTimeout;
43 begin
44 writeln('TestChat: hardtimeout');
45 ch.Close;
46 end;
48 procedure ChatHandler(var nchat:tChat; msg:tSMsg);
49 var d:ansistring;
50 var s:tMemoryStream;
51 begin
52 write('TestChat: ');
53 msg.stream.skip(1);
54 setlength(d,msg.stream.RDBufLen);
55 msg.stream.read(d[1],msg.stream.RDBufLen);
56 writeln('msg '+d);
57 //nchat.Ack;
58 s.init(GetMem(56),0,56);
59 nchat.AddHeaders(s);
60 d:='Test Chat Reply!';
61 s.Write(d[1],length(d));
62 nchat.Send(s);
63 nchat.Close;
64 end;
67 procedure init;
68 var o:^t;
69 var oi:word;
70 var s:tMemoryStream;
71 const opt='-test-chat';
72 var msg:string[50];
73 begin
74 oi:=OptIndex(opt);
75 if oi>0 then begin
76 assert(OptParamCount(oi)=1,opt+'(rcpt:tNetAddr)');
77 writeln('TestChat: rcpt '+paramstr(oi+1));
78 {init chat to rcpt, send message wait reply, print reply, done}
79 new(o); with o^ do begin
80 ch.Init(paramstr(oi+1));
81 ch.Callback:=@Reply;
82 ch.DisposeHook:=@Rekt;
83 ch.TMHook:=@Timeout;
84 Shedule(7000,@HardTimeout);
85 s.init(GetMem(56),0,56);
86 ch.AddHeaders(s);
87 msg:='Test Chat Message!';
88 s.WriteByte(32);
89 s.Write(msg[1],length(msg));
90 ch.Send(s);
91 end;
92 end;
93 SetChatHandler(32,@ChatHandler);
94 end;
96 BEGIN
97 init;
98 END.