| 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.001'; | |||||||||
| 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.001 | ||||||||||
| 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 | =cut | ||||||||||
| 100 | |||||||||||
| 101 | *IO::Async::Stream::sendfile = sub { | ||||||||||
| 102 | 32 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=22.5
| |||||||||||
| 103 | 32 | my %args = @_; | |||||||||
| 104 | 32 | ▶ | die "Stream must be added to loop first" unless $self->loop; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | |||||||||||
| 106 | 32 | ▶ | if(defined $args{file}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 32 | ▶ | open $args{fh}, '<', $args{file} or die "Could not open " . $args{file} . " for input - $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 32 | binmode $args{fh}; | |||||||||
| 109 | } | ||||||||||
| 110 | 32 | ▶ | die 'No file?' unless my $fh = delete $args{fh}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | |||||||||||
| 112 | # Work out how much we need to write | ||||||||||
| 113 | 32 | my $total = my $remaining = $args{length}; | |||||||||
| 114 | 32 | ▶ | unless(defined $total) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 32 | my $pos = tell $fh; | |||||||||
| 116 | 32 | ▶ | seek $fh, 0, SEEK_END or die "Unable to seek - $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 32 | $total = $remaining = tell $fh; | |||||||||
| 118 | 32 | ▶ | seek $fh, $pos, SEEK_SET or die "Unable to seek - $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | } | ||||||||||
| 120 | 32 | my $f = $self->loop->new_future; | |||||||||
| 121 | |||||||||||
| 122 | $self->write(sub { | ||||||||||
| 123 | 288 | ▶ | my $stream = shift; | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.8
| |||||||||||
| 124 | 288 | ▶ | return unless $remaining; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | |||||||||||
| 126 | 256 | ▶ | unless($remaining > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | $f->fail(EOF => "Attempt to write past EOF, remaining bytes: " . $remaining); | |||||||||
| 128 | 0 | return; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 256 | ▶ | if(my $written = sendfile $stream->write_handle, $fh, $remaining) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 256 | $remaining -= $written; | |||||||||
| 133 | 256 | return ''; # empty string => call us again please | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 0 | $f->fail(sendfile => $!, $remaining); | |||||||||
| 137 | 0 | return; | |||||||||
| 138 | }, on_flush => sub { | ||||||||||
| 139 | 32 | ▶ | $f->done($total) unless $f->is_ready; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 140 | 32 | }); | |||||||||
| 141 | 32 | return $f; | |||||||||
| 142 | 2 | }; | |||||||||
| 143 | |||||||||||
| 144 | |||||||||||
| 145 | 2 | 1; | |||||||||
| 146 | |||||||||||
| 147 | __END__ | ||||||||||