| 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 | delete $self->{'timer'}; | ||
| 65 | |||
| 66 | # NB: It’s important that we not run _time_out_in_loop() immediately | ||
| 67 | # because if we add_handle() with an easy object that’s already | ||
| 68 | # completed, we’ll end up trying to remove a handle that hasn’t been | ||
| 69 | # added yet. | ||
| 70 | my $cb = sub { | ||
| 71 | $self->_time_out_in_loop(); | ||
| 72 | }; | ||
| 73 | |||
| 74 | if ($timeout_ms < 0) { | ||
| 75 | if ($multi->handles()) { | ||
| 76 | $self->{'timer'} = AnyEvent->timer( | ||
| 77 | after => 5, | ||
| 78 | interval => 5, | ||
| 79 | cb => $cb, | ||
| 80 | ); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | $self->{timer} = AnyEvent->timer( | ||
| 85 | after => $timeout_ms / 1000, | ||
| 86 | cb => $cb, | ||
| 87 | ); | ||
| 88 | } | ||
| 89 | |||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _io { | ||
| 94 | my ($self, $fd, $direction, $action_num) = @_; | ||
| 95 | |||
| 96 | $self->{'_watches'}{$fd}{$direction} = AnyEvent->io( | ||
| 97 | fh => $fd, | ||
| 98 | poll => $direction, | ||
| 99 | cb => sub { | ||
| 100 | $self->_process_in_loop($fd, $action_num); | ||
| 101 | }, | ||
| 102 | ); | ||
| 103 | |||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _SET_POLL_IN { | ||
| 108 | my ($self, $fd) = @_; | ||
| 109 | |||
| 110 | $self->_io( $fd, 'r', Net::Curl::Multi::CURL_CSELECT_IN() ); | ||
| 111 | |||
| 112 | delete $self->{'_watches'}{$fd}{'w'}; | ||
| 113 | |||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _SET_POLL_OUT { | ||
| 118 | my ($self, $fd) = @_; | ||
| 119 | |||
| 120 | $self->_io( $fd, 'w', Net::Curl::Multi::CURL_CSELECT_OUT() ); | ||
| 121 | |||
| 122 | delete $self->{'_watches'}{$fd}{'r'}; | ||
| 123 | |||
| 124 | return; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _SET_POLL_INOUT { | ||
| 128 | my ($self, $fd) = @_; | ||
| 129 | |||
| 130 | $self->_io( $fd, 'r', Net::Curl::Multi::CURL_CSELECT_IN() ); | ||
| 131 | $self->_io( $fd, 'w', Net::Curl::Multi::CURL_CSELECT_OUT() ); | ||
| 132 | |||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub _STOP_POLL { | ||
| 137 | my ($self, $fd) = @_; | ||
| 138 | |||
| 139 | delete $self->{'_watches'}{$fd} or $self->_handle_extra_stop_poll($fd); | ||
| 140 | |||
| 141 | return; | ||
| 142 | } | ||
| 143 | |||
| 144 | 1; |