Chat API update. Breaks everything.
[brdnet.git] / TestChat.pas
blob0b497fa53a291fe07f2e1c67a36913ef2db00280
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;
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;
34 begin
35 writeln('TestChat: timeout, give up');
36 ch.Close;
37 end;
39 procedure t.HardTimeout;
40 begin
41 writeln('TestChat: hardtimeout');
42 ch.Close;
43 end;
45 procedure ChatHandler(var nchat:tChat; msg:tSMsg);
46 var d:ansistring;
47 var s:tMemoryStream;
48 begin
49 write('TestChat: ');
50 msg.stream.skip(1);
51 setlength(d,msg.stream.RDBufLen);
52 msg.stream.read(d[1],msg.stream.RDBufLen);
53 writeln('msg '+d);
54 //nchat.Ack;
55 s.init(GetMem(56),0,56);
56 nchat.AddHeaders(s);
57 d:='Test Chat Reply!';
58 s.Write(d[1],length(d));
59 nchat.Send(s);
60 nchat.Close;
61 end;
64 procedure init;
65 var o:^t;
66 var oi:word;
67 var s:tMemoryStream;
68 const opt='-test-chat';
69 var msg:string[50];
70 begin
71 oi:=OptIndex(opt);
72 if oi>0 then begin
73 assert(OptParamCount(oi)=1,opt+'(rcpt:tNetAddr)');
74 writeln('TestChat: rcpt '+paramstr(oi+1));
75 {init chat to rcpt, send message wait reply, print reply, done}
76 new(o); with o^ do begin
77 ch.Init(paramstr(oi+1));
78 ch.Callback:=@Reply;
79 ch.OnDispose:=@Rekt;
80 ch.OnTimeout:=@Timeout;
81 //Shedule(7000,@HardTimeout);
82 s.init(GetMem(56),0,56);
83 ch.SetTimeout(6000,8000);
84 ch.AddHeaders(s);
85 msg:='Test Chat Message!';
86 s.WriteByte(32);
87 s.Write(msg[1],length(msg));
88 ch.Send(s);
89 end;
90 end;
91 SetChatHandler(32,@ChatHandler);
92 end;
94 BEGIN
95 init;
96 END.