| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Stream; |
| 4 |
|
| 5 |
use Evenement\EventEmitter; |
| 6 |
use React\EventLoop\Loop; |
| 7 |
use React\EventLoop\LoopInterface; |
| 8 |
|
| 9 |
final class WritableResourceStream extends EventEmitter implements WritableStreamInterface |
| 10 |
{ |
| 11 |
private $stream; |
| 12 |
|
| 13 |
/** @var LoopInterface */ |
| 14 |
private $loop; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
private $softLimit; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var int |
| 23 |
*/ |
| 24 |
private $writeChunkSize; |
| 25 |
|
| 26 |
private $listening = false; |
| 27 |
private $writable = true; |
| 28 |
private $closed = false; |
| 29 |
private $data = ''; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param resource $stream |
| 33 |
* @param ?LoopInterface $loop |
| 34 |
* @param ?int $writeBufferSoftLimit |
| 35 |
* @param ?int $writeChunkSize |
| 36 |
*/ |
| 37 |
public function __construct($stream, $loop = null, $writeBufferSoftLimit = null, $writeChunkSize = null) |
| 38 |
{ |
| 39 |
if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") { |
| 40 |
throw new \InvalidArgumentException('First parameter must be a valid stream resource'); |
| 41 |
} |
| 42 |
|
| 43 |
// ensure resource is opened for writing (fopen mode must contain either of "waxc+") |
| 44 |
$meta = \stream_get_meta_data($stream); |
| 45 |
if (isset($meta['mode']) && $meta['mode'] !== '' && \strtr($meta['mode'], 'waxc+', '.....') === $meta['mode']) { |
| 46 |
throw new \InvalidArgumentException('Given stream resource is not opened in write mode'); |
| 47 |
} |
| 48 |
|
| 49 |
// this class relies on non-blocking I/O in order to not interrupt the event loop |
| 50 |
// e.g. pipes on Windows do not support this: https://bugs.php.net/bug.php?id=47918 |
| 51 |
if (\stream_set_blocking($stream, false) !== true) { |
| 52 |
throw new \RuntimeException('Unable to set stream resource to non-blocking mode'); |
| 53 |
} |
| 54 |
|
| 55 |
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1 |
| 56 |
throw new \InvalidArgumentException('Argument #2 ($loop) expected null|React\EventLoop\LoopInterface'); |
| 57 |
} |
| 58 |
|
| 59 |
$this->stream = $stream; |
| 60 |
$this->loop = $loop ?: Loop::get(); |
| 61 |
$this->softLimit = ($writeBufferSoftLimit === null) ? 65536 : (int)$writeBufferSoftLimit; |
| 62 |
$this->writeChunkSize = ($writeChunkSize === null) ? -1 : (int)$writeChunkSize; |
| 63 |
} |
| 64 |
|
| 65 |
public function isWritable() |
| 66 |
{ |
| 67 |
return $this->writable; |
| 68 |
} |
| 69 |
|
| 70 |
public function write($data) |
| 71 |
{ |
| 72 |
if (!$this->writable) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
$this->data .= $data; |
| 77 |
|
| 78 |
if (!$this->listening && $this->data !== '') { |
| 79 |
$this->listening = true; |
| 80 |
|
| 81 |
$this->loop->addWriteStream($this->stream, array($this, 'handleWrite')); |
| 82 |
} |
| 83 |
|
| 84 |
return !isset($this->data[$this->softLimit - 1]); |
| 85 |
} |
| 86 |
|
| 87 |
public function end($data = null) |
| 88 |
{ |
| 89 |
if (null !== $data) { |
| 90 |
$this->write($data); |
| 91 |
} |
| 92 |
|
| 93 |
$this->writable = false; |
| 94 |
|
| 95 |
// close immediately if buffer is already empty |
| 96 |
// otherwise wait for buffer to flush first |
| 97 |
if ($this->data === '') { |
| 98 |
$this->close(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public function close() |
| 103 |
{ |
| 104 |
if ($this->closed) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
if ($this->listening) { |
| 109 |
$this->listening = false; |
| 110 |
$this->loop->removeWriteStream($this->stream); |
| 111 |
} |
| 112 |
|
| 113 |
$this->closed = true; |
| 114 |
$this->writable = false; |
| 115 |
$this->data = ''; |
| 116 |
|
| 117 |
$this->emit('close'); |
| 118 |
$this->removeAllListeners(); |
| 119 |
|
| 120 |
if (\is_resource($this->stream)) { |
| 121 |
\fclose($this->stream); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** @internal */ |
| 126 |
public function handleWrite() |
| 127 |
{ |
| 128 |
$error = null; |
| 129 |
\set_error_handler(function ($_, $errstr) use (&$error) { |
| 130 |
$error = $errstr; |
| 131 |
}); |
| 132 |
|
| 133 |
if ($this->writeChunkSize === -1) { |
| 134 |
$sent = \fwrite($this->stream, $this->data); |
| 135 |
} else { |
| 136 |
$sent = \fwrite($this->stream, $this->data, $this->writeChunkSize); |
| 137 |
} |
| 138 |
|
| 139 |
\restore_error_handler(); |
| 140 |
|
| 141 |
// Only report errors if *nothing* could be sent and an error has been raised. |
| 142 |
// Ignore non-fatal warnings if *some* data could be sent. |
| 143 |
// Any hard (permanent) error will fail to send any data at all. |
| 144 |
// Sending excessive amounts of data will only flush *some* data and then |
| 145 |
// report a temporary error (EAGAIN) which we do not raise here in order |
| 146 |
// to keep the stream open for further tries to write. |
| 147 |
// Should this turn out to be a permanent error later, it will eventually |
| 148 |
// send *nothing* and we can detect this. |
| 149 |
if (($sent === 0 || $sent === false) && $error !== null) { |
| 150 |
$this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . $error))); |
| 151 |
$this->close(); |
| 152 |
|
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
$exceeded = isset($this->data[$this->softLimit - 1]); |
| 157 |
$this->data = (string) \substr($this->data, $sent); |
| 158 |
|
| 159 |
// buffer has been above limit and is now below limit |
| 160 |
if ($exceeded && !isset($this->data[$this->softLimit - 1])) { |
| 161 |
$this->emit('drain'); |
| 162 |
} |
| 163 |
|
| 164 |
// buffer is now completely empty => stop trying to write |
| 165 |
if ($this->data === '') { |
| 166 |
// stop waiting for resource to be writable |
| 167 |
if ($this->listening) { |
| 168 |
$this->loop->removeWriteStream($this->stream); |
| 169 |
$this->listening = false; |
| 170 |
} |
| 171 |
|
| 172 |
// buffer is end()ing and now completely empty => close buffer |
| 173 |
if (!$this->writable) { |
| 174 |
$this->close(); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |