From 060aeacdb18b8d0d40fff5223ff96a2fe26d6088 Mon Sep 17 00:00:00 2001 From: Martynas Kasparavicius Date: Fri, 6 Sep 2024 19:52:13 +0300 Subject: [PATCH] Stream Socket readChunk timeout of 5 minutes --- src/IO/StreamSocket.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/IO/StreamSocket.php b/src/IO/StreamSocket.php index 6276960..5c1706b 100644 --- a/src/IO/StreamSocket.php +++ b/src/IO/StreamSocket.php @@ -60,6 +60,11 @@ class StreamSocket extends AbstractIO private $configuration; + /** + * @var null|float + */ + private $lastRead = null; + /** * @param string $host * @param int $port @@ -175,6 +180,19 @@ public function read($n) public function readChunk($l = 8192) { $data = stream_socket_recvfrom($this->sock, $l); + if (empty($data)) { + if ($this->lastRead === null) { + $this->lastRead = microtime(true); + } else { + $timeSinceLastRead = microtime(true) - $this->lastRead; + // 5 minutes + if ($timeSinceLastRead > 60 * 5) { + throw new IOException('Timeout reached'); + } + } + } else { + $this->lastRead = null; + } //echo Helper::prettyHex($data); return $data;