| 1 | package Net::Curl::Promiser::IOAsync; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =encoding utf-8 | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::Curl::Promiser::IOAsync - support for L<IO::Async> | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | my $loop = IO::Async::Loop->new(); | ||
| 15 | |||
| 16 | my $promiser = Net::Curl::Promiser::IOAsync->new($loop); | ||
| 17 | |||
| 18 | my $handle = Net::Curl::Easy->new(); | ||
| 19 | $handle->setopt( CURLOPT_URL() => $url ); | ||
| 20 | |||
| 21 | $promiser->add_handle($handle)->then( | ||
| 22 | sub { print "$url completed.$/" }, | ||
| 23 | sub { warn "$url failed: " . shift }, | ||
| 24 | )->finally( sub { $loop->stop() } ); | ||
| 25 | |||
| 26 | $loop->run(); | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | This module provides an L<IO::Async>-compatible interface for | ||
| 31 | L<Net::Curl::Promiser>. | ||
| 32 | |||
| 33 | See F</examples> in the distribution for a fleshed-out demonstration. | ||
| 34 | |||
| 35 | B<NOTE:> The actual interface is that provided by | ||
| 36 | L<Net::Curl::Promiser::LoopBase>. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | #---------------------------------------------------------------------- | ||
| 41 | |||
| 42 | use parent 'Net::Curl::Promiser::LoopBase'; | ||
| 43 | |||
| 44 | use IO::Async::Handle (); | ||
| 45 | use Net::Curl::Multi (); | ||
| 46 | |||
| 47 | use Net::Curl::Promiser::FDFHStore (); | ||
| 48 | |||
| 49 | #---------------------------------------------------------------------- | ||
| 50 | |||
| 51 | sub _INIT { | ||
| 52 | my ($self, $args_ar) = @_; | ||
| 53 | |||
| 54 | $self->{'_loop'} = $args_ar->[0]; | ||
| 55 | |||
| 56 | $self->setopt( Net::Curl::Multi::CURLMOPT_TIMERFUNCTION(), \&_cb_timer ); | ||
| 57 | $self->setopt( Net::Curl::Multi::CURLMOPT_TIMERDATA(), $self ); | ||
| 58 | |||
| 59 | $self->{'_fhstore'} = Net::Curl::Promiser::FDFHStore->new(); | ||
| 60 | |||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _cb_timer { | ||
| 65 | my ($multi, $timeout_ms, $self) = @_; | ||
| 66 | |||
| 67 | my $loop = $self->{'_loop'}; | ||
| 68 | |||
| 69 | if ( my $old_id = $self->{'timer_id'} ) { | ||
| 70 | $loop->unwatch_time($old_id); | ||
| 71 | } | ||
| 72 | |||
| 73 | my $after; | ||
| 74 | |||
| 75 | if ($timeout_ms) { | ||
| 76 | if ($timeout_ms < 0) { | ||
| 77 | if ($multi->handles()) { | ||
| 78 | $after = 5; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | $after = $timeout_ms / 1000; | ||
| 83 | } | ||
| 84 | |||
| 85 | if ($after) { | ||
| 86 | $self->{'timer_id'} = $loop->watch_time( | ||
| 87 | after => $after, | ||
| 88 | code => sub { | ||
| 89 | $self->_time_out_in_loop(); | ||
| 90 | }, | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | $loop->later( sub { $self->_time_out_in_loop() } ); | ||
| 96 | } | ||
| 97 | |||
| 98 | return 1; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _get_handle { | ||
| 102 | my ($self, $fd) = @_; | ||
| 103 | |||
| 104 | return $self->{'_handle'}{$fd} ||= do { | ||
| 105 | my $s = $self->{'_fhstore'}->get_checked($fd); | ||
| 106 | |||
| 107 | my $handle = IO::Async::Handle->new( | ||
| 108 | read_handle => $s, | ||
| 109 | write_handle => $s, | ||
| 110 | |||
| 111 | on_read_ready => sub { | ||
| 112 | $self->_process_in_loop($fd, Net::Curl::Multi::CURL_CSELECT_IN()); | ||
| 113 | }, | ||
| 114 | |||
| 115 | on_write_ready => sub { | ||
| 116 | $self->_process_in_loop($fd, Net::Curl::Multi::CURL_CSELECT_OUT()); | ||
| 117 | }, | ||
| 118 | ); | ||
| 119 | |||
| 120 | $self->{'_loop'}->add($handle); | ||
| 121 | |||
| 122 | $handle; | ||
| 123 | }; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _SET_POLL_IN { | ||
| 127 | my ($self, $fd) = @_; | ||
| 128 | |||
| 129 | my $h = $self->_get_handle($fd); | ||
| 130 | |||
| 131 | $h->want_readready(1); | ||
| 132 | $h->want_writeready(0); | ||
| 133 | |||
| 134 | return; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub _SET_POLL_OUT { | ||
| 138 | my ($self, $fd) = @_; | ||
| 139 | |||
| 140 | my $h = $self->_get_handle($fd); | ||
| 141 | |||
| 142 | $h->want_readready(0); | ||
| 143 | $h->want_writeready(1); | ||
| 144 | |||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub _SET_POLL_INOUT { | ||
| 149 | my ($self, $fd) = @_; | ||
| 150 | |||
| 151 | my $h = $self->_get_handle($fd); | ||
| 152 | |||
| 153 | $h->want_readready(1); | ||
| 154 | $h->want_writeready(1); | ||
| 155 | |||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _STOP_POLL { | ||
| 160 | my ($self, $fd) = @_; | ||
| 161 | |||
| 162 | if ( my $fh = delete $self->{'_handle'}{$fd} ) { | ||
| 163 | $self->{'_loop'}->remove($fh); | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | $self->_handle_extra_stop_poll($fd); | ||
| 167 | } | ||
| 168 | |||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; |