From: beware Date: Mon, 31 Mar 2008 00:30:30 +0000 (+0000) Subject: secondlistener X-Git-Url: http://www.lcore.org/git/lcore.git/commitdiff_plain/d2948bcc680fb1c36613c8535757fe0ed1faf10f?ds=sidebyside secondlistener git-svn-id: file:///svnroot/lcore/trunk@11 b1de8a11-f9be-4011-bde0-cc7ace90066a --- diff --git a/lsocket.pas b/lsocket.pas index c37a66a..ffd411f 100755 --- a/lsocket.pas +++ b/lsocket.pas @@ -54,6 +54,14 @@ interface sockets, {$endif} classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync; + +{$ifdef ipv6} +{$define secondlistener} +const + v4listendefault:boolean=false; +{$endif} + + type sunB = packed record s_b1, s_b2, s_b3, s_b4: byte; @@ -94,6 +102,12 @@ type proto:string; udp:boolean; listenqueue:integer; + {$ifdef secondlistener} + secondlistener:tlsocket; + lastsessionfromsecond:boolean; + procedure secondaccepthandler(sender:tobject;error:word); + procedure internalclose(error:word);override; + {$endif} procedure connectionfailedhandler(error:word); procedure connecttimeouthandler(sender:tobject); procedure connectsuccesshandler; @@ -105,7 +119,7 @@ type function accept : longint; function sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer; virtual; function receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; virtual; - //procedure internalclose(error:word);override; + procedure handlefdtrigger(readtrigger,writetrigger:boolean); override; function send(data:pointer;len:integer):integer;override; procedure sendstr(const str : string);override; @@ -131,12 +145,6 @@ type {!!!function longipdns(s:string):longint;} -{$ifdef ipv6} -const - v4listendefault:boolean=false; -{$endif} - - const TCP_NODELAY=1; IPPROTO_TCP=6; @@ -260,7 +268,13 @@ end; function tlsocket.receive(Buf:Pointer;BufSize:integer):integer; begin if udp then begin - result := myfdread(self.fdhandlein,buf^,bufsize); + {$ifdef secondlistener} + if lastsessionfromsecond then begin + result := secondlistener.receive(buf,bufsize); + lastsessionfromsecond := false; + end else + {$endif} + result := myfdread(self.fdhandlein,buf^,bufsize); end else begin result := inherited receive(buf,bufsize); end; @@ -329,6 +343,7 @@ begin fdhandlein := socket(AF_INET,socktype,0); end; {$endif} + if fdhandlein = -1 then raise ESocketException.create('unable to create socket'); dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup @@ -357,6 +372,22 @@ begin {$endif} state := wsconnected; end; + + {$ifdef secondlistener} + //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that + if addr = '::' then begin + secondlistener := tlsocket.create(nil); + secondlistener.proto := proto; + secondlistener.addr := '0.0.0.0'; + secondlistener.port := port; + if udp then begin + secondlistener.ondataavailable := secondaccepthandler; + end else begin + secondlistener.onsessionAvailable := secondaccepthandler; + end; + secondlistener.listen; + end; + {$endif} finally if state = wsclosed then begin if fdhandlein >= 0 then begin @@ -374,12 +405,37 @@ begin //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein); end; +{$ifdef secondlistener} +procedure tlsocket.internalclose(error:word); +begin + if assigned(secondlistener) then secondlistener.destroy; + inherited internalclose(error); +end; + +procedure tlsocket.secondaccepthandler; +begin + lastsessionfromsecond := true; + if udp then begin + ondataavailable(self,error); + end else begin + if assigned(onsessionavailable) then onsessionavailable(self,error); + end; +end; +{$endif} + function tlsocket.accept : longint; var FromAddrSize : LongInt; // i don't realy know what to do with these at this FromAddr : TInetSockAddrV; // at this point time will tell :) a:integer; begin + {$ifdef secondlistener} + if (lastsessionfromsecond) then begin + lastsessionfromsecond := false; + result := secondlistener.accept; + exit; + end; + {$endif} FromAddrSize := Sizeof(FromAddr); {$ifdef win32} @@ -405,6 +461,12 @@ function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:in var destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute dest; begin + {$ifdef secondlistener} + if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin + result := secondlistener.sendto(dest,destlen,data,len); + exit; + end; + {$endif} result := {$ifdef win32}winsock{$else}sockets{$endif}.sendto(self.fdhandleout,data^,len,0,destx,destlen); end; @@ -412,6 +474,13 @@ function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;va var srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute src; begin + {$ifdef secondlistener} + if assigned(secondlistener) then if lastsessionfromsecond then begin + lastsessionfromsecond := false; + result := secondlistener.receivefrom(data,len,src,srclen); + exit; + end; + {$endif} result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen); end; @@ -630,6 +699,6 @@ end; result := winsock.recv(fd,buf,size,0); end; {$endif} - + end.