Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.7] Add getSocketResource method to enhance socket management #17

Merged
merged 9 commits into from
Mar 18, 2024
Merged
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
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;
}
}