Simple/Async/EV.pm untested

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