| 1 | package IO::Framed; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.03'; | ||
| 7 | |||
| 8 | =encoding utf-8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | IO::Framed - Convenience wrapper for frame-based I/O | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | Reading: | ||
| 17 | |||
| 18 | #See below about seed bytes. | ||
| 19 | my $iof = IO::Framed->new( $fh, 'seed bytes' ); | ||
| 20 | |||
| 21 | #This returns undef if the $in_fh doesn’t have at least | ||
| 22 | #the given length (5 in this case) of bytes to read. | ||
| 23 | $frame = $iof->read(5); | ||
| 24 | |||
| 25 | Writing, unqueued (i.e., for blocking writes): | ||
| 26 | |||
| 27 | #The second parameter (if given) is executed immediately after the final | ||
| 28 | #byte of the payload is written. For blocking I/O this happens | ||
| 29 | #before the following method returns. | ||
| 30 | $iof->write('hoohoo', sub { print 'sent!' } ); | ||
| 31 | |||
| 32 | Writing, queued (for non-blocking writes): | ||
| 33 | |||
| 34 | $iof->enable_write_queue(); | ||
| 35 | |||
| 36 | #This just adds to a memory queue: | ||
| 37 | $iof->write('hoohoo', sub { print 'sent!' } ); | ||
| 38 | |||
| 39 | #This will be 1, since we have 1 message/frame queued to send. | ||
| 40 | $iof->get_write_queue_count(); | ||
| 41 | |||
| 42 | #Returns 1 if it empties out the queue; 0 otherwise. | ||
| 43 | #Partial frame writes are accommodated; the callback given as 2nd | ||
| 44 | #argument to write() only fires when the queue item is sent completely. | ||
| 45 | my $empty = $iof->flush_write_queue(); | ||
| 46 | |||
| 47 | You can also use C<IO::Framed::Read> and C<IO::Framed::Write>, which | ||
| 48 | contain just the read and write features. (C<IO::Framed> is actually a | ||
| 49 | subclass of them both.) | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | While writing L<Net::WAMP> I noticed that I was reimplementing some of the | ||
| 54 | same patterns I’d used in L<Net::WebSocket> to parse frames from a stream: | ||
| 55 | |||
| 56 | =over | ||
| 57 | |||
| 58 | =item * Only read() entire frames, with a read queue for any partials. | ||
| 59 | |||
| 60 | =item * Continuance when a partial frame is delivered. | ||
| 61 | |||
| 62 | =item * Write queue with callbacks for non-blocking I/O | ||
| 63 | |||
| 64 | =item * Signal resilience: resume read/write after Perl receives a trapped | ||
| 65 | signal rather than throwing/giving EINTR. (cf. L<IO::SigGuard>) | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | These are now made available in this distribution. | ||
| 70 | |||
| 71 | =head1 ABOUT READS | ||
| 72 | |||
| 73 | The premise here is that you expect a given number of bytes at a given time | ||
| 74 | and that a partial read should be continued once it is sensible to do so. | ||
| 75 | |||
| 76 | As a result, C<read()> will throw an exception if the number of bytes given | ||
| 77 | for a continuance is not the same number as were originally requested. | ||
| 78 | |||
| 79 | Example: | ||
| 80 | |||
| 81 | #This reads only 2 bytes, so read() will return undef. | ||
| 82 | $iof->read(10); | ||
| 83 | |||
| 84 | #… wait for readiness if non-blocking … | ||
| 85 | |||
| 86 | #XXX This die()s because we’re in the middle of trying to read | ||
| 87 | #10 bytes, not 4. | ||
| 88 | $iof->read(4); | ||
| 89 | |||
| 90 | #If this completes the read (i.e., takes in 8 bytes), then it’ll | ||
| 91 | #return the full 10 bytes; otherwise, it’ll return undef again. | ||
| 92 | $iof->read(10); | ||
| 93 | |||
| 94 | EINTR prompts a redo of the read operation. EAGAIN and EWOULDBLOCK (the same | ||
| 95 | error generally, but not always) prompt an undef return. | ||
| 96 | Any other failures prompt an instance of L<IO::Framed::X::ReadError> to be | ||
| 97 | thrown. | ||
| 98 | |||
| 99 | =head2 EMPTY READS | ||
| 100 | |||
| 101 | This class’s C<read()> method will, by default, throw an instance of | ||
| 102 | L<IO::Framed::X::EmptyRead> on an empty read. This is normal and logical | ||
| 103 | behavior in contexts (like L<Net::WebSocket>) where the data stream itself | ||
| 104 | indicates when no more data will come across. In such cases an empty read | ||
| 105 | is genuinely an error condition: it either means you’re reading past when | ||
| 106 | you should, or the other side prematurely went away. | ||
| 107 | |||
| 108 | In some other cases, though, that empty read is the normal and expected way | ||
| 109 | to know that a filehandle/socket has no more data to read. | ||
| 110 | |||
| 111 | If you prefer, then, you can call the C<allow_empty_read()> method to switch | ||
| 112 | to a different behavior, e.g.: | ||
| 113 | |||
| 114 | $framed->allow_empty_read(); | ||
| 115 | |||
| 116 | my $frame = $framed->read(10); | ||
| 117 | |||
| 118 | if (length $frame) { | ||
| 119 | #yay, we got a frame! | ||
| 120 | } | ||
| 121 | elsif (defined $frame) { | ||
| 122 | #no more data will come in, so let’s close up shop | ||
| 123 | } | ||
| 124 | else { | ||
| 125 | #undef means we just haven’t gotten as much data as we want yet; | ||
| 126 | #in this case, that means fewer than 10 bytes are available. | ||
| 127 | } | ||
| 128 | |||
| 129 | Instead of throwing the aforementioned exception, C<read()> now returns | ||
| 130 | empty-string on an empty read. That means that you now have to distinguish | ||
| 131 | between multiple “falsey” states: undef for when the requested number | ||
| 132 | of bytes hasn’t yet arrived, and empty string for when no more bytes | ||
| 133 | will ever arrive. But it is also true now that the only exceptions thrown | ||
| 134 | are bona fide B<errors>, which will suit some applications better than the | ||
| 135 | default behavior. | ||
| 136 | |||
| 137 | =head1 ABOUT WRITES | ||
| 138 | |||
| 139 | Writes for blocking I/O are straightforward: the system will always send | ||
| 140 | the entire buffer. The OS’s C<write()> won’t return until everything | ||
| 141 | meant to be written is written. Life is pleasant; life is simple. :) | ||
| 142 | |||
| 143 | Non-blocking I/O is trickier. Not only can the OS’s C<write()> write | ||
| 144 | a subset of the data it’s given, but we also can’t know that the output | ||
| 145 | filehandle is ready right when we want it. This means that we have to queue up | ||
| 146 | our writes | ||
| 147 | then write them once we know (e.g., through C<select()>) that the filehandle | ||
| 148 | is ready. Each C<write()> call, then, enqueues one new buffer to write. | ||
| 149 | |||
| 150 | Since it’s often useful to know when a payload has been sent, | ||
| 151 | C<write()> accepts an optional callback that will be executed immediately | ||
| 152 | after the last byte of the payload is written to the output filehandle. | ||
| 153 | |||
| 154 | Empty out the write queue by calling C<flush_write_queue()> and looking for | ||
| 155 | a truthy response. (A falsey response means there is still data left in the | ||
| 156 | queue.) C<get_write_queue_count()> gives you the number of queue items left | ||
| 157 | to write. (A partially-written item is treated the same as a fully-unwritten | ||
| 158 | one.) | ||
| 159 | |||
| 160 | Note that, while it’s acceptable to activate and deactive the write queue, | ||
| 161 | the write queue must be empty in order to deactivate it. (You’ll get a | ||
| 162 | nasty, untyped exception otherwise!) | ||
| 163 | |||
| 164 | C<write()> returns undef on EAGAIN and EWOULDBLOCK. It retries on EINTR, | ||
| 165 | so you should never actually see this error from this module. | ||
| 166 | Other errors prompt a thrown exception. | ||
| 167 | |||
| 168 | NB: C<enable_write_queue()> and C<disable_write_queue()> return the object, | ||
| 169 | so you can instantiate thus: | ||
| 170 | |||
| 171 | my $nb_writer = IO::Framed::Write->new($fh)->enable_write_queue(); | ||
| 172 | |||
| 173 | =head1 ERROR RESPONSES | ||
| 174 | |||
| 175 | An empty read or any I/O error besides the ones mentioned previously | ||
| 176 | are indicated via an instance of one of the following exceptions. | ||
| 177 | |||
| 178 | All exceptions subclass L<X::Tiny::Base>. | ||
| 179 | |||
| 180 | =over | ||
| 181 | |||
| 182 | =item L<IO::Frame::X::ReadError> | ||
| 183 | |||
| 184 | =item L<IO::Frame::X::WriteError> | ||
| 185 | |||
| 186 | These both have an C<OS_ERROR> property (cf. L<X::Tiny::Base>’s accessor | ||
| 187 | method). | ||
| 188 | |||
| 189 | =item L<IO::Frame::X::EmptyRead> | ||
| 190 | |||
| 191 | No properties. If this is thrown, your peer has probably closed the connection. | ||
| 192 | Unless you have called C<allow_empty_read()> to set an alternate behavior, | ||
| 193 | you should always trap this exception if you call C<read()>. | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 197 | B<NOTE:> This distribution doesn’t write to C<$!>. EAGAIN and EWOULDBLOCK on | ||
| 198 | C<flush_write_queue()> are ignored; all other errors are converted | ||
| 199 | to thrown exceptions. | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | use parent qw( | ||
| 204 | IO::Framed::Read | ||
| 205 | IO::Framed::Write | ||
| 206 | ); | ||
| 207 | |||
| 208 | sub new { | ||
| 209 | my ( $class, $in_fh, $out_fh, $initial_buffer ) = @_; | ||
| 210 | |||
| 211 | my $self = $class->SUPER::new( $in_fh, $initial_buffer ); | ||
| 212 | |||
| 213 | $self->{'_out_fh'} = $out_fh || $in_fh, | ||
| 214 | |||
| 215 | return (bless $self, $class)->disable_write_queue(); | ||
| 216 | } | ||
| 217 | |||
| 218 | 1; | ||
| 219 | |||
| 220 | =head1 LEGACY CLASSES | ||
| 221 | |||
| 222 | This distribution also includes the following B<DEPRECATED> legacy classes: | ||
| 223 | |||
| 224 | =over | ||
| 225 | |||
| 226 | =item * IO::Frame::Write::Blocking | ||
| 227 | |||
| 228 | =item * IO::Frame::Write::NonBlocking | ||
| 229 | |||
| 230 | =item * IO::Frame::ReadWrite | ||
| 231 | |||
| 232 | =item * IO::Frame::ReadWrite::Blocking | ||
| 233 | |||
| 234 | =item * IO::Frame::ReadWrite::NonBlocking | ||
| 235 | |||
| 236 | =back | ||
| 237 | |||
| 238 | I’ll keep these in for the time being but eventually B<WILL> remove them. | ||
| 239 | Please adjust any calling code that you might have. | ||
| 240 | |||
| 241 | =head1 REPOSITORY | ||
| 242 | |||
| 243 | L<https://github.com/FGasper/p5-IO-Framed> | ||
| 244 | |||
| 245 | =head1 AUTHOR | ||
| 246 | |||
| 247 | Felipe Gasper (FELIPE) | ||
| 248 | |||
| 249 | =head1 COPYRIGHT | ||
| 250 | |||
| 251 | Copyright 2017 by L<Gasper Software Consulting, LLC|http://gaspersoftware.com> | ||
| 252 | |||
| 253 | =head1 LICENSE | ||
| 254 | |||
| 255 | This distribution is released under the same license as Perl. | ||
| 256 | |||
| 257 | =cut |