class LightIO::Core::Backend::NIO
Attributes
running[R]
Public Class Methods
new()
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 58 def initialize # @selector = NIO::Selector.new @current_loop_time = nil @running = false @timers = Timers.new @callbacks = [] @selector = ::NIO::Selector.new(env_backend) end
Public Instance Methods
add_callback(&blk)
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 79 def add_callback(&blk) @callbacks << blk end
add_io_wait(io, interests, &blk)
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 91 def add_io_wait(io, interests, &blk) monitor = @selector.register(io, interests) monitor.value = blk monitor end
add_timer(timer)
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 83 def add_timer(timer) timer.uuid = @timers.add_timer(timer) end
cancel_io_wait(io)
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 97 def cancel_io_wait(io) @selector.deregister(io) end
cancel_timer(timer)
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 87 def cancel_timer(timer) @timers.cancel_timer(timer) end
env_backend()
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 109 def env_backend key = 'LIGHTIO_BACKEND'.freeze ENV.has_key?(key) ? ENV[key].to_sym : nil end
run()
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 67 def run raise Error, "already running" if @running @running = true loop do @current_loop_time = Time.now run_timers run_callbacks handle_selectables end end
stop()
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 101 def stop return if closed? @running = false @selector.close end
Also aliased as: close
Private Instance Methods
handle_selectables()
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 120 def handle_selectables @selector.select(0) do |monitor| # invoke callback if io is ready monitor.value.call(monitor.io) end end
run_callbacks()
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 127 def run_callbacks # prevent 'add new callbacks' during callback call, new callbacks will run in next turn callbacks = @callbacks @callbacks = [] while (callback = callbacks.shift) callback.call end end
run_timers()
click to toggle source
# File lib/lightio/core/backend/nio.rb, line 116 def run_timers @timers.fire(@current_loop_time) end