Ferreteria/v0.6/clade/IO/Aspect/Connx/Process/@fx/PipeToStream
Jump to navigation
Jump to search
|
fx:
PipeToStream() |
About
- Action: Read from the given pipe into the given stream. If no input, and $doWait is TRUE, then wait for input before returning.
Code
#
protected function PipeToStream($rPipe,StreamIface $oStrm,bool $doWait) {
$nBufSize = 1024 * 1024; // 1M buffer
// Process the stream in a loop
#self::GotToHere();
stream_set_blocking($rPipe,FALSE);
$nStart = time();
$didWait = $doWait;
while (!feof($rPipe) && $doWait) {
$sPiece = fread($rPipe, $nBufSize);
if (empty($sPiece)) {
// Time-out if I/O hangs for more than 5 seconds:
$doWait = (time() <= ($nStart+10)); // triggers after n+1 seconds of no data
} else {
$nSent = $oStrm->PushBytes($sPiece);
$nStart = time(); // got data, so reset timer
if ($nSent !== strlen($sPiece)) {
$this->CodingPrompt('Need to deal with partially-sent data.');
}
}
}
if ($didWait && !$doWait) echo 'Note: timed out waiting for response.'.CRLF; // TODO: return this in an ItWent object.
}