Class: LightIO::Watchers::IO

Inherits:
Watcher
  • Object
show all
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

#callback

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, interests) ⇒ LightIO::Watchers::IO

Create a io watcher

Parameters:

  • io (Socket)

    An IO-able object

  • interests (Symbol)

    :r, :w, :rw - Is io readable? writeable? or both



18
19
20
21
22
23
24
25
26
# File 'lib/lightio/watchers/io.rb', line 18

def initialize(io, interests)
  @io = io
  @ioloop = LightIO::Core::IOloop.current
  @waiting = false
  @wait_for = nil
  # NIO monitor
  @monitor = @ioloop.add_io_wait(@io, interests) {callback_on_waiting}
  ObjectSpace.define_finalizer(self, self.class.finalizer(@monitor))
end

Class Method Details

.finalizer(monitor) ⇒ Object



29
30
31
# File 'lib/lightio/watchers/io.rb', line 29

def finalizer(monitor)
  proc {monitor.close if monitor && !monitor.close?}
end

Instance Method Details

#closeObject

stop io listening



82
83
84
# File 'lib/lightio/watchers/io.rb', line 82

def close
  @monitor.close
end

#close?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/lightio/watchers/io.rb', line 86

def close?
  @monitor.close?
end

#interestsObject



34
35
36
# File 'lib/lightio/watchers/io.rb', line 34

def interests
  @monitor.interests
end

#interests=(interests) ⇒ Object

Replace current interests



39
40
41
# File 'lib/lightio/watchers/io.rb', line 39

def interests=(interests)
  @monitor.interests = interests
end

#set_callback(&blk) ⇒ Object



98
99
100
# File 'lib/lightio/watchers/io.rb', line 98

def set_callback(&blk)
  @callback = blk
end

#start(ioloop) ⇒ Object



77
78
79
# File 'lib/lightio/watchers/io.rb', line 77

def start(ioloop)
  # do nothing
end

#waitObject

Raises:



90
91
92
93
94
95
96
# File 'lib/lightio/watchers/io.rb', line 90

def wait
  raise LightIO::Error, "Watchers::IO can't cross threads" if @ioloop != LightIO::Core::IOloop.current
  raise EOFError, "can't wait closed IO watcher" if @monitor.closed?
  @waiting = true
  @ioloop.wait(self)
  @waiting = false
end

#wait_for(interests) ⇒ Object

Blocking until io interests is satisfied



44
45
46
47
48
49
50
# File 'lib/lightio/watchers/io.rb', line 44

def wait_for(interests)
  if (self.interests == :w || self.interests == :r) && interests != self.interests
    raise ArgumentError, "IO interests is #{self.interests}, can't waiting for #{interests}"
  end
  @wait_for = interests
  wait
end

#wait_read(timeout = nil) ⇒ LightIO::Watchers::IO?

Blocking until io is readable

Parameters:

  • timeout (Numeric) (defaults to: nil)

    return nil after timeout seconds, otherwise return self

Returns:



55
56
57
58
59
60
61
62
# File 'lib/lightio/watchers/io.rb', line 55

def wait_read(timeout=nil)
  LightIO::Timeout.timeout(timeout) do
    wait_for :r
    self
  end
rescue Timeout::Error
  nil
end

#wait_write(timeout = nil) ⇒ LightIO::Watchers::IO?

Blocking until io is writeable

Parameters:

  • timeout (Numeric) (defaults to: nil)

    return nil after timeout seconds, otherwise return self

Returns:



67
68
69
70
71
72
73
74
# File 'lib/lightio/watchers/io.rb', line 67

def wait_write(timeout=nil)
  LightIO::Timeout.timeout(timeout) do
    wait_for :w
    self
  end
rescue Timeout::Error
  nil
end