From 6c5a7dbdcbffaf3b031723ff1fe24891dfd03a88 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 28 Oct 2019 21:28:38 +0100 Subject: [PATCH] Draft client/server socket without functionality --- Socket/Client/Client.php | 14 +++++++++++++- Socket/Server/Server.php | 14 ++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Socket/Client/Client.php b/Socket/Client/Client.php index 8c50d7d0b..76f58040b 100644 --- a/Socket/Client/Client.php +++ b/Socket/Client/Client.php @@ -72,6 +72,8 @@ class Client extends SocketAbstract \socket_connect($this->sock, $this->ip, $this->port); $i = 0; + $errorCounter = 0; + while ($this->run) { try { ++$i; @@ -80,6 +82,12 @@ class Client extends SocketAbstract $read = [$this->sock]; + if (\socket_last_error() !== 0) { + ++$errorCounter; + } + + // todo: create reset condition for errorCounter. Probably if a successfull read happened + //if (socket_select($read, $write = null, $except = null, 0) < 1) { // error // socket_last_error(); @@ -102,7 +110,11 @@ class Client extends SocketAbstract $this->commands->trigger($data[0], 0, $data); } } - } catch (\Error $e) { + + if ($errorCounter > 10) { + $this->run = false; + } + } catch (\Throwable $e) { $this->run = false; } } diff --git a/Socket/Server/Server.php b/Socket/Server/Server.php index e77e4373a..ea302ab78 100644 --- a/Socket/Server/Server.php +++ b/Socket/Server/Server.php @@ -128,13 +128,13 @@ class Server extends SocketAbstract public function handshake($client, $headers) : bool { // todo: different handshake for normal tcp connection - //return true; + return true; if (\preg_match("/Sec-WebSocket-Version: (.*)\r\n/", $headers, $match) === false) { return false; } - $version = (int) $match[1]; + $version = (int) ($match[1] ?? -1); if ($version !== 13) { return false; @@ -176,8 +176,8 @@ class Server extends SocketAbstract public function run() : void { $this->app->logger->info('Start listening...'); - \socket_listen($this->sock); - \socket_set_nonblock($this->sock); + @\socket_listen($this->sock); + @\socket_set_nonblock($this->sock); $this->conn[] = $this->sock; $this->app->logger->info('Start running...'); @@ -196,11 +196,12 @@ class Server extends SocketAbstract foreach ($read as $key => $socket) { if ($this->sock === $socket) { - $newc = \socket_accept($this->sock); + $newc = @\socket_accept($this->sock); $this->connectClient($newc); } else { $client = $this->clientManager->getBySocket($socket); - $data = \socket_read($socket, 1024); + $data = @\socket_read($socket, 1024, \PHP_NORMAL_READ); + var_dump($data); if (!$client->getHandshake()) { $this->app->logger->debug('Doing handshake...'); @@ -211,6 +212,7 @@ class Server extends SocketAbstract $this->disconnectClient($client); } } else { + // todo: maybe implement shutdown and help for testing? $this->packetManager->handle($this->unmask($data), $client); } }