WritableStreamInterface.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace React\Stream;
  3. use Evenement\EventEmitterInterface;
  4. /**
  5. * The `WritableStreamInterface` is responsible for providing an interface for
  6. * write-only streams and the writable side of duplex streams.
  7. *
  8. * Besides defining a few methods, this interface also implements the
  9. * `EventEmitterInterface` which allows you to react to certain events:
  10. *
  11. * drain event:
  12. * The `drain` event will be emitted whenever the write buffer became full
  13. * previously and is now ready to accept more data.
  14. *
  15. * ```php
  16. * $stream->on('drain', function () use ($stream) {
  17. * echo 'Stream is now ready to accept more data';
  18. * });
  19. * ```
  20. *
  21. * This event SHOULD be emitted once every time the buffer became full
  22. * previously and is now ready to accept more data.
  23. * In other words, this event MAY be emitted any number of times, which may
  24. * be zero times if the buffer never became full in the first place.
  25. * This event SHOULD NOT be emitted if the buffer has not become full
  26. * previously.
  27. *
  28. * This event is mostly used internally, see also `write()` for more details.
  29. *
  30. * pipe event:
  31. * The `pipe` event will be emitted whenever a readable stream is `pipe()`d
  32. * into this stream.
  33. * The event receives a single `ReadableStreamInterface` argument for the
  34. * source stream.
  35. *
  36. * ```php
  37. * $stream->on('pipe', function (ReadableStreamInterface $source) use ($stream) {
  38. * echo 'Now receiving piped data';
  39. *
  40. * // explicitly close target if source emits an error
  41. * $source->on('error', function () use ($stream) {
  42. * $stream->close();
  43. * });
  44. * });
  45. *
  46. * $source->pipe($stream);
  47. * ```
  48. *
  49. * This event MUST be emitted once for each readable stream that is
  50. * successfully piped into this destination stream.
  51. * In other words, this event MAY be emitted any number of times, which may
  52. * be zero times if no stream is ever piped into this stream.
  53. * This event MUST NOT be emitted if either the source is not readable
  54. * (closed already) or this destination is not writable (closed already).
  55. *
  56. * This event is mostly used internally, see also `pipe()` for more details.
  57. *
  58. * error event:
  59. * The `error` event will be emitted once a fatal error occurs, usually while
  60. * trying to write to this stream.
  61. * The event receives a single `Exception` argument for the error instance.
  62. *
  63. * ```php
  64. * $stream->on('error', function (Exception $e) {
  65. * echo 'Error: ' . $e->getMessage() . PHP_EOL;
  66. * });
  67. * ```
  68. *
  69. * This event SHOULD be emitted once the stream detects a fatal error, such
  70. * as a fatal transmission error.
  71. * It SHOULD NOT be emitted after a previous `error` or `close` event.
  72. * It MUST NOT be emitted if this is not a fatal error condition, such as
  73. * a temporary network issue that did not cause any data to be lost.
  74. *
  75. * After the stream errors, it MUST close the stream and SHOULD thus be
  76. * followed by a `close` event and then switch to non-writable mode, see
  77. * also `close()` and `isWritable()`.
  78. *
  79. * Many common streams (such as a TCP/IP connection or a file-based stream)
  80. * only deal with data transmission and may choose
  81. * to only emit this for a fatal transmission error once and will then
  82. * close (terminate) the stream in response.
  83. *
  84. * If this stream is a `DuplexStreamInterface`, you should also notice
  85. * how the readable side of the stream also implements an `error` event.
  86. * In other words, an error may occur while either reading or writing the
  87. * stream which should result in the same error processing.
  88. *
  89. * close event:
  90. * The `close` event will be emitted once the stream closes (terminates).
  91. *
  92. * ```php
  93. * $stream->on('close', function () {
  94. * echo 'CLOSED';
  95. * });
  96. * ```
  97. *
  98. * This event SHOULD be emitted once or never at all, depending on whether
  99. * the stream ever terminates.
  100. * It SHOULD NOT be emitted after a previous `close` event.
  101. *
  102. * After the stream is closed, it MUST switch to non-writable mode,
  103. * see also `isWritable()`.
  104. *
  105. * This event SHOULD be emitted whenever the stream closes, irrespective of
  106. * whether this happens implicitly due to an unrecoverable error or
  107. * explicitly when either side closes the stream.
  108. *
  109. * Many common streams (such as a TCP/IP connection or a file-based stream)
  110. * will likely choose to emit this event after flushing the buffer from
  111. * the `end()` method, after receiving a *successful* `end` event or after
  112. * a fatal transmission `error` event.
  113. *
  114. * If this stream is a `DuplexStreamInterface`, you should also notice
  115. * how the readable side of the stream also implements a `close` event.
  116. * In other words, after receiving this event, the stream MUST switch into
  117. * non-writable AND non-readable mode, see also `isReadable()`.
  118. * Note that this event should not be confused with the `end` event.
  119. *
  120. * The event callback functions MUST be a valid `callable` that obeys strict
  121. * parameter definitions and MUST accept event parameters exactly as documented.
  122. * The event callback functions MUST NOT throw an `Exception`.
  123. * The return value of the event callback functions will be ignored and has no
  124. * effect, so for performance reasons you're recommended to not return any
  125. * excessive data structures.
  126. *
  127. * Every implementation of this interface MUST follow these event semantics in
  128. * order to be considered a well-behaving stream.
  129. *
  130. * > Note that higher-level implementations of this interface may choose to
  131. * define additional events with dedicated semantics not defined as part of
  132. * this low-level stream specification. Conformance with these event semantics
  133. * is out of scope for this interface, so you may also have to refer to the
  134. * documentation of such a higher-level implementation.
  135. *
  136. * @see EventEmitterInterface
  137. * @see DuplexStreamInterface
  138. */
  139. interface WritableStreamInterface extends EventEmitterInterface
  140. {
  141. /**
  142. * Checks whether this stream is in a writable state (not closed already).
  143. *
  144. * This method can be used to check if the stream still accepts writing
  145. * any data or if it is ended or closed already.
  146. * Writing any data to a non-writable stream is a NO-OP:
  147. *
  148. * ```php
  149. * assert($stream->isWritable() === false);
  150. *
  151. * $stream->write('end'); // NO-OP
  152. * $stream->end('end'); // NO-OP
  153. * ```
  154. *
  155. * A successfully opened stream always MUST start in writable mode.
  156. *
  157. * Once the stream ends or closes, it MUST switch to non-writable mode.
  158. * This can happen any time, explicitly through `end()` or `close()` or
  159. * implicitly due to a remote close or an unrecoverable transmission error.
  160. * Once a stream has switched to non-writable mode, it MUST NOT transition
  161. * back to writable mode.
  162. *
  163. * If this stream is a `DuplexStreamInterface`, you should also notice
  164. * how the readable side of the stream also implements an `isReadable()`
  165. * method. Unless this is a half-open duplex stream, they SHOULD usually
  166. * have the same return value.
  167. *
  168. * @return bool
  169. */
  170. public function isWritable();
  171. /**
  172. * Write some data into the stream.
  173. *
  174. * A successful write MUST be confirmed with a boolean `true`, which means
  175. * that either the data was written (flushed) immediately or is buffered and
  176. * scheduled for a future write. Note that this interface gives you no
  177. * control over explicitly flushing the buffered data, as finding the
  178. * appropriate time for this is beyond the scope of this interface and left
  179. * up to the implementation of this interface.
  180. *
  181. * Many common streams (such as a TCP/IP connection or file-based stream)
  182. * may choose to buffer all given data and schedule a future flush by using
  183. * an underlying EventLoop to check when the resource is actually writable.
  184. *
  185. * If a stream cannot handle writing (or flushing) the data, it SHOULD emit
  186. * an `error` event and MAY `close()` the stream if it can not recover from
  187. * this error.
  188. *
  189. * If the internal buffer is full after adding `$data`, then `write()`
  190. * SHOULD return `false`, indicating that the caller should stop sending
  191. * data until the buffer drains.
  192. * The stream SHOULD send a `drain` event once the buffer is ready to accept
  193. * more data.
  194. *
  195. * Similarly, if the stream is not writable (already in a closed state)
  196. * it MUST NOT process the given `$data` and SHOULD return `false`,
  197. * indicating that the caller should stop sending data.
  198. *
  199. * The given `$data` argument MAY be of mixed type, but it's usually
  200. * recommended it SHOULD be a `string` value or MAY use a type that allows
  201. * representation as a `string` for maximum compatibility.
  202. *
  203. * Many common streams (such as a TCP/IP connection or a file-based stream)
  204. * will only accept the raw (binary) payload data that is transferred over
  205. * the wire as chunks of `string` values.
  206. *
  207. * Due to the stream-based nature of this, the sender may send any number
  208. * of chunks with varying sizes. There are no guarantees that these chunks
  209. * will be received with the exact same framing the sender intended to send.
  210. * In other words, many lower-level protocols (such as TCP/IP) transfer the
  211. * data in chunks that may be anywhere between single-byte values to several
  212. * dozens of kilobytes. You may want to apply a higher-level protocol to
  213. * these low-level data chunks in order to achieve proper message framing.
  214. *
  215. * @param mixed|string $data
  216. * @return bool
  217. */
  218. public function write($data);
  219. /**
  220. * Successfully ends the stream (after optionally sending some final data).
  221. *
  222. * This method can be used to successfully end the stream, i.e. close
  223. * the stream after sending out all data that is currently buffered.
  224. *
  225. * ```php
  226. * $stream->write('hello');
  227. * $stream->write('world');
  228. * $stream->end();
  229. * ```
  230. *
  231. * If there's no data currently buffered and nothing to be flushed, then
  232. * this method MAY `close()` the stream immediately.
  233. *
  234. * If there's still data in the buffer that needs to be flushed first, then
  235. * this method SHOULD try to write out this data and only then `close()`
  236. * the stream.
  237. * Once the stream is closed, it SHOULD emit a `close` event.
  238. *
  239. * Note that this interface gives you no control over explicitly flushing
  240. * the buffered data, as finding the appropriate time for this is beyond the
  241. * scope of this interface and left up to the implementation of this
  242. * interface.
  243. *
  244. * Many common streams (such as a TCP/IP connection or file-based stream)
  245. * may choose to buffer all given data and schedule a future flush by using
  246. * an underlying EventLoop to check when the resource is actually writable.
  247. *
  248. * You can optionally pass some final data that is written to the stream
  249. * before ending the stream. If a non-`null` value is given as `$data`, then
  250. * this method will behave just like calling `write($data)` before ending
  251. * with no data.
  252. *
  253. * ```php
  254. * // shorter version
  255. * $stream->end('bye');
  256. *
  257. * // same as longer version
  258. * $stream->write('bye');
  259. * $stream->end();
  260. * ```
  261. *
  262. * After calling this method, the stream MUST switch into a non-writable
  263. * mode, see also `isWritable()`.
  264. * This means that no further writes are possible, so any additional
  265. * `write()` or `end()` calls have no effect.
  266. *
  267. * ```php
  268. * $stream->end();
  269. * assert($stream->isWritable() === false);
  270. *
  271. * $stream->write('nope'); // NO-OP
  272. * $stream->end(); // NO-OP
  273. * ```
  274. *
  275. * If this stream is a `DuplexStreamInterface`, calling this method SHOULD
  276. * also end its readable side, unless the stream supports half-open mode.
  277. * In other words, after calling this method, these streams SHOULD switch
  278. * into non-writable AND non-readable mode, see also `isReadable()`.
  279. * This implies that in this case, the stream SHOULD NOT emit any `data`
  280. * or `end` events anymore.
  281. * Streams MAY choose to use the `pause()` method logic for this, but
  282. * special care may have to be taken to ensure a following call to the
  283. * `resume()` method SHOULD NOT continue emitting readable events.
  284. *
  285. * Note that this method should not be confused with the `close()` method.
  286. *
  287. * @param mixed|string|null $data
  288. * @return void
  289. */
  290. public function end($data = null);
  291. /**
  292. * Closes the stream (forcefully).
  293. *
  294. * This method can be used to forcefully close the stream, i.e. close
  295. * the stream without waiting for any buffered data to be flushed.
  296. * If there's still data in the buffer, this data SHOULD be discarded.
  297. *
  298. * ```php
  299. * $stream->close();
  300. * ```
  301. *
  302. * Once the stream is closed, it SHOULD emit a `close` event.
  303. * Note that this event SHOULD NOT be emitted more than once, in particular
  304. * if this method is called multiple times.
  305. *
  306. * After calling this method, the stream MUST switch into a non-writable
  307. * mode, see also `isWritable()`.
  308. * This means that no further writes are possible, so any additional
  309. * `write()` or `end()` calls have no effect.
  310. *
  311. * ```php
  312. * $stream->close();
  313. * assert($stream->isWritable() === false);
  314. *
  315. * $stream->write('nope'); // NO-OP
  316. * $stream->end(); // NO-OP
  317. * ```
  318. *
  319. * Note that this method should not be confused with the `end()` method.
  320. * Unlike the `end()` method, this method does not take care of any existing
  321. * buffers and simply discards any buffer contents.
  322. * Likewise, this method may also be called after calling `end()` on a
  323. * stream in order to stop waiting for the stream to flush its final data.
  324. *
  325. * ```php
  326. * $stream->end();
  327. * Loop::addTimer(1.0, function () use ($stream) {
  328. * $stream->close();
  329. * });
  330. * ```
  331. *
  332. * If this stream is a `DuplexStreamInterface`, you should also notice
  333. * how the readable side of the stream also implements a `close()` method.
  334. * In other words, after calling this method, the stream MUST switch into
  335. * non-writable AND non-readable mode, see also `isReadable()`.
  336. *
  337. * @return void
  338. * @see ReadableStreamInterface::close()
  339. */
  340. public function close();
  341. }