From a3f83bbcca6949b37adc6240c975f0700d944e37 Mon Sep 17 00:00:00 2001 From: Michal Stefanak Date: Wed, 3 Apr 2024 18:46:30 +0200 Subject: [PATCH] throw extra exception when reading empty buffer after timeout --- src/connection/Socket.php | 3 +++ src/connection/StreamSocket.php | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/connection/Socket.php b/src/connection/Socket.php index b343c9b..3f6f4c7 100644 --- a/src/connection/Socket.php +++ b/src/connection/Socket.php @@ -75,7 +75,10 @@ public function read(int $length = 2048): string throw new ConnectException('Not initialized socket'); $output = ''; + $t = microtime(true); do { + if (mb_strlen($output, '8bit') == 0 && $this->timeout > 0 && (microtime(true) - $t) >= $this->timeout) + throw new ConnectionTimeoutException('Read from connection reached timeout after ' . $this->timeout . ' seconds.'); $readed = @socket_read($this->socket, $length - mb_strlen($output, '8bit')); if ($readed === false) $this->throwConnectException(); diff --git a/src/connection/StreamSocket.php b/src/connection/StreamSocket.php index 68947fb..0aa07ce 100644 --- a/src/connection/StreamSocket.php +++ b/src/connection/StreamSocket.php @@ -90,11 +90,15 @@ public function write(string $buffer): void public function read(int $length = 2048): string { $output = ''; + $t = microtime(true); do { + if (mb_strlen($output, '8bit') == 0 && $this->timeout > 0 && (microtime(true) - $t) >= $this->timeout) + throw new ConnectionTimeoutException('Read from connection reached timeout after ' . $this->timeout . ' seconds.'); + $readed = stream_get_contents($this->stream, $length - mb_strlen($output, '8bit')); if (stream_get_meta_data($this->stream)['timed_out'] ?? false) - throw new ConnectionTimeoutException('Connection timeout reached after ' . $this->timeout . ' seconds.'); + throw new ConnectionTimeoutException('Stream connection timed out after ' . $this->timeout . ' seconds.'); if ($readed === false) throw new ConnectException('Read error');