Class: LightIO::Library::IO

Inherits:
Object
  • Object
show all
Includes:
Wrap::IOWrapper
Defined in:
lib/lightio/library/io.rb

Direct Known Subclasses

BasicSocket

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Wrap::IOWrapper

included, #initialize

Methods included from Wrap::Wrapper

included, #initialize, #method_missing

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class LightIO::Wrap::Wrapper

Class Method Details

.open(*args) ⇒ Object



122
123
124
125
126
127
# File 'lib/lightio/library/io.rb', line 122

def open(*args)
  io = self.new(*args)
  yield io
ensure
  io.close if io.respond_to? :close
end

.pipe(*args) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/lightio/library/io.rb', line 129

def pipe(*args)
  r, w = raw_class.pipe
  if block_given?
    begin
      return yield r, w
    ensure
      w.close
      r.close
    end
  end
  [IO._wrap(r), IO._wrap(w)]
end

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/lightio/library/io.rb', line 142

def select(read_fds, write_fds=nil, _except_fds=nil, timeout=nil)
  timer = timeout && Time.now
  # run once ioloop
  LightIO.sleep 0
  loop do
    r_fds = (read_fds || []).select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.instance_variable_get(:@io_watcher).readable?}
    w_fds = (write_fds || []).select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.instance_variable_get(:@io_watcher).writable?}
    e_fds = []
    if r_fds.empty? && w_fds.empty?
      LightIO.sleep 0
      if timeout && Time.now - timer > timeout
        return nil
      end
    else
      return [r_fds, w_fds, e_fds]
    end
  end
end

Instance Method Details

#close(*args) ⇒ Object



98
99
100
101
102
# File 'lib/lightio/library/io.rb', line 98

def close(*args)
  # close watcher before io closed
  @io_watcher.close
  @io.close(*args)
end

#eofObject Also known as: eof?



72
73
74
75
# File 'lib/lightio/library/io.rb', line 72

def eof
  wait_readable
  @io.eof?
end

#getbyteObject



37
38
39
# File 'lib/lightio/library/io.rb', line 37

def getbyte
  read(1)
end

#getcObject



41
42
43
44
# File 'lib/lightio/library/io.rb', line 41

def getc
  wait_readable
  @io.getc
end

#gets(*args) ⇒ Object

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/lightio/library/io.rb', line 79

def gets(*args)
  raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..2)" if args.size > 2
  return nil if eof?
  sep = $/
  if args[0].is_a?(Numeric)
    limit = args.shift
  else
    sep = args.shift if args.size > 0
    limit = args.shift if args.first.is_a?(Numeric)
  end
  s = ''
  while (c = getc)
    s << c
    break if limit && s.size == limit
    break if c == sep
  end
  $_ = s
end

#read(length = nil, outbuf = nil) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lightio/library/io.rb', line 11

def read(length=nil, outbuf=nil)
  raise ArgumentError, "negative length #{length} given" if length && length < 0
  (outbuf ||= "").clear
  loop do
    readlen = length.nil? ? 4096 : length - outbuf.size
    if (data = wait_nonblock(:read_nonblock, readlen))
      outbuf << data
      if length == outbuf.size
        return outbuf
      end
    else
      return length.nil? ? '' : nil
    end
  end
end

#readbyteObject

Raises:

  • (EOFError)


66
67
68
69
70
# File 'lib/lightio/library/io.rb', line 66

def readbyte
  b = getbyte
  raise EOFError, 'end of file reached' if b.nil?
  b
end

#readcharObject

Raises:

  • (EOFError)


60
61
62
63
64
# File 'lib/lightio/library/io.rb', line 60

def readchar
  c = getc
  raise EOFError, 'end of file reached' if c.nil?
  c
end

#readline(*args) ⇒ Object

Raises:

  • (EOFError)


46
47
48
49
50
# File 'lib/lightio/library/io.rb', line 46

def readline(*args)
  line = gets(*args)
  raise EOFError, 'end of file reached' if line.nil?
  line
end

#readlines(*args) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/lightio/library/io.rb', line 52

def readlines(*args)
  result = []
  until eof?
    result << readline(*args)
  end
  result
end

#readpartial(maxlen, outbuf = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/lightio/library/io.rb', line 27

def readpartial(maxlen, outbuf=nil)
  (outbuf ||= "").clear
  if (data = wait_nonblock(:read_nonblock, maxlen))
    outbuf << data
  else
    raise EOFError, 'end of file reached'
  end
  outbuf
end

#to_ioObject



104
105
106
# File 'lib/lightio/library/io.rb', line 104

def to_io
  self
end