Class: LightIO::Core::Backend::NIO
- Inherits:
-
Object
- Object
- LightIO::Core::Backend::NIO
- Defined in:
- lib/lightio/core/backend/nio.rb
Overview
LightIO use NIO as default event-driving backend
Instance Method Summary collapse
- #add_callback(&blk) ⇒ Object
- #add_io_wait(io, interests, &blk) ⇒ Object
- #add_timer(timer) ⇒ Object
- #backend ⇒ Object
- #cancel_io_wait(io) ⇒ Object
- #cancel_timer(timer) ⇒ Object
- #env_backend ⇒ Object
-
#initialize ⇒ NIO
constructor
A new instance of NIO.
- #run ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ NIO
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(env_backend) 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 |
#backend ⇒ Object
101 102 103 |
# File 'lib/lightio/core/backend/nio.rb', line 101 def backend @selector.backend 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 |
#env_backend ⇒ Object
105 106 107 108 |
# File 'lib/lightio/core/backend/nio.rb', line 105 def env_backend key = 'LIGHTIO_BACKEND'.freeze ENV.has_key?(key) ? ENV[key].to_sym : nil end |
#run ⇒ Object
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 |
#stop ⇒ Object
95 96 97 98 99 |
# File 'lib/lightio/core/backend/nio.rb', line 95 def stop return unless @running @running = false raise end |