-
-
Save jkramer/273e56bb24a07d1eb2a7be4d786dc306 to your computer and use it in GitHub Desktop.
Support for native sendfile(2) for Perl 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit package IO::SendFile; | |
use NativeCall; | |
multi sub send-file(IO::Handle:D $input, IO::Handle:D $output, Int:D $count where * > 0) is export { | |
given sendfile($output.native-descriptor, $input.native-descriptor, Nil, $count) { | |
when -1 { | |
my $errno := cglobal(Str, 'errno', int32); | |
fail strerror($errno); | |
} | |
default { | |
return $_; | |
} | |
} | |
} | |
multi sub send-file(IO::Handle:D $input, IO::Handle:D $output) is export { | |
constant BLOCK-SIZE = 2048; | |
my $total = 0; | |
loop { | |
$total += my $count = send-file($input, $output, BLOCK-SIZE); | |
last if $count < BLOCK-SIZE; | |
} | |
} | |
sub sendfile(int32, int32, Pointer, uint32) returns int32 is native(Str) { * } | |
sub strerror(int32) returns Str is native(Str) { * } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment