blib/lib/POE/Wheel/Sendfile.pm

filter: Statementstmt 73.4% 105 / 143 Branchbran 50.0% 29 / 58 Conditioncond 41.1% 7 / 17 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 87.5% 14 / 16 Podpod 100% 1 / 1 totaltotal 64.6% 159 / 246 CC 36 SCAR 27.4
CC
36
Cov
65%
__ANON__
56.0
__ANON__
16.6
_sendfile_setup
12.5
CRAP 15.5
↑ Summary
1 package POE::Wheel::Sendfile;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.0200';
7  
8 4 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub DEBUG () { 0 }
13  
14 4 ▶use base qw( POE::Wheel::ReadWrite );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #######################################
17 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 *HANDLE_OUTPUT = \&POE::Wheel::ReadWrite::HANDLE_OUTPUT;
19 4 *STATE_WRITE = \&POE::Wheel::ReadWrite::STATE_WRITE;
20 4 *EVENT_FLUSHED = \&POE::Wheel::ReadWrite::EVENT_FLUSHED;
21 4 *EVENT_ERROR = \&POE::Wheel::ReadWrite::EVENT_ERROR;
22 4 *UNIQUE_ID = \&POE::Wheel::ReadWrite::UNIQUE_ID;
23 4 *DRIVER_BOTH = \&POE::Wheel::ReadWrite::DRIVER_BOTH;
24 4 *AUTOFLUSH = \&POE::Wheel::ReadWrite::AUTOFLUSH;
25 }
26  
27 sub STATE_SENDFILE () { AUTOFLUSH+1 }
28  
29 #######################################
30 4 our $HAVE_SENDFILE;
31 4 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.0
32 4 ▶ unless( defined $HAVE_SENDFILE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $POE::Wheel::Sendfile::HAVE_SENDFILE) 4 0 4
33 4 $HAVE_SENDFILE = 0;
34 0 ▶ eval "use Sys::Sendfile 0.11 ();";
Subroutine BEGIN: called CC=1 SCAR=1.2
35 4 ▶ $HAVE_SENDFILE = 1 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 4 4
36 4 warn $@ if DEBUG and $@;
37 }
38 }
39  
40 #######################################
41 sub sendfile
42 4 ▶{
Subroutine sendfile: called CC=3 SCAR=11.3
Pod documented
43 4 my( $self, $fh ) = @_;
44 # Build a [SENDFILE] hash with details
45 4 my $S = $self->_sendfile_setup( $fh );
46 4 ▶ $S or return;
Branch: 1 branch
BranchTrueFalseTotal
$S or return 4 0 4
47 # Build a select write handler
48 4 ▶ $self->_sendfile_define_write( $S ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_sendfile_define_write($S) or return 4 0 4
49 # Call that handler
50 4 return $poe_kernel->call(
51 $poe_kernel->get_active_session,
52 $self->[STATE_SENDFILE],
53 $self->[HANDLE_OUTPUT]
54 );
55 }
56  
57 #######################################
58 sub _sendfile_setup
59 9 ▶{
Subroutine _sendfile_setup: called CC=11 SCAR=25.2
60 9 my( $self, $fh ) = @_;
61 9 my $event_error = \$self->[EVENT_ERROR];
62 9 my $unique_id = \$self->[UNIQUE_ID];
63  
64 9 ▶ if( $self->[STATE_SENDFILE] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[18]) 0 9 9
65 0 $@ = "Already sending a file";
66 0 return;
67 }
68 9 my $S = {};
69 9 ▶ if( 'HASH' eq ref $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref $fh) 6 3 9
70 6 $S = $fh;
71 6 $fh = delete $S->{file};
72 }
73 9 ▶ unless( ref $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh) { } 2 7 9
74 7 my $io = IO::File->new;
75 7 ▶ unless( $io->open( $fh ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($io->open($fh)) 0 7 7
76 0 my $me = $poe_kernel->get_active_session;
77 0 ▶ $$event_error && $poe_kernel->call(
Branch: 1 branch
BranchTrueFalseTotal
$$event_error and $POE::Wheel::Sendfile::poe_kernel->call($me, $$event_error, "open", $! + 0, "${fh}: $!", $unique_id) 0 0 0
78 $me, $$event_error, 'open', ($!+0), "$fh: $!", $unique_id
79 );
80 0 return;
81 }
82 7 $S->{file} = $fh;
83 7 $S->{fh} = $io;
84 }
85 else {
86 2 $S->{fh} = $fh;
87 }
88  
89 9 ▶ unless( $S->{offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($S->{"offset"}) 7 2 9
90 7 $S->{offset} = 0;
91 }
92  
93 9 ▶ unless( $S->{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($S->{'size'}) { } 4 5 9
94 5 $S->{size} = (stat $S->{fh})[7] - $S->{offset};
95 }
96 else {
97 4 $S->{size} += $S->{offset};
98 }
99 9 ▶ unless( $S->{blocksize} or $HAVE_SENDFILE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($S->{"blocksize"} or $POE::Wheel::Sendfile::HAVE_SENDFILE) 9 0 9
Condition33%1/3
$S->{"blocksize"} or $POE::Wheel::Sendfile::HAVE_SENDFILE
l!l&&r!l&&!r
009
MC/DC0%0/2
$S->{"blocksize"} or $POE::Wheel::Sendfile::HAVE_SENDFILE
$S->{"blocksize"} $POE::Wheel::Sendfile::HAVE_SENDFILE
Truth table33%1/3
A or B
A= $S->{"blocksize"}B= $POE::Wheel::Sendfile::HAVE_SENDFILE
ABresult
1X1
011
000
100 9 $S->{blocksize} = eval {
101 9 $SIG{__DIE__} = 'DEFAULT';
102 9 my $h = $self->[HANDLE_OUTPUT];
103 9 return unpack "i",
104 getsockopt($h, Socket::SOL_SOCKET(), Socket::SO_SNDBUF());
105 };
106 9 ▶ $S->{blocksize} ||= 7500;
Condition100%2/2
$S->{"blocksize"} ||= 7500
l!l
54
MC/DC100%1/1
$S->{"blocksize"} ||= 7500
$S->{"blocksize"}
Truth table100%2/2
A
A= $S->{"blocksize"}
Aresult
11
00
107 }
108  
109 9 return $S;
110 }
111  
112 #######################################
113 sub _sendfile_define_write
114 4 ▶{
Subroutine _sendfile_define_write: called CC=3 SCAR=11.2
115 4 my( $self, $S ) = @_;
116  
117 4 my @need = (
118 \$self->[EVENT_ERROR], # $event_error
119 \$self->[EVENT_FLUSHED], # $event_flush
120 \$self->[STATE_WRITE], # $state_write
121 \$self->[STATE_SENDFILE], # $state_sendfile
122 $self->[UNIQUE_ID], # $unique_id
123 $self->[DRIVER_BOTH], # $driver
124 \$S, # $sendfile
125 1 # $first
126 );
127  
128 4 my $state;
129 4 ▶ if( $HAVE_SENDFILE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($POE::Wheel::Sendfile::HAVE_SENDFILE) { } 0 4 4
130 0 $state = _mk_sendfile( \@need );
131 }
132 else {
133 4 $state = _mk_fallback( \@need );
134 }
135 4 ▶ die unless $state;
Branch: 1 branch
BranchTrueFalseTotal
unless $state 0 4 4
136 4 $self->[STATE_SENDFILE] = ref( $self ) . " ($self->[UNIQUE_ID]) -> sendfile write",
137 $poe_kernel->state( $self->[STATE_SENDFILE], $state );
138 4 $poe_kernel->select_write($self->[HANDLE_OUTPUT]);
139 4 $poe_kernel->select_write( $self->[HANDLE_OUTPUT],
140 $self->[STATE_SENDFILE]
141 );
142 4 return 1;
143 }
144  
145 # This is where all the work happens
146 # We call sendfile(), check it's return, update the offset, then
147 # wait for the flushed-event to happen.
148 # If we are at the end of the file, we let the flushed-event go to the
149 # OutputEvent handler
150 sub _mk_sendfile
151 0 ▶{
Subroutine _mk_sendfile: not called CC=1 SCAR=6.9
152 my(
153 $event_error, # \$self->[EVENT_ERROR];
154 $event_flushed, # \$self->[EVENT_FLUSHED];
155 $state_write, # \$self->[STATE_WRITE];
156 $state_sendfile, # \$self->[STATE_SENDFILE];
157 $unique_id, # $self->[UNIQUE_ID];
158 $driver, # $self->[DRIVER_BOTH];
159 $sendfile, # \$S;
160 $first # 1;
161 0 ) = @{ $_[0] };
162  
163 0 ▶ return sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
164 0 0 && CRIMSON_SCOPE_HACK('<');
165 0 my ($k, $me, $handle) = @_[KERNEL, SESSION, ARG0];
166  
167 0 my $need = $$sendfile->{size} - $$sendfile->{offset};
168 0 DEBUG and warn "sendfile #$unique_id, offset=$$sendfile->{offset}, need=$need, size=$$sendfile->{size}";
169 my $rv = Sys::Sendfile::sendfile(
170 0 $handle, $$sendfile->{fh}, $need, $$sendfile->{offset} );
171  
172 0 DEBUG and warn "sendfile #$unique_id, rv=$rv";
173  
174 # sendfile(2) - Applications may wish to fall back to
175 # read(2)/write(2) in the case where sendfile() fails with EINVAL or
176 # ENOSYS.
177  
178 0 ▶ unless( defined $rv and $rv >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv and $rv >= 0) 0 0 0
Condition0%0/3
defined $rv and $rv >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rv and $rv >= 0
defined $rv $rv >= 0
Truth table0%0/3
A and B
A= defined $rvB= $rv >= 0
ABresult
0X0
100
111
179 0 ▶ $$event_error && $k->call(
Branch: 1 branch
BranchTrueFalseTotal
$$event_error and $k->call($me, $$event_error, "sendfile", $! + 0, "$!", $unique_id) 0 0 0
180 $me, $$event_error, 'sendfile', ($!+0), "$!", $unique_id
181 );
182 0 return;
183 }
184  
185 0 $$sendfile->{offset} += $rv;
186 0 ▶ if( $rv == 0 or $$sendfile->{offset} >= $$sendfile->{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 0 or $$sendfile->{"offset"} >= $$sendfile->{"size"}) 0 0 0
Condition0%0/3
$rv == 0 or $$sendfile->{"offset"} >= $$sendfile->{"size"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$rv == 0 or $$sendfile->{"offset"} >= $$sendfile->{"size"}
$rv == 0 $$sendfile->{"offset"} >= $$sendfile->{"size"}
Truth table0%0/3
A or B
A= $rv == 0B= $$sendfile->{"offset"} >= $$sendfile->{"size"}
ABresult
1X1
011
000
187 0 DEBUG and warn "sendfile #$unique_id, done";
188 # We want the last flush to do to the session
189 0 $k->select_write( $handle );
190 0 $k->select_write( $handle, $$state_write );
191 # Remove this state
192 0 $k->state( $$state_sendfile );
193 # Nothing more to send
194 0 $$state_sendfile = undef();
195 0 return 1;
196 }
197  
198 0 ▶ if( $first ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 0 0 0
199 # Turn the select on
200 0 $k->select_resume_write( $handle );
201 0 $first = 0;
202 }
203 0 return 1;
204 0 };
205 }
206  
207  
208 #
209 # Fallback to doing it by hand
210 #
211 sub _mk_fallback
212 4 ▶{
Subroutine _mk_fallback: called CC=2 SCAR=7.4
213 my(
214 $event_error, # \$self->[EVENT_ERROR];
215 $event_flushed, # \$self->[EVENT_FLUSHED];
216 $state_write, # \$self->[STATE_WRITE];
217 $state_sendfile, # \$self->[STATE_SENDFILE];
218 $unique_id, # $self->[UNIQUE_ID];
219 $driver, # $self->[DRIVER_BOTH];
220 $sendfile, # \$S;
221 $first # 1;
222 4 ) = @{ $_[0] };
223  
224 4 my $rv = sysseek( $$sendfile->{fh}, $$sendfile->{offset}, 0 );
225 4 ▶ unless( defined $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 4 4
226 0 $@ = "Unable to sysseek to $$sendfile->{offset}: $!";
227 0 return;
228 }
229  
230 4 my $buffer = '';
231 8 ▶ return sub {
Subroutine __ANON__: called CC=13 SCAR=28.1
232 8 0 && CRIMSON_SCOPE_HACK('<');
233  
234 8 my ($k, $me, $handle) = @_[KERNEL, SESSION, ARG0];
235  
236 # Don't read to much if we only want a little
237 8 my $size = $$sendfile->{blocksize};
238 8 ▶ if( $size+$$sendfile->{offset} > $$sendfile->{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size + $$sendfile->{"offset"} > $$sendfile->{"size"}) 4 4 8
239 4 $size = $$sendfile->{size} - $$sendfile->{offset};
240 }
241  
242 8 my $rv = sysread( $$sendfile->{fh}, $buffer, $size );
243 8 ▶ unless( defined $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 8 8
244 0 ▶ $$event_error && $k->call(
Branch: 1 branch
BranchTrueFalseTotal
$$event_error and $k->call($me, $$event_error, "sysread", $! + 0, $!, $unique_id) 0 0 0
245 $me, $$event_error, 'sysread', ($!+0), $!, $unique_id
246 );
247 0 return;
248 }
249  
250 8 $$sendfile->{offset} += $rv;
251 8 ▶ if( $rv == 0 || $$sendfile->{offset} >= $$sendfile->{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 0 or $$sendfile->{"offset"} >= $$sendfile->{"size"}) 4 4 8
Condition66%2/3
$rv == 0 or $$sendfile->{"offset"} >= $$sendfile->{"size"}
l!l&&r!l&&!r
044
MC/DC50%1/2
$rv == 0 or $$sendfile->{"offset"} >= $$sendfile->{"size"}
$rv == 0 $$sendfile->{"offset"} >= $$sendfile->{"size"}
Truth table66%2/3
A or B
A= $rv == 0B= $$sendfile->{"offset"} >= $$sendfile->{"size"}
ABresult
1X1
011
000
252 # Nothing more to send
253 4 $$sendfile = undef();
254 # We want the last flush to go to the session
255 4 $k->select_write( $handle );
256 4 $k->select_write( $handle, $$state_write );
257 # Remove this state
258 4 $k->state( $$state_sendfile );
259 # Nothing more to send
260 4 $$state_sendfile = undef();
261 }
262
263 8 my $err = 0;
264 8 ▶ if( $rv != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv != 0) 8 0 8
265 8 ▶ if( $driver->put( [$buffer] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver->put([$buffer])) 8 0 8
266 8 $driver->flush( $handle );
267 8 ▶ if( $! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 8 8
268 0 ▶ $$event_error && $k->call(
Branch: 1 branch
BranchTrueFalseTotal
$$event_error and $k->call($me, $$event_error, "syswrite", $! + 0, $!, $unique_id) 0 0 0
269 $me, $$event_error, 'syswrite', ($!+0), $!, $unique_id
270 );
271 0 $err = 1;
272 }
273 }
274 8 ▶ if( $first and not $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first and not $err) 4 4 8
Condition66%2/3
$first and not $err
!ll&&!rl&&r
404
MC/DC50%1/2
$first and not $err
$first not $err
Truth table66%2/3
A and not(B)
A= $firstB= not $err
ABresult
0X0
100
111
275 # Turn the select on
276 4 $k->select_resume_write( $handle );
277 4 $first = 0;
278 }
279 }
280 8 ▶ return if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 8 8
281 8 return 1;
282 4 };
283 }
284  
285 4 1;
286  
287 __END__
↑ Summary