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
31 procedure sessionavailable(sender: tobject;error : word);
\r
32 procedure dataavailable(sender: tobject;error : word);
\r
33 procedure sessionconnected(sender: tobject;error : word);
\r
34 procedure taskrun(wparam,lparam:longint);
\r
35 procedure timehandler(sender:tobject);
\r
36 procedure dnsrequestdone(sender:tobject;error : word);
\r
37 procedure sessionclosed(sender:tobject;error : word);
\r
40 listensocket : tlsocket;
\r
41 serversocket : tlsocket;
\r
42 clientsocket : tlsocket;
\r
45 firststage : boolean;
\r
46 procedure tsc.sessionavailable(sender: tobject;error : word);
\r
48 writeln('received connection');
\r
49 serversocket.dup(listensocket.accept);
\r
53 receivebuf : string;
\r
54 receivecount : integer;
\r
55 procedure tsc.dataavailable(sender: tobject;error : word);
\r
57 receiveddata : string;
\r
58 receivedon : string;
\r
61 receiveddata := tlsocket(sender).receivestr;
\r
62 if sender=clientsocket then begin
\r
63 receivedon := 'client socket';
\r
65 receivedon := 'server socket';
\r
67 writeln('received data '+receiveddata+' on '+receivedon);
\r
69 receivebuf := receivebuf+receiveddata;
\r
71 if receivebuf = 'hello world' then begin
\r
73 writeln('received hello world creating task');
\r
74 task := tltask.create(sc.taskrun,nil,0,0);
\r
76 receivecount := receivecount +1;
\r
77 if receivecount >50 then begin
\r
78 writeln('received over 50 bits of data, pausing to let the operator take a look');
\r
82 while pos(#10,receivebuf) > 0 do begin
\r
84 setlength(line,pos(#10,receivebuf)-1);
\r
85 receivebuf := copy(receivebuf,pos(#10,receivebuf)+1,1000000);
\r
86 if uppercase(copy(line,1,4))='PING' then begin
\r
88 writeln('send pong:'+line);
\r
89 clientsocket.sendstr(line+#10);
\r
94 procedure tsc.sessionconnected(sender: tobject;error : word);
\r
97 if error=0 then begin
\r
98 writeln('session is connected, local address is'+clientsocket.getxaddr);
\r
100 if firststage then begin
\r
101 clientsocket.sendstr('hello world');
\r
103 clientsocket.sendstr('nick test'#13#10'user x x x x'#13#10);
\r
106 writeln('connect failed');
\r
113 procedure tsc.taskrun(wparam,lparam:longint);
\r
115 tempbinip : tbinip;
\r
118 writeln('task ran');
\r
119 writeln('closing client socket');
\r
120 clientsocket.close;
\r
122 writeln('looking up irc.p10link.net using dnsasync');
\r
123 das := tdnsasync.Create(nil);
\r
124 das.onrequestdone := sc.dnsrequestdone;
\r
125 //das.forwardfamily := af_inet6;
\r
126 das.forwardlookup('irc.p10link.net');
\r
130 procedure tsc.dnsrequestdone(sender:tobject;error : word);
\r
132 tempbinip : tbinip;
\r
133 tempbiniplist : tbiniplist;
\r
135 writeln('irc.p10link.net resolved to '+das.dnsresult+' connecting client socket there');
\r
136 das.dnsresultbin(tempbinip);
\r
137 tempbiniplist := biniplist_new;
\r
138 biniplist_add(tempbiniplist,tempbinip);
\r
139 clientsocket.addr := tempbiniplist;
\r
140 clientsocket.port := '6667';
\r
141 firststage := false;
\r
142 clientsocket.connect;
\r
143 //writeln(clientsocket.getxaddr);
\r
147 procedure tsc.timehandler(sender:tobject);
\r
149 //writeln('got timer event');
\r
151 procedure tsc.sessionclosed(sender:tobject;error : word);
\r
153 Writeln('session closed with error ',error);
\r
159 iplist : tbiniplist;
\r
162 ipbin := forwardlookup('invalid.domain',5);
\r
163 writeln(ipbintostr(ipbin));
\r
165 ipbin := forwardlookup('p10link.net',5);
\r
166 writeln(ipbintostr(ipbin));
\r
168 ipstrtobin('80.68.89.68',ipbin);
\r
169 writeln('80.68.89.68 reverses to '+reverselookup(ipbin,5));
\r
171 ipstrtobin('2001:200::8002:203:47ff:fea5:3085',ipbin);
\r
172 writeln('2001:200::8002:203:47ff:fea5:3085 reverses to '+reverselookup(ipbin,5));
\r
173 writeln('creating and setting up listen socket');
\r
174 listensocket := tlsocket.create(nil);
\r
175 listensocket.addr := '';
\r
176 listensocket.port := '12345';
\r
177 listensocket.onsessionavailable := sc.sessionavailable;
\r
178 writeln('listening');
\r
179 listensocket.listen;
\r
180 writeln(listensocket.getxport);
\r
181 writeln('listen socket is number ', listensocket.fdhandlein);
\r
182 writeln('creating and setting up server socket');
\r
183 serversocket := tlsocket.create(nil);
\r
184 serversocket.ondataavailable := sc.dataavailable;
\r
185 writeln('creating and setting up client socket');
\r
186 clientsocket := tlsocket.create(nil);
\r
187 //try connecting to ::1 first and if that fails try 127.0.0.1
\r
188 iplist := biniplist_new;
\r
189 ipstrtobin('::1',ipbin);
\r
190 biniplist_add(iplist,ipbin);
\r
191 ipstrtobin('127.0.0.1',ipbin);
\r
192 biniplist_add(iplist,ipbin);
\r
193 clientsocket.addr := iplist;
\r
194 clientsocket.port := '12345';
\r
195 clientsocket.onsessionconnected := sc.sessionconnected;
\r
196 clientsocket.ondataAvailable := sc.dataavailable;
\r
197 clientsocket.onsessionclosed := sc.sessionclosed;
\r
198 writeln('connecting');
\r
199 firststage := true;
\r
200 clientsocket.connect;
\r
201 writeln('client socket is number ',clientsocket.fdhandlein);
\r
202 writeln('creating and setting up timer');
\r
203 timer := tltimer.create(nil);
\r
204 timer.interval := 1000;
\r
205 timer.ontimer := sc.timehandler;
\r
206 timer.enabled := true;
\r
207 writeln('entering message loop');
\r
209 writeln('exiting cleanly');
\r