Skip to content

Commit

Permalink
added libssh2 timeout
Browse files Browse the repository at this point in the history
+ libssh2_session_get_timeout
+ libssh2_session_set_timeout
+ property KeepAliveTimeOut
  • Loading branch information
pult committed Dec 11, 2023
1 parent 1ccf877 commit 41eb635
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
69 changes: 49 additions & 20 deletions comp/uMySFTPClient.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ uMySFTPClient.pas } // version: 2020.1114.2017
{ uMySFTPClient.pas } // version: 2023.1211.1600
{ **
* Copyright (c) 2010, Zeljko Marjanovic <[email protected]>
* This code is licensed under MPL 1.1
Expand Down Expand Up @@ -202,7 +202,8 @@ TSSH2Client = class(TComponent)
FLastErrStr: string;
FBlockingMode: Boolean;
FKeepAlive: Boolean;
FTimeOut: Integer; // keepalive timeout interval in seconds
FKeepAliveTimeOut: Integer; // keepalive timeout interval in seconds
FTimeOut: Integer; // socket timeout interval in seconds
FSockBufLen: Integer;
FHashMgr: IHashManager;
FSocket: Integer;
Expand All @@ -221,6 +222,7 @@ TSSH2Client = class(TComponent)
function GetLibString: string;
procedure SetBlockingMode(Value: Boolean);
procedure SetKeepAlive(Value: Boolean);
procedure SetKeepAliveTimeOut(Value: Integer);
procedure SetTimeOut(Value: Integer);
protected
function GetSessionPtr: PLIBSSH2_SESSION;
Expand Down Expand Up @@ -251,6 +253,7 @@ TSSH2Client = class(TComponent)
property IPVersion: TIPVersion read FIPVersion write FIPVersion;
property BlockingMode: Boolean read FBlockingMode write SetBlockingMode default True;
property KeepAlive: Boolean read FKeepAlive write SetKeepAlive;
property KeepAliveTimeOut: Integer read FKeepAliveTimeOut write SetKeepAliveTimeOut;
property TimeOut: Integer read FTimeOut write SetTimeOut;
property SockSndRcvBufLen: Integer read FSockBufLen write FSockBufLen;
property AuthModes: TAuthModes read FAuthModes write SetAuthModes default [amTryAll];
Expand Down Expand Up @@ -2160,7 +2163,8 @@ constructor TSSH2Client.Create(AOwner: TComponent);
//FCanceled := False;
FBlockingMode := True;
//FKeepAlive := False;
FTimeOut := 10;
FKeepAliveTimeOut := 10;
FTimeOut := 90;
FSockBufLen := 8 * 1024;
FSocket := INVALID_SOCKET;
FCodePage := CP_UTF8;
Expand Down Expand Up @@ -2449,31 +2453,56 @@ procedure TSSH2Client.SetKeepAlive(Value: Boolean);
end;
end;

procedure TSSH2Client.SetTimeOut(Value: Integer);
var
KeepAliveTimeOut, R, i: Integer;
procedure TSSH2Client.SetKeepAliveTimeOut(Value: Integer);
var ATimeOut: Integer; //R, i: Integer;
begin
if Value < 0 then Value := 0;
if (FTimeOut <> Value) or (Assigned(FSession) and not fConnected) then
if Value < 0 then Value := 0; if (Value > 0) and (Value<10) then Value := 10;
if (FKeepAliveTimeOut <> Value) or (Assigned(FSession) and not fConnected) then
begin
FTimeOut := Value;
FKeepAliveTimeOut := Value;
//
if Assigned(FSession) then
begin
// send timeout
KeepAliveTimeOut := FTimeOut;
if (KeepAliveTimeOut > 0) and (KeepAliveTimeOut<10) then KeepAliveTimeOut := 10;
if fDebugMode then dbg(strwhen('connect: ', fConnected)+'libssh2_keepalive_config: active: '+BoolToStr(FKeepAlive, True)+'; seconds: ' + IntToStr(KeepAliveTimeOut));
libssh2_keepalive_config(FSession, bool2int(FKeepAlive), KeepAliveTimeOut); // FTimeOut - number of seconds
ATimeOut := FKeepAliveTimeOut;
if fDebugMode then dbg(strwhen('connect: ', fConnected)+'libssh2_keepalive_config: active: '+BoolToStr(FKeepAlive, True)+'; seconds: ' + IntToStr(ATimeOut));
libssh2_keepalive_config(FSession, bool2int(FKeepAlive), ATimeOut); // FTimeOut - number of seconds
// check/read keepalive timeout settings:
i := 0;
R := libssh2_keepalive_send(FSession, {seconds_to_next:}i);
while (R = LIBSSH2_ERROR_EAGAIN) do begin
waitsocket();
(*
if not fBlockingMode then
begin
i := 0; //? i := bool2int( (not fBlockingMode) );
R := libssh2_keepalive_send(FSession, {seconds_to_next:}i);
while (R = LIBSSH2_ERROR_EAGAIN) do begin
waitsocket();
R := libssh2_keepalive_send(FSession, {seconds_to_next:}i);
end;
//if R < 0 then ;
if fDebugMode then dbg(strwhen('connect: ', fConnected)+'libssh2_keepalive_config. OK: '+BoolToStr(i = KeepAliveTimeOut, True));
end;
//if R < 0 then ;
if fDebugMode then dbg(strwhen('connect: ', fConnected)+'libssh2_keepalive_config. OK: '+BoolToStr(i = KeepAliveTimeOut, True));
*)
end;
end;
end;

procedure TSSH2Client.SetTimeOut(Value: Integer);
var ATimeOut: Integer;
begin
if Value < 0 then Value := 0; if (Value > 0) and (Value<10) then Value := 10;
if (FTimeOut <> Value) or (Assigned(FSession) and not fConnected) then
begin
FTimeOut := Value;
//
if Assigned(FSession) then
begin
ATimeOut := FTimeOut;
if fDebugMode then dbg(strwhen('connect: ', fConnected)+'libssh2_session_set_timeout: seconds: ' + IntToStr(ATimeOut));
libssh2_session_set_timeout(FSession, ATimeOut);

ATimeOut := libssh2_session_get_timeout(FSession);
if fDebugMode then dbg(strwhen('connect: ', fConnected)+'libssh2_session_get_timeout: seconds: ' + IntToStr(ATimeOut));

if FTimeOut <> ATimeOut then
FTimeOut := ATimeOut;
end;
end;
end;
Expand Down
16 changes: 14 additions & 2 deletions libssh2.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ libssh2.pas } // version: 2021.0914.0553
{ libssh2.pas } // version: 2023.1210.1100
{ **
* Delphi/Pascal Wrapper around the library "libssh2"
* Base repository:
Expand Down Expand Up @@ -1046,6 +1046,14 @@ procedure libssh2_channel_set_blocking(channel: PLIBSSH2_CHANNEL;
blocking: Integer); cdecl;
{$ifend}

{$if not declared(uHVDll)}
function libssh2_session_get_timeout(session: PLIBSSH2_SESSION): LONG; cdecl;
procedure libssh2_session_set_timeout(session: PLIBSSH2_SESSION; timeout: LONG); cdecl;
{$else}
var libssh2_session_get_timeout: function (session: PLIBSSH2_SESSION): LONG; cdecl;
var libssh2_session_set_timeout procedure(session: PLIBSSH2_SESSION; timeout: LONG); cdecl;
{$ifend}

{+// libssh2_channel_handle_extended_data is DEPRECATED, do not use!*/ }

{$if not declared(uHVDll)}
Expand Down Expand Up @@ -1778,6 +1786,8 @@ function libssh2_channel_window_write_ex; external libssh2_name{$ifdef allow_del
procedure libssh2_session_set_blocking; external libssh2_name{$ifdef allow_delayed} delayed{$endif};
function libssh2_session_get_blocking; external libssh2_name{$ifdef allow_delayed} delayed{$endif};
procedure libssh2_channel_set_blocking; external libssh2_name{$ifdef allow_delayed} delayed{$endif};
function libssh2_session_get_timeout; external libssh2_name{$ifdef allow_delayed} delayed{$endif};
procedure libssh2_session_set_timeout; external libssh2_name{$ifdef allow_delayed} delayed{$endif};
procedure libssh2_channel_handle_extended_data; external libssh2_name{$ifdef allow_delayed} delayed{$endif};
function libssh2_channel_handle_extended_data2; external libssh2_name{$ifdef allow_delayed} delayed{$endif};
function libssh2_channel_flush_ex; external libssh2_name{$ifdef allow_delayed} delayed{$endif};
Expand Down Expand Up @@ -1821,7 +1831,7 @@ function libssh2_trace_sethandler; external libssh2_name{$ifdef allow_delayed} d
{$else}
var
dll_libssh2 : TDll;
dll_libssh2_entires : array[0..82] of HVDll.TEntry = (
dll_libssh2_entires : array[0..84] of HVDll.TEntry = (
(Proc: @@libssh2_init; Name: 'libssh2_init'),
(Proc: @@libssh2_exit; Name: 'libssh2_exit'),
(Proc: @@libssh2_session_init_ex; Name: 'libssh2_session_init_ex'),
Expand Down Expand Up @@ -1867,6 +1877,8 @@ function libssh2_trace_sethandler; external libssh2_name{$ifdef allow_delayed} d
(Proc: @@libssh2_session_set_blocking; Name: 'libssh2_session_set_blocking'),
(Proc: @@libssh2_session_get_blocking; Name: 'libssh2_session_get_blocking'),
(Proc: @@libssh2_channel_set_blocking; Name: 'libssh2_channel_set_blocking'),
(Proc: @@libssh2_session_get_timeout; Name: 'libssh2_session_get_timeout'),
(Proc: @@libssh2_session_set_timeout; Name: 'libssh2_session_set_timeout'),
(Proc: @@libssh2_channel_handle_extended_data; Name: 'libssh2_channel_handle_extended_data'),
(Proc: @@libssh2_channel_handle_extended_data2; Name: 'libssh2_channel_handle_extended_data2'),
(Proc: @@libssh2_channel_flush_ex; Name: 'libssh2_channel_flush_ex'),
Expand Down

0 comments on commit 41eb635

Please sign in to comment.