Class: LightIO::Core::Backend::NIO

Inherits:
Object
  • Object
show all
Defined in:
lib/lightio/core/backend/nio.rb

Overview

LightIO use NIO as default event-driving backend

Instance Method Summary collapse

Constructor Details

#initializeNIO

Returns a new instance of NIO.



52
53
54
55
56
57
58
59
# File 'lib/lightio/core/backend/nio.rb', line 52

def initialize
  # @selector = NIO::Selector.new
  @current_loop_time = nil
  @running = false
  @timers = Timers.new
  @callbacks = []
  @selector = ::NIO::Selector.new
end

Instance Method Details

#add_callback(&blk) ⇒ Object



73
74
75
# File 'lib/lightio/core/backend/nio.rb', line 73

def add_callback(&blk)
  @callbacks << blk
end

#add_io_wait(io, interests, &blk) ⇒ Object



85
86
87
88
89
# File 'lib/lightio/core/backend/nio.rb', line 85

def add_io_wait(io, interests, &blk)
  monitor = @selector.register(io, interests)
  monitor.value = blk
  monitor
end

#add_timer(timer) ⇒ Object



77
78
79
# File 'lib/lightio/core/backend/nio.rb', line 77

def add_timer(timer)
  timer.uuid = @timers.add_timer(timer)
end

#cancel_io_wait(io) ⇒ Object



91
92
93
# File 'lib/lightio/core/backend/nio.rb', line 91

def cancel_io_wait(io)
  @selector.deregister(io)
end

#cancel_timer(timer) ⇒ Object



81
82
83
# File 'lib/lightio/core/backend/nio.rb', line 81

def cancel_timer(timer)
  @timers.cancel_timer(timer)
end

#runObject

Raises:



61
62
63
64
65
66
67
68
69
70
# File 'lib/lightio/core/backend/nio.rb', line 61

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

#stopObject



95
96
97
98
99
# File 'lib/lightio/core/backend/nio.rb', line 95

def stop
  return unless @running
  @running = false
  raise
end