SocketClient change has.. can.. functions to return boolean and other minor cosmetic changes

This commit is contained in:
Deon George 2024-11-10 13:34:01 +11:00
parent 6c36f7f9aa
commit 810e620526
2 changed files with 38 additions and 47 deletions

View File

@ -11,6 +11,7 @@ final class SocketException extends \Exception {
public const CANT_CONNECT = 5; public const CANT_CONNECT = 5;
public const SOCKET_ERROR = 6; public const SOCKET_ERROR = 6;
public const SOCKET_EAGAIN = 11; public const SOCKET_EAGAIN = 11;
public const SOCKET_TIMEOUT = 15;
public const SOCKET_READ = 22; public const SOCKET_READ = 22;
public const CONNECTION_RESET = 104; public const CONNECTION_RESET = 104;
@ -22,6 +23,7 @@ final class SocketException extends \Exception {
self::CANT_CONNECT => 'Can\'t connect: "%s"', self::CANT_CONNECT => 'Can\'t connect: "%s"',
self::SOCKET_ERROR => 'Socket Error: "%s"', self::SOCKET_ERROR => 'Socket Error: "%s"',
self::SOCKET_EAGAIN => 'Socket Resource Temporarily Unavailable - Try again', self::SOCKET_EAGAIN => 'Socket Resource Temporarily Unavailable - Try again',
self::SOCKET_TIMEOUT => 'Timeout reached "%d"',
self::SOCKET_READ => 'Unable to read from socket', self::SOCKET_READ => 'Unable to read from socket',
self::CONNECTION_RESET => 'Connection reset by peer', self::CONNECTION_RESET => 'Connection reset by peer',
]; ];

View File

@ -48,7 +48,8 @@ final class SocketClient {
/** @var string Data in the RX buffer */ /** @var string Data in the RX buffer */
private string $rx_buf = ''; private string $rx_buf = '';
public function __construct (\Socket $connection,bool $originate=FALSE) { public function __construct (\Socket $connection,bool $originate=FALSE)
{
$this->connection = $connection; $this->connection = $connection;
if ($this->type === SOCK_STREAM) { if ($this->type === SOCK_STREAM) {
@ -191,38 +192,26 @@ final class SocketClient {
} }
} }
public function __get($key) { public function __get(string $key): mixed
switch ($key) { {
case 'address_remote': return match ($key) {
case 'port_remote': 'address_remote', 'port_remote' => $this->{$key},
return $this->{$key}; 'cps', 'speed' => Arr::get($this->session,$key),
'rx_free' => self::RX_BUF_SIZE-$this->rx_left,
case 'cps': 'rx_left' => strlen($this->rx_buf),
case 'speed': 'tx_free' => self::TX_BUF_SIZE-strlen($this->tx_buf),
return Arr::get($this->session,$key); 'type' => socket_get_option($this->connection,SOL_SOCKET,SO_TYPE),
default => throw new \Exception(sprintf('%s:! Unknown key [%s]:',self::LOGKEY, $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 __set($key,$value) { public function __set(string $key,mixed $value): void
{
switch ($key) { switch ($key) {
case 'cps': case 'cps':
case 'speed': case 'speed':
return $this->session[$key] = $value; $this->session[$key] = $value;
break;
default: default:
throw new \Exception(sprintf('%s:! Unknown key [%s]:',self::LOGKEY,$key)); throw new \Exception(sprintf('%s:! Unknown key [%s]:',self::LOGKEY,$key));
@ -350,7 +339,7 @@ final class SocketClient {
while (strlen($this->tx_buf)) { while (strlen($this->tx_buf)) {
$tv = $this->timer_rest($tm); $tv = $this->timer_rest($tm);
if (($rc=$this->canSend($tv)) > 0) { if ($rc=$this->canSend($tv)) {
if (self::DEBUG) if (self::DEBUG)
Log::debug(sprintf('%s:- Chars to send [%d]',self::LOGKEY,strlen($this->tx_buf))); Log::debug(sprintf('%s:- Chars to send [%d]',self::LOGKEY,strlen($this->tx_buf)));
@ -378,14 +367,14 @@ final class SocketClient {
/** /**
* @param int $timeout * @param int $timeout
* @return int * @return bool
* @throws \Exception * @throws \Exception
*/ */
public function canSend(int $timeout): int public function canSend(int $timeout): bool
{ {
$write = [$this->connection]; $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 * We have data in the buffer or on the socket
* *
* @param int $timeout * @param int $timeout
* @return int * @return bool
* @throws \Exception * @throws \Exception
*/ */
public function hasData(int $timeout): int public function hasData(int $timeout): bool
{ {
$read = [$this->connection]; $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 $timeout How long to wait for data
* @param int $len The amount of data we want * @param int $len The amount of data we want
* @param int $flags
* @return string|null * @return string|null
* @throws SocketException * @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 // We have data in our buffer
if ($this->rx_left >= $len) { if ($this->rx_left >= $len) {
@ -443,19 +433,19 @@ final class SocketClient {
return $result; return $result;
} }
if ($timeout AND ($this->hasData($timeout) === 0)) if ($timeout && (! $this->hasData($timeout)))
return NULL; throw new SocketException(SocketException::SOCKET_TIMEOUT,$timeout);
$buf = ''; $buf = '';
try { try {
switch ($this->type) { switch ($this->type) {
case SOCK_STREAM: 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; break;
case SOCK_DGRAM: 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; break;
default: default:
@ -515,12 +505,11 @@ final class SocketClient {
*/ */
public function read_ch(int $timeout): int public function read_ch(int $timeout): int
{ {
if ($this->hasData($timeout) > 0) { if ($this->hasData($timeout))
$ch = $this->read($timeout,1); $ch = $this->read($timeout,1);
} else { else
return self::TIMEOUT; throw new SocketException(SocketException::SOCKET_TIMEOUT,$timeout);
}
return ord($ch); return ord($ch);
} }
@ -549,12 +538,12 @@ final class SocketClient {
* *
* @param string $message * @param string $message
* @param int $timeout * @param int $timeout
* @return int|false * @return int|bool
* @throws \Exception * @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; return $rc;
if (self::DEBUG) if (self::DEBUG)