Skip to content

Commit e723b31

Browse files
committed
read data using stream_select fetch data using stream_get_line
1 parent 7f7dd43 commit e723b31

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/Connection.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ public function getMessage(null|int|float $timeout = 0): ?Message
6363
if (!is_resource($this->socket) || feof($this->socket)) {
6464
throw new LogicException('supplied resource is not a valid stream resource');
6565
}
66+
67+
$remainingTimeout = $max - microtime(true);
68+
if ($remainingTimeout <= 0) {
69+
break;
70+
}
71+
72+
$read = [$this->socket];
73+
$write = null;
74+
$except = null;
75+
$seconds = (int) floor($remainingTimeout);
76+
$microseconds = (int) (($remainingTimeout - $seconds) * 1_000_000);
77+
78+
$result = stream_select($read, $write, $except, $seconds, $microseconds);
79+
80+
if ($result === false || $result === 0) {
81+
break;
82+
}
83+
6684
$message = null;
6785
$line = stream_get_line($this->socket, 1024, "\r\n");
6886
$now = microtime(true);
@@ -98,9 +116,6 @@ public function getMessage(null|int|float $timeout = 0): ?Message
98116
}
99117
}
100118
}
101-
if ($now > $max) {
102-
break;
103-
}
104119
if ($message && $now < $max) {
105120
$this->logger?->debug('sleep', compact('max', 'now'));
106121
$this->config->delay($iteration++);

0 commit comments

Comments
 (0)