Simplified logging and fix BINKP issue when receiving less than the blksize

This commit is contained in:
Deon George
2021-08-17 23:49:39 +10:00
parent 5bf612e5b4
commit 978843b5e3
7 changed files with 285 additions and 253 deletions

View File

@@ -69,8 +69,9 @@ final class Binkp extends BaseProtocol
private int $rc;
private int $error;
private int $rx_ptr;
private int $rx_ptr; // @todo Whats the point of this? It seems its only the size of $rx_buf?
private int $rx_size;
private string $rx_buf = '';
private ?Collection $mqueue;
private string $tx_buf;
@@ -107,7 +108,7 @@ final class Binkp extends BaseProtocol
$this->session(self::SESSION_BINKP,$client,(new Address));
$this->client->close();
Log::info(sprintf('%s:= End - Connection closed [%s]',self::LOGKEY,$client->address_remote));
Log::info(sprintf('%s:= onConnect - Connection closed [%s]',self::LOGKEY,$client->address_remote));
exit(0);
}
@@ -301,14 +302,15 @@ final class Binkp extends BaseProtocol
*/
private function binkp_recv(): int
{
Log::debug(sprintf('%s:+ binkp_recv',self::LOGKEY));
if ($this->DEBUG)
Log::debug(sprintf('%s:+ binkp_recv',self::LOGKEY));
$buf = '';
$blksz = $this->rx_size == -1 ? BinkpMessage::BLK_HDR_SIZE : $this->rx_size;
Log::debug(sprintf('%s: - binkp_recv blksize [%d] rx_size [%d].',self::LOGKEY,$blksz,$this->rx_size));
if ($blksz !== 0) {
try {
$buf = $this->client->read(0,$blksz-$this->rx_ptr);
$this->rx_buf .= $this->client->read(0,$blksz-$this->rx_ptr);
} catch (SocketException $e) {
if ($e->getCode() == 11) {
@@ -318,6 +320,7 @@ final class Binkp extends BaseProtocol
return 1;
}
Log::error(sprintf('%s: - binkp_recv Exception [%s].',self::LOGKEY,$e->getCode()));
$this->socket_error = $e->getMessage();
$this->error = 1;
@@ -325,7 +328,7 @@ final class Binkp extends BaseProtocol
return 0;
}
if (strlen($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;
@@ -340,17 +343,21 @@ final class Binkp extends BaseProtocol
*/
}
$this->rx_ptr += strlen($buf);
$this->rx_ptr = strlen($this->rx_buf);
if ($this->DEBUG)
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) {
/* Header */
if ($this->rx_size == -1 ) {
$this->is_msg = ord(substr($buf,0,1)) >> 7;
$this->rx_size = ((ord(substr($buf,0,1))&0x7f) << 8 )+ord(substr($buf,1,1));
$this->is_msg = ord(substr($this->rx_buf,0,1)) >> 7;
$this->rx_size = ((ord(substr($this->rx_buf,0,1))&0x7f) << 8 )+ord(substr($this->rx_buf,1,1));
$this->rx_ptr = 0;
Log::debug(sprintf('%s: - binkp_recv HEADER, is_msg [%d]',self::LOGKEY,$this->is_msg));
if ($this->DEBUG)
Log::debug(sprintf('%s: - binkp_recv HEADER, is_msg [%d], rx_size [%d]',self::LOGKEY,$this->is_msg,$this->rx_size));
if ($this->rx_size == 0)
goto ZeroLen;
@@ -367,24 +374,25 @@ final class Binkp extends BaseProtocol
/* Handle zero length block */
if ($this->rx_size == 0 ) {
Log::debug(sprintf('%s: - binkp_recv Zero length msg - dropped',self::LOGKEY));
Log::debug(sprintf('%s:- binkp_recv Zero length msg - dropped',self::LOGKEY));
$this->rx_size = -1;
$this->rx_ptr = 0;
$this->rx_buf = '';
return 1;
}
Log::debug(sprintf('%s: - binkp_recv BUFFER [%d]',self::LOGKEY,strlen($buf)));
Log::debug(sprintf('%s: - binkp_recv BUFFER [%d]',self::LOGKEY,strlen($this->rx_buf)));
$rc = ord(substr($buf,0,1));
$rc = ord(substr($this->rx_buf,0,1));
if ($rc > self::BPM_MAX) {
Log::error(sprintf('%s: ! binkp_recv Unknown Message [%s] (%d)',self::LOGKEY,$buf,strlen($buf)));
Log::error(sprintf('%s: ! binkp_recv Unknown Message [%s] (%d)',self::LOGKEY,$this->rx_buf,strlen($this->rx_buf)));
$rc = 1;
} else {
//DEBUG(('B',2,"rcvd %s '%s'%s",mess[rc],bp->rx_buf + 1,CRYPT(bps))); //@todo CRYPT
$data = substr($buf,1);
$data = substr($this->rx_buf,1);
switch ($rc) {
case self::M_ADR:
$rc = $this->M_adr($data);
@@ -435,7 +443,7 @@ final class Binkp extends BaseProtocol
} else {
if ($this->recv->fd) {
try {
$rc = $this->recv->write($buf);
$rc = $this->recv->write($this->rx_buf);
} catch (Exception $e) {
Log::error(sprintf('%s: ! %s',self::LOGKEY,$e->getMessage()));
@@ -448,8 +456,8 @@ final class Binkp extends BaseProtocol
if ($this->recv->filepos == $this->recv->size) {
Log::info(sprintf('%s: - Finished receiving file [%s] with size [%d]',self::LOGKEY,$this->recv->name,$this->recv->size));
$this->recv->close();
$this->msgs(self::BPM_GOT,$this->recv->name_size_time);
$this->recv->close();
$rc = 1;
}
@@ -463,6 +471,8 @@ final class Binkp extends BaseProtocol
$this->rx_size = -1;
}
$this->rx_buf = '';
} else {
$rc = 1;
}
@@ -585,7 +595,7 @@ final class Binkp extends BaseProtocol
*/
private function msgs(string $id,string $msg_body): void
{
Log::debug(sprintf('%s: msgs [%s]',self::LOGKEY,$msg_body));
Log::debug(sprintf('%s:+ msgs [%d:%s]',self::LOGKEY,$id,$msg_body));
$this->mqueue->push(new BinkpMessage($id,$msg_body));
@@ -1076,7 +1086,7 @@ final class Binkp extends BaseProtocol
$this->msgs(self::BPM_NUL,sprintf('TRF %lu %lu',$this->send->mail_size,$this->send->file_size));
Log::debug(sprintf('%s:= End',self::LOGKEY));
Log::debug(sprintf('%s:= M_ok',self::LOGKEY));
return $this->binkp_hsdone();
}
@@ -1252,6 +1262,9 @@ final class Binkp extends BaseProtocol
if ($rd && ! $this->binkp_recv())
break;
if ($this->DEBUG)
Log::debug(sprintf('%s: - mqueue [%d]',self::LOGKEY,$this->mqueue->count()));
if (($this->mqueue->count() || $wd) && ! $this->binkp_send() && (! $this->send->total_count))
break;
}