From 1313eb755a7d0143ef377ab8ea558c75d963b89d Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 24 Sep 2020 21:00:49 +0200 Subject: [PATCH] Make closure a bit more reliable --- lib/Sync/ChannelledSocket.php | 21 +++++++++++---------- lib/functions.php | 1 + 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/Sync/ChannelledSocket.php b/lib/Sync/ChannelledSocket.php index 1084ef8..dd4c58e 100644 --- a/lib/Sync/ChannelledSocket.php +++ b/lib/Sync/ChannelledSocket.php @@ -28,6 +28,9 @@ final class ChannelledSocket implements Channel /** @var ResourceOutputStream */ private $write; + /** @var bool */ + private $closed = false; + /** @var int */ private $state = self::ESTABLISHED; @@ -56,7 +59,7 @@ final class ChannelledSocket implements Channel */ public function receive(): Promise { - if (!$this->channel) { + if ($this->closed) { return new Success(); } return call(function (): \Generator { @@ -85,24 +88,22 @@ final class ChannelledSocket implements Channel */ public function disconnect(): Promise { - if (!$this->channel) { + if ($this->closed) { return new Success(); } - $channel = $this->channel; - $this->channel = null; - return call(function () use ($channel): \Generator { - yield $channel->send(new ChannelCloseReq); - + $this->closed = true; + return call(function (): \Generator { + yield $this->channel->send(new ChannelCloseReq); if ($this->reading) { $this->closePromise = new Deferred; } do { - $data = yield ($this->closePromise ? $this->closePromise->promise() : $channel->receive()); + $data = yield ($this->closePromise ? $this->closePromise->promise() : $this->channel->receive()); if ($this->closePromise) { $this->closePromise = null; } if ($data instanceof ChannelCloseReq) { - yield $channel->send(new ChannelCloseAck); + yield $this->channel->send(new ChannelCloseAck); $this->state |= self::GOT_FIN_MASK; } elseif ($data instanceof ChannelCloseAck) { $this->state |= self::GOT_ACK_MASK; @@ -119,7 +120,7 @@ final class ChannelledSocket implements Channel */ public function send($data): Promise { - if (!$this->channel) { + if ($this->closed) { throw new ChannelException('The channel was already closed!'); } return $this->channel->send($data); diff --git a/lib/functions.php b/lib/functions.php index 97a234a..f608a54 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -18,6 +18,7 @@ function listen(string $uri): IpcServer { return new IpcServer($uri); } + /** * Connect to IPC server. *