public function onNext($data) { if (!$this->stream->isWritable()) { throw new \Exception('Stream must be writable'); } $this->stream->write($data); }
public function write($data) { if (!$this->in->isWritable()) { throw new \Exception('Stream not writable'); } $this->in->write($data); }
public function connectTarget(Stream $stream, array $target) { $stream->emit('target', $target); $that = $this; return $this->connector->create($target[0], $target[1])->then(function (Stream $remote) use($stream, $that) { if (!$stream->isWritable()) { $remote->close(); throw new UnexpectedValueException('Remote connection successfully established after client connection closed'); } $stream->pipe($remote, array('end' => false)); $remote->pipe($stream, array('end' => false)); // remote end closes connection => stop reading from local end, try to flush buffer to local and disconnect local $remote->on('end', function () use($stream, $that) { $stream->emit('shutdown', array('remote', null)); $that->endConnection($stream); }); // local end closes connection => stop reading from remote end, try to flush buffer to remote and disconnect remote $stream->on('end', function () use($remote, $that) { $that->endConnection($remote); }); // set bigger buffer size of 100k to improve performance $stream->bufferSize = $remote->bufferSize = 100 * 1024 * 1024; return $remote; }, function (Exception $error) { throw new UnexpectedValueException('Unable to connect to remote target', 0, $error); }); }