/[lcore]/trunk/lsocket.pas
ViewVC logotype

Diff of /trunk/lsocket.pas

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 126 by beware, Fri May 25 00:08:50 2012 UTC revision 127 by beware, Fri Nov 22 02:30:44 2013 UTC
# Line 42  Line 42 
42  interface  interface
43    uses    uses
44      sysutils,      sysutils,
45      {$ifdef win32}      {$ifdef mswindows}
46        windows,winsock,        windows,winsock,
47      {$else}      {$else}
48    
# Line 137  Line 137 
137    
138        {public in tlasio, and can't be private in both places, so should be public here.        {public in tlasio, and can't be private in both places, so should be public here.
139        fixes delphi warning --beware}        fixes delphi warning --beware}
140        {$ifdef win32}        {$ifdef mswindows}
141          procedure myfdclose(fd : integer); override;          procedure myfdclose(fd : integer); override;
142          function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override;          function myfdwrite(fd: LongInt;const buf;size: LongInt):LongInt; override;
143          function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override;          function myfdread(fd: LongInt;var buf;size: LongInt):LongInt; override;
# Line 174  Line 174 
174  //functions whose name clashes with them) by using sockets.* and but I can't do  //functions whose name clashes with them) by using sockets.* and but I can't do
175  //that anymore since in some cases connect is now provided by unixstuff.inc  //that anymore since in some cases connect is now provided by unixstuff.inc
176  //hence these wrapper functions --plugwash  //hence these wrapper functions --plugwash
177  {$ifndef win32}  {$ifndef mswindows}
178    function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean;    function system_Connect(Sock: LongInt;const Addr;Addrlen: LongInt):Boolean;
179    begin    begin
180      result := connect(sock,addr,addrlen);      result := connect(sock,addr,addrlen);
# Line 236  Line 236 
236      //we want internalclose to act as if there was      //we want internalclose to act as if there was
237      fdhandlein := -2;      fdhandlein := -2;
238      fdhandleout := -2;      fdhandleout := -2;
239      tltask.create(taskcallconnectionfailedhandler,self,{$ifdef win32}wsagetlasterror{$else}socketerror{$endif},0);      tltask.create(taskcallconnectionfailedhandler,self,{$ifdef mswindows}wsagetlasterror{$else}socketerror{$endif},0);
240      exit;      exit;
241    end;    end;
242    try    try
243      dup(a);      dup(a);
244      bindsocket;      bindsocket;
245      if dgram then begin      if dgram then begin
246        {$ifndef win32}        {$ifndef mswindows}
247          SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));          SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
248        {$else}        {$else}
249          SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));          SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
# Line 255  Line 255 
255        eventcore.wmasterclr(fdhandleout);        eventcore.wmasterclr(fdhandleout);
256      end else begin      end else begin
257        state :=wsconnecting;        state :=wsconnecting;
258        {$ifdef win32}        {$ifdef mswindows}
259          //writeln(inaddr.inaddr.port);          //writeln(inaddr.inaddr.port);
260          winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);          winsock.Connect(fdhandlein,winsock.psockaddr(@inADDR)^,getaddrsize);
261        {$else}        {$else}
# Line 347  Line 347 
347  procedure tlsocket.bindsocket;  procedure tlsocket.bindsocket;
348  var  var
349    inAddrtemp:TInetSockAddrV;    inAddrtemp:TInetSockAddrV;
350    inAddrtempx:{$ifdef win32}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;    inAddrtempx:{$ifdef mswindows}winsock.TSockaddr{$else}TInetSockAddrV{$endif} absolute inaddrtemp;
351    inaddrtempsize:integer;    inaddrtempsize:integer;
352  begin  begin
353    try    try
# Line 363  Line 363 
363        {$ifdef ipv6}        {$ifdef ipv6}
364          isv6socket := (inaddrtemp.inaddr.family = AF_INET6);          isv6socket := (inaddrtemp.inaddr.family = AF_INET6);
365        {$endif}        {$endif}
366        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
367          state := wsclosed;          state := wsclosed;
368          lasterror := {$ifdef win32}getlasterror{$else}socketerror{$endif};          lasterror := {$ifdef mswindows}getlasterror{$else}socketerror{$endif};
369          raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror));          raise ESocketException.create('unable to bind on address '+localaddr+'#'+localport+', error '+inttostr(lasterror));
370        end;        end;
371        state := wsbound;        state := wsbound;
# Line 380  Line 380 
380    
381  procedure tlsocket.listen;  procedure tlsocket.listen;
382  var  var
383    {$ifndef win32}    {$ifndef mswindows}
384    yes,no:longint;    yes,no:longint;
385    {$endif}    {$endif}
386    socktype:integer;    socktype:integer;
# Line 424  Line 424 
424    end;    end;
425    {$endif}    {$endif}
426    
427    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});
428    dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things    dupnowatch(fdhandlein); // sets up maxs and copies handle to fdhandleout among other things
429    //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup    //eventcore.setfdreverse(fdhandlein,self); //already taken care of by dup
430    state := wsclosed; // then set this back as it was an undesired side effect of dup    state := wsclosed; // then set this back as it was an undesired side effect of dup
431    
432    try    try
433      {$ifndef win32}      {$ifndef mswindows}
434        yes := $01010101;  {Copied this from existing code. Value is empiric,        yes := $01010101;  {Copied this from existing code. Value is empiric,
435                      but works. (yes=true<>0) }                      but works. (yes=true<>0) }
436        no := 0;        no := 0;
# Line 460  Line 460 
460      if not udp then begin      if not udp then begin
461        {!!! allow custom queue length? default 5}        {!!! allow custom queue length? default 5}
462        if listenqueue = 0 then listenqueue := 5;        if listenqueue = 0 then listenqueue := 5;
463        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
464  esocketexception.create('unable to listen');  esocketexception.create('unable to listen');
465        state := wsListening;        state := wsListening;
466      end else begin      end else begin
467        {$ifndef win32}        {$ifndef mswindows}
468          SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));          SetSocketOptions(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
469        {$else}        {$else}
470          SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));          SetSockOpt(fdhandleout, SOL_SOCKET, SO_BROADCAST, 'TRUE', Length('TRUE'));
# Line 545  Line 545 
545    {$endif}    {$endif}
546    
547    FromAddrSize := Sizeof(FromAddr);    FromAddrSize := Sizeof(FromAddr);
548    {$ifdef win32}    {$ifdef mswindows}
549      result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);      result := winsock.accept(fdhandlein,@fromaddr,@fromaddrsize);
550    {$else}    {$else}
551      result := system_accept(fdhandlein,fromaddr,fromaddrsize);      result := system_accept(fdhandlein,fromaddr,fromaddrsize);
# Line 554  Line 554 
554    eventcore.rmasterset(fdhandlein,true);    eventcore.rmasterset(fdhandlein,true);
555    
556    if result = -1 then begin    if result = -1 then begin
557      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');
558    end;    end;
559    if result > absoloutemaxs then begin    if result > absoloutemaxs then begin
560      myfdclose(result);      myfdclose(result);
# Line 571  Line 571 
571      realdest : tinetsockaddrv;      realdest : tinetsockaddrv;
572      biniptemp : tbinip;      biniptemp : tbinip;
573    {$endif}    {$endif}
574    destx : {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif};    destx : {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif};
575    
576  begin  begin
577    {$ifdef secondlistener}    {$ifdef secondlistener}
# Line 585  Line 585 
585        biniptemp := inaddrvtobinip(dest);        biniptemp := inaddrvtobinip(dest);
586        converttov6(biniptemp);        converttov6(biniptemp);
587        destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);        destlen := makeinaddrv(biniptemp,inttostr(ntohs(dest.InAddr.port)),realdest);
588        destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)        destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@realdest)
589      end else begin      end else begin
590        destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)        destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest)
591      end;      end;
592    {$else}    {$else}
593      destx := {$ifdef win32}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest);      destx := {$ifdef mswindows}winsock.pSockAddr{$else}pInetSockAddrV{$endif}(@dest);
594    {$endif}    {$endif}
595    
596    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);
597  end;  end;
598    
599    
# Line 601  Line 601 
601  var  var
602    tempsrc:TInetSockAddrV;    tempsrc:TInetSockAddrV;
603    tempsrclen:integer;    tempsrclen:integer;
604    srcx : {$ifdef win32}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;    srcx : {$ifdef mswindows}winsock.TSockAddr{$else}TInetSockAddrV{$endif} absolute tempsrc;
605    biniptemp:tbinip;    biniptemp:tbinip;
606  begin  begin
607    {$ifdef secondlistener}    {$ifdef secondlistener}
# Line 693  Line 693 
693          //connectread := true;          //connectread := true;
694          recvq.add(@tempbuf,numread);          recvq.add(@tempbuf,numread);
695        end else begin        end else begin
696          connectionfailedhandler({$ifdef win32}wsagetlasterror{$else}linuxerror{$endif});          connectionfailedhandler({$ifdef mswindows}wsagetlasterror{$else}linuxerror{$endif});
697          exit;          exit;
698        end;        end;
699        // if things went well here we are now in the state wsconnected with data sitting in our receive buffer        // if things went well here we are now in the state wsconnected with data sitting in our receive buffer
# Line 728  Line 728 
728    
729  function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;  function tlsocket.getpeername(var addr:tsockaddrin;addrlen:integer):integer;
730  var  var
731    addrx : {$ifdef win32}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;    addrx : {$ifdef mswindows}winsock.tsockaddr{$else}tsockaddrin{$endif} absolute addr;
732  begin  begin
733    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);
734  end;  end;
735    
736  procedure tlsocket.getxaddrbin(var binip:tbinip);  procedure tlsocket.getxaddrbin(var binip:tbinip);
# Line 741  Line 741 
741    i := sizeof(addr);    i := sizeof(addr);
742    fillchar(addr,sizeof(addr),0);    fillchar(addr,sizeof(addr),0);
743    
744    {$ifdef win32}    {$ifdef mswindows}
745      winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);      winsock.getsockname(self.fdhandlein,psockaddr(@addr)^,i);
746    {$else}    {$else}
747      getsocketname(self.fdhandlein,addr,i);      getsocketname(self.fdhandlein,addr,i);
# Line 757  Line 757 
757  begin  begin
758    i := sizeof(addr);    i := sizeof(addr);
759    fillchar(addr,sizeof(addr),0);    fillchar(addr,sizeof(addr),0);
760    {$ifdef win32}    {$ifdef mswindows}
761      winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);      winsock.getpeername(self.fdhandlein,psockaddr(@addr)^,i);
762    {$else}    {$else}
763      system_getpeername(self.fdhandlein,addr,i);      system_getpeername(self.fdhandlein,addr,i);
# Line 791  Line 791 
791    i:integer;    i:integer;
792  begin  begin
793    i := sizeof(addr);    i := sizeof(addr);
794    {$ifdef win32}    {$ifdef mswindows}
795      winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);      winsock.getsockname(self.fdhandlein,psockaddrin(@addr)^,i);
796    
797    {$else}    {$else}
# Line 807  Line 807 
807    i:integer;    i:integer;
808  begin  begin
809    i := sizeof(addr);    i := sizeof(addr);
810    {$ifdef win32}    {$ifdef mswindows}
811      winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);      winsock.getpeername(self.fdhandlein,psockaddrin(@addr)^,i);
812    
813    {$else}    {$else}
# Line 817  Line 817 
817    result := inttostr(htons(addr.InAddr.port));    result := inttostr(htons(addr.InAddr.port));
818  end;  end;
819    
820  {$ifdef win32}  {$ifdef mswindows}
821    procedure tlsocket.myfdclose(fd : integer);    procedure tlsocket.myfdclose(fd : integer);
822    begin    begin
823      closesocket(fd);      closesocket(fd);

Legend:
Removed from v.126  
changed lines
  Added in v.127

No admin address has been configured">No admin address has been configured
ViewVC Help
Powered by ViewVC 1.1.22