1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| | # -*- encoding: binary -*-
# Copyright (C) 2013-2015 all contributors <yahns-public@yhbt.net>
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
module Yahns::SendfileCompat
def trysendfile(io, offset, count)
return 0 if count == 0
count = 0x4000 if count > 0x4000
buf = Thread.current[:yahns_sfbuf] ||= ''
io.pos = offset
str = io.read(count, buf) or return # nil for EOF
n = 0
case rv = kgio_trywrite(str)
when :wait_writable, :wait_readable
return n > 0 ? n : rv
when nil
return n + str.size # yay!
else # String # partial write, keep trying
n += (str.size - rv.size)
str = rv
end while true
end
end
class IO
include Yahns::SendfileCompat
end
|