Acme-Sort-Sleep

 view release on metacpan or  search on metacpan

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

   my $self = shift;
   if( @_ ) {
      my ( $new ) = @_;

      $new = !!$new;
      return $new if !$new == !$self->{want_readready}; # compare bools

      if( $new ) {
         defined $self->read_handle or
            croak 'Cannot want_readready in a Handle with no read_handle';
      }

      my $old = $self->{want_readready};
      $self->{want_readready} = $new;

      $self->_watch_read( $new );

      return $old;
   }
   else {
      return $self->{want_readready};
   }
}

sub want_writeready
{
   my $self = shift;
   if( @_ ) {
      my ( $new ) = @_;

      $new = !!$new;
      return $new if !$new == !$self->{want_writeready}; # compare bools

      if( $new ) {
         defined $self->write_handle or
            croak 'Cannot want_writeready in a Handle with no write_handle';
      }

      my $old = $self->{want_writeready};
      $self->{want_writeready} = $new;

      $self->_watch_write( $new );

      return $old;
   }
   else {
      return $self->{want_writeready};
   }
}

=head2 socket

   $handle->socket( $ai )

Convenient shortcut to creating a socket handle, as given by an addrinfo
structure, and setting it as the read and write handle for the object.

C<$ai> may be either a C<HASH> or C<ARRAY> reference of the same form as given
to L<IO::Async::OS>'s C<extract_addrinfo> method.

This method returns nothing if it succeeds, or throws an exception if it
fails.

=cut

sub socket
{
   my $self = shift;
   my ( $ai ) = @_;

   # TODO: Something about closing the old one?

   my ( $family, $socktype, $protocol ) = IO::Async::OS->extract_addrinfo( $ai );

   my $sock = IO::Async::OS->socket( $family, $socktype, $protocol );
   $self->set_handle( $sock );
}

=head2 bind

   $handle = $handle->bind( %args )->get

Performs a C<getaddrinfo> resolver operation with the C<passive> flag set,
and then attempts to bind a socket handle of any of the return values.

=head2 bind (1 argument)

   $handle = $handle->bind( $ai )->get

When invoked with a single argument, this method is a convenient shortcut to
creating a socket handle and C<bind()>ing it to the address as given by an
addrinfo structure, and setting it as the read and write handle for the
object.

C<$ai> may be either a C<HASH> or C<ARRAY> reference of the same form as given
to L<IO::Async::OS>'s C<extract_addrinfo> method.

The returned future returns the handle object itself for convenience.

=cut

sub bind
{
   my $self = shift;

   if( @_ == 1 ) {
      my ( $ai ) = @_;

      $self->socket( $ai );
      my $addr = ( IO::Async::OS->extract_addrinfo( $ai ) )[3];

      $self->read_handle->bind( $addr ) or
         return Future->fail( "Cannot bind - $!", bind => $self->read_handle, $addr, $! );

      return Future->done( $self );
   }

   $self->loop->resolver->getaddrinfo( passive => 1, @_ )->then( sub {
      my @addrs = @_;

      try_repeat {



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