From 4a58bdcbdf15ff61d4544f3306bb2c284c5659fa Mon Sep 17 00:00:00 2001 From: divinity76 Date: Sun, 17 Mar 2024 14:44:10 +0100 Subject: [PATCH 1/9] Add getSocketResource method to enhance socket management This method is a prerequisite to eventually resolving https://github.com/chrome-php/chrome/issues/606 --- src/Client.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Client.php b/src/Client.php index 95d4b90..8a6ba0d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -286,4 +286,13 @@ protected function configure(array $options): void parent::configure($options); } + /** + * Retrieves the underlying socket resource. + * + * @return resource The socket resource + */ + public function getSocketResource() + { + return $this->socket->getResource(); + } } From cd622ec00a769305eb5c3aadc5a280b4c9cfa26e Mon Sep 17 00:00:00 2001 From: divinity76 Date: Sun, 17 Mar 2024 14:50:47 +0100 Subject: [PATCH 2/9] StyleCI --- src/Client.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Client.php b/src/Client.php index 8a6ba0d..84412d4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -286,6 +286,7 @@ protected function configure(array $options): void parent::configure($options); } + /** * Retrieves the underlying socket resource. * From e91c297a50a687330badaf0c05357a99534314d1 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Mon, 18 Mar 2024 11:24:03 +0100 Subject: [PATCH 3/9] public function waitForData --- src/Client.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Client.php b/src/Client.php index 84412d4..9e2d771 100644 --- a/src/Client.php +++ b/src/Client.php @@ -296,4 +296,28 @@ public function getSocketResource() { return $this->socket->getResource(); } + /** + * Waits for data to become available on the socket. + * + * @param float $maxSeconds The maximum amount of time to wait for data, in seconds. + * @return bool Returns true if data is available, false if the wait timed out. + */ + public function waitForData(float $maxSeconds): bool + { + $read = [$this->socket->getResource()]; + $write = null; + $except = null; + $seconds = (int)floor($maxSeconds); + $microseconds = (int)(($maxSeconds - $seconds) * 1e6); + $result = socket_select($read, $write, $except, $seconds, $microseconds); + if ($result === false) { + // An error occurred + throw new RuntimeException('Error occurred during socket_select.'); + } elseif ($result === 0) { + // Timeout occurred, no data available + return false; + } + // Data is available + return true; + } } From 2d732eba85b1a38dc681d48939e5a2dd34e61b66 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Mon, 18 Mar 2024 11:24:50 +0100 Subject: [PATCH 4/9] remove getSocketResource --- src/Client.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/Client.php b/src/Client.php index 9e2d771..ca081bd 100644 --- a/src/Client.php +++ b/src/Client.php @@ -287,15 +287,6 @@ protected function configure(array $options): void parent::configure($options); } - /** - * Retrieves the underlying socket resource. - * - * @return resource The socket resource - */ - public function getSocketResource() - { - return $this->socket->getResource(); - } /** * Waits for data to become available on the socket. * From f91f6a34d8be65f16509d40a71dd0b05b26fb7be Mon Sep 17 00:00:00 2001 From: divinity76 Date: Mon, 18 Mar 2024 11:29:16 +0100 Subject: [PATCH 5/9] StyleCI --- src/Client.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Client.php b/src/Client.php index ca081bd..1affff0 100644 --- a/src/Client.php +++ b/src/Client.php @@ -290,24 +290,26 @@ protected function configure(array $options): void /** * Waits for data to become available on the socket. * - * @param float $maxSeconds The maximum amount of time to wait for data, in seconds. - * @return bool Returns true if data is available, false if the wait timed out. + * @param float $maxSeconds the maximum amount of time to wait for data, in seconds + * + * @return bool returns true if data is available, false if the wait timed out */ public function waitForData(float $maxSeconds): bool { $read = [$this->socket->getResource()]; $write = null; $except = null; - $seconds = (int)floor($maxSeconds); - $microseconds = (int)(($maxSeconds - $seconds) * 1e6); - $result = socket_select($read, $write, $except, $seconds, $microseconds); - if ($result === false) { + $seconds = (int) \floor($maxSeconds); + $microseconds = (int) (($maxSeconds - $seconds) * 1e6); + $result = \socket_select($read, $write, $except, $seconds, $microseconds); + if (false === $result) { // An error occurred - throw new RuntimeException('Error occurred during socket_select.'); - } elseif ($result === 0) { + throw new \RuntimeException('Error occurred during socket_select.'); + } elseif (0 === $result) { // Timeout occurred, no data available return false; } + // Data is available return true; } From 39772a797be77bd1fc0d7f437c6e2477df4e80e3 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Mon, 18 Mar 2024 11:30:07 +0100 Subject: [PATCH 6/9] StyleCI --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 1affff0..0f6384c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -292,7 +292,7 @@ protected function configure(array $options): void * * @param float $maxSeconds the maximum amount of time to wait for data, in seconds * - * @return bool returns true if data is available, false if the wait timed out + * @return bool returns true if data is available, false if the wait timed out */ public function waitForData(float $maxSeconds): bool { From 79a3c8ce726c7dfc8ae8f4eef39f641503181203 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Mon, 18 Mar 2024 11:34:17 +0100 Subject: [PATCH 7/9] error message --- src/Client.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 0f6384c..8b1119a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -303,8 +303,9 @@ public function waitForData(float $maxSeconds): bool $microseconds = (int) (($maxSeconds - $seconds) * 1e6); $result = \socket_select($read, $write, $except, $seconds, $microseconds); if (false === $result) { - // An error occurred - throw new \RuntimeException('Error occurred during socket_select.'); + $errorcode = \socket_last_error($this->socket->getResource()); + $errormsg = \socket_strerror($errorcode); + throw new \RuntimeException("socket_select error $errorcode: $errormsg"); } elseif (0 === $result) { // Timeout occurred, no data available return false; From bfa13c4b309e3c5f2ae5a6d41699c2c9353f942e Mon Sep 17 00:00:00 2001 From: divinity76 Date: Mon, 18 Mar 2024 11:43:48 +0100 Subject: [PATCH 8/9] oops use stream_select not socket_select --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 8b1119a..f36ef00 100644 --- a/src/Client.php +++ b/src/Client.php @@ -301,7 +301,7 @@ public function waitForData(float $maxSeconds): bool $except = null; $seconds = (int) \floor($maxSeconds); $microseconds = (int) (($maxSeconds - $seconds) * 1e6); - $result = \socket_select($read, $write, $except, $seconds, $microseconds); + $result = \stream_select($read, $write, $except, $seconds, $microseconds); if (false === $result) { $errorcode = \socket_last_error($this->socket->getResource()); $errormsg = \socket_strerror($errorcode); From 799b8e0447bd1fa178e9e7030e1b0c932cd6f305 Mon Sep 17 00:00:00 2001 From: divinity76 Date: Mon, 18 Mar 2024 12:34:28 +0100 Subject: [PATCH 9/9] error handling --- src/Client.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Client.php b/src/Client.php index f36ef00..079254b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -292,9 +292,9 @@ protected function configure(array $options): void * * @param float $maxSeconds the maximum amount of time to wait for data, in seconds * - * @return bool returns true if data is available, false if the wait timed out + * @return ?bool Returns true if data is available, false if the wait timed out, and null on error. */ - public function waitForData(float $maxSeconds): bool + public function waitForData(float $maxSeconds): ?bool { $read = [$this->socket->getResource()]; $write = null; @@ -303,9 +303,8 @@ public function waitForData(float $maxSeconds): bool $microseconds = (int) (($maxSeconds - $seconds) * 1e6); $result = \stream_select($read, $write, $except, $seconds, $microseconds); if (false === $result) { - $errorcode = \socket_last_error($this->socket->getResource()); - $errormsg = \socket_strerror($errorcode); - throw new \RuntimeException("socket_select error $errorcode: $errormsg"); + // An error occurred. stream_select() probably triggered an error internally. + return null; } elseif (0 === $result) { // Timeout occurred, no data available return false;