package AnyEvent::Handle; no warnings; use strict; use AnyEvent (); use AnyEvent::Util (); use Scalar::Util (); use Carp (); use Fcntl (); use Errno qw/EAGAIN EINTR/; =head1 NAME AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent =cut our $VERSION = '0.02'; =head1 SYNOPSIS use AnyEvent; use AnyEvent::Handle; my $cv = AnyEvent->condvar; my $ae_fh = AnyEvent::Handle->new (fh => \*STDIN); #TODO # or use the constructor to pass the callback: my $ae_fh2 = AnyEvent::Handle->new ( fh => \*STDIN, on_eof => sub { $cv->broadcast; }, #TODO ); $cv->wait; =head1 DESCRIPTION This module is a helper module to make it easier to do event-based I/O on filehandles (and sockets, see L for an easy way to make non-blocking resolves and connects). In the following, when the documentation refers to of "bytes" then this means characters. As sysread and syswrite are used for all I/O, their treatment of characters applies to this module as well. All callbacks will be invoked with the handle object as their first argument. =head1 METHODS =over 4 =item B The constructor supports these arguments (all as key => value pairs). =over 4 =item fh => $filehandle [MANDATORY] The filehandle this L object will operate on. NOTE: The filehandle will be set to non-blocking (using AnyEvent::Util::fh_nonblocking). =item on_error => $cb->($self) [MANDATORY] This is the fatal error callback, that is called when a fatal error ocurs, such as not being able to resolve the hostname, failure to connect or a read error. The object will not be in a usable state when this callback has been called. On callback entrance, the value of C<$!> contains the opertaing system error (or C or C). =item on_eof => $cb->($self) [MANDATORY] Set the callback to be called on EOF. =item on_read => $cb->($self) This sets the default read callback, which is called when data arrives and no read request is in the queue. If the read callback is C or has never been set, than AnyEvent::Handle will cease reading from the filehandle. To access (and remove data from) the read buffer, use the C<< ->rbuf >> method or acces sthe C<$self->{rbuf}> member directly. When an EOF condition is detected then AnyEvent::Handle will first try to feed all the remaining data to the queued callbacks and C before calling the C callback. If no progress can be made, then a fatal error will be raised (with C<$!> set to C). =item on_drain => $cb->() This sets the callback that is called when the write buffer becomes empty (or when the callback is set and the buffer is empty already). To append to the write buffer, use the C<< ->push_write >> method. =item rbuf_max => If defined, then a fatal error will be raised (with C<$!> set to C) when the read buffer ever (strictly) exceeds this size. This is useful to avoid denial-of-service attacks. For example, a server accepting connections from untrusted sources should be configured to accept only so-and-so much data that it cannot act on (for example, when expecting a line, an attacker could send an unlimited amount of data without a callback ever being called as long as the line isn't finished). =item read_size => The default read block size (the amount of bytes this module will try to read on each [loop iteration). Default: C<4096>. =item low_water_mark => Sets the amount of bytes (default: C<0>) that make up an "empty" write buffer: If the write reaches this size or gets even samller it is considered empty. =back =cut sub new { my $class = shift; my $self = bless { @_ }, $class; $self->{fh} or Carp::croak "mandatory argument fh is missing"; AnyEvent::Util::fh_nonblocking $self->{fh}, 1; $self->on_error ((delete $self->{on_error}) or Carp::croak "mandatory argument on_error is missing"); $self->on_eof ((delete $self->{on_eof} ) or Carp::croak "mandatory argument on_eof is missing"); $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; $self->on_read (delete $self->{on_read} ) if $self->{on_read}; $self } sub _shutdown { my ($self) = @_; delete $self->{rw}; delete $self->{ww}; delete $self->{fh}; } sub error { my ($self) = @_; { local $!; $self->_shutdown; } $self->{on_error}($self); } =item $fh = $handle->fh This method returns the filehandle of the L object. =cut sub fh { $_[0]->{fh} } =item $handle->on_error ($cb) Replace the current C callback (see the C constructor argument). =cut sub on_error { $_[0]{on_error} = $_[1]; } =item $handle->on_eof ($cb) Replace the current C callback (see the C constructor argument). =cut ############################################################################# sub on_eof { $_[0]{on_eof} = $_[1]; } =item $handle->on_drain ($cb) Sets the C callback or clears it (see the description of C in the constructor). =cut sub on_drain { my ($self, $cb) = @_; $self->{on_drain} = $cb; $cb->($self) if $cb && $self->{low_water_mark} >= length $self->{wbuf}; } =item $handle->push_write ($data) Queues the given scalar to be written. You can push as much data as you want (only limited by the available memory), as C buffers it independently of the kernel. =cut sub push_write { my ($self, $data) = @_; $self->{wbuf} .= $data; unless ($self->{ww}) { Scalar::Util::weaken $self; my $cb = sub { my $len = syswrite $self->{fh}, $self->{wbuf}; if ($len > 0) { substr $self->{wbuf}, 0, $len, ""; $self->{on_drain}($self) if $self->{low_water_mark} >= length $self->{wbuf} && $self->{on_drain}; delete $self->{ww} unless length $self->{wbuf}; } elsif ($! != EAGAIN && $! != EINTR) { $self->error; } }; $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb); $cb->($self); }; } ############################################################################# sub _drain_rbuf { my ($self) = @_; return if exists $self->{in_drain}; local $self->{in_drain} = 1; while (my $len = length $self->{rbuf}) { no strict 'refs'; if (@{ $self->{queue} }) { if ($self->{queue}[0]($self)) { shift @{ $self->{queue} }; } elsif ($self->{eof}) { # no progress can be made (not enough data and no data forthcoming) $! = &Errno::EPIPE; return $self->error; } else { return; } } elsif ($self->{on_read}) { $self->{on_read}($self); if ( $self->{eof} # if no further data will arrive && $len == length $self->{rbuf} # and no data has been consumed && !@{ $self->{queue} } # and the queue is still empty && $self->{on_read} # and we still want to read data ) { # then no progress can be made $! = &Errno::EPIPE; return $self->error; } } else { # read side becomes idle delete $self->{rw}; return; } } if ($self->{eof}) { $self->_shutdown; $self->{on_eof}($self); } } =item $handle->on_read ($cb) This replaces the currently set C callback, or clears it (when the new callback is C). See the description of C in the constructor. =cut sub on_read { my ($self, $cb) = @_; $self->{on_read} = $cb; unless ($self->{rw} || $self->{eof}) { Scalar::Util::weaken $self; $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { my $len = sysread $self->{fh}, $self->{rbuf}, $self->{read_size} || 8192, length $self->{rbuf}; if ($len > 0) { if (exists $self->{rbuf_max}) { if ($self->{rbuf_max} < length $self->{rbuf}) { $! = &Errno::ENOSPC; return $self->error; } } } elsif (defined $len) { $self->{eof} = 1; delete $self->{rw}; } elsif ($! != EAGAIN && $! != EINTR) { return $self->error; } $self->_drain_rbuf; }); } } =item $handle->rbuf Returns the read buffer (as a modifiable lvalue). You can access the read buffer directly as the C<< ->{rbuf} >> member, if you want. NOTE: The read buffer should only be used or modified if the C, C or C methods are used. The other read methods automatically manage the read buffer. =cut sub rbuf : lvalue { $_[0]{rbuf} } =item $handle->push_read ($cb) =item $handle->unshift_read ($cb) Append the given callback to the end of the queue (C) or prepend it (C). The callback is called each time some additional read data arrives. It must check wether enough data is in the read buffer already. If not enough data is available, it must return the empty list or a false value, in which case it will be called repeatedly until enough data is available (or an error condition is detected). If enough data was available, then the callback must remove all data it is interested in (which can be none at all) and return a true value. After returning true, it will be removed from the queue. =cut sub push_read { my ($self, $cb) = @_; push @{ $self->{queue} }, $cb; $self->_drain_rbuf; } sub unshift_read { my ($self, $cb) = @_; push @{ $self->{queue} }, $cb; $self->_drain_rbuf; } =item $handle->push_read_chunk ($len, $cb->($self, $data)) =item $handle->unshift_read_chunk ($len, $cb->($self, $data)) Append the given callback to the end of the queue (C) or prepend it (C). The callback will be called only once C<$len> bytes have been read, and these C<$len> bytes will be passed to the callback. =cut sub _read_chunk($$) { my ($len, $cb) = @_; sub { $len <= length $_[0]{rbuf} or return; $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); 1 } } sub push_read_chunk { my ($self, $len, $cb) = @_; $self->push_read (_read_chunk $len, $cb); } sub unshift_read_chunk { my ($self, $len, $cb) = @_; $self->unshift_read (_read_chunk $len, $cb); } =item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol)) =item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol)) Append the given callback to the end of the queue (C) or prepend it (C). The callback will be called only once a full line (including the end of line marker, C<$eol>) has been read. This line (excluding the end of line marker) will be passed to the callback as second argument (C<$line>), and the end of line marker as the third argument (C<$eol>). The end of line marker, C<$eol>, can be either a string, in which case it will be interpreted as a fixed record end marker, or it can be a regex object (e.g. created by C), in which case it is interpreted as a regular expression. The end of line marker argument C<$eol> is optional, if it is missing (NOT undef), then C is used (which is good for most internet protocols). Partial lines at the end of the stream will never be returned, as they are not marked by the end of line marker. =cut sub _read_line($$) { my $cb = pop; my $eol = @_ ? shift : qr|(\015?\012)|; my $pos; $eol = qr|(\Q$eol\E)| unless ref $eol; $eol = qr|^(.*?)($eol)|; sub { $_[0]{rbuf} =~ s/$eol// or return; $cb->($1, $2); 1 } } sub push_read_line { my $self = shift; $self->push_read (&_read_line); } sub unshift_read_line { my $self = shift; $self->unshift_read (&_read_line); } =back =head1 AUTHOR Robin Redeker C<< >>, Marc Lehmann . =cut 1; # End of AnyEvent::Handle