| 1 | package Net::Curl::Promiser::Mojo; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =encoding utf-8 | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::Curl::Promiser::Mojo - support for L<Mojolicious> | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | my $promiser = Net::Curl::Promiser::Mojo->new(); | ||
| 15 | |||
| 16 | my $handle = Net::Curl::Easy->new(); | ||
| 17 | $handle->setopt( CURLOPT_URL() => $url ); | ||
| 18 | |||
| 19 | $promiser->add_handle_p($handle)->then( | ||
| 20 | sub { print "$url completed.$/" }, | ||
| 21 | sub { warn "$url failed: " . shift }, | ||
| 22 | )->wait(); | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This module provides a L<Mojolicious>-compatible interface for | ||
| 27 | L<Net::Curl::Promiser>. | ||
| 28 | |||
| 29 | See F</examples> in the distribution for a fleshed-out demonstration. | ||
| 30 | |||
| 31 | =head1 MOJOLICIOUS SPECIALTIES | ||
| 32 | |||
| 33 | This module’s interface is that provided by | ||
| 34 | L<Net::Curl::Promiser::LoopBase>, with the following tweaks to make it | ||
| 35 | more Mojo-friendly: | ||
| 36 | |||
| 37 | =over | ||
| 38 | |||
| 39 | =item * This module uses L<Mojo::Promise> rather than L<Promise::ES6> | ||
| 40 | as its promise implementation. | ||
| 41 | |||
| 42 | =item * C<add_handle_p()> is an alias for the base class’s C<add_handle()>. | ||
| 43 | This alias conforms to Mojo’s convention of postfixing C<_p> onto the end | ||
| 44 | of promise-returning functions. | ||
| 45 | |||
| 46 | =back | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | #---------------------------------------------------------------------- | ||
| 51 | |||
| 52 | use parent 'Net::Curl::Promiser::LoopBase'; | ||
| 53 | |||
| 54 | use Net::Curl::Multi (); | ||
| 55 | |||
| 56 | use Mojo::IOLoop (); | ||
| 57 | use Mojo::Promise (); | ||
| 58 | |||
| 59 | use constant PROMISE_CLASS => 'Mojo::Promise'; | ||
| 60 | |||
| 61 | #---------------------------------------------------------------------- | ||
| 62 | |||
| 63 | *add_handle_p = __PACKAGE__->can('add_handle'); | ||
| 64 | |||
| 65 | #---------------------------------------------------------------------- | ||
| 66 | |||
| 67 | sub _INIT { | ||
| 68 | my ($self, $args_ar) = @_; | ||
| 69 | |||
| 70 | $self->setopt( Net::Curl::Multi::CURLMOPT_TIMERFUNCTION(), \&_cb_timer ); | ||
| 71 | $self->setopt( Net::Curl::Multi::CURLMOPT_TIMERDATA(), $self ); | ||
| 72 | |||
| 73 | $self->{'_fhstore'} = Net::Curl::Promiser::Mojo::FDFHStore->new(); | ||
| 74 | |||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _cb_timer { | ||
| 79 | my ($multi, $timeout_ms, $self) = @_; | ||
| 80 | |||
| 81 | my ($ot, $rt) = delete @{$self}{'onetimer','recurtimer'}; | ||
| 82 | Mojo::IOLoop->remove($ot) if $ot; | ||
| 83 | Mojo::IOLoop->remove($rt) if $rt; | ||
| 84 | |||
| 85 | if ($timeout_ms < 0) { | ||
| 86 | if ($multi->handles()) { | ||
| 87 | my $cb = sub { | ||
| 88 | $self->_time_out_in_loop(); | ||
| 89 | }; | ||
| 90 | |||
| 91 | $self->{'onetimer'} = Mojo::IOLoop->timer( 5 => $cb ); | ||
| 92 | $self->{'recurtimer'} = Mojo::IOLoop->recurring( 5 => $cb ); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | $self->{onetimer} = Mojo::IOLoop->timer( | ||
| 97 | $timeout_ms / 1000, | ||
| 98 | sub { | ||
| 99 | $self->_time_out_in_loop(); | ||
| 100 | }, | ||
| 101 | ); | ||
| 102 | } | ||
| 103 | |||
| 104 | return 1; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _fh_is_stale { | ||
| 108 | local $!; | ||
| 109 | |||
| 110 | stat $_[0] or do { | ||
| 111 | return 1 if $!{'EBADF'}; | ||
| 112 | die "stat() on socket: $!"; | ||
| 113 | }; | ||
| 114 | |||
| 115 | return 0; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _io { | ||
| 119 | my ($self, $fd, $read_yn, $write_yn) = @_; | ||
| 120 | |||
| 121 | my $socket = $self->{'_watched_sockets'}{$fd} ||= do { | ||
| 122 | my $s = $self->{'_fhstore'}->get_checked($fd); | ||
| 123 | |||
| 124 | Mojo::IOLoop->singleton->reactor->io( | ||
| 125 | $s, | ||
| 126 | sub { | ||
| 127 | $self->_process_in_loop($fd, $_[1] ? Net::Curl::Multi::CURL_CSELECT_OUT() : Net::Curl::Multi::CURL_CSELECT_IN()); | ||
| 128 | }, | ||
| 129 | ); | ||
| 130 | |||
| 131 | $s; | ||
| 132 | }; | ||
| 133 | |||
| 134 | Mojo::IOLoop->singleton->reactor->watch( | ||
| 135 | $socket, | ||
| 136 | $read_yn, | ||
| 137 | $write_yn, | ||
| 138 | ); | ||
| 139 | |||
| 140 | return; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub _SET_POLL_IN { | ||
| 144 | my ($self, $fd) = @_; | ||
| 145 | |||
| 146 | $self->_io( $fd, 1, 0 ); | ||
| 147 | |||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub _SET_POLL_OUT { | ||
| 152 | my ($self, $fd) = @_; | ||
| 153 | |||
| 154 | $self->_io( $fd, 0, 1 ); | ||
| 155 | |||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _SET_POLL_INOUT { | ||
| 160 | my ($self, $fd) = @_; | ||
| 161 | |||
| 162 | $self->_io( $fd, 1, 1 ); | ||
| 163 | |||
| 164 | return; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _STOP_POLL { | ||
| 168 | my ($self, $fd) = @_; | ||
| 169 | |||
| 170 | if (my $socket = delete $self->{'_watched_sockets'}{$fd}) { | ||
| 171 | Mojo::IOLoop->remove($socket); | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | warn "Mojo “extra” stop: [$fd]"; | ||
| 175 | } | ||
| 176 | |||
| 177 | return; | ||
| 178 | } | ||
| 179 | |||
| 180 | #---------------------------------------------------------------------- | ||
| 181 | |||
| 182 | package Net::Curl::Promiser::Mojo::FDFHStore; | ||
| 183 | |||
| 184 | # Mojo::IOLoop doesn’t track FDs, just Perl filehandles. That means | ||
| 185 | # that, in order to track libcurl’s file descriptors, we have to | ||
| 186 | # create Perl filehandles for them. But we also have to ensure that | ||
| 187 | # those filehandles aren’t garbage-collected (GC) because GC will | ||
| 188 | # cause Perl to close() the file descriptors, which will break | ||
| 189 | # libcurl. | ||
| 190 | # | ||
| 191 | # So we keep a reference to each created socket via this object: | ||
| 192 | |||
| 193 | sub new { bless {}, shift } | ||
| 194 | |||
| 195 | sub _create { | ||
| 196 | open my $s, '+>>&=' . $_[0] or die "FD ($_[0]) to Perl FH failed: $!"; | ||
| 197 | $s; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub get_checked { | ||
| 201 | if (my $s = $_[0]->{ $_[1] }) { | ||
| 202 | |||
| 203 | # What if libcurl has closed the underlying file descriptor, though? | ||
| 204 | # We need to ensure that that hasn’t happened; if it has, then | ||
| 205 | # get rid of the filehandle and create a new one. This incurs an | ||
| 206 | # unfortunate overhead, but is there a better way? | ||
| 207 | return $s if _fh_is_active($s); | ||
| 208 | } | ||
| 209 | |||
| 210 | return $_[0]->{ $_[1] } = _create( $_[1] ); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _fh_is_active { | ||
| 214 | local $!; | ||
| 215 | |||
| 216 | stat $_[0] or do { | ||
| 217 | return 0 if $!{'EBADF'}; | ||
| 218 | die "stat() on socket: $!"; | ||
| 219 | }; | ||
| 220 | |||
| 221 | return 1; | ||
| 222 | } | ||
| 223 | |||
| 224 | 1; |