From 810e6205263eedfec2bea1424c7666a2b843f465 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 10 Nov 2024 13:34:01 +1100 Subject: [PATCH] SocketClient change has.. can.. functions to return boolean and other minor cosmetic changes --- .../Sock/Exception/SocketException.php | 2 + app/Classes/Sock/SocketClient.php | 83 ++++++++----------- 2 files changed, 38 insertions(+), 47 deletions(-) diff --git a/app/Classes/Sock/Exception/SocketException.php b/app/Classes/Sock/Exception/SocketException.php index 6e8aa03..ca1c992 100644 --- a/app/Classes/Sock/Exception/SocketException.php +++ b/app/Classes/Sock/Exception/SocketException.php @@ -11,6 +11,7 @@ final class SocketException extends \Exception { public const CANT_CONNECT = 5; public const SOCKET_ERROR = 6; public const SOCKET_EAGAIN = 11; + public const SOCKET_TIMEOUT = 15; public const SOCKET_READ = 22; public const CONNECTION_RESET = 104; @@ -22,6 +23,7 @@ final class SocketException extends \Exception { self::CANT_CONNECT => 'Can\'t connect: "%s"', self::SOCKET_ERROR => 'Socket Error: "%s"', self::SOCKET_EAGAIN => 'Socket Resource Temporarily Unavailable - Try again', + self::SOCKET_TIMEOUT => 'Timeout reached "%d"', self::SOCKET_READ => 'Unable to read from socket', self::CONNECTION_RESET => 'Connection reset by peer', ]; diff --git a/app/Classes/Sock/SocketClient.php b/app/Classes/Sock/SocketClient.php index b2b38b6..e93e76f 100644 --- a/app/Classes/Sock/SocketClient.php +++ b/app/Classes/Sock/SocketClient.php @@ -48,7 +48,8 @@ final class SocketClient { /** @var string Data in the RX buffer */ private string $rx_buf = ''; - public function __construct (\Socket $connection,bool $originate=FALSE) { + public function __construct (\Socket $connection,bool $originate=FALSE) + { $this->connection = $connection; if ($this->type === SOCK_STREAM) { @@ -191,38 +192,26 @@ final class SocketClient { } } - public function __get($key) { - switch ($key) { - case 'address_remote': - case 'port_remote': - return $this->{$key}; - - case 'cps': - case 'speed': - return Arr::get($this->session,$key); - - case 'rx_free': - return self::RX_BUF_SIZE-$this->rx_left; - - case 'rx_left': - return strlen($this->rx_buf); - - case 'tx_free': - return self::TX_BUF_SIZE-strlen($this->tx_buf); - - case 'type': - return socket_get_option($this->connection,SOL_SOCKET,SO_TYPE); - - default: - throw new \Exception(sprintf('%s:! Unknown key [%s]:',self::LOGKEY,$key)); - } + public function __get(string $key): mixed + { + return match ($key) { + 'address_remote', 'port_remote' => $this->{$key}, + 'cps', 'speed' => Arr::get($this->session,$key), + 'rx_free' => self::RX_BUF_SIZE-$this->rx_left, + 'rx_left' => strlen($this->rx_buf), + 'tx_free' => self::TX_BUF_SIZE-strlen($this->tx_buf), + 'type' => socket_get_option($this->connection,SOL_SOCKET,SO_TYPE), + default => throw new \Exception(sprintf('%s:! Unknown key [%s]:',self::LOGKEY, $key)), + }; } - public function __set($key,$value) { + public function __set(string $key,mixed $value): void + { switch ($key) { case 'cps': case 'speed': - return $this->session[$key] = $value; + $this->session[$key] = $value; + break; default: throw new \Exception(sprintf('%s:! Unknown key [%s]:',self::LOGKEY,$key)); @@ -350,7 +339,7 @@ final class SocketClient { while (strlen($this->tx_buf)) { $tv = $this->timer_rest($tm); - if (($rc=$this->canSend($tv)) > 0) { + if ($rc=$this->canSend($tv)) { if (self::DEBUG) Log::debug(sprintf('%s:- Chars to send [%d]',self::LOGKEY,strlen($this->tx_buf))); @@ -378,14 +367,14 @@ final class SocketClient { /** * @param int $timeout - * @return int + * @return bool * @throws \Exception */ - public function canSend(int $timeout): int + public function canSend(int $timeout): bool { $write = [$this->connection]; - return $this->socketSelect(NULL,$write,NULL,$timeout); + return $this->socketSelect(NULL,$write,NULL,$timeout) > 0; } /** @@ -412,14 +401,14 @@ final class SocketClient { * We have data in the buffer or on the socket * * @param int $timeout - * @return int + * @return bool * @throws \Exception */ - public function hasData(int $timeout): int + public function hasData(int $timeout): bool { $read = [$this->connection]; - return $this->rx_left ?: $this->socketSelect($read,NULL,NULL,$timeout); + return ($this->rx_left ?: $this->socketSelect($read,NULL,NULL,$timeout)) > 0; } /** @@ -427,10 +416,11 @@ final class SocketClient { * * @param int $timeout How long to wait for data * @param int $len The amount of data we want + * @param int $flags * @return string|null * @throws SocketException */ - public function read(int $timeout,int $len=1024): ?string + public function read(int $timeout,int $len=1024,int $flags=MSG_DONTWAIT): ?string { // We have data in our buffer if ($this->rx_left >= $len) { @@ -443,19 +433,19 @@ final class SocketClient { return $result; } - if ($timeout AND ($this->hasData($timeout) === 0)) - return NULL; + if ($timeout && (! $this->hasData($timeout))) + throw new SocketException(SocketException::SOCKET_TIMEOUT,$timeout); $buf = ''; try { switch ($this->type) { case SOCK_STREAM: - $recv = socket_recv($this->connection,$buf,self::RX_SIZE,MSG_DONTWAIT); + $recv = socket_recv($this->connection,$buf,self::RX_SIZE,$flags); break; case SOCK_DGRAM: - $recv = socket_recvfrom($this->connection,$buf,self::RX_SIZE,MSG_DONTWAIT,$this->address_remote,$this->port_remote); + $recv = socket_recvfrom($this->connection,$buf,self::RX_SIZE,$flags,$this->address_remote,$this->port_remote); break; default: @@ -515,12 +505,11 @@ final class SocketClient { */ public function read_ch(int $timeout): int { - if ($this->hasData($timeout) > 0) { + if ($this->hasData($timeout)) $ch = $this->read($timeout,1); - } else { - return self::TIMEOUT; - } + else + throw new SocketException(SocketException::SOCKET_TIMEOUT,$timeout); return ord($ch); } @@ -549,12 +538,12 @@ final class SocketClient { * * @param string $message * @param int $timeout - * @return int|false + * @return int|bool * @throws \Exception */ - public function send(string $message,int $timeout): int|false + public function send(string $message,int $timeout): int|bool { - if ($timeout AND (! $rc=$this->canSend($timeout))) + if ($timeout && (! $rc=$this->canSend($timeout))) return $rc; if (self::DEBUG)