From a4fc285518aca84b174ef005da8aaa7c9a168ea1 Mon Sep 17 00:00:00 2001 From: henzeb Date: Mon, 3 Oct 2022 14:50:44 +0200 Subject: [PATCH] fixes #31 --- src/Connection.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index 2e6e2df..fc2b6ba 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -4,6 +4,7 @@ use Generator; use Socket; +use ErrorException; class Connection { @@ -55,7 +56,15 @@ public function write(string $payload): self $except = null; - $selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds); + try { + $selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds); + } catch (ErrorException $e) { + if ($this->isInterruptionErrorException()) { + continue; + } + + throw $e; + } if ($selectResult === false) { break; @@ -90,7 +99,15 @@ public function read(): Generator $except = null; - $selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds); + try { + $selectResult = socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds); + } catch (ErrorException $e) { + if ($this->isInterruptionErrorException()) { + continue; + } + + throw $e; + } if ($selectResult === false) { break; @@ -109,4 +126,9 @@ public function read(): Generator yield $outputFromSocket; } } + + private function isInterruptionErrorException(): bool + { + return 4 === socket_last_error(); + } }