NAME
IO::Framed - Convenience wrapper for frame-based I/O
SYNOPSIS
Reading:
#See below about seed bytes.
my $reader = IO::Framed::Read->new( $in_fh, 'seed bytes' );
#This returns undef if the $in_fh doesn’t have at least
#the given length (5 in this case) of bytes to read.
$frame = $reader->read(5);
Writing, blocking I/O:
my $writer = IO::Framed::Write::Blocking->new( $out_fh );
#The second parameter (if given) is executed immediately after the final
#byte of the payload is written. For blocking I/O this happens
#before the following method returns.
$writer->write('hoohoo', sub { print 'sent!' } );
Writing, non-blocking I/O:
my $nb_writer = IO::Framed::Write::NonBlocking->new( $out_fh );
#This just adds to a memory queue:
$writer->write('hoohoo', sub { print 'sent!' } );
#This will be 1, since we have 1 message/frame queued to send.
$writer->get_write_queue_count();
#Returns 1 if it empties out the queue; 0 otherwise.
#Partial frame writes are accommodated; the callback given as 2nd
#argument to write() only fires when the queue item is sent completely.
my $empty = $writer->flush_write_queue();
There are also IO::Framed::ReadWrite::Blocking
and IO::Framed::ReadWrite::NonBlocking
, which combine the features of the respective read and write modules above.
DESCRIPTION
While writing Net::WAMP I noticed that I was reimplementing some of the same patterns I’d used in Net::WebSocket to parse frames from a stream:
Only read() entire frames, with a read queue for any partials.
Continuance when a partial frame is delivered.
Write queue with callbacks for non-blocking I/O
Signal resilience: resume read/write after Perl receives a trapped signal rather than throwing/giving EINTR. (cf. IO::SigGuard)
These are now made available in this distribution.
ABOUT READS
The premise here is that you expect a given number of bytes at a given time and that a partial read should be continued once it is sensible to do so.
As a result, read()
will throw an exception if the number of bytes given for a continuance is not the same number as were originally requested.
Example:
#This reads only 2 bytes, so read() will return undef.
$framed->read(10);
#… wait for readiness if non-blocking …
#XXX This die()s because we’re in the middle of trying to read
#10 bytes, not 4.
$framed->read(4);
#If this completes the read (i.e., takes in 8 bytes), then it’ll
#return the full 10 bytes; otherwise, it’ll return undef again.
$framed->read(10);
EINTR prompts a redo of the read operation. EAGAIN and EWOULDBLOCK (the same error generally, but not always) prompt an undef return. Any other failures prompt an instance of IO::Framed::X::ReadError to be thrown.
ABOUT WRITES
Blocking writes are straightforward: the system will always send the entire buffer.
Non-blocking writes are trickier. Since we can’t know that the output filehandle is ready right when we want it, we have to queue up our writes then write them once we know (e.g., through select()
) that the filehandle is ready. Each write()
call, then, enqueues one new buffer to write.
Since it’s often useful to know when a payload has been sent, write()
accepts a callback that will be executed immediately after the last byte of the payload is written to the output filehandle.
Note that both blocking and non-blocking I/O expose a write()
method, though NonBlocking.pm’s module is just a “push” onto a queue. This allows anything that writes to the object not to care whether it’s blocking or non-blocking I/O.
Empty out the write queue by calling flush_write_queue()
and looking for a truthy response. (A falsey response means there is still data left in the queue.) get_write_queue_count()
gives you the number of queue items left to write. (A partially-written item is treated the same as a fully-unwritten one.) Since version 0.014 Blocking.pm includes stubs of these methods as well so that applications need not care whether they have blocking or non-blocking I/O.
write()
returns undef on EAGAIN and EWOULDBLOCK and retries on EINTR; other errors prompt a thrown exception.
ERROR RESPONSES
An empty read or any I/O error besides the ones mentioned previously are indicated via an instance of one of the following exceptions.
All exceptions subclass X::Tiny::Base.
IO::Frame::X::ReadError
IO::Frame::X::WriteError
These both have an OS_ERROR
property (cf. X::Tiny::Base’s accessor method).
IO::Frame::X::EmptyRead
No properties. If this is thrown, your peer has probably closed the connection. You probably should thus always trap this exception.
NOTE: This distribution doesn’t write to $!
.
#----------------------------------------------------------------------
REPOSITORY
https://github.com/FGasper/p5-IO-Framed
AUTHOR
Felipe Gasper (FELIPE)
COPYRIGHT
Copyright 2017 by Gasper Software Consulting, LLC
LICENSE
This distribution is released under the same license as Perl.
2 POD Errors
The following errors were encountered while parsing the POD:
- Around line 140:
You forgot a '=back' before '=head2'
- Around line 152:
=back without =over