\r
{socket code by plugwash}\r
\r
-{use this code for whatever you like in programs under whater licence you like\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
-}\r
-\r
+{ Copyright (C) 2005 Bas Steendijk and Peter Green\r
+ For conditions of distribution and use, see copyright notice in zlib_license.txt\r
+ which is included in the package\r
+ ----------------------------------------------------------------------------- }\r
{\r
changes by plugwash (20030728)\r
* created handlefdtrigger virtual method in tlasio (overridden in tlsocket) and moved a lot of code from messageloop into it\r
\r
\r
unit lsocket;\r
-{$mode delphi}\r
+{$ifdef fpc}\r
+ {$mode delphi}\r
+{$endif}\r
+\r
+{$include lcoreconfig.inc}\r
+\r
interface\r
uses\r
sysutils,\r
- {$ifdef VER1_0}\r
- linux,\r
+ {$ifdef win32}\r
+ windows,winsock,\r
{$else}\r
- baseunix,unix,\r
+\r
+ {$ifdef VER1_0}\r
+ linux,\r
+ {$else}\r
+ baseunix,unix,unixutil,\r
+ {$endif}\r
+ sockets,\r
{$endif}\r
- sockets,classes,pgdebugout,pgtypes,lcore,fd_utils,binipstuff,dnssync;\r
+ classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync;\r
+\r
+{$ifdef ipv6}\r
+const\r
+ v4listendefault:boolean=false;\r
+{$endif}\r
+\r
+\r
type\r
sunB = packed record\r
s_b1, s_b2, s_b3, s_b4: byte;\r
1: (S_un_w: SunW);\r
2: (S_addr: cardinal);\r
end;\r
- {$ifdef ipv6}\r
- {$ifdef ver1_0}\r
- cuint16=word;\r
- cuint32=dword;\r
- sa_family_t=word;\r
-\r
-\r
- TInetSockAddr6 = packed Record\r
- sin6_family : sa_family_t;\r
- sin6_port : cuint16;\r
- sin6_flowinfo : cuint32;\r
- sin6_addr : Tin6_addr;\r
- sin6_scope_id : cuint32;\r
- end;\r
- {$endif}\r
- {$endif}\r
- TinetSockAddrv = packed record\r
- case integer of\r
- 0: (InAddr:TInetSockAddr);\r
- {$ifdef ipv6}\r
- 1: (InAddr6:TInetSockAddr6);\r
- {$endif}\r
- end;\r
-\r
- type\r
- tsockaddrin=TInetSockAddr;\r
\r
type\r
TLsocket = class(tlasio)\r
//a: string;\r
\r
inAddr : TInetSockAddrV;\r
+\r
+ biniplist:tbiniplist;\r
+ trymoreips:boolean;\r
+ currentip:integer;\r
+ connecttimeout:tltimer;\r
+\r
{ inAddrSize:integer;}\r
\r
//host : THostentry ;\r
proto:string;\r
udp:boolean;\r
listenqueue:integer;\r
+ {$ifdef secondlistener}\r
+ secondlistener:tlsocket;\r
+ lastsessionfromsecond:boolean;\r
+ procedure secondaccepthandler(sender:tobject;error:word);\r
+ procedure internalclose(error:word);override;\r
+ {$endif}\r
+ procedure connectionfailedhandler(error:word);\r
+ procedure connecttimeouthandler(sender:tobject);\r
+ procedure connectsuccesshandler;\r
function getaddrsize:integer;\r
procedure connect; virtual;\r
+ procedure realconnect;\r
procedure bindsocket;\r
procedure listen;\r
function accept : longint;\r
- function sendto(dest:TInetSockAddr;destlen:integer;data:pointer;len:integer):integer; virtual;\r
- function receivefrom(data:pointer;len:integer;var src:TInetSockAddr;var srclen:integer):integer; virtual;\r
- //procedure internalclose(error:word);override;\r
+ function sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer; virtual;\r
+ function receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer; virtual;\r
+\r
procedure handlefdtrigger(readtrigger,writetrigger:boolean); override;\r
function send(data:pointer;len:integer):integer;override;\r
procedure sendstr(const str : string);override;\r
function getXport:string; virtual;\r
function getpeerport:string; virtual;\r
constructor Create(AOwner: TComponent); override;\r
+ {$ifdef win32}\r
+ procedure myfdclose(fd : integer); override;\r
+ function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override;\r
+ function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override;\r
+ {$endif}\r
end;\r
tsocket=longint; // for compatibility with twsocket\r
\r
twsocket=tlsocket; {easy}\r
\r
-function htons(w:word):word;\r
-function htonl(i:integer):integer;\r
-{!!!function longipdns(s:string):longint;}\r
-\r
-{$ifdef ipv6}\r
-const\r
- v4listendefault:boolean=false;\r
-{$endif}\r
-\r
\r
const\r
TCP_NODELAY=1;\r
implementation\r
{$include unixstuff.inc}\r
\r
-function longip(s:string):longint;inline;\r
-var\r
- l:longint;\r
- a,b:integer;\r
-\r
-function convertbyte(const s:string):integer;inline;\r
-begin\r
- result := strtointdef(s,-1);\r
- if result < 0 then exit;\r
- if result > 255 then exit;\r
-\r
- {01 exception}\r
- if (result <> 0) and (s[1] = '0') then begin\r
- result := -1;\r
- exit;\r
- end;\r
-\r
- {+1 exception}\r
- if not (s[1] in ['0'..'9']) then begin\r
- result := -1;\r
- exit\r
- end;\r
-end;\r
-\r
-begin\r
- result := 0;\r
- a := pos('.',s);\r
- if a = 0 then exit;\r
- b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;\r
- l := b shl 24;\r
- s := copy(s,a+1,256);\r
- a := pos('.',s);\r
- if a = 0 then exit;\r
- b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;\r
- l := l or b shl 16;\r
- s := copy(s,a+1,256);\r
- a := pos('.',s);\r
- if a = 0 then exit;\r
- b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;\r
- l := l or b shl 8;\r
- s := copy(s,a+1,256);\r
- b := convertbyte(copy(s,1,256));if (b < 0) then exit;\r
- l := l or b;\r
- result := l;\r
-end;\r
-\r
-(*!!!\r
-function longipdns(s:string):longint;\r
-var\r
- host : thostentry;\r
-begin\r
- if s = '0.0.0.0' then begin\r
- result := 0;\r
- end else begin\r
- result := longip(s);\r
- if result = 0 then begin\r
- if gethostbyname(s,host) then begin;\r
- result := htonl(Longint(Host.Addr));\r
- end;\r
- //writeln(inttohex(longint(host.addr),8))\r
- end;\r
- if result = 0 then begin\r
- if resolvehostbyname(s,host) then begin;\r
- result := htonl(Longint(Host.Addr));\r
- end;\r
- //writeln(inttohex(longint(host.addr),8))\r
- end;\r
- end;\r
-end;\r
-*)\r
-\r
-\r
-function htons(w:word):word;\r
-begin\r
- {$ifdef ENDIAN_LITTLE}\r
- result := ((w and $ff00) shr 8) or ((w and $ff) shl 8);\r
- {$else}\r
- result := w;\r
- {$endif}\r
-end;\r
-\r
-function htonl(i:integer):integer;\r
-begin\r
- {$ifdef ENDIAN_LITTLE}\r
- result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000);\r
- {$else}\r
- result := i;\r
- {$endif}\r
-end;\r
\r
function tlsocket.getaddrsize:integer;\r
begin\r
- {$ifdef ipv6}\r
- if inaddr.inaddr.family = AF_INET6 then result := sizeof(tinetsockaddr6) else\r
- {$endif}\r
- result := sizeof(tinetsockaddr);\r
+ result := inaddrsize(inaddr);\r
end;\r
\r
-function makeinaddrv(addr,port:string;var inaddr:tinetsockaddrv):integer;\r
-var\r
- biniptemp:tbinip;\r
-begin\r
- result := 0;\r
- biniptemp := forwardlookup(addr,10);\r
- fillchar(inaddr,sizeof(inaddr),0);\r
- if biniptemp.family = AF_INET then begin\r
- inAddr.InAddr.family:=AF_INET;\r
- inAddr.InAddr.port:=htons(strtointdef(port,0));\r
- inAddr.InAddr.addr:=biniptemp.ip;\r
- result := sizeof(tinetsockaddr);\r
- end else\r
- {$ifdef ipv6}\r
- if biniptemp.family = AF_INET6 then begin\r
- inAddr.InAddr6.sin6_family:=AF_INET6;\r
- inAddr.InAddr6.sin6_port:=htons(strtointdef(port,0));\r
- inAddr.InAddr6.sin6_addr:=biniptemp.ip6;\r
- result := sizeof(tinetsockaddr6);\r
- end else\r
- {$endif}\r
- raise esocketexception.create('unable to resolve address: '+addr);\r
-end;\r
\r
-procedure tlsocket.connect;\r
+procedure tlsocket.realconnect;\r
var\r
a:integer;\r
-begin\r
- if state <> wsclosed then close;\r
- //prevtime := 0;\r
- makeinaddrv(addr,port,inaddr);\r
\r
+begin\r
+// writeln('trying to connect to ',ipbintostr(biniplist_get(biniplist,currentip)),'#',port);\r
+ makeinaddrv(biniplist_get(biniplist,currentip),port,inaddr);\r
+ inc(currentip);\r
+ if (currentip >= biniplist_getcount(biniplist)) then trymoreips := false;\r
udp := uppercase(proto) = 'UDP';\r
if udp then a := SOCK_DGRAM else a := SOCK_STREAM;\r
a := Socket(inaddr.inaddr.family,a,0);\r
-\r
+ //writeln(ord(inaddr.inaddr.family));\r
if a = -1 then begin\r
- lasterror := socketerror;\r
+ lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};\r
raise esocketexception.create('unable to create socket');\r
end;\r
try\r
dup(a);\r
bindsocket;\r
if udp then begin\r
+ {$ifndef win32}\r
+ SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
+ {$endif}\r
state := wsconnected;\r
if assigned(onsessionconnected) then onsessionconnected(self,0);\r
+\r
+ eventcore.rmasterset(fdhandlein,false);\r
+ eventcore.wmasterclr(fdhandleout);\r
end else begin\r
state :=wsconnecting;\r
- sockets.Connect(fdhandlein,inADDR,getaddrsize);\r
- end;\r
- rmasterset(fdhandlein);\r
- if udp then begin\r
- wmasterclr(fdhandleout);\r
- end else begin\r
- wmasterset(fdhandleout);\r
+ {$ifdef win32}\r
+ //writeln(inaddr.inaddr.port);\r
+ winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);\r
+ {$else}\r
+ sockets.Connect(fdhandlein,inADDR,getaddrsize);\r
+ {$endif}\r
+ eventcore.rmasterset(fdhandlein,false);\r
+ eventcore.wmasterset(fdhandleout);\r
+ if trymoreips then connecttimeout.enabled := true;\r
end;\r
//sendq := '';\r
except\r
raise; //reraise the exception\r
end;\r
end;\r
+\r
+end;\r
+\r
+procedure tlsocket.connecttimeouthandler(sender:tobject);\r
+begin\r
+ connecttimeout.enabled := false;\r
+ destroying := true; //hack to not cause handler to trigger\r
+ internalclose(0);\r
+ destroying := false;\r
+ realconnect;\r
+end;\r
+\r
+procedure tlsocket.connect;\r
+var\r
+ a:integer;\r
+ ip:tbinip;\r
+begin\r
+ if state <> wsclosed then close;\r
+ //prevtime := 0;\r
+ if isbiniplist(addr) then biniplist := addr else biniplist := forwardlookuplist(addr,0);\r
+ if biniplist_getcount(biniplist) = 0 then raise exception.create('unable to resolve '+addr);\r
+\r
+ //makeinaddrv(addr,port,inaddr);\r
+\r
+ currentip := 0;\r
+ if not assigned(connecttimeout) then begin\r
+ connecttimeout := tltimer.create(self);\r
+ connecttimeout.Tag := integer(self);\r
+ connecttimeout.ontimer := connecttimeouthandler;\r
+ connecttimeout.interval := 2500;\r
+ connecttimeout.enabled := false;\r
+ end;\r
+ realconnect;\r
end;\r
\r
procedure tlsocket.sendstr(const str : string);\r
function tlsocket.send(data:pointer;len:integer):integer;\r
begin\r
if udp then begin\r
- result := sendto(inaddr.inaddr,getaddrsize,data,len)\r
+// writeln('sending to '+ipbintostr(inaddrvtobinip(inaddr)),' ',htons(inaddr.inaddr.port),' ',len,' bytes');\r
+ result := sendto(inaddr,getaddrsize,data,len);\r
+\r
+// writeln('send result ',result);\r
+// writeln('errno',errno);\r
end else begin\r
result := inherited send(data,len);\r
end;\r
function tlsocket.receive(Buf:Pointer;BufSize:integer):integer;\r
begin\r
if udp then begin\r
- result := fdread(self.fdhandlein,buf^,bufsize);\r
+ {$ifdef secondlistener}\r
+ if lastsessionfromsecond then begin\r
+ result := secondlistener.receive(buf,bufsize);\r
+ lastsessionfromsecond := false;\r
+ end else\r
+ {$endif}\r
+ result := myfdread(self.fdhandlein,buf^,bufsize);\r
end else begin\r
result := inherited receive(buf,bufsize);\r
end;\r
var\r
a:integer;\r
inAddrtemp:TInetSockAddrV;\r
+ inAddrtempx:{$ifdef win32}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;\r
inaddrtempsize:integer;\r
begin\r
try\r
end;\r
//gethostbyname(localaddr,host);\r
\r
- inaddrtempsize := makeinaddrv(localaddr,localport,inaddrtemp);\r
+ inaddrtempsize := makeinaddrv(forwardlookup(localaddr,0),localport,inaddrtemp);\r
\r
- If Not Bind(fdhandlein,inaddrtemp,inaddrtempsize) Then begin\r
+ If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin\r
state := wsclosed;\r
- lasterror := socketerror;\r
+ lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};\r
raise ESocketException.create('unable to bind, error '+inttostr(lasterror));\r
end;\r
state := wsbound;\r
{$endif}\r
addr := '0.0.0.0';\r
end;\r
- biniptemp := forwardlookup(addr,10);\r
+ if isbiniplist(addr) then biniptemp := biniplist_get(addr,0) else biniptemp := forwardlookup(addr,10);\r
addr := ipbintostr(biniptemp);\r
fdhandlein := socket(biniptemp.family,socktype,0);\r
{$ifdef ipv6}\r
fdhandlein := socket(AF_INET,socktype,0);\r
end;\r
{$endif}\r
+\r
if fdhandlein = -1 then raise ESocketException.create('unable to create socket');\r
- dup(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things\r
- fdreverse[fdhandlein] := self;\r
+ dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things\r
+ //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup\r
state := wsclosed; // then set this back as it was an undesired side effect of dup\r
\r
try\r
yes := $01010101; {Copied this from existing code. Value is empiric,\r
but works. (yes=true<>0) }\r
- if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin\r
- raise ESocketException.create('unable to set socket options');\r
- end;\r
-\r
+ {$ifndef win32}\r
+ if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin\r
+ raise ESocketException.create('unable to set socket options');\r
+ end;\r
+ {$endif}\r
localaddr := addr;\r
localport := port;\r
bindsocket;\r
if not udp then begin\r
{!!! allow custom queue length? default 5}\r
if listenqueue = 0 then listenqueue := 5;\r
- If Not sockets.Listen(fdhandlein,listenqueue) Then raise esocketexception.create('unable to listen');\r
+ If {$ifdef win32}winsock{$else}sockets{$endif}.Listen(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise esocketexception.create('unable to listen');\r
state := wsListening;\r
end else begin\r
+ {$ifndef win32}\r
+ SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
+ {$endif}\r
state := wsconnected;\r
end;\r
+\r
+ {$ifdef secondlistener}\r
+ //listening on ::. try to listen on 0.0.0.0 as well for platforms which don't already do that\r
+ if addr = '::' then begin\r
+ secondlistener := tlsocket.create(nil);\r
+ secondlistener.proto := proto;\r
+ secondlistener.addr := '0.0.0.0';\r
+ secondlistener.port := port;\r
+ if udp then begin\r
+ secondlistener.ondataavailable := secondaccepthandler;\r
+ end else begin\r
+ secondlistener.onsessionAvailable := secondaccepthandler;\r
+ end;\r
+ try\r
+ secondlistener.listen;\r
+ except\r
+ secondlistener.destroy;\r
+ secondlistener := nil;\r
+ end;\r
+ end;\r
+ {$endif}\r
finally\r
if state = wsclosed then begin\r
if fdhandlein >= 0 then begin\r
{one *can* get here without fd -beware}\r
- rmasterclr(fdhandlein);\r
- fdclose(fdhandlein); // we musnt leak file discriptors\r
- fdreverse[fdhandlein] := nil;\r
+ eventcore.rmasterclr(fdhandlein);\r
+ myfdclose(fdhandlein); // we musnt leak file discriptors\r
+ eventcore.setfdreverse(fdhandlein,nil);\r
fdhandlein := -1;\r
end;\r
end else begin\r
- rmasterset(fdhandlein);\r
+ eventcore.rmasterset(fdhandlein,not udp);\r
end;\r
- if fdhandleout >= 0 then wmasterclr(fdhandleout);\r
+ if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout);\r
+ end;\r
+ //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein);\r
+end;\r
+\r
+{$ifdef secondlistener}\r
+procedure tlsocket.internalclose(error:word);\r
+begin\r
+ if assigned(secondlistener) then begin\r
+ secondlistener.destroy;\r
+ secondlistener := nil;\r
+ end;\r
+ inherited internalclose(error);\r
+end;\r
+\r
+procedure tlsocket.secondaccepthandler;\r
+begin\r
+ lastsessionfromsecond := true;\r
+ if udp then begin\r
+ ondataavailable(self,error);\r
+ end else begin\r
+ if assigned(onsessionavailable) then onsessionavailable(self,error);\r
end;\r
end;\r
+{$endif}\r
\r
function tlsocket.accept : longint;\r
var\r
FromAddrSize : LongInt; // i don't realy know what to do with these at this\r
- FromAddr : TInetSockAddr; // at this point time will tell :)\r
+ FromAddr : TInetSockAddrV; // at this point time will tell :)\r
+ a:integer;\r
begin\r
- rmasterset(fdhandlein);\r
+ {$ifdef secondlistener}\r
+ if (lastsessionfromsecond) then begin\r
+ lastsessionfromsecond := false;\r
+ result := secondlistener.accept;\r
+ exit;\r
+ end;\r
+ {$endif}\r
+\r
FromAddrSize := Sizeof(FromAddr);\r
- result := sockets.accept(fdhandlein,fromaddr,fromaddrsize);\r
- if result = -1 then raise esocketexception.create('error '+inttostr(socketerror)+' while accepting');\r
+ {$ifdef win32}\r
+ result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);\r
+ {$else}\r
+ result := sockets.accept(fdhandlein,fromaddr,fromaddrsize);\r
+ {$endif}\r
+ //now we have accepted one request start monitoring for more again\r
+ eventcore.rmasterset(fdhandlein,true);\r
+\r
+ if result = -1 then begin\r
+ raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting');\r
+ end;\r
if result > absoloutemaxs then begin\r
- fdclose(result);\r
+ myfdclose(result);\r
+ a := result;\r
result := -1;\r
- raise esocketexception.create('file discriptor out of range');\r
+ raise esocketexception.create('file discriptor out of range: '+inttostr(a));\r
+ end;\r
+end;\r
+\r
+function tlsocket.sendto(dest:TInetSockAddrV;destlen:integer;data:pointer;len:integer):integer;\r
+var\r
+ destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute dest;\r
+begin\r
+ {$ifdef secondlistener}\r
+ if assigned(secondlistener) then if (dest.inaddr.family = AF_INET) then begin\r
+ result := secondlistener.sendto(dest,destlen,data,len);\r
+ exit;\r
+ end;\r
+ {$endif}\r
+ result := {$ifdef win32}winsock{$else}sockets{$endif}.sendto(self.fdhandleout,data^,len,0,destx,destlen);\r
+end;\r
+\r
+function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;var srclen:integer):integer;\r
+var\r
+ srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute src;\r
+begin\r
+ {$ifdef secondlistener}\r
+ if assigned(secondlistener) then if lastsessionfromsecond then begin\r
+ lastsessionfromsecond := false;\r
+ result := secondlistener.receivefrom(data,len,src,srclen);\r
+ exit;\r
end;\r
+ {$endif}\r
+ result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen);\r
end;\r
\r
-function tlsocket.sendto;\r
+procedure tlsocket.connectionfailedhandler(error:word);\r
begin\r
- result := sockets.sendto(self.fdhandleout,data^,len,0,dest,destlen);\r
+ if trymoreips then begin\r
+// writeln('failed with error ',error);\r
+ connecttimeout.enabled := false;\r
+ destroying := true;\r
+ state := wsconnected;\r
+ self.internalclose(0);\r
+ destroying := false;\r
+ realconnect;\r
+ end else begin\r
+ state := wsconnected;\r
+ if assigned(onsessionconnected) then onsessionconnected(self,error);\r
+ self.internalclose(0);\r
+ recvq.del(maxlongint);\r
+ end;\r
end;\r
\r
-function tlsocket.receivefrom;\r
+procedure tlsocket.connectsuccesshandler;\r
begin\r
- result := sockets.recvfrom(self.fdhandlein,data^,len,0,src,srclen);\r
+ trymoreips := false;\r
+ connecttimeout.enabled := false;\r
+ if assigned(onsessionconnected) then onsessionconnected(self,0);\r
end;\r
\r
+\r
procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean);\r
var\r
tempbuf:array[0..receivebufsize-1] of byte;\r
begin\r
+// writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger,' state=',integer(state));\r
if (state =wslistening) and readtrigger then begin\r
{ debugout('listening socket triggered on read');}\r
- rmasterclr(fdhandlein);\r
+ eventcore.rmasterclr(fdhandlein);\r
if assigned(onsessionAvailable) then onsessionAvailable(self,0);\r
end;\r
if udp and readtrigger then begin\r
// code for dealing with the reults of a non-blocking connect is\r
// rather complex\r
// if just write is triggered it means connect suceeded\r
- // if both read and write are suceededed it can mean 2 things\r
+ // if both read and write are triggered it can mean 2 things\r
// 1: connect ok and data availible\r
// 2: connect fail\r
// to find out which you must read from the socket and look for errors\r
// the read event\r
if not readtrigger then begin\r
state := wsconnected;\r
- if assigned(onsessionconnected) then onsessionconnected(self,0);\r
+ connectsuccesshandler;\r
end else begin\r
- numread := fdread(fdhandlein,tempbuf,sizeof(tempbuf));\r
+ numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf));\r
if numread <> -1 then begin\r
state := wsconnected;\r
- if assigned(onsessionconnected) then onsessionconnected(self,0);\r
+ connectsuccesshandler;\r
//connectread := true;\r
recvq.add(@tempbuf,numread);\r
end else begin\r
- state := wsconnected;\r
- if assigned(onsessionconnected) then onsessionconnected(self,linuxerror);\r
-{ debugout('connect fail');}\r
- self.internalclose(0);\r
- recvq.del(maxlongint);\r
+ connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});\r
+ exit;\r
end;\r
// if things went well here we are now in the state wsconnected with data sitting in our receive buffer\r
// so we drop down into the processing for data availible\r
end;\r
if fdhandlein >= 0 then begin\r
if state = wsconnected then begin\r
- rmasterset(fdhandlein);\r
+ eventcore.rmasterset(fdhandlein,false);\r
end else begin\r
- rmasterclr(fdhandlein);\r
+ eventcore.rmasterclr(fdhandlein);\r
end;\r
end;\r
if fdhandleout >= 0 then begin\r
if sendq.size = 0 then begin\r
//don't clear the bit in fdswmaster if data is in the sendq\r
- wmasterclr(fdhandleout);\r
+ eventcore.wmasterclr(fdhandleout);\r
end;\r
end;\r
\r
begin\r
inherited create(aowner);\r
closehandles := true;\r
+ trymoreips := true;\r
end;\r
\r
\r
function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;\r
+var\r
+ addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;\r
begin\r
- result := sockets.getpeername(self.fdhandlein,addr,addrlen);\r
+ result := {$ifdef win32}winsock{$else}sockets{$endif}.getpeername(self.fdhandlein,addrx,addrlen);\r
end;\r
\r
procedure tlsocket.getxaddrbin(var binip:tbinip);\r
begin\r
i := sizeof(addr);\r
fillchar(addr,sizeof(addr),0);\r
- sockets.getsocketname(self.fdhandlein,addr,i);\r
\r
+ {$ifdef win32}\r
+ winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);\r
+ {$else}\r
+ sockets.getsocketname(self.fdhandlein,addr,i);\r
+ {$endif}\r
binip.family := addr.inaddr.family;\r
{$ifdef ipv6}\r
if addr.inaddr6.sin6_family = AF_INET6 then begin\r
begin\r
i := sizeof(addr);\r
fillchar(addr,sizeof(addr),0);\r
- sockets.getpeername(self.fdhandlein,addr,i);\r
+ {$ifdef win32}\r
+ winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);\r
+ {$else}\r
+ sockets.getpeername(self.fdhandlein,addr,i);\r
+ {$endif}\r
\r
binip.family := addr.inaddr.family;\r
{$ifdef ipv6}\r
\r
function tlsocket.getXport:string;\r
var\r
- addr:tinetsockaddr;\r
+ addr:tinetsockaddrv;\r
i:integer;\r
begin\r
i := sizeof(addr);\r
- sockets.getsocketname(self.fdhandlein,addr,i);\r
- i := htons(addr.port);\r
- result := inttostr(i);\r
+ {$ifdef win32}\r
+ winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);\r
+\r
+ {$else}\r
+ sockets.getsocketname(self.fdhandlein,addr,i);\r
+\r
+ {$endif}\r
+ result := inttostr(htons(addr.InAddr.port));\r
end;\r
\r
function tlsocket.getpeerport:string;\r
var\r
- addr:tinetsockaddr;\r
+ addr:tinetsockaddrv;\r
i:integer;\r
begin\r
i := sizeof(addr);\r
- sockets.getpeername(self.fdhandlein,addr,i);\r
- i := htons(addr.port);\r
- result := inttostr(i);\r
+ {$ifdef win32}\r
+ winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);\r
+\r
+ {$else}\r
+ sockets.getpeername(self.fdhandlein,addr,i);\r
+\r
+ {$endif}\r
+ result := inttostr(htons(addr.InAddr.port));\r
end;\r
\r
+{$ifdef win32}\r
+ procedure tlsocket.myfdclose(fd : integer);\r
+ begin\r
+ closesocket(fd);\r
+ end;\r
+ function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt;\r
+ begin\r
+ result := winsock.send(fd,(@buf)^,size,0);\r
+ end;\r
+ function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt;\r
+ begin\r
+ result := winsock.recv(fd,buf,size,0);\r
+ end;\r
+{$endif}\r
+
end.\r
+\r