Class: Pwrake::NBIO::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/nbio.rb

Overview


Direct Known Subclasses

MultiReader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selector, io) ⇒ Reader

Returns a new instance of Reader.



210
211
212
213
214
215
216
217
# File 'lib/pwrake/nbio.rb', line 210

def initialize(selector, io)
  @selector = selector
  @io = io
  @waiter = []
  @buf = ''
  @sep = "\n"
  @chunk_size = 8192
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



218
219
220
# File 'lib/pwrake/nbio.rb', line 218

def io
  @io
end

#timeoutObject

Returns the value of attribute timeout.



219
220
221
# File 'lib/pwrake/nbio.rb', line 219

def timeout
  @timeout
end

Instance Method Details

#_read(sz) ⇒ Object

from Bartender



267
268
269
270
271
272
273
274
275
# File 'lib/pwrake/nbio.rb', line 267

def _read(sz)
  @io.read_nonblock(sz)
rescue EOFError
  nil
rescue IO::WaitReadable
  return nil if @halting
  select_io
  retry
end

#callObject

call from Selector#run



222
223
224
# File 'lib/pwrake/nbio.rb', line 222

def call
  @waiter.each{|f| f.resume}
end

#error(e) ⇒ Object



253
254
255
256
# File 'lib/pwrake/nbio.rb', line 253

def error(e)
  @closed = true
  raise e
end

#haltObject



258
259
260
261
262
263
# File 'lib/pwrake/nbio.rb', line 258

def halt
  @halting = true
  call
ensure
  @halting = false
end

#read(n) ⇒ Object



277
278
279
280
281
282
283
284
# File 'lib/pwrake/nbio.rb', line 277

def read(n)
  while @buf.bytesize < n
    chunk = _read(n)
    break if chunk.nil? || chunk.empty?
    @buf += chunk
  end
  @buf.slice!(0, n)
end

#read_line_nonblockObject

call from MultiReader#call



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/pwrake/nbio.rb', line 227

def read_line_nonblock
  until index = @buf.index(@sep)
    @buf << @io.read_nonblock(@chunk_size)
  end
  @buf.slice!(0, index+@sep.bytesize)
rescue EOFError => e
  if @buf.empty?
    #return nil
    raise e
  else
    buf = @buf; @buf = ''
    return buf
  end
#rescue IO::WaitReadable
end

#read_until(sep = "\r\n", chunk_size = 8192) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/pwrake/nbio.rb', line 286

def read_until(sep="\r\n", chunk_size=8192)
  until i = @buf.index(sep)
    if s = _read(chunk_size)
      @buf += s
    else
      if @buf.empty?
        return nil
      else
        buf = @buf; @buf = ''
        return buf
      end
    end
  end
  @buf.slice!(0, i+sep.bytesize)
end

#readlnObject Also known as: get_line



302
303
304
# File 'lib/pwrake/nbio.rb', line 302

def readln
  read_until(@sep)
end

#select_ioObject

call from Reader#_read and FiberReaderQueue#deq



244
245
246
247
248
249
250
251
# File 'lib/pwrake/nbio.rb', line 244

def select_io
  @selector.add_reader(self) if @waiter.empty?
  @waiter.push(Fiber.current)
  Fiber.yield
ensure
  @waiter.delete(Fiber.current)
  @selector.delete_reader(self) if @waiter.empty?
end