Class: LightIO::Watchers::IO
- Extended by:
- Forwardable
- Defined in:
- lib/lightio/watchers/io.rb
Overview
LightIO::Watchers::IO provide a NIO::Monitor wrap to manage ‘raw’ socket / io
@Example:
#- wait_read for server socket
io_watcher = LightIO::Watchers::IO.new(server_socket, :r)
loop do
io_watcher.wait_read
client_socket = server_socket.accept
# do something
end
io_watcher.close
Instance Attribute Summary
Attributes inherited from Watcher
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(io, interests = :rw) ⇒ LightIO::Watchers::IO
constructor
Create a io watcher.
-
#monitor(interests = :rw) ⇒ Object
NIO::Monitor.
- #readable? ⇒ Boolean
- #set_callback(&blk) ⇒ Object
-
#start(ioloop) ⇒ Object
just implement IOloop#wait watcher interface.
- #wait(timeout = nil, mode = :read) ⇒ Object
-
#wait_readable(timeout = nil) ⇒ LightIO::Watchers::IO?
Blocking until io is readable.
-
#wait_writable(timeout = nil) ⇒ LightIO::Watchers::IO?
Blocking until io is writable.
- #writable? ⇒ Boolean (also: #writeable?)
Constructor Details
#initialize(io, interests = :rw) ⇒ LightIO::Watchers::IO
Create a io watcher
20 21 22 23 24 25 26 |
# File 'lib/lightio/watchers/io.rb', line 20 def initialize(io, interests=:rw) @io = io @ioloop = LightIO::Core::IOloop.current @waiting = false ObjectSpace.define_finalizer(self, self.class.finalizer(@monitor)) @error = nil end |
Class Method Details
.finalizer(monitor) ⇒ Object
36 37 38 |
# File 'lib/lightio/watchers/io.rb', line 36 def finalizer(monitor) proc {monitor.close if monitor && !monitor.close?} end |
Instance Method Details
#close ⇒ Object
82 83 84 85 86 87 |
# File 'lib/lightio/watchers/io.rb', line 82 def close return if closed? monitor.close @error = IOError.new('closed stream') callback_on_waiting end |
#monitor(interests = :rw) ⇒ Object
NIO::Monitor
29 30 31 32 33 |
# File 'lib/lightio/watchers/io.rb', line 29 def monitor(interests=:rw) @monitor ||= begin @ioloop.add_io_wait(@io, interests) {callback_on_waiting} end end |
#readable? ⇒ Boolean
44 45 46 47 |
# File 'lib/lightio/watchers/io.rb', line 44 def readable? check_monitor_read monitor.readable? end |
#set_callback(&blk) ⇒ Object
95 96 97 |
# File 'lib/lightio/watchers/io.rb', line 95 def set_callback(&blk) @callback = blk end |
#start(ioloop) ⇒ Object
just implement IOloop#wait watcher interface
91 92 93 |
# File 'lib/lightio/watchers/io.rb', line 91 def start(ioloop) # do nothing end |
#wait(timeout = nil, mode = :read) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lightio/watchers/io.rb', line 70 def wait(timeout=nil, mode=:read) LightIO::Timeout.timeout(timeout) do check_monitor(mode) in_waiting(mode) do wait_in_ioloop end self end rescue Timeout::Error nil end |
#wait_readable(timeout = nil) ⇒ LightIO::Watchers::IO?
Blocking until io is readable
59 60 61 |
# File 'lib/lightio/watchers/io.rb', line 59 def wait_readable(timeout=nil) wait timeout, :read end |
#wait_writable(timeout = nil) ⇒ LightIO::Watchers::IO?
Blocking until io is writable
66 67 68 |
# File 'lib/lightio/watchers/io.rb', line 66 def wait_writable(timeout=nil) wait timeout, :write end |
#writable? ⇒ Boolean Also known as: writeable?
49 50 51 52 |
# File 'lib/lightio/watchers/io.rb', line 49 def writable? check_monitor_write monitor.writable? end |