NAME
IO::Async::Stream
- event callbacks and write bufering for a stream filehandle
SYNOPSIS
use IO::Async::Stream;
use IO::Async::Loop;
my $loop = IO::Async::Loop->new;
my $stream = IO::Async::Stream->new(
read_handle => \*STDIN,
write_handle => \*STDOUT,
on_read => sub {
my ( $self, $buffref, $eof ) = @_;
while( $$buffref =~ s/^(.*\n)// ) {
print "Received a line $1";
}
if( $eof ) {
print "EOF; last partial line is $$buffref\n";
}
return 0;
}
);
$loop->add( $stream );
$stream->write( "An initial line here\n" );
DESCRIPTION
This subclass of IO::Async::Handle contains a filehandle that represents a byte-stream. It provides buffering for both incoming and outgoing data. It invokes the on_read
handler when new data is read from the filehandle. Data may be written to the filehandle by calling the write
method.
For implementing real network protocols that are based on messages sent over a byte-stream (such as a TCP socket), it may be more appropriate to use a subclass of IO::Async::Protocol::Stream.
EVENTS
The following events are invoked, either using subclass methods or CODE references in parameters:
$ret = on_read \$buffer, $eof
Invoked when more data is available in the internal receiving buffer.
The first argument is a reference to a plain perl string. The code should inspect and remove any data it likes, but is not required to remove all, or indeed any of the data. Any data remaining in the buffer will be preserved for the next call, the next time more data is received from the handle.
In this way, it is easy to implement code that reads records of some form when completed, but ignores partially-received records, until all the data is present. If the handler is confident no more useful data remains, it should return 0
. If not, it should return 1
, and the handler will be called again. This makes it easy to implement code that handles multiple incoming records at the same time. See the examples at the end of this documentation for more detail.
The second argument is a scalar indicating whether the stream has reported an end-of-file (EOF) condition. A reference to the buffer is passed to the handler in the usual way, so it may inspect data contained in it. Once the handler returns a false value, it will not be called again, as the handle is now at EOF and no more data can arrive.
The on_read
code may also dynamically replace itself with a new callback by returning a CODE reference instead of 0
or 1
. The original callback or method that the object first started with may be restored by returning undef
. Whenever the callback is changed in this way, the new code is called again; even if the read buffer is currently empty. See the examples at the end of this documentation for more detail.
on_read_eof
Optional. Invoked when the read handle indicates an end-of-file (EOF) condition. If there is any data in the buffer still to be processed, the on_read
event will be invoked first, before this one.
on_write_eof
Optional. Invoked when the write handle indicates an end-of-file (EOF) condition. Note that this condition can only be detected after a write
syscall returns the EPIPE
error. If there is no data pending to be written then it will not be detected yet.
on_read_error $errno
Optional. Invoked when the sysread
method on the read handle fails.
on_write_error $errno
Optional. Invoked when the syswrite
method on the write handle fails.
The on_read_error
and on_write_error
handlers are passed the value of $!
at the time the error occured. (The $!
variable itself, by its nature, may have changed from the original error by the time this handler runs so it should always use the value passed in).
If an error occurs when the corresponding error callback is not supplied, and there is not a handler for it, then the close
method is called instead.
on_outgoing_empty
Optional. Invoked when the writing data buffer becomes empty.
PARAMETERS
The following named parameters may be passed to new
or configure
:
- read_handle => IO
-
The IO handle to read from. Must implement
fileno
andsysread
methods. - write_handle => IO
-
The IO handle to write to. Must implement
fileno
andsyswrite
methods. - handle => IO
-
Shortcut to specifying the same IO handle for both of the above.
- on_read => CODE
- on_read_error => CODE
- on_outgoing_empty => CODE
- on_write_error => CODE
-
CODE references for event handlers.
- autoflush => BOOL
-
Optional. If true, the
write
method will attempt to write data to the operating system immediately, without waiting for the loop to indicate the filehandle is write-ready. This is useful, for example, on streams that should contain up-to-date logging or console information.It currently defaults to false for any file handle, but future versions of
IO::Async
may enable this by default on STDOUT and STDERR. - read_len => INT
-
Optional. Sets the buffer size for
read
calls. Defaults to 8 KiBytes. - read_all => BOOL
-
Optional. If true, attempt to read as much data from the kernel as possible when the handle becomes readable. By default this is turned off, meaning at most one fixed-size buffer is read. If there is still more data in the kernel's buffer, the handle will still be readable, and will be read from again.
This behaviour allows multiple streams and sockets to be multiplexed simultaneously, meaning that a large bulk transfer on one cannot starve other filehandles of processing time. Turning this option on may improve bulk data transfer rate, at the risk of delaying or stalling processing on other filehandles.
- write_len => INT
-
Optional. Sets the buffer size for
write
calls. Defaults to 8 KiBytes. - write_all => BOOL
-
Optional. Analogous to the
read_all
option, but for writing. Whenautoflush
is enabled, this option only affects deferred writing if the initial attempt failed due to buffer space. - close_on_read_eof => BOOL
-
Optional. Usually true, but if set to a false value then the stream will not be
close
d when an EOF condition occurs on read. This is normally not useful as at that point the underlying stream filehandle is no longer useable, but it may be useful for reading regular files, or interacting with TTY devices. - encoding => STRING
-
If supplied, sets the name of encoding of the underlying stream. If an encoding is set, then the
print
method will expect to receive Unicode strings and encodes them into bytes, and incoming bytes will be decoded into Unicode strings for theon_read
event.If an encoding is not supplied then
print
andon_read
will work in byte strings.IMPORTANT NOTE: in order to handle reads of UTF-8 content or other multibyte encodings, the code implementing the
on_read
event uses a feature of Encode; theSTOP_AT_PARTIAL
flag. While this flag has existed for a while and is used by the:encoding
PerlIO layer itself for similar purposes, the flag is not officially documented by theEncode
module. In principle this undocumented feature could be subject to change, in practice I believe it to be reasonably stable.This note applies only to the
on_read
event; data written using thewrite
method does not rely on any undocumented features ofEncode
.
If a read handle is given, it is required that either an on_read
callback reference is configured, or that the object provides an on_read
method. It is optional whether either is true for on_outgoing_empty
; if neither is supplied then no action will be taken when the writing buffer becomes empty.
An on_read
handler may be supplied even if no read handle is yet given, to be used when a read handle is eventually provided by the set_handles
method.
This condition is checked at the time the object is added to a Loop; it is allowed to create a IO::Async::Stream
object with a read handle but without a on_read
handler, provided that one is later given using configure
before the stream is added to its containing Loop, either directly or by being a child of another Notifier already in a Loop, or added to one.
METHODS
$stream->close
A synonym for close_when_empty
. This should not be used when the deferred wait behaviour is required, as the behaviour of close
may change in a future version of IO::Async
. Instead, call close_when_empty
directly.
$stream->close_when_empty
If the write buffer is empty, this method calls close
on the underlying IO handles, and removes the stream from its containing loop. If the write buffer still contains data, then this is deferred until the buffer is empty. This is intended for "write-then-close" one-shot streams.
$stream->write( "Here is my final data\n" );
$stream->close_when_empty;
Because of this deferred nature, it may not be suitable for error handling. See instead the close_now
method.
$stream->close_now
This method immediately closes the underlying IO handles and removes the stream from the containing loop. It will not wait to flush the remaining data in the write buffer.
$stream->write( $data, %params )
This method adds data to the outgoing data queue, or writes it immediately, according to the autoflush
parameter.
If the autoflush
option is set, this method will try immediately to write the data to the underlying filehandle. If this completes successfully then it will have been written by the time this method returns. If it fails to write completely, then the data is queued as if autoflush
were not set, and will be flushed as normal.
$data
can either be a plain string, or a CODE reference. If it is a CODE reference, it will be invoked to generate data to be written. Each time the filehandle is ready to receive more data to it, the function is invoked, and what it returns written to the filehandle. Once the function has finished generating data it should return undef. The function is passed the Stream object as its first argument.
For example, to stream the contents of an existing opened filehandle:
open my $fileh, "<", $path or die "Cannot open $path - $!";
$stream->write( sub {
my ( $stream ) = @_;
sysread $fileh, my $buffer, 8192 or return;
return $buffer;
} );
Takes the following optional named parameters in %params
:
- on_flush => CODE
-
A CODE reference which will be invoked once the data queued by this
write
call has been flushed. This will be invoked even if the buffer itself is not yet empty; if more data has been queued since the call.$on_flush->( $stream )
If the object is not yet a member of a loop and doesn't yet have a write_handle
, then calls to the write
method will simply queue the data and return. It will be flushed when the object is added to the loop.
If $data
is a defined but empty string, the write is still queued, and the on_flush
continuation will be invoked, if supplied. This can be used to obtain a marker, to invoke some code once the output queue has been flushed up to this point.
UTILITY CONSTRUCTORS
$stream = IO::Async::Stream->new_for_stdin
$stream = IO::Async::Stream->new_for_stdout
$stream = IO::Async::Stream->new_for_stdio
Return a IO::Async::Stream
object preconfigured with the correct read_handle
, write_handle
or both.
EXAMPLES
A line-based on_read
method
The following on_read
method accepts incoming \n
-terminated lines and prints them to the program's STDOUT
stream.
sub on_read
{
my $self = shift;
my ( $buffref, $eof ) = @_;
while( $$buffref =~ s/^(.*\n)// ) {
print "Received a line: $1";
}
return 0;
}
Because a reference to the buffer itself is passed, it is simple to use a s///
regular expression on the scalar it points at, to both check if data is ready (i.e. a whole line), and to remove it from the buffer. If no data is available then 0
is returned, to indicate it should not be tried again. If a line was successfully extracted, then 1
is returned, to indicate it should try again in case more lines exist in the buffer.
For implementing real network protocols that are based on lines of text it may be more appropriate to use a subclass of IO::Async::Protocol::LineStream.
Reading binary data
This on_read
method accepts incoming records in 16-byte chunks, printing each one.
sub on_read
{
my ( $self, $buffref, $eof ) = @_;
if( length $$buffref >= 16 ) {
my $record = substr( $$buffref, 0, 16, "" );
print "Received a 16-byte record: $record\n";
return 1;
}
if( $eof and length $$buffref ) {
print "EOF: a partial record still exists\n";
}
return 0;
}
The 4-argument form of substr()
extracts the 16-byte record from the buffer and assigns it to the $record
variable, if there was enough data in the buffer to extract it.
A lot of protocols use a fixed-size header, followed by a variable-sized body of data, whose size is given by one of the fields of the header. The following on_read
method extracts messages in such a protocol.
sub on_read
{
my ( $self, $buffref, $eof ) = @_;
return 0 unless length $$buffref >= 8; # "N n n" consumes 8 bytes
my ( $len, $x, $y ) = unpack $$buffref, "N n n";
return 0 unless length $$buffref >= 8 + $len;
substr( $$buffref, 0, 8, "" );
my $data = substr( $$buffref, 0, $len, "" );
print "A record with values x=$x y=$y\n";
return 1;
}
In this example, the header is unpack()
ed first, to extract the body length, and then the body is extracted. If the buffer does not have enough data yet for a complete message then 0
is returned, and the buffer is left unmodified for next time. Only when there are enough bytes in total does it use substr()
to remove them.
Dynamic replacement of on_read
Consider the following protocol (inspired by IMAP), which consists of \n
-terminated lines that may have an optional data block attached. The presence of such a data block, as well as its size, is indicated by the line prefix.
sub on_read
{
my $self = shift;
my ( $buffref, $eof ) = @_;
if( $$buffref =~ s/^DATA (\d+):(.*)\n// ) {
my $length = $1;
my $line = $2;
return sub {
my $self = shift;
my ( $buffref, $eof ) = @_;
return 0 unless length $$buffref >= $length;
# Take and remove the data from the buffer
my $data = substr( $$buffref, 0, $length, "" );
print "Received a line $line with some data ($data)\n";
return undef; # Restore the original method
}
}
elsif( $$buffref =~ s/^LINE:(.*)\n// ) {
my $line = $1;
print "Received a line $line with no data\n";
return 1;
}
else {
print STDERR "Unrecognised input\n";
# Handle it somehow
}
}
In the case where trailing data is supplied, a new temporary on_read
callback is provided in a closure. This closure captures the $length
variable so it knows how much data to expect. It also captures the $line
variable so it can use it in the event report. When this method has finished reading the data, it reports the event, then restores the original method by returning undef
.
SEE ALSO
IO::Handle - Supply object methods for I/O handles
AUTHOR
Paul Evans <leonerd@leonerd.org.uk>