X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/4782a5c5afee47721cc617daa40dd29828342c2b..0a9944546bc1cf591dfce10112a4385049909f02:/lsocket.pas diff --git a/lsocket.pas b/lsocket.pas index 58f157d..509bf8e 100755 --- a/lsocket.pas +++ b/lsocket.pas @@ -2,10 +2,12 @@ {socket code by plugwash} -{ Copyright (C) 2005 Bas Steendijk and Peter Green - For conditions of distribution and use, see copyright notice in zlib_license.txt - which is included in the package - ----------------------------------------------------------------------------- } +{use this code for whatever you like in programs under whater licence you like + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +} + { changes by plugwash (20030728) * created handlefdtrigger virtual method in tlasio (overridden in tlsocket) and moved a lot of code from messageloop into it @@ -33,24 +35,16 @@ beware (20031017) unit lsocket; -{$ifdef fpc} - {$mode delphi} -{$endif} +{$mode delphi} interface uses sysutils, - {$ifdef win32} - windows,winsock, + {$ifdef VER1_0} + linux, {$else} - - {$ifdef VER1_0} - linux, - {$else} - baseunix,unix,unixutil, - {$endif} - sockets, + baseunix,unix, {$endif} - classes,{pgdebugout,}pgtypes,lcore,fd_utils,binipstuff,dnssync; + sockets,classes,pgdebugout,pgtypes,lcore,fd_utils,binipstuff,dnssync; type sunB = packed record s_b1, s_b2, s_b3, s_b4: byte; @@ -89,8 +83,6 @@ type 1: (InAddr6:TInetSockAddr6); {$endif} end; - Pinetsockaddrv = ^Tinetsockaddrv; - type tsockaddrin=TInetSockAddr; @@ -133,11 +125,6 @@ type function getXport:string; virtual; function getpeerport:string; virtual; constructor Create(AOwner: TComponent); override; - {$ifdef win32} - 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; - {$endif} end; tsocket=longint; // for compatibility with twsocket @@ -160,12 +147,12 @@ const implementation {$include unixstuff.inc} -function longip(s:string):longint;{$ifdef fpc}inline;{$endif} +function longip(s:string):longint;inline; var l:longint; a,b:integer; -function convertbyte(const s:string):integer;{$ifdef fpc}inline;{$endif} +function convertbyte(const s:string):integer;inline; begin result := strtointdef(s,-1); if result < 0 then exit; @@ -234,7 +221,7 @@ end; function htons(w:word):word; begin - {$ifndef ENDIAN_BIG} + {$ifdef ENDIAN_LITTLE} result := ((w and $ff00) shr 8) or ((w and $ff) shl 8); {$else} result := w; @@ -243,7 +230,7 @@ end; function htonl(i:integer):integer; begin - {$ifndef ENDIAN_BIG} + {$ifdef ENDIAN_LITTLE} result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000); {$else} result := i; @@ -265,7 +252,6 @@ begin result := 0; biniptemp := forwardlookup(addr,10); fillchar(inaddr,sizeof(inaddr),0); - //writeln('converted address '+addr+' to binip '+ipbintostr(biniptemp)); if biniptemp.family = AF_INET then begin inAddr.InAddr.family:=AF_INET; inAddr.InAddr.port:=htons(strtointdef(port,0)); @@ -295,34 +281,25 @@ begin if udp then a := SOCK_DGRAM else a := SOCK_STREAM; a := Socket(inaddr.inaddr.family,a,0); - //writeln(ord(inaddr.inaddr.family)); if a = -1 then begin - lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif}; + lasterror := socketerror; raise esocketexception.create('unable to create socket'); end; try dup(a); bindsocket; if udp then begin - {$ifndef win32} - SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); - {$endif} state := wsconnected; if assigned(onsessionconnected) then onsessionconnected(self,0); end else begin state :=wsconnecting; - {$ifdef win32} - //writeln(inaddr.inaddr.port); - winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize); - {$else} - sockets.Connect(fdhandlein,inADDR,getaddrsize); - {$endif} + sockets.Connect(fdhandlein,inADDR,getaddrsize); end; - eventcore.rmasterset(fdhandlein,false); + rmasterset(fdhandlein); if udp then begin - eventcore.wmasterclr(fdhandleout); + wmasterclr(fdhandleout); end else begin - eventcore.wmasterset(fdhandleout); + wmasterset(fdhandleout); end; //sendq := ''; except @@ -345,11 +322,7 @@ end; function tlsocket.send(data:pointer;len:integer):integer; begin if udp then begin - //writeln('sending to '+inttohex(inaddr.inaddr.addr,8)); result := sendto(inaddr.inaddr,getaddrsize,data,len) -; - //writeln('send result',result); - //writeln('errno',errno); end else begin result := inherited send(data,len); end; @@ -359,7 +332,7 @@ end; function tlsocket.receive(Buf:Pointer;BufSize:integer):integer; begin if udp then begin - result := myfdread(self.fdhandlein,buf^,bufsize); + result := fdread(self.fdhandlein,buf^,bufsize); end else begin result := inherited receive(buf,bufsize); end; @@ -369,7 +342,6 @@ procedure tlsocket.bindsocket; var a:integer; inAddrtemp:TInetSockAddrV; - inAddrtempx:{$ifdef win32}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp; inaddrtempsize:integer; begin try @@ -384,9 +356,9 @@ begin inaddrtempsize := makeinaddrv(localaddr,localport,inaddrtemp); - If Bind(fdhandlein,inaddrtempx,inaddrtempsize)<> {$ifdef win32}0{$else}true{$endif} Then begin + If Not Bind(fdhandlein,inaddrtemp,inaddrtempsize) Then begin state := wsclosed; - lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif}; + lasterror := socketerror; raise ESocketException.create('unable to bind, error '+inttostr(lasterror)); end; state := wsbound; @@ -429,18 +401,17 @@ begin 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 + dup(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things + fdreverse[fdhandlein] := self; state := wsclosed; // then set this back as it was an undesired side effect of dup try yes := $01010101; {Copied this from existing code. Value is empiric, but works. (yes=true<>0) } - {$ifndef win32} - if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin - raise ESocketException.create('unable to set socket options'); - end; - {$endif} + if SetSocketOptions(fdhandlein, SOL_SOCKET, SO_REUSEADDR,yes,sizeof(yes))=-1 then begin + raise ESocketException.create('unable to set socket options'); + end; + localaddr := addr; localport := port; bindsocket; @@ -448,76 +419,60 @@ begin if not udp then begin {!!! allow custom queue length? default 5} if listenqueue = 0 then listenqueue := 5; - If {$ifdef win32}winsock{$else}sockets{$endif}.Listen(fdhandlein,listenqueue)<>{$ifdef win32}0{$else}true{$endif} Then raise esocketexception.create('unable to listen'); + If Not sockets.Listen(fdhandlein,listenqueue) Then raise esocketexception.create('unable to listen'); state := wsListening; end else begin - {$ifndef win32} - SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE')); - {$endif} state := wsconnected; end; finally if state = wsclosed then begin if fdhandlein >= 0 then begin {one *can* get here without fd -beware} - eventcore.rmasterclr(fdhandlein); - myfdclose(fdhandlein); // we musnt leak file discriptors - eventcore.setfdreverse(fdhandlein,nil); + rmasterclr(fdhandlein); + fdclose(fdhandlein); // we musnt leak file discriptors + fdreverse[fdhandlein] := nil; fdhandlein := -1; end; end else begin - eventcore.rmasterset(fdhandlein,true); + rmasterset(fdhandlein); end; - if fdhandleout >= 0 then eventcore.wmasterclr(fdhandleout); + if fdhandleout >= 0 then wmasterclr(fdhandleout); end; - //writeln('listened on addr '+addr+':'+port+' '+proto+' using socket number ',fdhandlein); end; 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 :) + FromAddr : TInetSockAddr; // at this point time will tell :) begin - + rmasterset(fdhandlein); FromAddrSize := Sizeof(FromAddr); - {$ifdef win32} - result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize); - {$else} - result := sockets.accept(fdhandlein,fromaddr,fromaddrsize); - {$endif} - //now we have accepted one request start monitoring for more again - eventcore.rmasterset(fdhandlein,true); - - if result = -1 then raise esocketexception.create('error '+inttostr({$ifdef win32}getlasterror{$else}socketerror{$endif})+' while accepting'); + result := sockets.accept(fdhandlein,fromaddr,fromaddrsize); + if result = -1 then raise esocketexception.create('error '+inttostr(socketerror)+' while accepting'); if result > absoloutemaxs then begin - myfdclose(result); + fdclose(result); result := -1; raise esocketexception.create('file discriptor out of range'); end; end; -function tlsocket.sendto(dest:TInetSockAddr;destlen:integer;data:pointer;len:integer):integer; -var - destx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddr{$endif} absolute dest; +function tlsocket.sendto; begin - result := {$ifdef win32}winsock{$else}sockets{$endif}.sendto(self.fdhandleout,data^,len,0,destx,destlen); + result := sockets.sendto(self.fdhandleout,data^,len,0,dest,destlen); end; -function tlsocket.receivefrom(data:pointer;len:integer;var src:TInetSockAddr;var srclen:integer):integer; -var - srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddr{$endif} absolute src; +function tlsocket.receivefrom; begin - result := {$ifdef win32}winsock{$else}sockets{$endif}.recvfrom(self.fdhandlein,data^,len,0,srcx,srclen); + result := sockets.recvfrom(self.fdhandlein,data^,len,0,src,srclen); end; procedure tlsocket.handlefdtrigger(readtrigger,writetrigger:boolean); var tempbuf:array[0..receivebufsize-1] of byte; begin - //writeln('got a fd trigger, readtrigger=',readtrigger,' writetrigger=',writetrigger); if (state =wslistening) and readtrigger then begin { debugout('listening socket triggered on read');} - eventcore.rmasterclr(fdhandlein); + rmasterclr(fdhandlein); if assigned(onsessionAvailable) then onsessionAvailable(self,0); end; if udp and readtrigger then begin @@ -529,7 +484,7 @@ begin // code for dealing with the reults of a non-blocking connect is // rather complex // if just write is triggered it means connect suceeded - // if both read and write are triggered it can mean 2 things + // if both read and write are suceededed it can mean 2 things // 1: connect ok and data availible // 2: connect fail // to find out which you must read from the socket and look for errors @@ -539,7 +494,7 @@ begin state := wsconnected; if assigned(onsessionconnected) then onsessionconnected(self,0); end else begin - numread := myfdread(fdhandlein,tempbuf,sizeof(tempbuf)); + numread := fdread(fdhandlein,tempbuf,sizeof(tempbuf)); if numread <> -1 then begin state := wsconnected; if assigned(onsessionconnected) then onsessionconnected(self,0); @@ -547,7 +502,7 @@ begin recvq.add(@tempbuf,numread); end else begin state := wsconnected; - if assigned(onsessionconnected) then onsessionconnected(self,{$ifdef win32}wsagetlasterror{$else}linuxerror{$endif}); + if assigned(onsessionconnected) then onsessionconnected(self,linuxerror); { debugout('connect fail');} self.internalclose(0); recvq.del(maxlongint); @@ -557,15 +512,15 @@ begin end; if fdhandlein >= 0 then begin if state = wsconnected then begin - eventcore.rmasterset(fdhandlein,false); + rmasterset(fdhandlein); end else begin - eventcore.rmasterclr(fdhandlein); + rmasterclr(fdhandlein); end; end; if fdhandleout >= 0 then begin if sendq.size = 0 then begin //don't clear the bit in fdswmaster if data is in the sendq - eventcore.wmasterclr(fdhandleout); + wmasterclr(fdhandleout); end; end; @@ -581,10 +536,8 @@ end; function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer; -var - addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr; begin - result := {$ifdef win32}winsock{$else}sockets{$endif}.getpeername(self.fdhandlein,addrx,addrlen); + result := sockets.getpeername(self.fdhandlein,addr,addrlen); end; procedure tlsocket.getxaddrbin(var binip:tbinip); @@ -594,12 +547,8 @@ var begin i := sizeof(addr); fillchar(addr,sizeof(addr),0); + sockets.getsocketname(self.fdhandlein,addr,i); - {$ifdef win32} - winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i); - {$else} - sockets.getsocketname(self.fdhandlein,addr,i); - {$endif} binip.family := addr.inaddr.family; {$ifdef ipv6} if addr.inaddr6.sin6_family = AF_INET6 then begin @@ -619,11 +568,7 @@ var begin i := sizeof(addr); fillchar(addr,sizeof(addr),0); - {$ifdef win32} - winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i); - {$else} - sockets.getpeername(self.fdhandlein,addr,i); - {$endif} + sockets.getpeername(self.fdhandlein,addr,i); binip.family := addr.inaddr.family; {$ifdef ipv6} @@ -657,50 +602,24 @@ end; function tlsocket.getXport:string; var - addr:tinetsockaddrv; + addr:tinetsockaddr; i:integer; begin i := sizeof(addr); - {$ifdef win32} - winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i); - - {$else} - sockets.getsocketname(self.fdhandlein,addr,i); - - {$endif} - result := inttostr(htons(addr.InAddr.port)); + sockets.getsocketname(self.fdhandlein,addr,i); + i := htons(addr.port); + result := inttostr(i); end; function tlsocket.getpeerport:string; var - addr:tinetsockaddrv; + addr:tinetsockaddr; i:integer; begin i := sizeof(addr); - {$ifdef win32} - winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i); - - {$else} - sockets.getpeername(self.fdhandlein,addr,i); - - {$endif} - result := inttostr(htons(addr.InAddr.port)); + sockets.getpeername(self.fdhandlein,addr,i); + i := htons(addr.port); + result := inttostr(i); end; -{$ifdef win32} - procedure tlsocket.myfdclose(fd : integer); - begin - closesocket(fd); - end; - function tlsocket.myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; - begin - result := winsock.send(fd,(@buf)^,size,0); - end; - function tlsocket.myfdread(fd: LongInt;var buf;size: LongInt):LongInt; - begin - result := winsock.recv(fd,buf,size,0); - end; -{$endif} - end. -