React\Stream\Buffer::handleWrite PHP Method

handleWrite() public method

public handleWrite ( )
    public function handleWrite()
    {
        $error = null;
        set_error_handler(function ($errno, $errstr, $errfile, $errline) use(&$error) {
            $error = array('message' => $errstr, 'number' => $errno, 'file' => $errfile, 'line' => $errline);
        });
        $sent = fwrite($this->stream, $this->data);
        restore_error_handler();
        // Only report errors if *nothing* could be sent.
        // Any hard (permanent) error will fail to send any data at all.
        // Sending excessive amounts of data will only flush *some* data and then
        // report a temporary error (EAGAIN) which we do not raise here in order
        // to keep the stream open for further tries to write.
        // Should this turn out to be a permanent error later, it will eventually
        // send *nothing* and we can detect this.
        if ($sent === 0 || $sent === false) {
            if ($error === null) {
                $error = new \RuntimeException('Send failed');
            } else {
                $error = new \ErrorException($error['message'], 0, $error['number'], $error['file'], $error['line']);
            }
            $this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . $error->getMessage(), 0, $error), $this));
            return;
        }
        $exceeded = isset($this->data[$this->softLimit - 1]);
        $this->data = (string) substr($this->data, $sent);
        // buffer has been above limit and is now below limit
        if ($exceeded && !isset($this->data[$this->softLimit - 1])) {
            $this->emit('drain', array($this));
        }
        // buffer is now completely empty (and not closed already)
        if ($this->data === '' && $this->listening) {
            $this->loop->removeWriteStream($this->stream);
            $this->listening = false;
            $this->emit('full-drain', array($this));
        }
    }

Usage Example

Example #1
0
 /**
  * @covers React\Stream\Buffer::write
  * @covers React\Stream\Buffer::handleWrite
  */
 public function testCloseDuringDrainWillNotEmitFullDrain()
 {
     $stream = fopen('php://temp', 'r+');
     $loop = $this->createLoopMock();
     $buffer = new Buffer($stream, $loop);
     $buffer->softLimit = 2;
     // close buffer on drain event => expect close event, but no full-drain after
     $buffer->on('drain', $this->expectCallableOnce());
     $buffer->on('drain', array($buffer, 'close'));
     $buffer->on('close', $this->expectCallableOnce());
     $buffer->on('full-drain', $this->expectCallableNever());
     $buffer->write("foo");
     $buffer->handleWrite();
 }
All Usage Examples Of React\Stream\Buffer::handleWrite