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

@@ -14,6 +14,8 @@ use Illuminate\Support\Str;
* @property int speed
*/
final class SocketClient {
private const LOGKEY = 'SC-';
// For deep debugging
private bool $DEBUG = FALSE;
@@ -53,10 +55,10 @@ final class SocketClient {
private int $rx_left = 0;
private string $rx_buf = '';
public function __construct (\Socket $connection,int $speed=self::TCP_SPEED) {
public function __construct (\Socket $connection) {
socket_getsockname($connection,$this->address_local,$this->port_local);
socket_getpeername($connection,$this->address_remote,$this->port_remote);
Log::info(sprintf('%s: + Connection from [%s] on port [%d]',__METHOD__,$this->address_remote,$this->port_remote));
Log::info(sprintf('%s:+ Connection from [%s] on port [%d]',self::LOGKEY,$this->address_remote,$this->port_remote));
$this->connection = $connection;
}
@@ -72,7 +74,7 @@ final class SocketClient {
return Arr::get($this->session,$key);
default:
throw new \Exception(sprintf('%s: Unknown key [%s]:',__METHOD__,$key));
throw new \Exception(sprintf('%s:! Unknown key [%s]:',self::LOGKEY,$key));
}
}
@@ -83,7 +85,7 @@ final class SocketClient {
return $this->session[$key] = $value;
default:
throw new \Exception(sprintf('%s: Unknown key [%s]:',__METHOD__,$key));
throw new \Exception(sprintf('%s:! Unknown key [%s]:',self::LOGKEY,$key));
}
}
@@ -97,7 +99,7 @@ final class SocketClient {
public function buffer_add(string $data): void
{
if ($this->DEBUG)
Log::debug(sprintf('%s: + Start [%s] (%d)',__METHOD__,$data,strlen($data)));
Log::debug(sprintf('%s:+ Start [%s] (%d)',self::LOGKEY,$data,strlen($data)));
//$rc = self::OK;
//$tx_ptr = self::TX_BUF_SIZE-$this->tx_free;
@@ -107,7 +109,7 @@ final class SocketClient {
while ($num_bytes) {
if ($this->DEBUG)
Log::debug(sprintf('%s: - Num Bytes [%d]: TX Free [%d]',__METHOD__,$num_bytes,$this->tx_free));
Log::debug(sprintf('%s: - Num Bytes [%d]: TX Free [%d]',self::LOGKEY,$num_bytes,$this->tx_free));
if ($num_bytes > $this->tx_free) {
do {
@@ -125,7 +127,7 @@ final class SocketClient {
} else {
if ($this->DEBUG)
Log::debug(sprintf('%s: - Remaining data to send [%d]',__METHOD__,$num_bytes));
Log::debug(sprintf('%s: - Remaining data to send [%d]',self::LOGKEY,$num_bytes));
$this->tx_buf .= substr($data,$ptr,$num_bytes);
$this->tx_free -= $num_bytes;
@@ -134,7 +136,7 @@ final class SocketClient {
}
if ($this->DEBUG)
Log::debug(sprintf('%s: = End [%s]',__METHOD__,strlen($this->tx_buf)));
Log::debug(sprintf('%s:= End [%s]',self::LOGKEY,strlen($this->tx_buf)));
}
/**
@@ -156,7 +158,7 @@ final class SocketClient {
public function buffer_flush(int $timeout): int
{
if ($this->DEBUG)
Log::debug(sprintf('%s: + Start [%d]',__METHOD__,$timeout));
Log::debug(sprintf('%s:+ Start [%d]',self::LOGKEY,$timeout));
$rc = self::OK;
$tx_ptr = 0;
@@ -168,9 +170,9 @@ final class SocketClient {
if ($rc = $this->canSend($tv)>0) {
if ($this->DEBUG)
Log::debug(sprintf('%s: - Sending [%d]',__METHOD__,$restsize));
Log::debug(sprintf('%s: - Sending [%d]',self::LOGKEY,$restsize));
$rc = $this->send(substr($this->tx_buf,$tx_ptr,$restsize),0);
Log::debug(sprintf('%s: - Sent [%d] (%s)',__METHOD__,$rc,Str::limit($this->tx_buf,15)));
Log::debug(sprintf('%s: - Sent [%d] (%s)',self::LOGKEY,$rc,Str::limit($this->tx_buf,15)));
if ($rc == $restsize) {
$this->tx_buf = '';
@@ -194,7 +196,7 @@ final class SocketClient {
}
if ($this->DEBUG)
Log::debug(sprintf('%s: = End [%d]',__METHOD__,$rc));
Log::debug(sprintf('%s:= End [%d]',self::LOGKEY,$rc));
return $rc;
}
@@ -230,7 +232,7 @@ final class SocketClient {
*/
public static function create(string $address,int $port,int $speed=self::TCP_SPEED): self
{
Log::debug(sprintf('%s: + Creating connection to [%s:%d]',__METHOD__,$address,$port));
Log::debug(sprintf('%s:+ Creating connection to [%s:%d]',self::LOGKEY,$address,$port));
$address = gethostbyname($address);
@@ -271,7 +273,7 @@ final class SocketClient {
public function read(int $timeout,int $len=1024)
{
if ($this->DEBUG)
Log::debug(sprintf('%s: + Start [%d] (%d)',__METHOD__,$len,$timeout));
Log::debug(sprintf('%s:+ Start [%d] (%d)',self::LOGKEY,$len,$timeout));
if ($timeout AND ($this->hasData($timeout) === 0))
return '';
@@ -279,7 +281,7 @@ final class SocketClient {
$buf = '';
$rc = socket_recv($this->connection,$buf, $len,MSG_DONTWAIT);
if ($this->DEBUG)
Log::debug(sprintf('%s: - Read [%d]',__METHOD__,$rc));
Log::debug(sprintf('%s: - Read [%d]',self::LOGKEY,$rc));
if ($rc === FALSE)
throw new SocketException($x=socket_last_error($this->connection),socket_strerror($x));
@@ -309,7 +311,7 @@ final class SocketClient {
public function read_ch(int $timeout): int
{
if ($this->DEBUG)
Log::debug(sprintf('%s: + Start [%d]',__METHOD__,$timeout),['rx_left'=>$this->rx_left,'rx_ptr'=>$this->rx_ptr]);
Log::debug(sprintf('%s:+ Start [%d] - rx_left[%d], rx_ptr[%d]',self::LOGKEY,$timeout,$this->rx_left,$this->rx_ptr));
// If our buffer is empty, we'll try and read from the remote
if ($this->rx_left == 0) {
@@ -317,7 +319,7 @@ final class SocketClient {
try {
if (! strlen($this->rx_buf = $this->read(0,self::RX_BUF_SIZE))) {
if ($this->DEBUG)
Log::debug(sprintf('%s: - Nothing read',__METHOD__));
Log::debug(sprintf('%s: - Nothing read',self::LOGKEY));
return self::TTY_TIMEOUT;
}
@@ -327,7 +329,7 @@ final class SocketClient {
}
if ($this->DEBUG)
Log::info(sprintf('%s: - Read [%d] bytes',__METHOD__,strlen($this->rx_buf)));
Log::info(sprintf('%s: - Read [%d] bytes',self::LOGKEY,strlen($this->rx_buf)));
$this->rx_ptr = 0;
$this->rx_left = strlen($this->rx_buf);
@@ -343,7 +345,7 @@ final class SocketClient {
$this->rx_ptr++;
if ($this->DEBUG)
Log::debug(sprintf('%s: = Return [%x] (%c)',__METHOD__,$rc,$rc));
Log::debug(sprintf('%s:= Return [%x] (%c)',self::LOGKEY,$rc,$rc));
return $rc;
}