Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ public function run()
$timeout = $scheduledAt - $this->timers->getTime();
if ($timeout < 0) {
$timeout = 0;
} else {
$timeout *= self::MICROSECONDS_PER_SECOND;
}

// The only possible event is stream activity, so wait forever ...
Expand Down Expand Up @@ -243,7 +241,7 @@ private function waitForStreamActivity($timeout)
*
* @param array &$read An array of read streams to select upon.
* @param array &$write An array of write streams to select upon.
* @param integer|null $timeout Activity timeout in microseconds, or null to wait forever.
* @param integer|null $timeout Activity timeout in seconds, or null to wait forever.
*
* @return integer The total number of streams that are ready for read/write.
*/
Expand All @@ -252,7 +250,10 @@ protected function streamSelect(array &$read, array &$write, $timeout)
if ($read || $write) {
$except = null;

return stream_select($read, $write, $except, $timeout === null ? null : 0, $timeout);
$tv_sec = $timeout === null ?: floor($timeout);
// convert the fractional part of $timeout to microseconds
$tv_usec = $timeout === null ?: round(fmod($timeout, 1) * self::MICROSECONDS_PER_SECOND);
Copy link
Member

@jsor jsor Feb 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When $timeout === null, both $tv_sec and $tv_usec will become true because of the elvis operator.

This should be

$tv_sec = $timeout === null ? null : floor($timeout);
// convert the fractional part of $timeout to microseconds
$tv_usec = $timeout === null ? null : round(fmod($timeout, 1) * self::MICROSECONDS_PER_SECOND);

return stream_select($read, $write, $except, $tv_sec, $tv_usec);
}

usleep($timeout);
Expand Down