-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestChat.pas
96 lines (86 loc) · 1.83 KB
/
TestChat.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
unit TestChat;
INTERFACE
IMPLEMENTATION
USES ServerLoop,Chat,SysUtils,MemStream,NetAddr;
type t=object
ch: tChat;
//procedure UserInput
procedure Reply(msg:tSMsg; data:boolean);
procedure Rekt;
procedure Timeout;
procedure HardTimeout;
end;
procedure t.Reply(msg:tSMsg; data:boolean);
var d:string;
begin
write('TestChat: ');
if data then begin
setlength(d,msg.stream.RDBufLen);
msg.stream.read(d[1],msg.stream.RDBufLen);
writeln('reply '+d);
end else writeln('ack');
end;
procedure t.Rekt;
begin
writeln('TestChat: rekt');
FreeMem(@self,sizeof(self));
end;
procedure t.Timeout;
begin
writeln('TestChat: timeout, give up');
ch.Close;
end;
procedure t.HardTimeout;
begin
writeln('TestChat: hardtimeout');
ch.Close;
end;
procedure ChatHandler(var nchat:tChat; msg:tSMsg);
var d:ansistring;
var s:tMemoryStream;
begin
write('TestChat: ');
msg.stream.skip(1);
setlength(d,msg.stream.RDBufLen);
msg.stream.read(d[1],msg.stream.RDBufLen);
writeln('msg '+d);
//nchat.Ack;
s.init(GetMem(56),0,56);
nchat.AddHeaders(s);
d:='Test Chat Reply!';
s.Write(d[1],length(d));
nchat.Send(s);
nchat.Close;
end;
procedure init;
var o:^t;
var oi:word;
var s:tMemoryStream;
const opt='-test-chat';
var msg:string[50];
begin
oi:=OptIndex(opt);
if oi>0 then begin
assert(OptParamCount(oi)=1,opt+'(rcpt:tNetAddr)');
writeln('TestChat: rcpt '+paramstr(oi+1));
{init chat to rcpt, send message wait reply, print reply, done}
new(o); with o^ do begin
ch.Init(paramstr(oi+1));
ch.Callback:=@Reply;
ch.OnDispose:=@Rekt;
ch.OnTimeout:=@Timeout;
//Shedule(7000,@HardTimeout);
s.init(GetMem(56),0,56);
ch.SetTimeout(6000,8000);
ch.AddHeaders(s);
msg:='Test Chat Message!';
s.WriteByte(32);
s.Write(msg[1],length(msg));
ch.Send(s);
end;
end;
SetChatHandler(32,@ChatHandler);
end;
BEGIN
init;
END.