| 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 | #---------------------------------------------------------------------- | ||
| 48 | |||
| 49 | sub _INIT { | ||
| 50 | my ($self, $args_ar) = @_; | ||
| 51 | |||
| 52 | $self->{'_loop'} = $args_ar->[0]; | ||
| 53 | |||
| 54 | $self->setopt( Net::Curl::Multi::CURLMOPT_TIMERFUNCTION(), \&_cb_timer ); | ||
| 55 | $self->setopt( Net::Curl::Multi::CURLMOPT_TIMERDATA(), $self ); | ||
| 56 | |||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _cb_timer { | ||
| 61 | my ($multi, $timeout_ms, $self) = @_; | ||
| 62 | |||
| 63 | my $loop = $self->{'_loop'}; | ||
| 64 | |||
| 65 | if ( my $old_id = $self->{'timer_id'} ) { | ||
| 66 | $loop->unwatch_time($old_id); | ||
| 67 | } | ||
| 68 | |||
| 69 | my $after; | ||
| 70 | |||
| 71 | if ($timeout_ms) { | ||
| 72 | if ($timeout_ms < 0) { | ||
| 73 | if ($multi->handles()) { | ||
| 74 | $after = 5; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | $after = $timeout_ms / 1000; | ||
| 79 | } | ||
| 80 | |||
| 81 | if ( $after) { | ||
| 82 | $self->{'timer_id'} = $loop->watch_time( | ||
| 83 | after => $after, | ||
| 84 | code => sub { | ||
| 85 | $self->_time_out_in_loop(); | ||
| 86 | }, | ||
| 87 | ); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | $self->_time_out_in_loop(); | ||
| 92 | } | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _get_handle { | ||
| 98 | my ($self, $fd) = @_; | ||
| 99 | |||
| 100 | return $self->{'_handle'}{$fd} ||= do { | ||
| 101 | |||
| 102 | my $handle = IO::Async::Handle->new( | ||
| 103 | read_fileno => $fd, | ||
| 104 | write_fileno => $fd, | ||
| 105 | |||
| 106 | on_read_ready => sub { | ||
| 107 | $self->_process_in_loop($fd, Net::Curl::Multi::CURL_CSELECT_IN()); | ||
| 108 | }, | ||
| 109 | |||
| 110 | on_write_ready => sub { | ||
| 111 | $self->_process_in_loop($fd, Net::Curl::Multi::CURL_CSELECT_OUT()); | ||
| 112 | }, | ||
| 113 | ); | ||
| 114 | |||
| 115 | $self->{'_loop'}->add($handle); | ||
| 116 | |||
| 117 | $handle; | ||
| 118 | }; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _SET_POLL_IN { | ||
| 122 | my ($self, $fd) = @_; | ||
| 123 | |||
| 124 | $self->_get_handle($fd)->want_readready(1); | ||
| 125 | $self->_get_handle($fd)->want_writeready(0); | ||
| 126 | |||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _SET_POLL_OUT { | ||
| 131 | my ($self, $fd) = @_; | ||
| 132 | |||
| 133 | $self->_get_handle($fd)->want_readready(0); | ||
| 134 | $self->_get_handle($fd)->want_writeready(1); | ||
| 135 | |||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _SET_POLL_INOUT { | ||
| 140 | my ($self, $fd) = @_; | ||
| 141 | |||
| 142 | $self->_get_handle($fd)->want_readready(1); | ||
| 143 | $self->_get_handle($fd)->want_writeready(1); | ||
| 144 | |||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub _STOP_POLL { | ||
| 149 | my ($self, $fd) = @_; | ||
| 150 | |||
| 151 | $self->{'_loop'}->remove( delete $self->{'_handle'}{$fd} ); | ||
| 152 | |||
| 153 | return; | ||
| 154 | } | ||
| 155 | |||
| 156 | 1; |