From 1a4ba7259fe3c01cb7e06f1f668afe1a62ee04ed Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sun, 10 Dec 2023 02:12:00 +0400 Subject: [PATCH] Fix connection closing --- src/Socket/Client.php | 11 +++++++++-- src/Socket/Server.php | 11 +++++------ src/Socket/SocketStream.php | 3 ++- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Socket/Client.php b/src/Socket/Client.php index d90523d6..ac7d7551 100644 --- a/src/Socket/Client.php +++ b/src/Socket/Client.php @@ -40,10 +40,17 @@ public function __destruct() } catch (\Throwable) { // Do nothing. } finally { - ($this->onClose)(); + isset($this->onClose) and ($this->onClose)(); } } + public function close(): void + { + isset($this->onClose) and ($this->onClose)(); + // Unlink all closures and free resources. + unset($this->onClose, $this->onPayload, $this->readBuffer); + } + public function disconnect(): void { $this->toDisconnect = true; @@ -86,7 +93,7 @@ public function process(): void throw new \RuntimeException('Socket exception.'); } - if ($this->toDisconnect && $this->writeQueue !== []) { + if ($this->toDisconnect && $this->writeQueue === []) { throw new DisconnectClient(); } Fiber::suspend(); diff --git a/src/Socket/Server.php b/src/Socket/Server.php index e53cb165..8ead496e 100644 --- a/src/Socket/Server.php +++ b/src/Socket/Server.php @@ -54,7 +54,7 @@ public function __destruct() \socket_close($this->socket); } finally { foreach ($this->clients as $client) { - $client->__destruct(); + $client->close(); } unset($this->socket, $this->clients, $this->fibers); } @@ -76,17 +76,16 @@ public static function init( public function process(): void { - $socket = @\socket_accept($this->socket); - if ($socket !== false) { - $key = (int)\array_key_last($this->clients) + 1; + while (false !== ($socket = \socket_accept($this->socket))) { $client = null; try { $client = Client::init($socket, $this->payloadSize); + $key = (int)\array_key_last($this->clients) + 1; $this->clients[$key] = $client; $this->clientInflector !== null and ($this->clientInflector)($client, $key); $this->fibers[$key] = new Fiber($client->process(...)); } catch (\Throwable) { - $client?->__destruct(); + $client?->close(); unset($client, $this->clients[$key], $this->fibers[$key]); } } @@ -102,7 +101,7 @@ public function process(): void if ($e instanceof DisconnectClient) { $this->logger->info('Custom disconnect.'); } - $this->clients[$key]->__destruct(); + $this->clients[$key]->close(); // Logger::exception($e, 'Client fiber.'); unset($this->clients[$key], $this->fibers[$key]); } diff --git a/src/Socket/SocketStream.php b/src/Socket/SocketStream.php index aca31bfd..d730102e 100644 --- a/src/Socket/SocketStream.php +++ b/src/Socket/SocketStream.php @@ -26,7 +26,7 @@ final class SocketStream implements IteratorAggregate, StreamClient private readonly DateTimeImmutable $createdAt; private function __construct( - private readonly Client $client, + private Client $client, public readonly int $clientId, ) { $this->queue = new \SplQueue(); @@ -41,6 +41,7 @@ public static function create(Client $client, int $id): self }); $client->setOnClose(function () use ($self): void { $self->disconnected = true; + unset($self->client); }); return $self;