class Stream
Attributes
_callbacks[RW]
buffer[RW]
Public Class Methods
new(io)
click to toggle source
# File lib/evented/stream.rb, line 4 def initialize(io) @io = io @buffer = '' streams << self @_callbacks ||= Hash.new { |hash, key| hash[key] = Array.new } on(:close) do close end end
Public Instance Methods
close()
click to toggle source
# File lib/evented/stream.rb, line 51 def close @io.close rescue IOError streams.delete(self) end
emit(event, *args)
click to toggle source
# File lib/evented/stream.rb, line 22 def emit(event, *args) @_callbacks[event].each do |callback| callback.call(*args) end end
gets()
click to toggle source
# File lib/evented/stream.rb, line 57 def gets @io.gets end
handle_read()
click to toggle source
# File lib/evented/stream.rb, line 28 def handle_read chunk = @io.read_nonblock(1024) emit(:data, chunk) rescue IO::WaitReadable rescue EOFError, IOError, Errno::ECONNRESET emit(:close) end
handle_write()
click to toggle source
# File lib/evented/stream.rb, line 36 def handle_write return if @buffer.empty? @io.write_nonblock(@buffer) @buffer = '' emit(:sent) rescue IO::WaitWritable rescue EOFError, IOError, Errno::ECONNRESET emit(:close) end
on(event, &block)
click to toggle source
# File lib/evented/stream.rb, line 18 def on(event, &block) @_callbacks[event] << block end
send(chunk, &block)
click to toggle source
# File lib/evented/stream.rb, line 46 def send(chunk, &block) @buffer << chunk on(:sent, &block) if block_given? end
to_io()
click to toggle source
# File lib/evented/stream.rb, line 14 def to_io @io end