7 lcorewsaasyncselect in 'lcorewsaasyncselect.pas',
\r
15 //we don't actually make any use of the units below in this app, we just
\r
16 //include it to check if it compiles ok ;)
\r
19 {$ifndef nomessages}
\r
32 procedure sessionavailable(sender: tobject;error : word);
\r
33 procedure dataavailable(sender: tobject;error : word);
\r
34 procedure sessionconnected(sender: tobject;error : word);
\r
35 procedure taskrun(wparam,lparam:longint);
\r
36 procedure timehandler(sender:tobject);
\r
37 procedure dnsrequestdone(sender:tobject;error : word);
\r
38 procedure sessionclosed(sender:tobject;error : word);
\r
40 treleasetest=class(tlcomponent)
\r
41 destructor destroy; override;
\r
44 listensocket : tlsocket;
\r
45 serversocket : tlsocket;
\r
46 clientsocket : tlsocket;
\r
49 firststage : boolean;
\r
50 procedure tsc.sessionavailable(sender: tobject;error : word);
\r
52 writeln('received connection');
\r
53 serversocket.dup(listensocket.accept);
\r
57 receivebuf : string;
\r
58 receivecount : integer;
\r
59 procedure tsc.dataavailable(sender: tobject;error : word);
\r
61 receiveddata : string;
\r
62 receivedon : string;
\r
65 receiveddata := tlsocket(sender).receivestr;
\r
66 if sender=clientsocket then begin
\r
67 receivedon := 'client socket';
\r
69 receivedon := 'server socket';
\r
71 writeln('received data '+receiveddata+' on '+receivedon);
\r
73 receivebuf := receivebuf+receiveddata;
\r
75 if receivebuf = 'hello world' then begin
\r
77 writeln('received hello world creating task');
\r
78 task := tltask.create(sc.taskrun,nil,0,0);
\r
80 receivecount := receivecount +1;
\r
81 if receivecount >50 then begin
\r
82 writeln('received over 50 bits of data, pausing to let the operator take a look');
\r
86 while pos(#10,receivebuf) > 0 do begin
\r
88 setlength(line,pos(#10,receivebuf)-1);
\r
89 receivebuf := copy(receivebuf,pos(#10,receivebuf)+1,1000000);
\r
90 if uppercase(copy(line,1,4))='PING' then begin
\r
92 writeln('send pong:'+line);
\r
93 clientsocket.sendstr(line+#10);
\r
98 procedure tsc.sessionconnected(sender: tobject;error : word);
\r
101 if error=0 then begin
\r
102 writeln('session is connected, local address is'+clientsocket.getxaddr);
\r
104 if firststage then begin
\r
105 clientsocket.sendstr('hello world');
\r
107 clientsocket.sendstr('nick test'#13#10'user x x x x'#13#10);
\r
110 writeln('connect failed');
\r
117 procedure tsc.taskrun(wparam,lparam:longint);
\r
119 tempbinip : tbinip;
\r
122 writeln('task ran');
\r
123 writeln('closing client socket');
\r
124 clientsocket.close;
\r
126 writeln('looking up irc.p10link.net using dnsasync');
\r
127 das := tdnsasync.Create(nil);
\r
128 das.onrequestdone := sc.dnsrequestdone;
\r
129 //das.forwardfamily := af_inet6;
\r
130 das.forwardlookup('irc.p10link.net');
\r
134 procedure tsc.dnsrequestdone(sender:tobject;error : word);
\r
136 tempbinip : tbinip;
\r
137 tempbiniplist : tbiniplist;
\r
139 writeln('irc.p10link.net resolved to '+das.dnsresult+' connecting client socket there');
\r
140 das.dnsresultbin(tempbinip);
\r
141 tempbiniplist := biniplist_new;
\r
142 biniplist_add(tempbiniplist,tempbinip);
\r
143 clientsocket.addr := tempbiniplist;
\r
144 clientsocket.port := '6667';
\r
145 firststage := false;
\r
146 clientsocket.connect;
\r
147 //writeln(clientsocket.getxaddr);
\r
151 procedure tsc.timehandler(sender:tobject);
\r
153 //writeln('got timer event');
\r
156 destructor treleasetest.destroy;
\r
158 writeln('releasetest.destroy called');
\r
162 procedure tsc.sessionclosed(sender:tobject;error : word);
\r
164 Writeln('session closed with error ',error);
\r
170 iplist : tbiniplist;
\r
171 releasetest : treleasetest;
\r
174 releasetest := treleasetest.create(nil);
\r
175 releasetest.release;
\r
177 ipbin := forwardlookup('invalid.domain',5);
\r
178 writeln(ipbintostr(ipbin));
\r
180 ipbin := forwardlookup('p10link.net',5);
\r
181 writeln(ipbintostr(ipbin));
\r
183 ipstrtobin('80.68.89.68',ipbin);
\r
184 writeln('80.68.89.68 reverses to '+reverselookup(ipbin,5));
\r
186 ipstrtobin('2001:200::8002:203:47ff:fea5:3085',ipbin);
\r
187 writeln('2001:200::8002:203:47ff:fea5:3085 reverses to '+reverselookup(ipbin,5));
\r
188 writeln('creating and setting up listen socket');
\r
189 listensocket := tlsocket.create(nil);
\r
190 listensocket.addr := '';
\r
191 listensocket.port := '12345';
\r
192 listensocket.onsessionavailable := sc.sessionavailable;
\r
193 writeln('listening');
\r
194 listensocket.listen;
\r
195 writeln(listensocket.getxport);
\r
196 writeln('listen socket is number ', listensocket.fdhandlein);
\r
197 writeln('creating and setting up server socket');
\r
198 serversocket := tlsocket.create(nil);
\r
199 serversocket.ondataavailable := sc.dataavailable;
\r
200 writeln('creating and setting up client socket');
\r
201 clientsocket := tlsocket.create(nil);
\r
202 //try connecting to ::1 first and if that fails try 127.0.0.1
\r
203 iplist := biniplist_new;
\r
204 ipstrtobin('::1',ipbin);
\r
205 biniplist_add(iplist,ipbin);
\r
206 ipstrtobin('127.0.0.1',ipbin);
\r
207 biniplist_add(iplist,ipbin);
\r
208 clientsocket.addr := iplist;
\r
209 clientsocket.port := '12345';
\r
210 clientsocket.onsessionconnected := sc.sessionconnected;
\r
211 clientsocket.ondataAvailable := sc.dataavailable;
\r
212 clientsocket.onsessionclosed := sc.sessionclosed;
\r
213 writeln('connecting');
\r
214 firststage := true;
\r
215 clientsocket.connect;
\r
216 writeln('client socket is number ',clientsocket.fdhandlein);
\r
217 writeln('creating and setting up timer');
\r
218 timer := tltimer.create(nil);
\r
219 timer.interval := 1000;
\r
220 timer.ontimer := sc.timehandler;
\r
221 timer.enabled := true;
\r
222 writeln('entering message loop');
\r
224 writeln('exiting cleanly');
\r