| 1 | package Net::Curl::Promiser::AnyEvent; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =encoding utf-8 | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::Curl::Promiser::AnyEvent - support for L<AnyEvent> | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | my $promiser = Net::Curl::Promiser::AnyEvent->new(); | ||
| 15 | |||
| 16 | my $handle = Net::Curl::Easy->new(); | ||
| 17 | $handle->setopt( CURLOPT_URL() => $url ); | ||
| 18 | |||
| 19 | my $cv = AnyEvent->condvar(); | ||
| 20 | |||
| 21 | $promiser->add_handle($handle)->then( | ||
| 22 | sub { print "$url completed.$/" }, | ||
| 23 | sub { warn "$url failed: " . shift }, | ||
| 24 | )->finally($cv); | ||
| 25 | |||
| 26 | $cv->recv(); | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | This module provides an L<AnyEvent>-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 Net::Curl::Multi (); | ||
| 45 | |||
| 46 | use AnyEvent; | ||
| 47 | |||
| 48 | #---------------------------------------------------------------------- | ||
| 49 | |||
| 50 | sub _INIT { | ||
| 51 | my ($self, $args_ar) = @_; | ||
| 52 | |||
| 53 | $self->{'_watches'} = {}; | ||
| 54 | |||
| 55 | $self->setopt( Net::Curl::Multi::CURLMOPT_TIMERFUNCTION(), \&_cb_timer ); | ||
| 56 | $self->setopt( Net::Curl::Multi::CURLMOPT_TIMERDATA(), $self ); | ||
| 57 | |||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _cb_timer { | ||
| 62 | my ($multi, $timeout_ms, $self) = @_; | ||
| 63 | |||
| 64 | my $cb = sub { | ||
| 65 | #$self->_time_out_in_loop(); | ||
| 66 | #$multi->socket_action( Net::Curl::Multi::CURL_SOCKET_TIMEOUT() ); | ||
| 67 | $self->{'multi'}->socket_action( Net::Curl::Multi::CURL_SOCKET_TIMEOUT() ); | ||
| 68 | |||
| 69 | # $self->_process_pending(); | ||
| 70 | |||
| 71 | return; | ||
| 72 | }; | ||
| 73 | |||
| 74 | delete $self->{'timer'}; | ||
| 75 | |||
| 76 | if ($timeout_ms < 0) { | ||
| 77 | if ($multi->handles()) { | ||
| 78 | $self->{'timer'} = AnyEvent->timer( | ||
| 79 | after => 5, | ||
| 80 | interval => 5, | ||
| 81 | cb => $cb, | ||
| 82 | ); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | $self->{timer} = AnyEvent->timer( | ||
| 87 | after => $timeout_ms / 1000, | ||
| 88 | cb => $cb, | ||
| 89 | ); | ||
| 90 | } | ||
| 91 | |||
| 92 | return 1; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _io { | ||
| 96 | my ($self, $fd, $direction, $action_num) = @_; | ||
| 97 | |||
| 98 | $self->{'_watches'}{$fd}{$direction} = AnyEvent->io( | ||
| 99 | fh => $fd, | ||
| 100 | poll => $direction, | ||
| 101 | cb => sub { | ||
| 102 | $self->_process_in_loop($fd, $action_num); | ||
| 103 | }, | ||
| 104 | ); | ||
| 105 | |||
| 106 | return; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _SET_POLL_IN { | ||
| 110 | my ($self, $fd) = @_; | ||
| 111 | |||
| 112 | $self->_io( $fd, 'r', Net::Curl::Multi::CURL_CSELECT_IN() ); | ||
| 113 | |||
| 114 | delete $self->{'_watches'}{$fd}{'w'}; | ||
| 115 | |||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _SET_POLL_OUT { | ||
| 120 | my ($self, $fd) = @_; | ||
| 121 | |||
| 122 | $self->_io( $fd, 'w', Net::Curl::Multi::CURL_CSELECT_OUT() ); | ||
| 123 | |||
| 124 | delete $self->{'_watches'}{$fd}{'r'}; | ||
| 125 | |||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _SET_POLL_INOUT { | ||
| 130 | my ($self, $fd) = @_; | ||
| 131 | |||
| 132 | $self->_io( $fd, 'r', Net::Curl::Multi::CURL_CSELECT_IN() ); | ||
| 133 | $self->_io( $fd, 'w', Net::Curl::Multi::CURL_CSELECT_OUT() ); | ||
| 134 | |||
| 135 | return; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _STOP_POLL { | ||
| 139 | my ($self, $fd) = @_; | ||
| 140 | |||
| 141 | delete $self->{'_watches'}{$fd}; | ||
| 142 | |||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; |