ruby_io_splice.git  about / heads / tags
zero-copy pipe I/O for Linux and Ruby
blob ad20152e737e3b1f0871de59eb00b6f8e4bee457 808 bytes (raw)
$ git show HEAD:lib/io/splice/mri_18.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
30
31
32
33
34
 
# :enddoc:
require "io/nonblock"
module IO::Splice
  class << self
    remove_method :full
    remove_method :partial
  end
  def self.full(src, dst, len, src_offset)
    dst.to_io.nonblock = src.to_io.nonblock = true
    spliced = 0
    case n = IO.trysplice(src, src_offset, dst, nil, len, IO::Splice::F_MOVE)
    when :EAGAIN
      src.to_io.wait
      IO.select(nil, [dst])
    when Integer
      spliced += n
      len -= n
      src_offset += n if src_offset
    when nil
      break
    end while len > 0
    spliced
  end

  def self.partial(src, dst, len, src_offset)
    dst.to_io.nonblock = src.to_io.nonblock = true
    begin
      src.to_io.wait
      IO.select(nil, [dst])
      rv = IO.trysplice(src, src_offset, dst, nil, len, IO::Splice::F_MOVE)
    end while rv == :EAGAIN
    rv
  end
end

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