| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Socket::More::Resolver::IO::Async - Resolver IO::Async Integration | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use IO::Async; | ||
| 8 | |||
| 9 | use Socket::More::Resolver; | ||
| 10 | |||
| 11 | ... | ||
| 12 | |||
| 13 | getaddrinfo... | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | Built in driver for integrating L<Socket::More::Resolver> into the | ||
| 18 | L<IO::Async> event loop. | ||
| 19 | |||
| 20 | If the event loop module is already in memory, it should automatically be | ||
| 21 | detected when using L<Socket::More::Resolver>; | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | use warnings; | ||
| 26 | use strict; | ||
| 27 | package Socket::More::Resolver::IO::Async; | ||
| 28 | use parent qw(IO::Async::Notifier); | ||
| 29 | use IO::Async::Handle; | ||
| 30 | |||
| 31 | use Socket::More::Resolver (); # no not run import | ||
| 32 | |||
| 33 | # Shared object, | ||
| 34 | my $_shared; | ||
| 35 | |||
| 36 | |||
| 37 | sub new { | ||
| 38 | my $package=shift; | ||
| 39 | return $_shared if $_shared; | ||
| 40 | |||
| 41 | # | ||
| 42 | my $self=bless {}, $package; | ||
| 43 | $self->{watchers}=[]; | ||
| 44 | $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub _add_to_loop { | ||
| 48 | my $self = shift; | ||
| 49 | my ( $loop ) = @_; | ||
| 50 | |||
| 51 | # Code here to set up event handling on $loop that may be required | ||
| 52 | my @fh=Socket::More::Resolver::to_watch; | ||
| 53 | my $watchers=$self->{watchers}; | ||
| 54 | for(@fh){ | ||
| 55 | my $fh=$_; | ||
| 56 | my $w= IO::Async::Handle->new( | ||
| 57 | read_handle=>$fh, | ||
| 58 | on_read_ready=> sub { | ||
| 59 | my $in_fd=fileno $fh; | ||
| 60 | Socket::More::Resolver::process_results $in_fd; | ||
| 61 | } | ||
| 62 | ); | ||
| 63 | $loop->add($w); | ||
| 64 | push @$watchers,$w; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _remove_from_loop { | ||
| 69 | my $self = shift; | ||
| 70 | my ( $loop ) = @_; | ||
| 71 | |||
| 72 | # Code here to undo the event handling set up above | ||
| 73 | |||
| 74 | for($self->{watchers}->@*){ | ||
| 75 | $loop->remove($_); | ||
| 76 | } | ||
| 77 | $self->{watchers}=[]; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub { | ||
| 81 | $Socket::More::Resolver::Shared= __PACKAGE__->new; | ||
| 82 | } |