ssposix.inc - improvements inspired by ACBr

git-svn-id: https://svn.code.sf.net/p/synalist/code/trunk@246 7c85be65-684b-0410-a082-b2ed4fbef004
This commit is contained in:
geby 2022-01-12 18:17:17 +00:00
parent 6722d1bd92
commit da9579d128

View File

@ -45,6 +45,9 @@
{:@exclude} {:@exclude}
{$WARN UNIT_PLATFORM OFF}
{$WARN SYMBOL_PLATFORM OFF}
{$IFDEF POSIX} {$IFDEF POSIX}
{for delphi XE2+} {for delphi XE2+}
@ -67,7 +70,7 @@ partially compatible with NextGen Delphi compiler - iOS
interface interface
uses uses
SyncObjs, SysUtils, Classes, SyncObjs, SysUtils, Classes, synafpc,
Posix.SysSocket, Posix.SysSelect, Posix.SysTime, Posix.NetinetIn, Posix.SysSocket, Posix.SysSelect, Posix.SysTime, Posix.NetinetIn,
Posix.StrOpts, Posix.Errno; Posix.StrOpts, Posix.Errno;
@ -97,21 +100,24 @@ type
Ttimeval = Posix.SysTime.timeval; Ttimeval = Posix.SysTime.timeval;
const const
{$IFDEF MACOS} //not declared in all Delphi versions...
FIONREAD = $4004667F; // oSX FIONREAD = Posix.StrOpts.FIONREAD; {$IF DECLARED(Posix.StrOpts.FIONREAD)}
FIONBIO = $8004667E; //OSX FIONBIO = Posix.StrOpts.FIONBIO; FIONREAD = Posix.StrOpts.FIONREAD;
FIOASYNC = $8004667D; //OSX FIOASYNC = Posix.StrOpts.FIOASYNC; // not defined in XE2 {$Else}
{$ELSE} FIONREAD = {$IFDEF ANDROID}$541B{$ELSE}$4004667F{$ENDIF};
// LINUX & ANDROID {$IfEnd}
FIONREAD = $541B;
FIONBIO = $5421;
FIOASYNC = $5452;
{$ENDIF}
{$IF DECLARED(Posix.StrOpts.FIONBIO)}
FIONBIO = Posix.StrOpts.FIONBIO;
{$Else}
FIONBIO = {$IFDEF ANDROID}$5421{$ELSE}$8004667E{$ENDIF};
{$IfEnd}
{ FIONREAD = $541B; // LINUX? {$IF DECLARED(Posix.StrOpts.FIOASYNC)}
FIONBIO = $5421; FIOASYNC = Posix.StrOpts.FIOASYNC;
FIOASYNC = $5452; } {$Else}
FIOASYNC = {$IFDEF ANDROID}$5452{$ELSE}$8004667D{$ENDIF};
{$IfEnd}
const const
IPPROTO_IP = Posix.NetinetIn.IPPROTO_IP; { Dummy } IPPROTO_IP = Posix.NetinetIn.IPPROTO_IP; { Dummy }
@ -238,11 +244,11 @@ const
AF_UNSPEC = Posix.SysSocket.AF_UNSPEC;// 0; { unspecified } AF_UNSPEC = Posix.SysSocket.AF_UNSPEC;// 0; { unspecified }
AF_INET = Posix.SysSocket.AF_INET; // 2; { internetwork: UDP, TCP, etc. } AF_INET = Posix.SysSocket.AF_INET; // 2; { internetwork: UDP, TCP, etc. }
AF_INET6 = Posix.SysSocket.AF_INET6; // !! 30 { Internetwork Version 6 } AF_INET6 = Posix.SysSocket.AF_INET6; // !! 30 { Internetwork Version 6 }
{$IFDEF ANDROID} {$IF DECLARED(Posix.SysSocket.AF_MAX)}
AF_MAX = 24;
{$ELSE}
AF_MAX = Posix.SysSocket.AF_MAX; // !! - variable by OS AF_MAX = Posix.SysSocket.AF_MAX; // !! - variable by OS
{$ENDIF} {$Else}
AF_MAX = 43; //not declared for Android
{$IfEnd}
{ Protocol families, same as address families for now. } { Protocol families, same as address families for now. }
PF_UNSPEC = AF_UNSPEC; PF_UNSPEC = AF_UNSPEC;
@ -375,7 +381,7 @@ type
case sin_family: sa_family_t of case sin_family: sa_family_t of
AF_INET: (sin_port: word; AF_INET: (sin_port: word;
sin_addr: TInAddr; sin_addr: TInAddr;
sin_zero: array[0..7] of Char); sin_zero: array[0..7] of byte);
AF_INET6: (sin6_port: word; AF_INET6: (sin6_port: word;
sin6_flowinfo: longword; sin6_flowinfo: longword;
sin6_addr: TInAddr6; sin6_addr: TInAddr6;
@ -480,12 +486,11 @@ end;
{$IFDEF NEXTGEN} {$IFDEF NEXTGEN}
function GetHostByName(const name: string):Phostent; function GetHostByName(const name: string):Phostent;
var
h: Phostent;
begin begin
h := Posix.NetDB.gethostbyname(MarshaledAString(TMarshal.AsAnsi(name))); Result := Posix.NetDB.gethostbyname(MarshaledAString(TMarshal.AsAnsi(name)));
end; end;
{$ENDIF} {$ENDIF}
{=============================================================================} {=============================================================================}
function WSAStartup(wVersionRequired: Word; var WSData: TWSAData): Integer; function WSAStartup(wVersionRequired: Word; var WSData: TWSAData): Integer;
@ -589,18 +594,21 @@ var
const const
cMaxHostLength = 255; cMaxHostLength = 255;
begin begin
Result := '';
SetLength(name, cMaxHostLength); SetLength(name, cMaxHostLength);
if Posix.Unistd.GetHostName(MarshaledAString(name), cMaxHostLength) = 0 then if Posix.Unistd.GetHostName(MarshaledAString(name), cMaxHostLength) = 0 then
Result := TEncoding.UTF8.GetString(name).ToUpper; Result := TEncoding.UTF8.GetString(name).ToUpper
{$ELSE} {$ELSE}
var var
s: AnsiString; s: AnsiString;
begin begin
Result := ''; Result := '';
setlength(s, 255); setlength(s, cMaxHostLength);
Posix.Unistd.GetHostName(PAnsiChar(s), Length(s) - 1); Posix.Unistd.GetHostName(PAnsiChar(s), Length(s) - 1);
Result := PChar(string(s)); Result := PChar(string(s));
{$ENDIF} {$ENDIF}
if Result = '' then
Result := cLocalHostStr;
end; end;
function Send(s: TSocket; Buf: TMemory; len, flags: Integer): Integer; function Send(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
@ -716,19 +724,6 @@ begin
Result := (Family = AF_INET6) and SockWship6Api; Result := (Family = AF_INET6) and SockWship6Api;
end; end;
{$IFDEF NEXTGEN}
function GetAddrInfo(hostname, servname: string;
const hints: addrinfo; out res: Paddrinfo): Integer;
begin
end;
{$ENDIF}
function gethostbyname(const name: PAnsiChar): PHostEnt; cdecl;
external libc name _PU + 'gethostbyname';
function gethostbyaddr(var addr; len: socklen_t; atype: integer): PHostEnt; cdecl;
external libc name _PU + 'gethostbyaddr';
function SetVarSin(var Sin: TVarSin; IP, Port: string; Family, SockProtocol, SockType: integer; PreferIP4: Boolean): integer; function SetVarSin(var Sin: TVarSin; IP, Port: string; Family, SockProtocol, SockType: integer; PreferIP4: Boolean): integer;
var var
ProtoEnt: PProtoEnt; ProtoEnt: PProtoEnt;