Skip to content

Commit

Permalink
[1.7] Add getSocketResource method to enhance socket management (#17)
Browse files Browse the repository at this point in the history
* Add getSocketResource method to enhance socket management

This method is a prerequisite to eventually resolving chrome-php/chrome#606

* StyleCI

* public function waitForData

* remove getSocketResource

* StyleCI

* StyleCI

* error message

* oops use stream_select not socket_select

* error handling
  • Loading branch information
divinity76 authored Mar 18, 2024
1 parent 5b9a4d8 commit 47aa368
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,31 @@ protected function configure(array $options): void

parent::configure($options);
}

/**
* Waits for data to become available on the socket.
*
* @param float $maxSeconds the maximum amount of time to wait for data, in seconds
*
* @return ?bool Returns true if data is available, false if the wait timed out, and null on error.
*/
public function waitForData(float $maxSeconds): ?bool
{
$read = [$this->socket->getResource()];
$write = null;
$except = null;
$seconds = (int) \floor($maxSeconds);
$microseconds = (int) (($maxSeconds - $seconds) * 1e6);
$result = \stream_select($read, $write, $except, $seconds, $microseconds);
if (false === $result) {
// An error occurred. stream_select() probably triggered an error internally.
return null;
} elseif (0 === $result) {
// Timeout occurred, no data available
return false;
}

// Data is available
return true;
}
}

0 comments on commit 47aa368

Please sign in to comment.