Class: Pwrake::NBIO::Reader
- Inherits:
-
Object
- Object
- Pwrake::NBIO::Reader
- Defined in:
- lib/pwrake/nbio.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#check_timeout ⇒ Object
Returns the value of attribute check_timeout.
-
#host ⇒ Object
Returns the value of attribute host.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#waiter ⇒ Object
readonly
Returns the value of attribute waiter.
Instance Method Summary collapse
-
#_read(sz) ⇒ Object
from Bartender.
-
#call ⇒ Object
call from Selector#run.
- #eof? ⇒ Boolean
- #error(e) ⇒ Object
- #halt ⇒ Object
-
#initialize(selector, io) ⇒ Reader
constructor
A new instance of Reader.
- #read(n) ⇒ Object
-
#read_line_nonblock ⇒ Object
call from MultiReader#call.
- #read_until(sep = "\r\n", chunk_size = 8192) ⇒ Object
- #readln ⇒ Object (also: #get_line, #gets)
-
#select_io ⇒ Object
call from Reader#_read and FiberReaderQueue#deq.
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_timeout ⇒ Object
Returns the value of attribute check_timeout.
257 258 259 |
# File 'lib/pwrake/nbio.rb', line 257 def check_timeout @check_timeout end |
#host ⇒ Object
Returns the value of attribute host.
257 258 259 |
# File 'lib/pwrake/nbio.rb', line 257 def host @host end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
256 257 258 |
# File 'lib/pwrake/nbio.rb', line 256 def io @io end |
#waiter ⇒ Object (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 |
#call ⇒ Object
call from Selector#run
260 261 262 |
# File 'lib/pwrake/nbio.rb', line 260 def call @waiter.each{|f| f.resume} end |
#eof? ⇒ 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 |
#halt ⇒ Object
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_nonblock ⇒ Object
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 |
#readln ⇒ Object Also known as: get_line, gets
352 353 354 |
# File 'lib/pwrake/nbio.rb', line 352 def readln read_until(@sep) end |
#select_io ⇒ Object
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 |