Fix SocketClient checking for more data, now that we buffer received data

This commit is contained in:
Deon George 2023-06-16 23:18:35 +10:00
parent ade5f07e37
commit b41d65a8fd
2 changed files with 25 additions and 6 deletions

View File

@ -309,9 +309,10 @@ final class Binkp extends BaseProtocol
if ($this->DEBUG)
Log::debug(sprintf('%s:+ binkp_recv',self::LOGKEY));
$blksz = $this->rx_size == -1 ? BinkpMessage::BLK_HDR_SIZE : $this->rx_size;
$blksz = $this->rx_size === -1 ? BinkpMessage::BLK_HDR_SIZE : $this->rx_size;
if ($this->DEBUG)
Log::debug(sprintf('%s: - binkp_recv blksize [%d] rx_size [%d].',self::LOGKEY,$blksz,$this->rx_size));
Log::debug(sprintf('%s: - binkp_recv blksize [%d] rx_size [%d] rx_ptr (%d).',self::LOGKEY,$blksz,$this->rx_size,$this->rx_ptr));
if ($blksz !== 0) {
try {
@ -333,7 +334,7 @@ final class Binkp extends BaseProtocol
return 0;
}
if (strlen($this->rx_buf) == 0) {
if (strlen($this->rx_buf) === 0) {
// @todo Check that this is correct.
Log::debug(sprintf('%s: - binkp_recv Was the socket closed by the remote?',self::LOGKEY));
$this->error = -2;
@ -354,7 +355,7 @@ final class Binkp extends BaseProtocol
Log::debug(sprintf('%s: - binkp_recv rx_ptr [%d] blksz [%d].',self::LOGKEY,$this->rx_ptr,$blksz));
/* Received complete block */
if ($this->rx_ptr == $blksz) {
if ($this->rx_ptr === $blksz) {
/* Header */
if ($this->rx_size == -1 ) {
$this->is_msg = ord(substr($this->rx_buf,0,1)) >> 7;
@ -498,7 +499,7 @@ final class Binkp extends BaseProtocol
if ($this->DEBUG)
Log::debug(sprintf('%s:+ binkp_send - tx_left [%d]',self::LOGKEY,$this->tx_left));
if ($this->tx_left == 0 ) { /* tx buffer is empty */
if ($this->tx_left === 0 ) { /* tx buffer is empty */
$this->tx_ptr = $this->tx_left = 0;
if ($this->DEBUG)
@ -550,8 +551,14 @@ final class Binkp extends BaseProtocol
} else {
try {
if ($this->DEBUG)
Log::debug(sprintf('%s:+ sending - tx_ptr [%d] tx_buf [%d]',self::LOGKEY,$this->tx_ptr,strlen($this->tx_buf)));
$rc = $this->client->send(substr($this->tx_buf,$this->tx_ptr,$this->tx_left),self::BP_TIMEOUT);
if ($this->DEBUG)
Log::debug(sprintf('%s:+ sent [%d]',self::LOGKEY,$rc));
} catch (Exception $e) {
if ($e->getCode() == 11)
return 1;
@ -1214,6 +1221,9 @@ final class Binkp extends BaseProtocol
$this->binkp_hs();
while (TRUE) {
if ($this->DEBUG)
Log::debug(sprintf('%s: - protocol_session LOOP',self::LOGKEY));
if (! $this->sessionGet(self::SE_INIT|self::SE_SENDFILE|self::SE_SENTEOB|self::SE_NOFILES) && ! $this->send->fd) {
// Open our next file to send
if ($this->send->total_count && ! $this->send->fd)
@ -1261,9 +1271,15 @@ final class Binkp extends BaseProtocol
$rd = TRUE;
try {
if ($this->DEBUG)
Log::debug(sprintf('%s: - Checking if there more data (ttySelect), timeout [%d]',self::LOGKEY,self::BP_TIMEOUT));
// @todo we need to catch a timeout if there are no reads/writes
$rc = $this->client->ttySelect($rd,$wd,self::BP_TIMEOUT);
if ($this->DEBUG)
Log::debug(sprintf('%s: - ttySelect returned [%d]',self::LOGKEY,$rc));
} catch (Exception) {
$this->error_close();
$this->error = -2;

View File

@ -502,7 +502,7 @@ final class SocketClient {
}
/**
* See if we there is data waiting to collect, or if we can send
* See if there is data waiting to collect, or if we can send
*
* @param bool $read
* @param bool $write
@ -512,6 +512,9 @@ final class SocketClient {
*/
public function ttySelect(bool $read,bool $write, int $timeout): int
{
if (strlen($this->rx_buf) && ($this->rx_left))
return 1;
$read = $read ? [$this->connection] : NULL;
$write = $write ? [$this->connection] : NULL;