SSH2: add setKeepAlive() method

This commit is contained in:
terrafrost 2020-09-28 06:17:49 -05:00
parent 2ae683479a
commit 96c4c3bc58

View File

@ -692,6 +692,14 @@ class Net_SSH2
*/ */
var $curTimeout; var $curTimeout;
/**
* Keep Alive Interval
*
* @see self::setKeepAlive()
* @access private
*/
var $keepAlive;
/** /**
* Real-time log file pointer * Real-time log file pointer
* *
@ -2691,6 +2699,19 @@ class Net_SSH2
$this->timeout = $this->curTimeout = $timeout; $this->timeout = $this->curTimeout = $timeout;
} }
/**
* Set Keep Alive
*
* Sends an SSH2_MSG_IGNORE message every x seconds, if x is a positive non-zero number.
*
* @param mixed $timeout
* @access public
*/
function setKeepAlive($interval)
{
$this->keepAlive = $interval;
}
/** /**
* Get the output from stdError * Get the output from stdError
* *
@ -3684,8 +3705,15 @@ class Net_SSH2
$read = array($this->fsock); $read = array($this->fsock);
$write = $except = null; $write = $except = null;
if (!$this->curTimeout) { if ($this->curTimeout <= 0) {
@stream_select($read, $write, $except, null); if ($this->keepAlive <= 0) {
@stream_select($read, $write, $except, null);
} else {
if (!@stream_select($read, $write, $except, $this->keepAlive) && !count($read)) {
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_IGNORE, 0));
continue;
}
}
} else { } else {
if ($this->curTimeout < 0) { if ($this->curTimeout < 0) {
$this->is_timeout = true; $this->is_timeout = true;
@ -3696,8 +3724,21 @@ class Net_SSH2
$write = $except = null; $write = $except = null;
$start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
if ($this->keepAlive > 0 && $this->keepAlive < $this->curTimeout) {
if (!@stream_select($read, $write, $except, $this->keepAlive) && !count($read)) {
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_IGNORE, 0));
$elapsed = strtok(microtime(), ' ') + strtok('') - $start;
$this->curTimeout-= $elapsed;
continue;
}
$elapsed = strtok(microtime(), ' ') + strtok('') - $start;
$this->curTimeout-= $elapsed;
}
$sec = floor($this->curTimeout); $sec = floor($this->curTimeout);
$usec = 1000000 * ($this->curTimeout - $sec); $usec = 1000000 * ($this->curTimeout - $sec);
// on windows this returns a "Warning: Invalid CRT parameters detected" error // on windows this returns a "Warning: Invalid CRT parameters detected" error
if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) { if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
$this->is_timeout = true; $this->is_timeout = true;