Class: Pwrake::NBIO::Reader
- Inherits:
-
Object
- Object
- Pwrake::NBIO::Reader
- Defined in:
- lib/pwrake/nbio.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#_read(sz) ⇒ Object
from Bartender.
-
#call ⇒ Object
call from Selector#run.
- #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)
-
#select_io ⇒ Object
call from Reader#_read and FiberReaderQueue#deq.
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
#io ⇒ Object (readonly)
Returns the value of attribute io.
218 219 220 |
# File 'lib/pwrake/nbio.rb', line 218 def io @io end |
#timeout ⇒ Object
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 |
#call ⇒ Object
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 |
#halt ⇒ Object
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_nonblock ⇒ Object
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 |
#readln ⇒ Object Also known as: get_line
302 303 304 |
# File 'lib/pwrake/nbio.rb', line 302 def readln read_until(@sep) end |
#select_io ⇒ Object
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 |