Skip to content

Commit

Permalink
Stream Socket readChunk timeout of 5 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
MartynasKasp committed Sep 6, 2024
1 parent 5b5938e commit 060aeac
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/IO/StreamSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class StreamSocket extends AbstractIO

private $configuration;

/**
* @var null|float
*/
private $lastRead = null;

/**
* @param string $host
* @param int $port
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 060aeac

Please sign in to comment.