Change DEBUG to a static const

This commit is contained in:
Deon George 2023-07-19 10:27:47 +10:00
parent ee03604de3
commit dcae06aad9
5 changed files with 48 additions and 48 deletions

View File

@ -14,7 +14,7 @@ use App\Models\{Address,Setup,System,SystemLog};
abstract class Protocol
{
// Enable extra debugging
protected bool $DEBUG = FALSE;
protected const DEBUG = FALSE;
private const LOGKEY = 'P--';

View File

@ -362,7 +362,7 @@ final class Binkp extends BaseProtocol
} catch (SocketException $e) {
if ($e->getCode() === 11) {
// @todo We maybe should count these and abort if there are too many?
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:- Got a socket EAGAIN',self::LOGKEY));
return TRUE;
@ -424,7 +424,7 @@ final class Binkp extends BaseProtocol
return TRUE;
}
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - binkp_recv BUFFER [%d]',self::LOGKEY,strlen($this->rx_buf)));
$msg = ord(substr($this->rx_buf,0,1));
@ -534,7 +534,7 @@ final class Binkp extends BaseProtocol
$rc = TRUE;
}
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:= binkp_recv [%d]',self::LOGKEY,$rc));
return $rc;

View File

@ -277,7 +277,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
*/
private function emsi_parsedat(string $str): int
{
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:+ emsi_parsedat',self::LOGKEY));
$l = 0;
@ -826,7 +826,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
*/
protected function protocol_init(): int
{
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:+ Starting EMSI Protocol INIT',self::LOGKEY));
$got = 0;
@ -852,7 +852,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
$t2 = $this->client->timer_set(self::EMSI_RESEND_TO);
while (TRUE) {
$ch = $this->client->read_ch(max( 1,min($this->client->timer_rest($t1),$this->client->timer_rest($t2))));
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:- Got [%x] (%c)',self::LOGKEY,$ch,$ch));
if (($ch != self::TIMEOUT) && ($ch < 0))
@ -918,7 +918,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
while (! $this->client->timer_expired($t1)) {
$ch = $this->client->read_ch(max( 1,min($this->client->timer_rest($t1),$this->client->timer_rest($t2))));
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:- Got [%x] (%c)',self::LOGKEY,$ch,$ch));
if (($ch != self::TIMEOUT) && ($ch < 0))

View File

@ -877,12 +877,12 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
case self::XOFF:
case self::XON|0x80:
case self::XOFF|0x80:
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zdonereceiver XON/XOFF, skip it',self::LOGKEY));
break;
case self::ZPAD:
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zdonereceiver ZPAD',self::LOGKEY));
if (($rc=$this->ls_zrecvhdr($this->ls_rxHdr,$this->ls_HeaderTimeout)) < 0)
@ -1085,7 +1085,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zrecvdata16(string &$data,int &$len,int $timeout): int
{
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:+ ls_zrecvdata16',self::LOGKEY),['d'=>$data]);
$got = 0; /* Bytes total got */
@ -1094,7 +1094,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$rcvdata = 1; /* Data is being received NOW (not CRC) */
while ($rcvdata && (($c = $this->ls_readzdle($timeout)) >= 0)) {
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zrecvdata16 got [%x] (%c)',self::LOGKEY,$c,($c<31 ? 32 : $c)),['c'=>serialize($c)]);
if ($c < 256) {
@ -1140,7 +1140,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$incrc = crc16($data.chr($frametype));
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zrecvdata16 CRC%d got %08x - calculated %08x',self::LOGKEY,16,$incrc,$crc));
if ($incrc != $crc)
@ -1148,7 +1148,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$len = $got;
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:= ls_zrecvdata16 - frametype [%d]',self::LOGKEY,$frametype));
return $frametype;
@ -1164,7 +1164,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zrecvdata32(string &$data,int &$len,int $timeout): int
{
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:+ ls_zrecvdata32',self::LOGKEY),['d'=>$data]);
$got = 0; /* Bytes total got */
@ -1173,7 +1173,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$rcvdata = 1; /* Data is being received NOW (not CRC) */
while ($rcvdata && (($c=$this->ls_readzdle($timeout)) >= 0)) {
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zrecvdata32 got [%x] (%c)',self::LOGKEY,$c,($c<31 ? 32 : $c)),['c'=>serialize($c)]);
if ($c < 256) {
@ -1225,7 +1225,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$crc |= ($c << 0x18);
$incrc = crc32($data.chr($frametype));
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zrecvdata32 CRC%d got %08x - calculated %08x',self::LOGKEY,32,$incrc,$crc));
if ($incrc != $crc)
@ -1233,7 +1233,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$len = $got;
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:= ls_zrecvdata32 - frametype [%d]',self::LOGKEY,$frametype));
return $frametype;
@ -1550,7 +1550,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zrecvhdr(array &$hdr,int $timeout): int
{
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:+ Receive header with [%d] timeout',self::LOGKEY,$timeout));
$state = self::rhInit;
@ -1567,7 +1567,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$c = -1;
if ($state === self::rhInit) {
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:- ls_zrecvhdr Init State',self::LOGKEY));
$frametype = self::ERROR;
@ -1581,7 +1581,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
}
while ($rc=$this->client->hasData($timeout)) {
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:- ls_zrecvhdr hasData - readmode [%d], garbage chars [%d], state [%d]',self::LOGKEY,$readmode,$garbage,$state));
switch ($readmode) {
@ -1602,7 +1602,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
}
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zrecvhdr [%x] (%c)',self::LOGKEY,$c,$c));
/* Here is error */
@ -1717,7 +1717,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
case self::rhBYTE:
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: / ls_zrecvhdr [%02x] (%d)',self::LOGKEY,$c,$got));
$hdr[$got] = $c;
@ -1730,7 +1730,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
break;
case self::rhCRC:
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: %% ls_zrecvhdr [%02x] (%d|%d)',self::LOGKEY,$c,$crcgot+1,$got));
if ($crcl === 2) {
@ -1763,7 +1763,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$this->ls_Protocol |= self::LSZ_OPTCRC32;
}
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:- ls_zrecvhdr CRC%d got [%08x] - calculated [%08x]',self::LOGKEY,(($crcl==2) ? 16 : 32),$incrc,$crc));
if ($incrc != $crc)
@ -1788,7 +1788,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* we need LF after <CR> */
case self::CR:
case self::CR|0x80:
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:? ls_zrecvhdr rhCR, ignoring any remaining chars [%x]',self::LOGKEY,$c));
/* At this point, most implementations ignore checking for the remaining chars */
@ -1802,7 +1802,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
case self::XON:
case self::XON|0x80:
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:? ls_zrecvhdr rhCR, got XON without CR/LF [%x]',self::LOGKEY,$c));
return $frametype;
@ -1833,7 +1833,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
case self::rhXON:
$state = self::rhInit;
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zrecvhdr rhXON',self::LOGKEY));
switch ($c) {
@ -2004,11 +2004,11 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
*/
private function ls_zsenddata(string $data,int $frame): int
{
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:+ ls_zsenddata [%d] (%d)',self::LOGKEY,strlen($data),$frame));
if ($this->ls_Protocol&self::LSZ_OPTCRC32) {
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zsenddata CRC32',self::LOGKEY));
for ($n=0;$n<strlen($data);$n++)
@ -2037,7 +2037,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
$crc >>= 8;
} else {
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zsenddata CRC16',self::LOGKEY));
for ($n=0;$n<strlen($data);$n++) {
@ -2255,7 +2255,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
case self::ZACK:
$this->ls_txLastACK = $this->ls_fetchlong($this->ls_rxHdr);
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s: - ls_zsendfile ZACK',self::LOGKEY),['ls_rxHdr'=>$this->ls_rxHdr,'ls_txLastACK'=>$this->ls_txLastACK,'ls_rxHdr'=>$this->ls_rxHdr]);
break;
@ -2522,7 +2522,7 @@ final class Zmodem extends Protocol implements CRCInterface,ZmodemInterface
/* Send HEX header. Use CRC16, send var. len. if could */
private function ls_zsendhhdr(int $frametype,array $hdr): int
{
if ($this->DEBUG)
if (static::DEBUG)
Log::debug(sprintf('%s:+ ls_zsendhhdr',self::LOGKEY));
/* Send **<DLE> */

View File

@ -17,7 +17,7 @@ final class SocketClient {
private const LOGKEY = 'SC-';
// For deep debugging
private bool $DEBUG = TRUE;
private const DEBUG = FALSE;
private \Socket $connection;
private string $address_local = '';
@ -162,11 +162,11 @@ final class SocketClient {
$num_bytes = strlen($data);
while ($num_bytes) {
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:- To add [%d] to the TX buffer',self::LOGKEY,$num_bytes));
if ($num_bytes > $this->tx_free) {
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:- TX buffer will be too full, draining...',self::LOGKEY));
do {
@ -185,7 +185,7 @@ final class SocketClient {
}
}
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:= TX buffer has [%d] space left',self::LOGKEY,$this->tx_free));
}
@ -198,7 +198,7 @@ final class SocketClient {
*/
public function buffer_flush(int $timeout): int
{
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:+ Emptying TX buffer with [%d] chars, and timeout [%d]',self::LOGKEY,strlen($this->tx_buf),$timeout));
$tm = $this->timer_set($timeout);
@ -208,12 +208,12 @@ final class SocketClient {
$tv = $this->timer_rest($tm);
if (($rc=$this->canSend($tv)) > 0) {
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:- Chars to send [%d]',self::LOGKEY,strlen($this->tx_buf)));
$sent = $this->send(substr($this->tx_buf,0,self::TX_SIZE),0);
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:- Sent [%d] chars [%s]',self::LOGKEY,$sent,Str::limit($this->tx_buf,15)));
$this->tx_buf = substr($this->tx_buf,$sent);
@ -291,7 +291,7 @@ final class SocketClient {
{
// We have data in our buffer
if ($this->rx_left >= $len) {
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:- Returning [%d] chars from the RX buffer',self::LOGKEY,$len));
$result = substr($this->rx_buf,0,$len);
@ -329,7 +329,7 @@ final class SocketClient {
if ($recv === FALSE) {
// If we have something in the buffer, we'll send it
if ($this->rx_left) {
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:- Network read return an error, returning final [%d] chars from the RX buffer',self::LOGKEY,strlen($this->rx_buf)));
$result = $this->rx_buf;
@ -355,7 +355,7 @@ final class SocketClient {
$this->rx_buf .= $buf;
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:- Added [%d] chars to the RX buffer',self::LOGKEY,strlen($buf)),['rx_buf'=>hex_dump($this->rx_buf)]);
// Loop again and return the data, now that it is in the RX buffer
@ -384,7 +384,7 @@ final class SocketClient {
public function rx_purge(): void
{
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:+ Discarding [%d] chars from the RX buffer',self::LOGKEY,strlen($this->tx_buf)));
$this->rx_buf = '';
@ -395,7 +395,7 @@ final class SocketClient {
*/
public function tx_purge(): void
{
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:+ Discarding [%d] chars from the TX buffer',self::LOGKEY,strlen($this->tx_buf)));
$this->tx_buf = '';
@ -414,7 +414,7 @@ final class SocketClient {
if ($timeout AND (! $rc=$this->canSend($timeout)))
return $rc;
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:- Sending [%d] chars [%s]',self::LOGKEY,strlen($message),Str::limit($message,15)));
switch ($this->type) {
@ -446,7 +446,7 @@ final class SocketClient {
if ($rc === FALSE)
throw new \Exception('Socket Error: '.socket_strerror(socket_last_error()));
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:= Socket select returned [%d] with timeout (%d)',self::LOGKEY,$rc,$timeout),['read'=>$read,'write'=>$write,'except'=>$except]);
return $rc;
@ -480,7 +480,7 @@ final class SocketClient {
public function ttySelect(bool $read,bool $write, int $timeout): int
{
if ($this->rx_left) {
if ($this->DEBUG)
if (self::DEBUG)
Log::debug(sprintf('%s:= We still have [%d] chars in the RX buffer.',self::LOGKEY,$this->rx_left));
return 1;