X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/e6290c8bee34fc9a6fdbce30a9dd85faffeaac5a..7cad80355c81569210547a281e30cd491b5bb353:/lsocket.pas diff --git a/lsocket.pas b/lsocket.pas index 72ef891..b493ccd 100755 --- a/lsocket.pas +++ b/lsocket.pas @@ -42,7 +42,7 @@ unit lsocket; interface uses sysutils, - {$ifdef win32} + {$ifdef mswindows} windows,winsock, {$else} @@ -137,7 +137,7 @@ type {public in tlasio, and can't be private in both places, so should be public here. fixes delphi warning --beware} - {$ifdef win32} + {$ifdef mswindows} procedure myfdclose(fd : integer); override; function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override; function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override; @@ -174,7 +174,7 @@ end; //functions whose name clashes with them) by using sockets.* and but I can't do //that anymore since in some cases connect is now provided by unixstuff.inc //hence these wrapper functions --plugwash -{$ifndef win32} +{$ifndef mswindows} function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean; begin result := connect(sock,addr,addrlen); @@ -236,14 +236,14 @@ begin //we want internalclose to act as if there was fdhandlein := -2; fdhandleout := -2; - tltask.create(taskcallconnectionfailedhandler,self,{$ifdef win32}wsagetlasterror{$else}socketerror{$endif},0); + tltask.create(taskcallconnectionfailedhandler,self,{$ifdef mswindows}wsagetlasterror{$else}socketerror{$endif},0); exit; end; try dup(a); bindsocket; if dgram then begin - {$ifndef win32} + {$ifndef mswindows} SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); {$else} SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); @@ -255,7 +255,7 @@ begin eventcore.wmasterclr(fdhandleout); end else begin state :=wsconnecting; - {$ifdef win32} + {$ifdef mswindows} //writeln(inaddr.inaddr.port); winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize); {$else} @@ -347,7 +347,7 @@ end; procedure tlsocket.bindsocket; var inAddrtemp:TInetSockAddrV; - inAddrtempx:{$ifdef win32}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp; + inAddrtempx:{$ifdef mswindows}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp; inaddrtempsize:integer; begin try @@ -363,9 +363,9 @@ begin {$ifdef ipv6} isv6socket := (inaddrtemp.inaddr.family = AF_INET6); {$endif} - If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin + If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef mswindows}0{$else}true{$endif} Then begin state := wsclosed; - lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif}; + lasterror := {$ifdef mswindows}getlasterror{$else}socketerror{$endif}; raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror)); end; state := wsbound; @@ -380,7 +380,7 @@ end; procedure tlsocket.listen; var - {$ifndef win32} + {$ifndef mswindows} yes,no:longint; {$endif} socktype:integer; @@ -424,13 +424,13 @@ begin end; {$endif} - if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef win32}+' error='+inttostr(wsagetlasterror){$endif}); + if fdhandlein = -1 then raise ESocketException.create('unable to create socket'{$ifdef mswindows}+' error='+inttostr(wsagetlasterror){$endif}); dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup state := wsclosed; // then set this back as it was an undesired side effect of dup try - {$ifndef win32} + {$ifndef mswindows} yes := $01010101; {Copied this from existing code. Value is empiric, but works. (yes=true<>0) } no := 0; @@ -449,6 +449,9 @@ begin end; end; + {$else} + SetSockOpt(fdhandlein, SOL_SOCKET, SO_REUSEADDR, 'TRUE', Length('TRUE')); + {$endif} localaddr := addr; localport := port; @@ -457,11 +460,11 @@ begin if not udp then begin {!!! allow custom queue length? default 5} if listenqueue = 0 then listenqueue := 5; - If {$ifdef win32}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise + If {$ifdef mswindows}winsock.listen{$else}system_listen{$endif}(fdhandlein,listenqueue)<>{$ifdef mswindows}0{$else}true{$endif} Then raise esocketexception.create('unable to listen'); state := wsListening; end else begin - {$ifndef win32} + {$ifndef mswindows} SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); {$else} SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); @@ -542,7 +545,7 @@ begin {$endif} FromAddrSize := Sizeof(FromAddr); - {$ifdef win32} + {$ifdef mswindows} result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize); {$else} result := system_accept(fdhandlein,fromaddr,fromaddrsize); @@ -551,7 +554,7 @@ begin eventcore.rmasterset(fdhandlein,true); if result = -1 then begin - raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting'); + raise esocketexception.create('error '+inttostr({$ifdef mswindows}getlasterror{$else}socketerror{$endif})+' while accepting'); end; if result > absoloutemaxs then begin myfdclose(result); @@ -568,7 +571,7 @@ var realdest : tinetsockaddrv; biniptemp : tbinip; {$endif} - destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}; + destx : {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}; begin {$ifdef secondlistener} @@ -582,15 +585,15 @@ begin biniptemp := inaddrvtobinip(dest); converttov6(biniptemp); destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest); - destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest) + destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest) end else begin - destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest) + destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest) end; {$else} - destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest) + destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest); {$endif} - result := {$ifdef win32}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen); + result := {$ifdef mswindows}winsock.sendto{$else}system_sendto{$endif}(self.fdhandleout,data^,len,0,destx^,destlen); end; @@ -598,7 +601,7 @@ function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddrV;va var tempsrc:TInetSockAddrV; tempsrclen:integer; - srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc; + srcx : {$ifdef mswindows}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc; biniptemp:tbinip; begin {$ifdef secondlistener} @@ -690,7 +693,7 @@ begin //connectread := true; recvq.add(@tempbuf,numread); end else begin - connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif}); + connectionfailedhandler({$ifdef mswindows}wsagetlasterror{$else}linuxerror{$endif}); exit; end; // if things went well here we are now in the state wsconnected with data sitting in our receive buffer @@ -725,9 +728,9 @@ end; function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer; var - addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr; + addrx : {$ifdef mswindows}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr; begin - result := {$ifdef win32}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen); + result := {$ifdef mswindows}winsock.getpeername{$else}system_getpeername{$endif}(self.fdhandlein,addrx,addrlen); end; procedure tlsocket.getxaddrbin(var binip:tbinip); @@ -738,7 +741,7 @@ begin i := sizeof(addr); fillchar(addr,sizeof(addr),0); - {$ifdef win32} + {$ifdef mswindows} winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i); {$else} getsocketname(self.fdhandlein,addr,i); @@ -754,7 +757,7 @@ var begin i := sizeof(addr); fillchar(addr,sizeof(addr),0); - {$ifdef win32} + {$ifdef mswindows} winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i); {$else} system_getpeername(self.fdhandlein,addr,i); @@ -788,7 +791,7 @@ var i:integer; begin i := sizeof(addr); - {$ifdef win32} + {$ifdef mswindows} winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i); {$else} @@ -804,7 +807,7 @@ var i:integer; begin i := sizeof(addr); - {$ifdef win32} + {$ifdef mswindows} winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i); {$else} @@ -814,7 +817,7 @@ begin result := inttostr(htons(addr.InAddr.port)); end; -{$ifdef win32} +{$ifdef mswindows} procedure tlsocket.myfdclose(fd : integer); begin closesocket(fd);