| 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 Net::Curl::Promiser::FDFHStore (); | ||
| 60 | |||
| 61 | use constant PROMISE_CLASS => 'Mojo::Promise'; | ||
| 62 | |||
| 63 | #---------------------------------------------------------------------- | ||
| 64 | |||
| 65 | *add_handle_p = __PACKAGE__->can('add_handle'); | ||
| 66 | |||
| 67 | #---------------------------------------------------------------------- | ||
| 68 | |||
| 69 | sub _INIT { | ||
| 70 | my ($self, $args_ar) = @_; | ||
| 71 | |||
| 72 | $self->setopt( Net::Curl::Multi::CURLMOPT_TIMERFUNCTION(), \&_cb_timer ); | ||
| 73 | $self->setopt( Net::Curl::Multi::CURLMOPT_TIMERDATA(), $self ); | ||
| 74 | |||
| 75 | $self->{'_fhstore'} = Net::Curl::Promiser::FDFHStore->new(); | ||
| 76 | |||
| 77 | return; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _cb_timer { | ||
| 81 | my ($multi, $timeout_ms, $self) = @_; | ||
| 82 | |||
| 83 | my ($ot, $rt) = delete @{$self}{'onetimer','recurtimer'}; | ||
| 84 | Mojo::IOLoop->remove($ot) if $ot; | ||
| 85 | Mojo::IOLoop->remove($rt) if $rt; | ||
| 86 | |||
| 87 | if ($timeout_ms < 0) { | ||
| 88 | if ($multi->handles()) { | ||
| 89 | my $cb = sub { | ||
| 90 | $self->_time_out_in_loop(); | ||
| 91 | }; | ||
| 92 | |||
| 93 | $self->{'onetimer'} = Mojo::IOLoop->timer( 5 => $cb ); | ||
| 94 | $self->{'recurtimer'} = Mojo::IOLoop->recurring( 5 => $cb ); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | else { | ||
| 98 | $self->{onetimer} = Mojo::IOLoop->timer( | ||
| 99 | $timeout_ms / 1000, | ||
| 100 | sub { | ||
| 101 | $self->_time_out_in_loop(); | ||
| 102 | }, | ||
| 103 | ); | ||
| 104 | } | ||
| 105 | |||
| 106 | return 1; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _fh_is_stale { | ||
| 110 | local $!; | ||
| 111 | |||
| 112 | stat $_[0] or do { | ||
| 113 | return 1 if $!{'EBADF'}; | ||
| 114 | die "stat() on socket: $!"; | ||
| 115 | }; | ||
| 116 | |||
| 117 | return 0; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _io { | ||
| 121 | my ($self, $fd, $read_yn, $write_yn) = @_; | ||
| 122 | |||
| 123 | my $socket = $self->{'_watched_sockets'}{$fd} ||= do { | ||
| 124 | my $s = $self->{'_fhstore'}->get_checked($fd); | ||
| 125 | |||
| 126 | Mojo::IOLoop->singleton->reactor->io( | ||
| 127 | $s, | ||
| 128 | sub { | ||
| 129 | $self->_process_in_loop($fd, $_[1] ? Net::Curl::Multi::CURL_CSELECT_OUT() : Net::Curl::Multi::CURL_CSELECT_IN()); | ||
| 130 | }, | ||
| 131 | ); | ||
| 132 | |||
| 133 | $s; | ||
| 134 | }; | ||
| 135 | |||
| 136 | Mojo::IOLoop->singleton->reactor->watch( | ||
| 137 | $socket, | ||
| 138 | $read_yn, | ||
| 139 | $write_yn, | ||
| 140 | ); | ||
| 141 | |||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _SET_POLL_IN { | ||
| 146 | my ($self, $fd) = @_; | ||
| 147 | |||
| 148 | $self->_io( $fd, 1, 0 ); | ||
| 149 | |||
| 150 | return; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub _SET_POLL_OUT { | ||
| 154 | my ($self, $fd) = @_; | ||
| 155 | |||
| 156 | $self->_io( $fd, 0, 1 ); | ||
| 157 | |||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _SET_POLL_INOUT { | ||
| 162 | my ($self, $fd) = @_; | ||
| 163 | |||
| 164 | $self->_io( $fd, 1, 1 ); | ||
| 165 | |||
| 166 | return; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub _STOP_POLL { | ||
| 170 | my ($self, $fd) = @_; | ||
| 171 | |||
| 172 | if (my $socket = delete $self->{'_watched_sockets'}{$fd}) { | ||
| 173 | Mojo::IOLoop->remove($socket); | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | $self->_handle_extra_stop_poll($fd); | ||
| 177 | } | ||
| 178 | |||
| 179 | return; | ||
| 180 | } | ||
| 181 | |||
| 182 | #---------------------------------------------------------------------- | ||
| 183 | |||
| 184 | 1; |