Stream is a component that provides consistent interface for managing and operating on stream including asynchronous writing and reading.
Stream features:
This section contains terminology, useful concepts and definitions, that might be helpful to learn to fully understand this component purpose.
Asynchronous processing is an approach of processing instructions in non-blocking way, that only initiates the operations without wait for response. The response is discovered later using specially designed mechanisms. You can learn more in asynchronity section.
This section contains examples and patterns that can be used with described component.
All of asynchronous streams' constructors are also the same. This applies to AsyncStream
, AsyncStreamReader
and AsyncStreamWriter
.
$stream = new AsyncStream($resource, $loop); // $loop have to be instance of Kraken\Loop\LoopInterface
All of synchronous streams' constructors are the same. This applies to Stream
, StreamReader
, StreamWriter
and StreamSeeker
.
$stream = new Stream($resource);
Regardless of chosen type of stream, might it be a synchronous or asynchronous one, the API is the same. The only difference is that in synchronous stream readers the read
operation is blocking and returns read value while in asynchronous it is non-blocking, and the only way of obtaining read value is via event data
.
$reader->on('data', function($reader, $data) {
// data event will be fired regardless of sync or async reading method
});
$reader->on('close', function() {
// close event will also be fired
});
$data = $reader->read(); // this operation is blocking or non-blocking depending on stream type
$reader->close();
Regardless of chosen type of stream, might it be a synchronous or asynchronous one, the API is the same. The only difference is that in synchronous stream writers the write
operation is blocking, while in asynchronous it is non-blocking.
$writer->on('drain', function() {
// drain event will be fired regardless of sync or async writing method
});
$writer->on('close', function() {
// close event will also be fired
});
$writer->write($data); // this operation is blocking or non-blocking depending on stream type
$writer->close();
This section contains list of most important classes and interfaces shipped with this component. It does not include all classes and interface.
class AsyncStream extends Stream implements AsyncStreamInterface
AsyncStream is a concrete implementation of stream API allowing asynchronous reading and writing.
interface AsyncStreamInterface extends AsyncStreamWriterInterface, AsyncStreamReaderInterface
class AsyncStreamReader extends StreamReader implements AsyncStreamReaderInterface
AsyncStreamReader is a concrete implementation of stream API allowing asynchronous reading.
interface AsyncStreamReaderInterface extends StreamReaderInterface, LoopResourceInterface
class AsyncStreamWriter extends StreamWriter implements AsyncStreamWriterInterface
AsyncStreamWriter is a concrete implementation of stream API allowing asynchronous writing.
interface AsyncStreamWriterInterface extends StreamWriterInterface, LoopResourceInterface
class Stream extends StreamSeeker implements StreamInterface
Stream is a concrete implementation of stream API allowing reading and writing synchronously.
interface StreamInterface extends StreamWriterInterface, StreamReaderInterface
class StreamReader extends StreamSeeker implements StreamReaderInterface
StreamReader is a concrete implementation of stream API allowing reading synchronously.
/**
* @event data : callable(object, string)
*/
interface StreamReaderInterface extends EventEmitterInterface, StreamSeekerInterface
class StreamWriter extends StreamSeeker implements StreamWriterInterface
StreamWriter is a concrete implementation of stream API allowing writing synchronously.
/**
* @event drain : callable(object)
*/
interface StreamWriterInterface extends EventEmitterInterface, StreamSeekerInterface
class StreamSeeker extends BaseEventEmitter implements StreamSeekerInterface
StreamSeeker is a concrete implementation of stream API without possibility of reading or writing.
/**
* @event seek : callable(object, int)
*/
interface StreamSeekerInterface extends EventEmitterInterface, StreamBaseInterface