Skip to content

Commit

Permalink
Fix connection closing
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Dec 9, 2023
1 parent 8f70ba6 commit 1a4ba72
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/Socket/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ public function __destruct()
} catch (\Throwable) {
// Do nothing.
} finally {
($this->onClose)();
isset($this->onClose) and ($this->onClose)();

Check failure on line 43 in src/Socket/Client.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

RedundantPropertyInitializationCheck

src/Socket/Client.php:43:13: RedundantPropertyInitializationCheck: Property $this->onClose with type Closure should already be set in the constructor (see https://psalm.dev/261)
}
}

public function close(): void
{
isset($this->onClose) and ($this->onClose)();

Check failure on line 49 in src/Socket/Client.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

RedundantPropertyInitializationCheck

src/Socket/Client.php:49:9: RedundantPropertyInitializationCheck: Property $this->onClose with type Closure should already be set in the constructor (see https://psalm.dev/261)
// Unlink all closures and free resources.
unset($this->onClose, $this->onPayload, $this->readBuffer);
}

public function disconnect(): void
{
$this->toDisconnect = true;
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 5 additions & 6 deletions src/Socket/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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]);

Check failure on line 89 in src/Socket/Server.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

PossiblyUndefinedVariable

src/Socket/Server.php:89:47: PossiblyUndefinedVariable: Possibly undefined variable $key defined in try block (see https://psalm.dev/018)

Check failure on line 89 in src/Socket/Server.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.2, OS ubuntu-latest)

PossiblyUndefinedVariable

src/Socket/Server.php:89:68: PossiblyUndefinedVariable: Possibly undefined variable $key defined in try block (see https://psalm.dev/018)
}
}
Expand All @@ -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]);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Socket/SocketStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down

0 comments on commit 1a4ba72

Please sign in to comment.