| 1 | package Net::Curl::Simple::Async::Select; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Net::Curl::Multi; | ||
| 6 | use base qw(Net::Curl::Multi); | ||
| 7 | BEGIN { | ||
| 8 | no strict 'refs'; | ||
| 9 | if ( defined ${ 'Coro::VERSION' } ) { | ||
| 10 | require Coro::Select; | ||
| 11 | Coro::Select->import( 'select' ); | ||
| 12 | } | ||
| 13 | } | ||
| 14 | |||
| 15 | sub _rip_child($$) | ||
| 16 | { | ||
| 17 | my ( $multi, $needle ) = @_; | ||
| 18 | |||
| 19 | while ( my ( $msg, $easy, $result ) = $multi->info_read() ) { | ||
| 20 | if ( $msg == Net::Curl::Multi::CURLMSG_DONE ) { | ||
| 21 | $multi->remove_handle( $easy ); | ||
| 22 | $easy->_finish( $result ); | ||
| 23 | |||
| 24 | if ( not $needle or $needle == $easy ) { | ||
| 25 | return $easy; | ||
| 26 | } | ||
| 27 | } else { | ||
| 28 | die "I don't know what to do with message $msg.\n"; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | return undef; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _loop($$) | ||
| 35 | { | ||
| 36 | my ( $multi, $needle ) = @_; | ||
| 37 | |||
| 38 | my $active = $multi->handles; | ||
| 39 | |||
| 40 | while ( $active ) { | ||
| 41 | my $t = $multi->timeout; | ||
| 42 | if ( $t != 0 ) { | ||
| 43 | $t = 10000 if $t < 0; | ||
| 44 | my ( $r, $w, $e ) = $multi->fdset; | ||
| 45 | |||
| 46 | select $r, $w, $e, $t / 1000; | ||
| 47 | } | ||
| 48 | |||
| 49 | my $ret = $multi->perform(); | ||
| 50 | if ( $active != $ret ) { | ||
| 51 | $ret = _rip_child( $multi, $needle ); | ||
| 52 | return $ret if $ret; | ||
| 53 | $active = $multi->handles; | ||
| 54 | } | ||
| 55 | }; | ||
| 56 | |||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub get_one | ||
| 61 | { | ||
| 62 | my ( $multi, $easy ) = @_; | ||
| 63 | |||
| 64 | my $ret = _rip_child( $multi, $easy ); | ||
| 65 | return $ret if $ret; | ||
| 66 | |||
| 67 | return _loop( $multi, $easy ); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | 1; | ||
| 72 | |||
| 73 | # vim: ts=4:sw=4 |