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.



247
248
249
250
251
252
253
254
255
# File 'lib/pwrake/nbio.rb', line 247

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

Instance Attribute Details

#check_timeoutObject

Returns the value of attribute check_timeout.



257
258
259
# File 'lib/pwrake/nbio.rb', line 257

def check_timeout
  @check_timeout
end

#hostObject

Returns the value of attribute host.



257
258
259
# File 'lib/pwrake/nbio.rb', line 257

def host
  @host
end

#ioObject (readonly)

Returns the value of attribute io.



256
257
258
# File 'lib/pwrake/nbio.rb', line 256

def io
  @io
end

#waiterObject (readonly)

Returns the value of attribute waiter.



256
257
258
# File 'lib/pwrake/nbio.rb', line 256

def waiter
  @waiter
end

Instance Method Details

#_read(sz) ⇒ Object

from Bartender



316
317
318
319
320
321
322
323
324
325
# File 'lib/pwrake/nbio.rb', line 316

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

#callObject

call from Selector#run



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

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

#eof?Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/pwrake/nbio.rb', line 310

def eof?
  @eof && @buf.empty?
end

#error(e) ⇒ Object



298
299
300
301
# File 'lib/pwrake/nbio.rb', line 298

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

#haltObject



303
304
305
306
307
308
# File 'lib/pwrake/nbio.rb', line 303

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

#read(n) ⇒ Object



327
328
329
330
331
332
333
334
# File 'lib/pwrake/nbio.rb', line 327

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



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

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?
    raise e
  else
    buf = @buf; @buf = ''
    return buf
  end
end

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



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/pwrake/nbio.rb', line 336

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, gets



352
353
354
# File 'lib/pwrake/nbio.rb', line 352

def readln
  read_until(@sep)
end

#select_ioObject

call from Reader#_read and FiberReaderQueue#deq



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/pwrake/nbio.rb', line 280

def select_io
  if @waiter.empty?
    @selector.add_reader(self)
  else
    if @selector.reader[@io] != self
      raise RuntimeError, "access from multiple Fiber"
    end
  end
  @waiter.push(Fiber.current)
  if $debug && defined? Log
    Log.debug("Reader#select_io: #{Fiber.current.inspect}\n "+caller.join("\n "))
  end
  Fiber.yield
ensure
  @waiter.delete(Fiber.current)
  @selector.delete_reader(self) if @waiter.empty?
end