| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Stream; |
| 4 |
|
| 5 |
use Evenement\EventEmitter; |
| 6 |
use React\EventLoop\Loop; |
| 7 |
use React\EventLoop\LoopInterface; |
| 8 |
use InvalidArgumentException; |
| 9 |
|
| 10 |
final class DuplexResourceStream extends EventEmitter implements DuplexStreamInterface |
| 11 |
{ |
| 12 |
private $stream; |
| 13 |
|
| 14 |
/** @var LoopInterface */ |
| 15 |
private $loop; |
| 16 |
|
| 17 |
/** |
| 18 |
* Controls the maximum buffer size in bytes to read at once from the stream. |
| 19 |
* |
| 20 |
* This can be a positive number which means that up to X bytes will be read |
| 21 |
* at once from the underlying stream resource. Note that the actual number |
| 22 |
* of bytes read may be lower if the stream resource has less than X bytes |
| 23 |
* currently available. |
| 24 |
* |
| 25 |
* This can be `-1` which means read everything available from the |
| 26 |
* underlying stream resource. |
| 27 |
* This should read until the stream resource is not readable anymore |
| 28 |
* (i.e. underlying buffer drained), note that this does not neccessarily |
| 29 |
* mean it reached EOF. |
| 30 |
* |
| 31 |
* @var int |
| 32 |
*/ |
| 33 |
private $bufferSize; |
| 34 |
private $buffer; |
| 35 |
|
| 36 |
private $readable = true; |
| 37 |
private $writable = true; |
| 38 |
private $closing = false; |
| 39 |
private $listening = false; |
| 40 |
|
| 41 |
/** |
| 42 |
* @param resource $stream |
| 43 |
* @param ?LoopInterface $loop |
| 44 |
* @param ?int $readChunkSize |
| 45 |
* @param ?WritableStreamInterface $buffer |
| 46 |
*/ |
| 47 |
public function __construct($stream, $loop = null, $readChunkSize = null, $buffer = null) |
| 48 |
{ |
| 49 |
if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") { |
| 50 |
throw new InvalidArgumentException('First parameter must be a valid stream resource'); |
| 51 |
} |
| 52 |
|
| 53 |
// ensure resource is opened for reading and wrting (fopen mode must contain "+") |
| 54 |
$meta = \stream_get_meta_data($stream); |
| 55 |
if (isset($meta['mode']) && $meta['mode'] !== '' && \strpos($meta['mode'], '+') === false) { |
| 56 |
throw new InvalidArgumentException('Given stream resource is not opened in read and write mode'); |
| 57 |
} |
| 58 |
|
| 59 |
// this class relies on non-blocking I/O in order to not interrupt the event loop |
| 60 |
// e.g. pipes on Windows do not support this: https://bugs.php.net/bug.php?id=47918 |
| 61 |
if ($buffer !== null && !$buffer instanceof WritableResourceStream && \stream_set_blocking($stream, false) !== true) { |
| 62 |
throw new \RuntimeException('Unable to set stream resource to non-blocking mode'); |
| 63 |
} |
| 64 |
|
| 65 |
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1 |
| 66 |
throw new \InvalidArgumentException('Argument #2 ($loop) expected null|React\EventLoop\LoopInterface'); |
| 67 |
} |
| 68 |
if ($buffer !== null && !$buffer instanceof WritableStreamInterface) { // manual type check to support legacy PHP < 7.1 |
| 69 |
throw new \InvalidArgumentException('Argument #4 ($buffer) expected null|React\Stream\WritableStreamInterface'); |
| 70 |
} |
| 71 |
|
| 72 |
// Use unbuffered read operations on the underlying stream resource. |
| 73 |
// Reading chunks from the stream may otherwise leave unread bytes in |
| 74 |
// PHP's stream buffers which some event loop implementations do not |
| 75 |
// trigger events on (edge triggered). |
| 76 |
// This does not affect the default event loop implementation (level |
| 77 |
// triggered), so we can ignore platforms not supporting this (HHVM). |
| 78 |
// Pipe streams (such as STDIN) do not seem to require this and legacy |
| 79 |
// PHP versions cause SEGFAULTs on unbuffered pipe streams, so skip this. |
| 80 |
if (\function_exists('stream_set_read_buffer') && !$this->isLegacyPipe($stream)) { |
| 81 |
\stream_set_read_buffer($stream, 0); |
| 82 |
} |
| 83 |
|
| 84 |
if ($buffer === null) { |
| 85 |
$buffer = new WritableResourceStream($stream, $loop); |
| 86 |
} |
| 87 |
|
| 88 |
$this->stream = $stream; |
| 89 |
$this->loop = $loop ?: Loop::get(); |
| 90 |
$this->bufferSize = ($readChunkSize === null) ? 65536 : (int)$readChunkSize; |
| 91 |
$this->buffer = $buffer; |
| 92 |
|
| 93 |
$that = $this; |
| 94 |
|
| 95 |
$this->buffer->on('error', function ($error) use ($that) { |
| 96 |
$that->emit('error', array($error)); |
| 97 |
}); |
| 98 |
|
| 99 |
$this->buffer->on('close', array($this, 'close')); |
| 100 |
|
| 101 |
$this->buffer->on('drain', function () use ($that) { |
| 102 |
$that->emit('drain'); |
| 103 |
}); |
| 104 |
|
| 105 |
$this->resume(); |
| 106 |
} |
| 107 |
|
| 108 |
public function isReadable() |
| 109 |
{ |
| 110 |
return $this->readable; |
| 111 |
} |
| 112 |
|
| 113 |
public function isWritable() |
| 114 |
{ |
| 115 |
return $this->writable; |
| 116 |
} |
| 117 |
|
| 118 |
public function pause() |
| 119 |
{ |
| 120 |
if ($this->listening) { |
| 121 |
$this->loop->removeReadStream($this->stream); |
| 122 |
$this->listening = false; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public function resume() |
| 127 |
{ |
| 128 |
if (!$this->listening && $this->readable) { |
| 129 |
$this->loop->addReadStream($this->stream, array($this, 'handleData')); |
| 130 |
$this->listening = true; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
public function write($data) |
| 135 |
{ |
| 136 |
if (!$this->writable) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
return $this->buffer->write($data); |
| 141 |
} |
| 142 |
|
| 143 |
public function close() |
| 144 |
{ |
| 145 |
if (!$this->writable && !$this->closing) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$this->closing = false; |
| 150 |
|
| 151 |
$this->readable = false; |
| 152 |
$this->writable = false; |
| 153 |
|
| 154 |
$this->emit('close'); |
| 155 |
$this->pause(); |
| 156 |
$this->buffer->close(); |
| 157 |
$this->removeAllListeners(); |
| 158 |
|
| 159 |
if (\is_resource($this->stream)) { |
| 160 |
\fclose($this->stream); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public function end($data = null) |
| 165 |
{ |
| 166 |
if (!$this->writable) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
$this->closing = true; |
| 171 |
|
| 172 |
$this->readable = false; |
| 173 |
$this->writable = false; |
| 174 |
$this->pause(); |
| 175 |
|
| 176 |
$this->buffer->end($data); |
| 177 |
} |
| 178 |
|
| 179 |
public function pipe(WritableStreamInterface $dest, array $options = array()) |
| 180 |
{ |
| 181 |
return Util::pipe($this, $dest, $options); |
| 182 |
} |
| 183 |
|
| 184 |
/** @internal */ |
| 185 |
public function handleData($stream) |
| 186 |
{ |
| 187 |
$error = null; |
| 188 |
\set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) { |
| 189 |
$error = new \ErrorException( |
| 190 |
$errstr, |
| 191 |
0, |
| 192 |
$errno, |
| 193 |
$errfile, |
| 194 |
$errline |
| 195 |
); |
| 196 |
}); |
| 197 |
|
| 198 |
$data = \stream_get_contents($stream, $this->bufferSize); |
| 199 |
|
| 200 |
\restore_error_handler(); |
| 201 |
|
| 202 |
if ($error !== null) { |
| 203 |
$this->emit('error', array(new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error))); |
| 204 |
$this->close(); |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
if ($data !== '') { |
| 209 |
$this->emit('data', array($data)); |
| 210 |
} elseif (\feof($this->stream)) { |
| 211 |
// no data read => we reached the end and close the stream |
| 212 |
$this->emit('end'); |
| 213 |
$this->close(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Returns whether this is a pipe resource in a legacy environment |
| 219 |
* |
| 220 |
* This works around a legacy PHP bug (#61019) that was fixed in PHP 5.4.28+ |
| 221 |
* and PHP 5.5.12+ and newer. |
| 222 |
* |
| 223 |
* @param resource $resource |
| 224 |
* @return bool |
| 225 |
* @link https://github.com/reactphp/child-process/issues/40 |
| 226 |
* |
| 227 |
* @codeCoverageIgnore |
| 228 |
*/ |
| 229 |
private function isLegacyPipe($resource) |
| 230 |
{ |
| 231 |
if (\PHP_VERSION_ID < 50428 || (\PHP_VERSION_ID >= 50500 && \PHP_VERSION_ID < 50512)) { |
| 232 |
$meta = \stream_get_meta_data($resource); |
| 233 |
|
| 234 |
if (isset($meta['stream_type']) && $meta['stream_type'] === 'STDIO') { |
| 235 |
return true; |
| 236 |
} |
| 237 |
} |
| 238 |
return false; |
| 239 |
} |
| 240 |
} |