Module: LightIO::Module::IO::ClassMethods

Includes:
Base::Helper
Included in:
Library::IO
Defined in:
lib/lightio/module/io.rb

Instance Method Summary collapse

Instance Method Details

#open(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/lightio/module/io.rb', line 34

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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lightio/module/io.rb', line 44

def pipe(*args)
  r, w = origin_pipe(*args)
  if block_given?
    begin
      return yield r, w
    ensure
      w.close
      r.close
    end
  end
  # [r, w]
  [wrap_to_library(r), wrap_to_library(w)]
end

#select(read_fds, write_fds = nil, _except_fds = nil, timeout = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/lightio/module/io.rb', line 58

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