Improve debugging and code optimisations for Socket operations

This commit is contained in:
2023-06-28 19:50:24 +10:00
parent ad36da0bb1
commit f9f9fb5345
2 changed files with 246 additions and 292 deletions

View File

@@ -22,26 +22,38 @@ final class SocketServer {
$this->port = $port;
$this->type = $type;
$this->_init();
}
$this->createSocket();
/**
* Bind to our Socket
*
* @throws SocketException
*/
private function _bindSocket(): void
{
if (socket_bind($this->server,$this->bind,$this->port) === FALSE)
throw new SocketException(SocketException::CANT_BIND_SOCKET,socket_strerror(socket_last_error($this->server)));
}
public function __get($key)
{
switch ($key) {
case 'handler':
return $this->handler;
default:
throw new \Exception('Unknown key: '.$key);
}
}
public function __set($key,$value)
{
switch ($key) {
case 'handler':
return $this->handler = $value;
default:
throw new \Exception('Unknown key: '.$key);
}
}
/**
* Create our Socket
*
* @throws SocketException
*/
private function _createSocket(): void
private function createSocket(): void
{
/**
* Check dependencies
@@ -71,17 +83,6 @@ final class SocketServer {
socket_set_option($this->server,SOL_SOCKET,SO_REUSEADDR,1);
}
/**
* Setup Socket and Bind
*
* @throws SocketException
*/
private function _init(): void
{
$this->_createSocket();
$this->_bindSocket();
}
/**
* Our main loop where we listen for connections
*
@@ -118,7 +119,7 @@ final class SocketServer {
*
* @throws SocketException
*/
private function loop_tcp()
private function loop_tcp(): void
{
while (TRUE) {
if (($accept = socket_accept($this->server)) === FALSE)
@@ -136,7 +137,7 @@ final class SocketServer {
}
}
private function loop_udp()
private function loop_udp(): void
{
while (TRUE) {
$r = new SocketClient($this->server);
@@ -149,14 +150,4 @@ final class SocketServer {
}
}
}
/**
* Set our connection handler Class and Method
*
* @param array $handler
*/
public function setConnectionHandler(array $handler): void
{
$this->handler = $handler;
}
}