\r
beware (20030905)\r
* if connect failed (conn refused) set state to connected and call internalclose, to get closed handler (instead of fdclose)\r
-* (lcore) if closing the fd's in internalcose, set fd's to -1 because closing an fd makes it invalid\r
+* (lcore) if closing the fd's in internalclose, set fds to -1 because closing an fd makes it invalid\r
\r
beware (20030927)\r
* fixed: on connect failed, tried to close fdhandle's which were already set to -1, added check\r
interface\r
uses\r
sysutils,\r
- {$ifdef win32}\r
+ {$ifdef mswindows}\r
windows,winsock,\r
{$else}\r
\r
\r
//host : THostentry ;\r
\r
- //mainthread : boolean ; //for debuggin only\r
+ //mainthread : boolean ; //for debugging only\r
addr:thostname;\r
port:ansistring;\r
localaddr:thostname;\r
\r
{public in tlasio, and can't be private in both places, so should be public here. \r
fixes delphi warning --beware}\r
- {$ifdef win32}\r
+ {$ifdef mswindows}\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
//functions whose name clashes with them) by using sockets.* and but I can't do\r
//that anymore since in some cases connect is now provided by unixstuff.inc\r
//hence these wrapper functions --plugwash\r
-{$ifndef win32}\r
+{$ifndef mswindows}\r
function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean;\r
begin\r
result := connect(sock,addr,addrlen);\r
//we want internalclose to act as if there was\r
fdhandlein := -2;\r
fdhandleout := -2;\r
- tltask.create(taskcallconnectionfailedhandler,self,{$ifdef win32}wsagetlasterror{$else}socketerror{$endif},0);\r
+ tltask.create(taskcallconnectionfailedhandler,self,{$ifdef mswindows}wsagetlasterror{$else}socketerror{$endif},0);\r
exit;\r
end;\r
try\r
dup(a);\r
bindsocket;\r
if dgram then begin\r
- {$ifndef win32}\r
+ {$ifndef mswindows}\r
SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
{$else}\r
SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
eventcore.wmasterclr(fdhandleout);\r
end else begin\r
state :=wsconnecting;\r
- {$ifdef win32}\r
+ {$ifdef mswindows}\r
+ //beware: atleast on windows, wsaasyncselect set interest in events before connecting, or a connect error isn't seen if it happens immediately during connect\r
+ eventcore.rmasterset(fdhandlein,false);\r
+ eventcore.wmasterset(fdhandleout);\r
//writeln(inaddr.inaddr.port);\r
winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);\r
{$else}\r
system_Connect(fdhandlein,inADDR,getaddrsize);\r
+ eventcore.rmasterset(fdhandlein,false);\r
+ eventcore.wmasterset(fdhandleout);\r
{$endif}\r
- eventcore.rmasterset(fdhandlein,false);\r
- eventcore.wmasterset(fdhandleout);\r
if trymoreips then connecttimeout.enabled := true;\r
end;\r
//sendq := '';\r
procedure tlsocket.bindsocket;\r
var\r
inAddrtemp:TInetSockAddrV;\r
- inAddrtempx:{$ifdef win32}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;\r
+ inAddrtempx:{$ifdef mswindows}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;\r
inaddrtempsize:integer;\r
begin\r
try\r
{$ifdef ipv6}\r
isv6socket := (inaddrtemp.inaddr.family = AF_INET6);\r
{$endif}\r
- If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin\r
+ If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef mswindows}0{$else}true{$endif} Then begin\r
state := wsclosed;\r
- lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};\r
+ lasterror := {$ifdef mswindows}getlasterror{$else}socketerror{$endif};\r
raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror));\r
end;\r
state := wsbound;\r
\r
procedure tlsocket.listen;\r
var\r
- {$ifndef win32}\r
+ {$ifndef mswindows}\r
yes,no:longint;\r
{$endif}\r
socktype:integer;\r
end;\r
{$endif}\r
\r
- if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef win32}+' error='+inttostr(wsagetlasterror){$endif});\r
+ if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef mswindows}+' error='+inttostr(wsagetlasterror){$endif});\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
- {$ifndef win32}\r
+ {$ifndef mswindows}\r
yes := $01010101; {Copied this from existing code. Value is empiric,\r
but works. (yes=true<>0) }\r
no := 0;\r
\r
end;\r
end;\r
+ {$else}\r
+ SetSockOpt(fdhandlein, SOL_SOCKET, SO_REUSEADDR, 'TRUE', Length('TRUE'));\r
+\r
{$endif}\r
localaddr := addr;\r
localport := port;\r
if not udp then begin\r
{!!! allow custom queue length? default 5}\r
if listenqueue = 0 then listenqueue := 5;\r
- If {$ifdef win32}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise\r
+ If {$ifdef mswindows}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef mswindows}0{$else}true{$endif} Then raise\r
esocketexception.create('unable to listen');\r
state := wsListening;\r
end else begin\r
- {$ifndef win32}\r
+ {$ifndef mswindows}\r
SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
{$else}\r
SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));\r
if fdhandlein >= 0 then begin\r
{one *can* get here without fd -beware}\r
eventcore.rmasterclr(fdhandlein);\r
- myfdclose(fdhandlein); // we musnt leak file discriptors\r
+ myfdclose(fdhandlein); // we musnt leak file descriptors\r
eventcore.setfdreverse(fdhandlein,nil);\r
fdhandlein := -1;\r
end;\r
\r
function tlsocket.accept : longint;\r
var\r
- FromAddrSize : LongInt; // i don't realy know what to do with these at this\r
+ FromAddrSize : LongInt; // i don't really know what to do with these at this\r
FromAddr : TInetSockAddrV; // at this point time will tell :)\r
- a:integer;\r
+ a,acceptlasterror:integer;\r
begin\r
{$ifdef secondlistener}\r
if (lastsessionfromsecond) then begin\r
{$endif}\r
\r
FromAddrSize := Sizeof(FromAddr);\r
- {$ifdef win32}\r
+ {$ifdef mswindows}\r
result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);\r
{$else}\r
result := system_accept(fdhandlein,fromaddr,fromaddrsize);\r
{$endif}\r
+\r
+ if (result = -1) then acceptlasterror := {$ifdef mswindows}getlasterror{$else}socketerror{$endif} else acceptlasterror := 0;\r
+\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
+ raise esocketexception.create('error '+inttostr(acceptlasterror)+' while accepting');\r
end;\r
- if result > absoloutemaxs then begin\r
+ if result > absolutemaxs then begin\r
myfdclose(result);\r
a := result;\r
{ result := -1;}\r
- raise esocketexception.create('file discriptor out of range: '+inttostr(a));\r
+ raise esocketexception.create('file descriptor out of range: '+inttostr(a));\r
end;\r
end;\r
\r
realdest : tinetsockaddrv;\r
biniptemp : tbinip;\r
{$endif}\r
- destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif};\r
+ destx : {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif};\r
\r
begin\r
{$ifdef secondlistener}\r
biniptemp := inaddrvtobinip(dest);\r
converttov6(biniptemp);\r
destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);\r
- destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)\r
+ destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)\r
end else begin\r
- destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)\r
+ destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)\r
end;\r
{$else}\r
- destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)\r
+ destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest);\r
{$endif}\r
\r
- result := {$ifdef win32}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen);\r
+ result := {$ifdef mswindows}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen);\r
end;\r
\r
\r
var\r
tempsrc:TInetSockAddrV;\r
tempsrclen:integer;\r
- srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;\r
+ srcx : {$ifdef mswindows}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;\r
biniptemp:tbinip;\r
begin\r
{$ifdef secondlistener}\r
exit;\r
end;\r
if (state =wsconnecting) and writetrigger then begin\r
- // code for dealing with the reults of a non-blocking connect is\r
+ // code for dealing with the results of a non-blocking connect is\r
// rather complex\r
- // if just write is triggered it means connect suceeded\r
+ // if just write is triggered it means connect succeeded\r
// if both read and write are triggered it can mean 2 things\r
- // 1: connect ok and data availible\r
+ // 1: connect ok and data available\r
// 2: connect fail\r
// to find out which you must read from the socket and look for errors\r
- // there if we read successfully we drop through into the code for fireing\r
+ // there if we read successfully we drop through into the code for firing\r
// the read event\r
if not readtrigger then begin\r
state := wsconnected;\r
//connectread := true;\r
recvq.add(@tempbuf,numread);\r
end else begin\r
- connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});\r
+ connectionfailedhandler({$ifdef mswindows}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
+ // so we drop down into the processing for data available\r
end;\r
if fdhandlein >= 0 then begin\r
if state = wsconnected then begin\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
+ addrx : {$ifdef mswindows}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;\r
begin\r
- result := {$ifdef win32}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen);\r
+ result := {$ifdef mswindows}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen);\r
end;\r
\r
procedure tlsocket.getxaddrbin(var binip:tbinip);\r
i := sizeof(addr);\r
fillchar(addr,sizeof(addr),0);\r
\r
- {$ifdef win32}\r
+ {$ifdef mswindows}\r
winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);\r
{$else}\r
getsocketname(self.fdhandlein,addr,i);\r
begin\r
i := sizeof(addr);\r
fillchar(addr,sizeof(addr),0);\r
- {$ifdef win32}\r
+ {$ifdef mswindows}\r
winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);\r
{$else}\r
system_getpeername(self.fdhandlein,addr,i);\r
i:integer;\r
begin\r
i := sizeof(addr);\r
- {$ifdef win32}\r
+ {$ifdef mswindows}\r
winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);\r
\r
{$else}\r
i:integer;\r
begin\r
i := sizeof(addr);\r
- {$ifdef win32}\r
+ {$ifdef mswindows}\r
winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);\r
\r
{$else}\r
result := inttostr(htons(addr.InAddr.port));\r
end;\r
\r
-{$ifdef win32}\r
+{$ifdef mswindows}\r
procedure tlsocket.myfdclose(fd : integer);\r
begin\r
closesocket(fd);\r