module LightIO::Module::IO::ClassMethods
Public Instance Methods
copy_stream(*args)
click to toggle source
# File lib/lightio/module/io.rb, line 65 def copy_stream(*args) raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 2..4)" unless (2..4).include?(args.size) src, dst, copy_length, src_offset = args src = src.respond_to?(:to_io) ? src.to_io : LightIO::Library::File.open(src, 'r') unless src.is_a?(IO) dst = dst.respond_to?(:to_io) ? dst.to_io : LightIO::Library::File.open(dst, 'w') unless dst.is_a?(IO) buf_size = 4096 copy_chars = 0 buf_size = [buf_size, copy_length].min if copy_length src.seek(src_offset) if src_offset while (buf = src.read(buf_size)) size = dst.write(buf) copy_chars += size if copy_length copy_length -= size break if copy_length.zero? buf_size = [buf_size, copy_length].min end end copy_chars end
open(*args) { |io| ... }
click to toggle source
# File lib/lightio/module/io.rb, line 42 def open(*args) io = self.new(*args) return io unless block_given? begin yield io ensure io.close if io.respond_to? :close end end
pipe(*args) { |r, w| ... }
click to toggle source
# File lib/lightio/module/io.rb, line 52 def pipe(*args) r, w = origin_pipe(*args) if block_given? begin return yield r, w ensure w.close r.close end end [wrap_to_library(r), wrap_to_library(w)] end
select(read_fds, write_fds=nil, _except_fds=nil, timeout=nil)
click to toggle source
# File lib/lightio/module/io.rb, line 86 def select(read_fds, write_fds=nil, _except_fds=nil, timeout=nil) timer = timeout && Time.now read_fds ||= [] write_fds ||= [] loop do # make sure io registered, then clear io watcher status read_fds.each {|fd| LightIO::Module::IO.get_io_watcher(fd).tap {|io| io.readable?; io.clear_status}} write_fds.each {|fd| LightIO::Module::IO.get_io_watcher(fd).tap {|io| io.writable?; io.clear_status}} # run ioloop once LightIO.sleep 0 r_fds = read_fds.select {|fd| io = LightIO::Module::IO.convert_to_io(fd) io.closed? ? raise(IOError, 'closed stream') : LightIO::Module::IO.get_io_watcher(io).readable? } w_fds = write_fds.select {|fd| io = LightIO::Module::IO.convert_to_io(fd) io.closed? ? raise(IOError, 'closed stream') : LightIO::Module::IO.get_io_watcher(io).writable? } e_fds = [] if r_fds.empty? && w_fds.empty? if timeout && Time.now - timer > timeout return nil end else return [r_fds, w_fds, e_fds] end end end