Simple/Async/CoroEV.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 161 CC 15 SCAR 24.1
CC
15
Cov
0%
_cb_socket
20.0
_rip_child
20.0
BEGIN
12.0
CRAP 11.1
1 package Net::Curl::Simple::Async::CoroEV;
2  
3 use strict;
4 use warnings;
5 use Coro::EV;
6 use Coro::Signal;
7 use Net::Curl::Multi qw(/^CURL_POLL_/ /^CURL_CSELECT_/);
8 use base qw(Net::Curl::Multi);
9  
10 BEGIN {
11 if ( not Net::Curl::Multi->can( 'CURLMOPT_TIMERFUNCTION' ) ) {
12 die "Net::Curl::Multi is missing timer callback,\n" .
13 "rebuild Net::Curl with libcurl 7.16.0 or newer\n";
14 }
15  
16 # make sure those constants match, so we won't have to do any conversions
17 if ( EV::READ != CURL_CSELECT_IN or EV::WRITE != CURL_CSELECT_OUT
18 or CURL_CSELECT_IN != CURL_POLL_IN
19 or CURL_CSELECT_OUT != CURL_POLL_OUT
20 or ( CURL_CSELECT_IN | CURL_CSELECT_OUT ) != CURL_POLL_INOUT
21 ) {
22 die "Expected EV::READ == CURL_CSELECT_IN and " .
23 "EV::WRITE == CURL_CSELECT_OUT\n";
24 }
25 }
26  
27 use constant {
28 CONDVAR => 0,
29 ACTIVE => 1,
30 TIMER => 2,
31 LAST_EASY => 3,
32 };
33  
34 sub new
35 {
36 my $class = shift;
37  
38 my $multi = $class->SUPER::new( [ undef, -1, undef ] );
39  
40 $multi->setopt( Net::Curl::Multi::CURLMOPT_SOCKETFUNCTION,
41 \&_cb_socket );
42 $multi->setopt( Net::Curl::Multi::CURLMOPT_TIMERFUNCTION,
43 \&_cb_timer );
44  
45 return $multi;
46 }
47  
48  
49 sub _cb_socket
50 {
51 my ( $multi, $easy, $socket, $poll, $watcher ) = @_;
52  
53 if ( $poll == CURL_POLL_REMOVE ) {
54 # delete the watcher
55 $multi->assign( $socket )
56 if $watcher;
57 } else {
58 if ( $watcher ) {
59 $watcher->events( $poll );
60 } else {
61 $watcher = EV::io $socket, $poll, sub {
62 socket_action( $multi, $socket, $_[1] );
63 };
64 $multi->assign( $socket, $watcher );
65 }
66 }
67  
68 return 1;
69 }
70  
71  
72 sub _cb_timer
73 {
74 my ( $multi, $timeout_ms ) = @_;
75  
76 my $t = $multi->[ TIMER ] ||= EV::timer 10, 10, sub {
77 socket_action( $multi );
78 };
79  
80 if ( $timeout_ms < 0 ) {
81 if ( $multi->handles ) {
82 $t->set( 10, 10 );
83 } else {
84 $multi->[ TIMER ] = undef;
85 }
86 } else {
87 # This will trigger timeouts if there are any.
88 $t->set( $timeout_ms / 1000, 10 );
89 }
90  
91 return 1;
92 }
93  
94 sub add_handle($$)
95 {
96 my $multi = shift;
97 my $easy = shift;
98  
99 $multi->[ ACTIVE ] = -1;
100 $multi->SUPER::add_handle( $easy );
101 }
102  
103 sub _rip_child
104 {
105 my $multi = shift;
106  
107 while ( my ( $msg, $easy, $result ) = $multi->info_read() ) {
108 if ( $msg == Net::Curl::Multi::CURLMSG_DONE ) {
109 my $ecv = delete $easy->{cv};
110 my $mcv = $multi->[ CONDVAR ];
111 $multi->[ CONDVAR ] = undef;
112  
113 $multi->remove_handle( $easy );
114 $easy->_finish( $result );
115  
116 $ecv->broadcast if $ecv;
117 if ( $mcv ) {
118 $multi->[ LAST_EASY ] = $easy;
119 $mcv->broadcast;
120 }
121 } else {
122 die "I don't know what to do with message $msg.\n";
123 }
124 }
125 }
126  
127 # perform and call any callbacks that have finished
128 sub socket_action
129 {
130 my $multi = shift;
131  
132 my $active = $multi->SUPER::socket_action( @_ );
133 return if $multi->[ ACTIVE ] == $active;
134  
135 $multi->[ ACTIVE ] = $active;
136  
137 _rip_child( $multi );
138  
139 $multi->[ TIMER ] = undef
140 unless $multi->[ ACTIVE ];
141 }
142  
143 sub get_one
144 {
145 my ( $multi, $easy ) = @_;
146  
147 my $cv = Coro::Signal->new;
148 if ( $easy ) {
149 $easy->{cv} = $cv;
150 _rip_child( $multi );
151 $cv->wait;
152 return $easy;
153 } else {
154 return undef unless $multi->handles;
155 $multi->[ CONDVAR ] = $cv;
156 _rip_child( $multi );
157 $cv->wait;
158 $easy = $multi->[ LAST_EASY ];
159 $multi->[ LAST_EASY ] = undef;
160 return $easy;
161 }
162 }
163  
164 1;