blib/lib/IO/AsyncX/Sendfile.pm

filter: Statementstmt 91.6% 44 / 48 Branchbran 54.5% 12 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 82.2% 65 / 79 SCAR 25.8
CC
12
Cov
80%
__ANON__
9.5
__ANON__
4.8
__ANON__
2.1
CRAP 13.2
↑ Summary
1 package IO::AsyncX::Sendfile;
2 # ABSTRACT: sendfile support for IO::Async
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.002';
7  
8 =head1 NAME
9  
10 IO::AsyncX::Sendfile - adds support for L<Sys::Sendfile> to L<IO::Async::Stream>
11  
12 =head1 VERSION
13  
14 version 0.002
15  
16 =head1 SYNOPSIS
17  
18 $stream->sendfile(
19 file => 'somefile',
20 )->on_done(sub {
21 $stream->close;
22 });
23  
24 =head1 DESCRIPTION
25  
26 B<NOTE>: This is currently a proof-of-concept, the actual API may vary in later
27 versions. Eventually this functionality will be incorporated into the generic async
28 filehandling API, so this module is provided as a workaround in the interim.
29  
30 Provides a L</sendfile> method on L<IO::Async::Stream>.
31  
32 =cut
33  
34 2 ▶use Sys::Sendfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use Fcntl qw(SEEK_SET SEEK_END);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use IO::Async::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 =head1 METHODS
40  
41 Note that these methods are injected directly into L<IO::Async::Stream>.
42  
43 =cut
44  
45 =head2 sendfile
46  
47 Write the contents of the file directly to the socket without reading it
48 into memory first (using the kernel's sendfile call if available).
49  
50 Called with the following named parameters:
51  
52 =over 4
53  
54 =item * file - if defined, this will be used as the filename to open
55  
56 =item * fh - if defined, we'll use this as the filehandle
57  
58 =item * length - if defined, send this much data from the file (default is
59 'everything from current position to end')
60  
61 =back
62  
63 Returns a L<Future> which will be resolved with the number of bytes written
64 when successful.
65  
66 Example usage:
67  
68 my $listener = $loop->listen(
69 addr => {
70 family => 'unix',
71 socktype => 'stream',
72 path => 'sendfile.sock',
73 },
74 on_stream => sub {
75 my $stream = shift;
76 $stream->configure(
77 on_read => sub {
78 my ($self, $buffref, $eof) = @_;
79 $$buffref = '';
80 return 0;
81 },
82 );
83 if('send one file') {
84 $stream->sendfile(
85 file => 'test.dat',
86 )->on_done(sub {
87 warn "File send complete: @_\n";
88 $stream->close;
89 });
90 } else {
91 $stream->sendfile(file => 'first.dat');
92 $stream->sendfile(file => 'second.dat');
93 $stream->write('EOF', on_flush => sub { shift->close });
94 }
95 $loop->add($stream);
96 }
97 );
98  
99 If the sendfile call fails, the returned L<Future> will fail with the
100 string exception from $! as the failure reason, with sendfile => numeric $!,
101 remaining bytes as the remaining details:
102  
103 ==> ->fail("Some generic I/O error", "sendfile", EIO, 60000)
104  
105 =cut
106  
107 *IO::Async::Stream::sendfile = sub {
108 32 ▶ my $self = shift;
Subroutine __ANON__: called CC=8 SCAR=22.5
109 32 my %args = @_;
110 32 ▶ die "Stream must be added to loop first" unless $self->loop;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->loop 0 32 32
111  
112 32 ▶ if(defined $args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"file"}) 32 0 32
113 32 ▶ open $args{fh}, '<', $args{file} or die "Could not open " . $args{file} . " for input - $!";
Branch: 1 branch
BranchTrueFalseTotal
open $args{"fh"}, "<", $args{"file"} or die 'Could not open ' . $args{"file"} . " for input - $!" 0 32 32
114 32 binmode $args{fh};
115 }
116 32 ▶ die 'No file?' unless my $fh = delete $args{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless my $fh = delete $args{"fh"} 0 32 32
117  
118 # Work out how much we need to write
119 32 my $total = my $remaining = $args{length};
120 32 ▶ unless(defined $total) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $total) 32 0 32
121 32 my $pos = tell $fh;
122 32 ▶ seek $fh, 0, SEEK_END or die "Unable to seek - $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 2 or die "Unable to seek - $!" 0 32 32
123 32 $total = $remaining = tell $fh;
124 32 ▶ seek $fh, $pos, SEEK_SET or die "Unable to seek - $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $pos, 0 or die "Unable to seek - $!" 0 32 32
125 }
126 32 my $f = $self->loop->new_future;
127  
128 $self->write(sub {
129 288 ▶ my $stream = shift;
Subroutine __ANON__: called CC=4 SCAR=15.8
130 288 ▶ return unless $remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless $remaining 32 256 288
131  
132 256 ▶ unless($remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($remaining > 0) 0 256 256
133 0 $f->fail(EOF => "Attempt to write past EOF, remaining bytes: " . $remaining);
134 0 return;
135 }
136  
137 256 ▶ if(my $written = sendfile $stream->write_handle, $fh, $remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $written = &sendfile(scalar $stream->write_handle, $fh, $remaining)) 256 0 256
138 256 $remaining -= $written;
139 256 return ''; # empty string => call us again please
140 }
141  
142 0 $f->fail("$!", sendfile => 0 + $!, $remaining);
143 0 return;
144 }, on_flush => sub {
145 32 ▶ $f->done($total) unless $f->is_ready;
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 32 0 32
146 32 });
147 32 return $f;
148 2 };
149  
150  
151 2 1;
152  
153 __END__
↑ Summary