Acme-Sort-Sleep

 view release on metacpan or  search on metacpan

local/lib/perl5/IO/Async/Handle.pm  view on Meta::CPAN

#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2006-2015 -- leonerd@leonerd.org.uk

package IO::Async::Handle;

use strict;
use warnings;
use base qw( IO::Async::Notifier );

our $VERSION = '0.70';

use Carp;

use IO::Handle; # give methods to bare IO handles

use Future;
use Future::Utils qw( try_repeat );

use IO::Async::OS;

=head1 NAME

C<IO::Async::Handle> - event callbacks for a non-blocking file descriptor

=head1 SYNOPSIS

This class is likely not to be used directly, because subclasses of it exist
to handle more specific cases. Here is an example of how it would be used to
watch a listening socket for new connections. In real code, it is likely that
the C<< Loop->listen >> method would be used instead.

 use IO::Socket::INET;
 use IO::Async::Handle;

 use IO::Async::Loop;
 my $loop = IO::Async::Loop->new;

 my $socket = IO::Socket::INET->new( LocalPort => 1234, Listen => 1 );

 my $handle = IO::Async::Handle->new(
    handle => $socket,

    on_read_ready  => sub {
       my $new_client = $socket->accept; 
       ...
    },
 );

 $loop->add( $handle );

For most other uses with sockets, pipes or other filehandles that carry a byte
stream, the L<IO::Async::Stream> class is likely to be more suitable. For
non-stream sockets, see L<IO::Async::Socket>.

=head1 DESCRIPTION

This subclass of L<IO::Async::Notifier> allows non-blocking IO on filehandles.
It provides event handlers for when the filehandle is read- or write-ready.

=cut

=head1 EVENTS

The following events are invoked, either using subclass methods or CODE
references in parameters:

=head2 on_read_ready

Invoked when the read handle becomes ready for reading.

=head2 on_write_ready

Invoked when the write handle becomes ready for writing.

=head2 on_closed

Optional. Invoked when the handle becomes closed.

This handler is invoked before the filehandles are closed and the Handle
removed from its containing Loop. The C<loop> will still return the containing
Loop object.

=cut

=head1 PARAMETERS

The following named parameters may be passed to C<new> or C<configure>:

=head2 read_handle => IO

=head2 write_handle => IO

The reading and writing IO handles. Each must implement the C<fileno> method.
Primarily used for passing C<STDIN> / C<STDOUT>; see the SYNOPSIS section of
L<IO::Async::Stream> for an example.

=head2 handle => IO

The IO handle for both reading and writing; instead of passing each separately
as above. Must implement C<fileno> method in way that C<IO::Handle> does.

=head2 read_fileno => INT

=head2 write_fileno => INT

File descriptor numbers for reading and writing. If these are given as an
alternative to C<read_handle> or C<write_handle> then a new C<IO::Handle>
instance will be constructed around each.

=head2 on_read_ready => CODE

=head2 on_write_ready => CODE

=head2 on_closed => CODE

CODE references for event handlers.

=head2 want_readready => BOOL

=head2 want_writeready => BOOL

If present, enable or disable read- or write-ready notification as per the
C<want_readready> and C<want_writeready> methods.

It is required that a matching C<on_read_ready> or C<on_write_ready> are
available for any handle that is provided; either passed as a callback CODE
reference or as an overridden the method. I.e. if only a C<read_handle> is
given, then C<on_write_ready> can be absent. If C<handle> is used as a
shortcut, then both read and write-ready callbacks or methods are required.

If no IO handles are provided at construction time, the object is still
created but will not yet be fully-functional as a Handle. IO handles can be
assigned later using the C<set_handle> or C<set_handles> methods, or by
C<configure>. This may be useful when constructing an object to represent a
network connection, before the C<connect(2)> has actually been performed yet.

=cut

sub configure
{
   my $self = shift;
   my %params = @_;

   if( exists $params{on_read_ready} ) {
      $self->{on_read_ready} = delete $params{on_read_ready};
      undef $self->{cb_r};

      $self->_watch_read(0), $self->_watch_read(1) if $self->want_readready;
   }

   if( exists $params{on_write_ready} ) {
      $self->{on_write_ready} = delete $params{on_write_ready};
      undef $self->{cb_w};

      $self->_watch_write(0), $self->_watch_write(1) if $self->want_writeready;
   }

   if( exists $params{on_closed} ) {
      $self->{on_closed} = delete $params{on_closed};
   }

   if( defined $params{read_fileno} and defined $params{write_fileno} and
       $params{read_fileno} == $params{write_fileno} ) {
      $params{handle} = IO::Handle->new_from_fd( $params{read_fileno}, "r+" );

      delete $params{read_fileno};
      delete $params{write_fileno};
   }
   else {
      $params{read_handle} = IO::Handle->new_from_fd( delete $params{read_fileno}, "r" )
         if defined $params{read_fileno};

      $params{write_handle} = IO::Handle->new_from_fd( delete $params{write_fileno}, "w" )
         if defined $params{write_fileno};
   }

   # 'handle' is a shortcut for setting read_ and write_
   if( exists $params{handle} ) {
      $params{read_handle}  = $params{handle};
      $params{write_handle} = $params{handle};
      delete $params{handle};
   }

   if( exists $params{read_handle} ) {
      my $read_handle = delete $params{read_handle};

      if( defined $read_handle ) {
         if( !defined eval { $read_handle->fileno } ) {
            croak 'Expected that read_handle can ->fileno';



( run in 1.211 second using v1.01-cache-2.11-cpan-39bf76dae61 )