blib/lib/Net/Curl/Multi/EV.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 123 CC 7 SCAR 33.7
CC
7
Cov
0%
curl_ev
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 29.0
1 package Net::Curl::Multi::EV;
2  
3 use strict;
4 use warnings;
5 use EV;
6 use Net::Curl::Easy qw(CURLE_OPERATION_TIMEDOUT);
7 use Net::Curl::Multi qw(CURLMSG_DONE CURL_SOCKET_TIMEOUT /^CURL_POLL_/ /^CURL_CSELECT_/ CURLMOPT_SOCKETFUNCTION CURLMOPT_TIMERFUNCTION);
8 use Scalar::Util qw(weaken);
9  
10 our $VERSION = '0.08';
11  
12 BEGIN {
13 Net::Curl::Multi->can('CURLMOPT_TIMERFUNCTION') or
14 die "Net::Curl::Multi is missing timer callback,\nrebuild Net::Curl with libcurl 7.16.0 or newer\n";
15 }
16  
17  
18 sub curl_ev {
19 my ($multi) = @_;
20  
21 my %finish = (); # easy => finish
22 my %timer = (); # easy => sub
23  
24 # perform and call any callbacks that have finished
25 my $multi_active = -1;
26 my $socket_action = sub {
27  
28 my $active = $multi->socket_action(@_);
29 return if $multi_active == $active;
30 $multi_active = $active;
31  
32 while (my ($msg, $easy, $result) = $multi->info_read()) {
33 if ($msg == CURLMSG_DONE) {
34 delete $timer{$easy};
35 $multi->remove_handle($easy);
36 $finish{$easy}->($easy, $result);
37 delete $finish{$easy};
38 } else {
39 die "I don't know what to do with message $msg.\n";
40 }
41 }
42 };
43  
44 my $cb_timeout = sub { $socket_action->(CURL_SOCKET_TIMEOUT) };
45  
46 my $add_handle_timer;
47  
48 # add one handle and kickstart download
49 my $add_handle = sub {
50 my ($easy, $finish, $timeout) = @_;
51  
52 $finish{$easy} = $finish;
53  
54 # Calling socket_action with default arguments will trigger
55 # socket callback and register IO events.
56 #
57 # It _must_ be called _after_ add_handle(); EV will take care
58 # of that.
59 #
60 # We are delaying the call because in some cases socket_action
61 # may finish inmediatelly (i.e. there was some error or we used
62 # persistent connections and server returned data right away)
63 # and it could confuse our application -- it would appear to
64 # have finished before it started.
65  
66 $add_handle_timer = EV::timer 0, 0, $cb_timeout;
67  
68 $multi->add_handle($easy);
69  
70 if ($timeout) {
71 $timer{$easy} = EV::timer $timeout, 0, sub {
72 delete $timer{$easy};
73 $multi->remove_handle($easy);
74 $finish{$easy}->($easy, bless( do{\(my $o = CURLE_OPERATION_TIMEDOUT)}, 'Net::Curl::Easy::Code' ));
75 delete $finish{$easy};
76 };
77 }
78 };
79  
80  
81 # socket callback: will be called by curl any time events on some
82 # socket must be updated
83 my %wr = (); # socket => $w
84 my %ww = (); # socket => $w
85 my $cb_socket = sub {
86 my ($multi, $easy, $socket, $poll) = @_;
87  
88 # Right now $socket belongs to that $easy, but it can be
89 # shared with another easy handle if server supports persistent
90 # connections.
91 # This is why we register socket events inside multi object
92 # and not $easy.
93  
94 # register read event
95 if ($poll == CURL_POLL_IN or $poll == CURL_POLL_INOUT) {
96 $wr{$socket} ||= EV::io $socket, EV::READ, sub { $socket_action->($socket, CURL_CSELECT_IN) };
97 } else {
98 delete $wr{$socket};
99 }
100  
101 # register write event
102 if ($poll == CURL_POLL_OUT or $poll == CURL_POLL_INOUT) {
103 $ww{$socket} ||= EV::io $socket, EV::WRITE, sub { $socket_action->($socket, CURL_CSELECT_OUT) };
104 } else {
105 delete $ww{$socket};
106 }
107  
108 return 1;
109 };
110  
111  
112 # timer callback: It triggers timeout update. Timeout value tells
113 # us how soon socket_action must be called if there were no actions
114 # on sockets. This will allow curl to trigger timeout events.
115 my $timer;
116 my $cb_timer = sub {
117 my ($multi, $timeout_ms) = @_;
118  
119 if ($timeout_ms < 0) {
120 # Negative timeout means there is no timeout at all.
121 # Normally happens if there are no handles anymore.
122 #
123 # However, curl_multi_timeout(3) says:
124 #
125 # Note: if libcurl returns a -1 timeout here, it just means
126 # that libcurl currently has no stored timeout value. You
127 # must not wait too long (more than a few seconds perhaps)
128 # before you call curl_multi_perform() again.
129  
130 $timer = EV::timer 10, 10, $cb_timeout;
131 } else {
132 # This will trigger timeouts if there are any.
133 my $t = $timeout_ms / 1000;
134 $timer = EV::timer $t, $t, $cb_timeout;
135 }
136  
137 return 1;
138 };
139  
140 $multi->setopt(CURLMOPT_SOCKETFUNCTION, $cb_socket);
141 $multi->setopt(CURLMOPT_TIMERFUNCTION, $cb_timer);
142  
143 weaken $multi;
144  
145 return $add_handle;
146 }
147  
148  
149 1;
150  
151 __END__