Overview

Namespaces

  • Evenement
  • None
  • PHP
  • Psr
    • Http
      • Message
  • Ratchet
    • Http
    • RFC6455
      • Handshake
      • Messaging
    • Server
    • Session
      • Serialize
      • Storage
        • Proxy
    • Wamp
    • WebSocket
  • React
    • EventLoop
      • Tick
      • Timer
    • Socket
    • Stream
  • Symfony
    • Component
      • HttpFoundation
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
      • Routing
        • Annotation
        • Exception
        • Generator
          • Dumper
        • Loader
          • DependencyInjection
        • Matcher
          • Dumper
        • Tests
          • Annotation
          • Fixtures
            • AnnotatedClasses
            • OtherAnnotatedClasses
          • Generator
            • Dumper
          • Loader
          • Matcher
            • Dumper

Classes

  • BufferedSink
  • CompositeStream
  • DuplexResourceStream
  • ReadableResourceStream
  • ReadableStream
  • ThroughStream
  • Util
  • WritableResourceStream
  • WritableStream

Interfaces

  • DuplexStreamInterface
  • ReadableStreamInterface
  • WritableStreamInterface
  • Overview
  • Namespace
  • Class
  • Tree

Class DuplexResourceStream

The DuplexStreamInterface is responsible for providing an interface for duplex streams (both readable and writable).

It builds on top of the existing interfaces for readable and writable streams and follows the exact same method and event semantics. If you're new to this concept, you should look into the ReadableStreamInterface and WritableStreamInterface first.

Besides defining a few methods, this interface also implements the EventEmitterInterface which allows you to react to the same events defined on the ReadbleStreamInterface and WritableStreamInterface.

Evenement\EventEmitter implements Evenement\EventEmitterInterface uses Evenement\EventEmitterTrait
Extended by React\Stream\DuplexResourceStream implements React\Stream\DuplexStreamInterface
Namespace: React\Stream
See: React\Stream\ReadableStreamInterface
See: React\Stream\WritableStreamInterface
Located at DuplexResourceStream.php
Methods summary
public
# __construct( mixed $stream, React\EventLoop\LoopInterface $loop, React\Stream\WritableStreamInterface $buffer = null )
public boolean
# isReadable( )

Checks whether this stream is in a readable state (not closed already).

Checks whether this stream is in a readable state (not closed already).

This method can be used to check if the stream still accepts incoming data events or if it is ended or closed already. Once the stream is non-readable, no further data or end events SHOULD be emitted.

`php assert($stream->isReadable() === false);

$stream->on('data', assertNeverCalled()); $stream->on('end', assertNeverCalled()); `

A successfully opened stream always MUST start in readable mode.

Once the stream ends or closes, it MUST switch to non-readable mode. This can happen any time, explicitly through close() or implicitly due to a remote close or an unrecoverable transmission error. Once a stream has switched to non-readable mode, it MUST NOT transition back to readable mode.

If this stream is a DuplexStreamInterface, you should also notice how the writable side of the stream also implements an isWritable() method. Unless this is a half-open duplex stream, they SHOULD usually have the same return value.

Returns

boolean

Implementation of

React\Stream\ReadableStreamInterface::isReadable()
public boolean
# isWritable( )

Checks whether this stream is in a writable state (not closed already).

Checks whether this stream is in a writable state (not closed already).

This method can be used to check if the stream still accepts writing any data or if it is ended or closed already. Writing any data to a non-writable stream is a NO-OP:

`php assert($stream->isWritable() === false);

$stream->write('end'); // NO-OP $stream->end('end'); // NO-OP `

A successfully opened stream always MUST start in writable mode.

Once the stream ends or closes, it MUST switch to non-writable mode. This can happen any time, explicitly through end() or close() or implicitly due to a remote close or an unrecoverable transmission error. Once a stream has switched to non-writable mode, it MUST NOT transition back to writable mode.

If this stream is a DuplexStreamInterface, you should also notice how the readable side of the stream also implements an isReadable() method. Unless this is a half-open duplex stream, they SHOULD usually have the same return value.

Returns

boolean

Implementation of

React\Stream\WritableStreamInterface::isWritable()
public
# pause( )

Pauses reading incoming data events.

Pauses reading incoming data events.

Removes the data source file descriptor from the event loop. This allows you to throttle incoming data.

Unless otherwise noted, a successfully opened stream SHOULD NOT start in paused state.

Once the stream is paused, no futher data or end events SHOULD be emitted.

`php $stream->pause();

$stream->on('data', assertShouldNeverCalled()); $stream->on('end', assertShouldNeverCalled()); `

This method is advisory-only, though generally not recommended, the stream MAY continue emitting data events.

You can continue processing events by calling resume() again.

Note that both methods can be called any number of times, in particular calling pause() more than once SHOULD NOT have any effect.

See

React\Stream\DuplexResourceStream::resume()

Implementation of

React\Stream\ReadableStreamInterface::pause()
public
# resume( )

Resumes reading incoming data events.

Resumes reading incoming data events.

Re-attach the data source after a previous pause().

`php $stream->pause();

$loop->addTimer(1.0, function () use ($stream) { $stream->resume(); }); `

Note that both methods can be called any number of times, in particular calling resume() without a prior pause() SHOULD NOT have any effect.

See

React\Stream\DuplexResourceStream::pause()

Implementation of

React\Stream\ReadableStreamInterface::resume()
public boolean
# write( mixed|string $data )

Write some data into the stream.

Write some data into the stream.

A successful write MUST be confirmed with a boolean true, which means that either the data was written (flushed) immediately or is buffered and scheduled for a future write. Note that this interface gives you no control over explicitly flushing the buffered data, as finding the appropriate time for this is beyond the scope of this interface and left up to the implementation of this interface.

Many common streams (such as a TCP/IP connection or file-based stream) may choose to buffer all given data and schedule a future flush by using an underlying EventLoop to check when the resource is actually writable.

If a stream cannot handle writing (or flushing) the data, it SHOULD emit an error event and MAY close() the stream if it can not recover from this error.

If the internal buffer is full after adding $data, then write() SHOULD return false, indicating that the caller should stop sending data until the buffer drains. The stream SHOULD send a drain event once the buffer is ready to accept more data.

Similarly, if the the stream is not writable (already in a closed state) it MUST NOT process the given $data and SHOULD return false, indicating that the caller should stop sending data.

The given $data argument MAY be of mixed type, but it's usually recommended it SHOULD be a string value or MAY use a type that allows representation as a string for maximum compatibility.

Many common streams (such as a TCP/IP connection or a file-based stream) will only accept the raw (binary) payload data that is transferred over the wire as chunks of string values.

Due to the stream-based nature of this, the sender may send any number of chunks with varying sizes. There are no guarantees that these chunks will be received with the exact same framing the sender intended to send. In other words, many lower-level protocols (such as TCP/IP) transfer the data in chunks that may be anywhere between single-byte values to several dozens of kilobytes. You may want to apply a higher-level protocol to these low-level data chunks in order to achieve proper message framing.

Parameters

$data
mixed|string
$data

Returns

boolean

Implementation of

React\Stream\WritableStreamInterface::write()
public
# close( )

Closes the stream (forcefully).

Closes the stream (forcefully).

This method can be used to (forcefully) close the stream.

`php $stream->close(); `

Once the stream is closed, it SHOULD emit a close event. Note that this event SHOULD NOT be emitted more than once, in particular if this method is called multiple times.

After calling this method, the stream MUST switch into a non-readable mode, see also isReadable(). This means that no further data or end events SHOULD be emitted.

`php $stream->close(); assert($stream->isReadable() === false);

$stream->on('data', assertNeverCalled()); $stream->on('end', assertNeverCalled()); `

If this stream is a DuplexStreamInterface, you should also notice how the writable side of the stream also implements a close() method. In other words, after calling this method, the stream MUST switch into non-writable AND non-readable mode, see also isWritable(). Note that this method should not be confused with the end() method.

See

React\Stream\WritableStreamInterface::close()

Implementation of

React\Stream\ReadableStreamInterface::close()
public
# end( mixed|string|null $data = null )

Successfully ends the stream (after optionally sending some final data).

Successfully ends the stream (after optionally sending some final data).

This method can be used to successfully end the stream, i.e. close the stream after sending out all data that is currently buffered.

`php $stream->write('hello'); $stream->write('world'); $stream->end(); `

If there's no data currently buffered and nothing to be flushed, then this method MAY close() the stream immediately.

If there's still data in the buffer that needs to be flushed first, then this method SHOULD try to write out this data and only then close() the stream. Once the stream is closed, it SHOULD emit a close event.

Note that this interface gives you no control over explicitly flushing the buffered data, as finding the appropriate time for this is beyond the scope of this interface and left up to the implementation of this interface.

Many common streams (such as a TCP/IP connection or file-based stream) may choose to buffer all given data and schedule a future flush by using an underlying EventLoop to check when the resource is actually writable.

You can optionally pass some final data that is written to the stream before ending the stream. If a non-null value is given as $data, then this method will behave just like calling write($data) before ending with no data.

`php // shorter version $stream->end('bye');

// same as longer version $stream->write('bye'); $stream->end(); `

After calling this method, the stream MUST switch into a non-writable mode, see also isWritable(). This means that no further writes are possible, so any additional write() or end() calls have no effect.

`php $stream->end(); assert($stream->isWritable() === false);

$stream->write('nope'); // NO-OP $stream->end(); // NO-OP `

If this stream is a DuplexStreamInterface, calling this method SHOULD also end its readable side, unless the stream supports half-open mode. In other words, after calling this method, these streams SHOULD switch into non-writable AND non-readable mode, see also isReadable(). This implies that in this case, the stream SHOULD NOT emit any data or end events anymore. Streams MAY choose to use the pause() method logic for this, but special care may have to be taken to ensure a following call to the resume() method SHOULD NOT continue emitting readable events.

Note that this method should not be confused with the close() method.

Parameters

$data
mixed|string|null
$data

Implementation of

React\Stream\WritableStreamInterface::end()
public React\Stream\WritableStreamInterface
# pipe( React\Stream\WritableStreamInterface $dest, array $options = array() )

Pipes all the data from this readable source into the given writable destination.

Pipes all the data from this readable source into the given writable destination.

Automatically sends all incoming data to the destination. Automatically throttles the source based on what the destination can handle.

`php $source->pipe($dest); `

Similarly, you can also pipe an instance implementing DuplexStreamInterface into itself in order to write back all the data that is received. This may be a useful feature for a TCP/IP echo service:

`php $connection->pipe($connection); `

This method returns the destination stream as-is, which can be used to set up chains of piped streams:

`php $source->pipe($decodeGzip)->pipe($filterBadWords)->pipe($dest); `

By default, this will call end() on the destination stream once the source stream emits an end event. This can be disabled like this:

`php $source->pipe($dest, array('end' => false)); `

Note that this only applies to the end event. If an error or explicit close event happens on the source stream, you'll have to manually close the destination stream:

`php $source->pipe($dest); $source->on('close', function () use ($dest) { $dest->end('BYE!'); }); `

If the source stream is not readable (closed state), then this is a NO-OP.

`php $source->close(); $source->pipe($dest); // NO-OP `

If the destinantion stream is not writable (closed state), then this will simply throttle (pause) the source stream:

`php $dest->close(); $source->pipe($dest); // calls $source->pause() `

Similarly, if the destination stream is closed while the pipe is still active, it will also throttle (pause) the source stream:

`php $source->pipe($dest); $dest->close(); // calls $source->pause() `

Once the pipe is set up successfully, the destination stream MUST emit a pipe event with this source stream an event argument.

Parameters

$dest
React\Stream\WritableStreamInterface
$dest
$options
array
$options

Returns

React\Stream\WritableStreamInterface
$dest stream as-is

Implementation of

React\Stream\ReadableStreamInterface::pipe()
public
# handleData( mixed $stream )
public
# handleClose( )
public React\Stream\WritableStreamInterface
# getBuffer( )

Returns

React\Stream\WritableStreamInterface
Methods inherited from Evenement\EventEmitterInterface
emit(), listeners(), on(), once(), removeAllListeners(), removeListener()
Methods used from Evenement\EventEmitterTrait
(), (), (), (), (), ()
Properties summary
public integer|null $bufferSize 65536
#

Controls the maximum buffer size in bytes to read at once from the stream.

Controls the maximum buffer size in bytes to read at once from the stream.

This can be a positive number which means that up to X bytes will be read at once from the underlying stream resource. Note that the actual number of bytes read may be lower if the stream resource has less than X bytes currently available.

This can be null which means read everything available from the underlying stream resource. This should read until the stream resource is not readable anymore (i.e. underlying buffer drained), note that this does not neccessarily mean it reached EOF.

public mixed $stream
#
protected boolean $readable true
#
protected boolean $writable true
#
protected boolean $closing false
#
protected mixed $loop
#
protected mixed $buffer
#
Properties used from Evenement\EventEmitterTrait
$listeners
Ratchet API documentation generated by ApiGen 2.8.0