Ferreteria/v0.6/clade/IO/Aspect/Connx/Process/@fx/PipeToStream

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | IO‎ | Aspect‎ | Connx‎ | Process
Jump to navigation Jump to search

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

As of 2025-12-23:

#
    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.
    }