yahns.git  about / heads / tags
sleepy, multi-threaded, non-blocking application server for Ruby
blob e3f53d14ad5726eec021075980f09760d1c4747d 799 bytes (raw)
$ git show the_metal:lib/yahns/sendfile_compat.rb	# shows this blob on the CLI

 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
28
29
 
# -*- encoding: binary -*-
# Copyright (C) 2009-2014, Eric Wong <normalperson@yhbt.net> et. al.
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require 'io/extra' # gem install io-extra

module Yahns::SendfileCompat
  def trysendfile(io, offset, count)
    return 0 if count == 0
    count = 0x4000 if count > 0x4000
    str = IO.pread(io.fileno, count, offset)
    if count > str.bytesize
      raise EOFError, "end of file reached"
    end
    n = 0
    case rv = kgio_trywrite(str)
    when String # partial write, keep trying
      n += (str.bytesize - rv.bytesize)
      str = rv
    when :wait_writable, :wait_readable
      return n > 0 ? n : rv
    when nil
      return n + str.bytesize # yay!
    end while true
  end
end

class IO
  include Yahns::SendfileCompat
end

git clone git://yhbt.net/yahns.git
git clone https://yhbt.net/yahns.git