|
@@ -221,13 +221,19 @@ type
|
|
TBlockingMode = (bmBlocking,bmNonBlocking);
|
|
TBlockingMode = (bmBlocking,bmNonBlocking);
|
|
TBlockingModes = Set of TBlockingMode;
|
|
TBlockingModes = Set of TBlockingMode;
|
|
|
|
|
|
|
|
+{$if defined(unix) or defined(windows)}
|
|
|
|
+{$DEFINE HAVENONBLOCKING}
|
|
|
|
+{$endif}
|
|
|
|
+
|
|
TInetSocket = Class(TSocketStream)
|
|
TInetSocket = Class(TSocketStream)
|
|
Private
|
|
Private
|
|
FHost : String;
|
|
FHost : String;
|
|
FPort : Word;
|
|
FPort : Word;
|
|
Protected
|
|
Protected
|
|
|
|
+{$IFDEF HAVENONBLOCKING}
|
|
function SetSocketBlockingMode(ASocket: cint; ABlockMode: TBlockingMode; AFDSPtr: Pointer): Integer; virtual;
|
|
function SetSocketBlockingMode(ASocket: cint; ABlockMode: TBlockingMode; AFDSPtr: Pointer): Integer; virtual;
|
|
function CheckSocketConnectTimeout(ASocket: cint; AFDSPtr: Pointer; ATimeVPtr: Pointer): Integer; virtual;
|
|
function CheckSocketConnectTimeout(ASocket: cint; AFDSPtr: Pointer; ATimeVPtr: Pointer): Integer; virtual;
|
|
|
|
+{$ENDIF}
|
|
Public
|
|
Public
|
|
Constructor Create(const AHost: String; APort: Word; AHandler : TSocketHandler = Nil); Overload;
|
|
Constructor Create(const AHost: String; APort: Word; AHandler : TSocketHandler = Nil); Overload;
|
|
Procedure Connect; Virtual;
|
|
Procedure Connect; Virtual;
|
|
@@ -977,6 +983,7 @@ begin
|
|
Connect;
|
|
Connect;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{$IFDEF HAVENONBLOCKING}
|
|
function TInetSocket.SetSocketBlockingMode(ASocket: cint; ABlockMode: TBlockingMode; AFDSPtr: Pointer): Integer;
|
|
function TInetSocket.SetSocketBlockingMode(ASocket: cint; ABlockMode: TBlockingMode; AFDSPtr: Pointer): Integer;
|
|
|
|
|
|
Const
|
|
Const
|
|
@@ -984,17 +991,13 @@ Const
|
|
(SocketBlockingMode, SocketNonBlockingMode);
|
|
(SocketBlockingMode, SocketNonBlockingMode);
|
|
|
|
|
|
|
|
|
|
-{$if defined(unix) or defined(windows)}
|
|
|
|
var
|
|
var
|
|
locFDS: PFDSet;
|
|
locFDS: PFDSet;
|
|
-{$endif}
|
|
|
|
{$ifdef unix}
|
|
{$ifdef unix}
|
|
flags: Integer;
|
|
flags: Integer;
|
|
{$endif}
|
|
{$endif}
|
|
begin
|
|
begin
|
|
- {$if defined(unix) or defined(windows)}
|
|
|
|
locFDS := PFDSet(AFDSPtr);
|
|
locFDS := PFDSet(AFDSPtr);
|
|
- {$endif}
|
|
|
|
if (AblockMode = bmNonBlocking) then
|
|
if (AblockMode = bmNonBlocking) then
|
|
begin
|
|
begin
|
|
{$ifdef unix}
|
|
{$ifdef unix}
|
|
@@ -1022,20 +1025,18 @@ begin
|
|
end;
|
|
end;
|
|
|
|
|
|
function TInetSocket.CheckSocketConnectTimeout(ASocket: cint; AFDSPtr: Pointer; ATimeVPtr: Pointer): Integer;
|
|
function TInetSocket.CheckSocketConnectTimeout(ASocket: cint; AFDSPtr: Pointer; ATimeVPtr: Pointer): Integer;
|
|
-{$if defined(unix) or defined(windows)}
|
|
|
|
|
|
+
|
|
var
|
|
var
|
|
Err: LongInt = 1;
|
|
Err: LongInt = 1;
|
|
ErrLen: LongInt;
|
|
ErrLen: LongInt;
|
|
locTimeVal: PTimeVal;
|
|
locTimeVal: PTimeVal;
|
|
locFDS: PFDSet;
|
|
locFDS: PFDSet;
|
|
-{$endif}
|
|
|
|
|
|
+
|
|
begin
|
|
begin
|
|
locTimeVal := PTimeVal(ATimeVPtr);
|
|
locTimeVal := PTimeVal(ATimeVPtr);
|
|
locFDS := PFDSet(AFDSPtr);
|
|
locFDS := PFDSet(AFDSPtr);
|
|
- {$if defined(unix) or defined(windows)}
|
|
|
|
- locTimeVal^.tv_usec := 0;
|
|
|
|
- locTimeVal^.tv_sec := FConnectTimeout div 1000;
|
|
|
|
- {$endif}
|
|
|
|
|
|
+ locTimeVal^.tv_usec := 0;
|
|
|
|
+ locTimeVal^.tv_sec := FConnectTimeout div 1000;
|
|
{$ifdef unix}
|
|
{$ifdef unix}
|
|
Result := fpSelect(ASocket + 1, nil, locFDS, nil, locTimeVal); // 0 -> TimeOut
|
|
Result := fpSelect(ASocket + 1, nil, locFDS, nil, locTimeVal); // 0 -> TimeOut
|
|
if Result > 0 then
|
|
if Result > 0 then
|
|
@@ -1064,6 +1065,7 @@ begin
|
|
{$endif}
|
|
{$endif}
|
|
{$endif}
|
|
{$endif}
|
|
end;
|
|
end;
|
|
|
|
+{$ENDIF HAVENONBLOCKING}
|
|
|
|
|
|
procedure TInetSocket.Connect;
|
|
procedure TInetSocket.Connect;
|
|
|
|
|
|
@@ -1071,10 +1073,11 @@ Var
|
|
A : THostAddr;
|
|
A : THostAddr;
|
|
addr: TInetSockAddr;
|
|
addr: TInetSockAddr;
|
|
Res : Integer;
|
|
Res : Integer;
|
|
- {$if defined(unix) or defined(windows)}
|
|
|
|
|
|
+{$IFDEF HAVENONBLOCKING}
|
|
FDS: TFDSet;
|
|
FDS: TFDSet;
|
|
TimeV: TTimeVal;
|
|
TimeV: TTimeVal;
|
|
- {$endif}
|
|
|
|
|
|
+{$endif}
|
|
|
|
+
|
|
|
|
|
|
begin
|
|
begin
|
|
A := StrToHostAddr(FHost);
|
|
A := StrToHostAddr(FHost);
|
|
@@ -1090,18 +1093,22 @@ begin
|
|
addr.sin_family := AF_INET;
|
|
addr.sin_family := AF_INET;
|
|
addr.sin_port := ShortHostToNet(FPort);
|
|
addr.sin_port := ShortHostToNet(FPort);
|
|
addr.sin_addr.s_addr := HostToNet(a.s_addr);
|
|
addr.sin_addr.s_addr := HostToNet(a.s_addr);
|
|
|
|
+{$IFDEF HAVENONBLOCKING}
|
|
if ConnectTimeOut>0 then
|
|
if ConnectTimeOut>0 then
|
|
SetSocketBlockingMode(Handle, bmNonBlocking, @FDS) ;
|
|
SetSocketBlockingMode(Handle, bmNonBlocking, @FDS) ;
|
|
|
|
+{$ENDIF}
|
|
{$ifdef unix}
|
|
{$ifdef unix}
|
|
Res:=ESysEINTR;
|
|
Res:=ESysEINTR;
|
|
While (Res=ESysEINTR) do
|
|
While (Res=ESysEINTR) do
|
|
{$endif}
|
|
{$endif}
|
|
Res:=fpConnect(Handle, @addr, sizeof(addr));
|
|
Res:=fpConnect(Handle, @addr, sizeof(addr));
|
|
- if (ConnectTimeOut>0) then
|
|
|
|
- begin
|
|
|
|
- Res:=CheckSocketConnectTimeout(Handle, @FDS, @TimeV);
|
|
|
|
- SetSocketBlockingMode(Handle, bmBlocking, @FDS);
|
|
|
|
- end;
|
|
|
|
|
|
+{$IFDEF HAVENONBLOCKING}
|
|
|
|
+ if (ConnectTimeOut>0) then
|
|
|
|
+ begin
|
|
|
|
+ Res:=CheckSocketConnectTimeout(Handle, @FDS, @TimeV);
|
|
|
|
+ SetSocketBlockingMode(Handle, bmBlocking, @FDS);
|
|
|
|
+ end;
|
|
|
|
+{$ENDIF}
|
|
If Not (Res<0) then
|
|
If Not (Res<0) then
|
|
if not FHandler.Connect then
|
|
if not FHandler.Connect then
|
|
begin
|
|
begin
|