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
29 procedure sessionavailable(sender: tobject;error : word);
\r
30 procedure dataavailable(sender: tobject;error : word);
\r
31 procedure sessionconnected(sender: tobject;error : word);
\r
32 procedure taskrun(wparam,lparam:longint);
\r
33 procedure timehandler(sender:tobject);
\r
34 procedure dnsrequestdone(sender:tobject;error : word);
\r
35 procedure sessionclosed(sender:tobject;error : word);
\r
38 listensocket : tlsocket;
\r
39 serversocket : tlsocket;
\r
40 clientsocket : tlsocket;
\r
43 firststage : boolean;
\r
44 procedure tsc.sessionavailable(sender: tobject;error : word);
\r
46 writeln('received connection');
\r
47 serversocket.dup(listensocket.accept);
\r
51 receivebuf : string;
\r
52 receivecount : integer;
\r
53 procedure tsc.dataavailable(sender: tobject;error : word);
\r
55 receiveddata : string;
\r
56 receivedon : string;
\r
59 receiveddata := tlsocket(sender).receivestr;
\r
60 if sender=clientsocket then begin
\r
61 receivedon := 'client socket';
\r
63 receivedon := 'server socket';
\r
65 writeln('received data '+receiveddata+' on '+receivedon);
\r
67 receivebuf := receivebuf+receiveddata;
\r
69 if receivebuf = 'hello world' then begin
\r
71 writeln('received hello world creating task');
\r
72 task := tltask.create(sc.taskrun,nil,0,0);
\r
74 receivecount := receivecount +1;
\r
75 if receivecount >50 then begin
\r
76 writeln('received over 50 bits of data, pausing to let the operator take a look');
\r
80 while pos(#10,receivebuf) > 0 do begin
\r
82 setlength(line,pos(#10,receivebuf)-1);
\r
83 receivebuf := copy(receivebuf,pos(#10,receivebuf)+1,1000000);
\r
84 if uppercase(copy(line,1,4))='PING' then begin
\r
86 writeln('send pong:'+line);
\r
87 clientsocket.sendstr(line+#10);
\r
92 procedure tsc.sessionconnected(sender: tobject;error : word);
\r
95 if error=0 then begin
\r
96 writeln('session is connected, local address is'+clientsocket.getxaddr);
\r
98 if firststage then begin
\r
99 clientsocket.sendstr('hello world');
\r
101 clientsocket.sendstr('nick test'#13#10'user x x x x'#13#10);
\r
104 writeln('connect failed');
\r
111 procedure tsc.taskrun(wparam,lparam:longint);
\r
113 tempbinip : tbinip;
\r
116 writeln('task ran');
\r
117 writeln('closing client socket');
\r
118 clientsocket.close;
\r
120 writeln('looking up irc.p10link.net using dnsasync');
\r
121 das := tdnsasync.Create(nil);
\r
122 das.onrequestdone := sc.dnsrequestdone;
\r
123 //das.forwardfamily := af_inet6;
\r
124 das.forwardlookup('irc.p10link.net');
\r
128 procedure tsc.dnsrequestdone(sender:tobject;error : word);
\r
130 tempbinip : tbinip;
\r
131 tempbiniplist : tbiniplist;
\r
133 writeln('irc.p10link.net resolved to '+das.dnsresult+' connecting client socket there');
\r
134 das.dnsresultbin(tempbinip);
\r
135 tempbiniplist := biniplist_new;
\r
136 biniplist_add(tempbiniplist,tempbinip);
\r
137 clientsocket.addr := tempbiniplist;
\r
138 clientsocket.port := '6667';
\r
139 firststage := false;
\r
140 clientsocket.connect;
\r
141 //writeln(clientsocket.getxaddr);
\r
145 procedure tsc.timehandler(sender:tobject);
\r
147 //writeln('got timer event');
\r
149 procedure tsc.sessionclosed(sender:tobject;error : word);
\r
151 Writeln('session closed with error ',error);
\r
157 iplist : tbiniplist;
\r
160 ipbin := forwardlookup('invalid.domain',5);
\r
161 writeln(ipbintostr(ipbin));
\r
163 ipbin := forwardlookup('p10link.net',5);
\r
164 writeln(ipbintostr(ipbin));
\r
166 ipstrtobin('80.68.89.68',ipbin);
\r
167 writeln('80.68.89.68 reverses to '+reverselookup(ipbin,5));
\r
169 ipstrtobin('2001:200::8002:203:47ff:fea5:3085',ipbin);
\r
170 writeln('2001:200::8002:203:47ff:fea5:3085 reverses to '+reverselookup(ipbin,5));
\r
171 writeln('creating and setting up listen socket');
\r
172 listensocket := tlsocket.create(nil);
\r
173 listensocket.addr := '';
\r
174 listensocket.port := '12345';
\r
175 listensocket.onsessionavailable := sc.sessionavailable;
\r
176 writeln('listening');
\r
177 listensocket.listen;
\r
178 writeln(listensocket.getxport);
\r
179 writeln('listen socket is number ', listensocket.fdhandlein);
\r
180 writeln('creating and setting up server socket');
\r
181 serversocket := tlsocket.create(nil);
\r
182 serversocket.ondataavailable := sc.dataavailable;
\r
183 writeln('creating and setting up client socket');
\r
184 clientsocket := tlsocket.create(nil);
\r
185 //try connecting to ::1 first and if that fails try 127.0.0.1
\r
186 iplist := biniplist_new;
\r
187 ipstrtobin('::1',ipbin);
\r
188 biniplist_add(iplist,ipbin);
\r
189 ipstrtobin('127.0.0.1',ipbin);
\r
190 biniplist_add(iplist,ipbin);
\r
191 clientsocket.addr := iplist;
\r
192 clientsocket.port := '12345';
\r
193 clientsocket.onsessionconnected := sc.sessionconnected;
\r
194 clientsocket.ondataAvailable := sc.dataavailable;
\r
195 clientsocket.onsessionclosed := sc.sessionclosed;
\r
196 writeln('connecting');
\r
197 firststage := true;
\r
198 clientsocket.connect;
\r
199 writeln('client socket is number ',clientsocket.fdhandlein);
\r
200 writeln('creating and setting up timer');
\r
201 timer := tltimer.create(nil);
\r
202 timer.interval := 1000;
\r
203 timer.ontimer := sc.timehandler;
\r
204 timer.enabled := true;
\r
205 writeln('entering message loop');
\r
207 writeln('exiting cleanly');
\r