Class: Async::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/async/wrapper.rb

Overview

Represents an asynchronous IO within a reactor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, reactor = nil) ⇒ Wrapper

Returns a new instance of Wrapper.

Parameters:

  • io

    the native object to wrap.

  • reactor (Reactor) (defaults to: nil)

    the reactor that is managing this wrapper, or not specified, it's looked up by way of Task.current.

  • bound (Boolean)

    whether the underlying socket will be closed if the wrapper is closed.



27
28
29
30
31
32
# File 'lib/async/wrapper.rb', line 27

def initialize(io, reactor = nil)
	@io = io
	
	@reactor = reactor || Task.current.reactor
	@monitor = nil
end

Instance Attribute Details

#ioObject (readonly)

The underlying native io.



35
36
37
# File 'lib/async/wrapper.rb', line 35

def io
  @io
end

#reactorObject (readonly)

The reactor this wrapper is associated with.



38
39
40
# File 'lib/async/wrapper.rb', line 38

def reactor
  @reactor
end

Instance Method Details

#closeObject

Close the monitor.



58
59
60
61
62
# File 'lib/async/wrapper.rb', line 58

def close
	close_monitor
	
	@io.close if @io
end

#wait_any(interests = :rw, duration = nil) ⇒ Object

Wait fo the io to become either readable or writable.

Parameters:

  • interests (:r | :w | :rw) (defaults to: :rw)

    what events to wait for.

  • duration (Float) (defaults to: nil)

    timeout after the given duration if not nil.



53
54
55
# File 'lib/async/wrapper.rb', line 53

def wait_any(interests = :rw, duration = nil)
	monitor(interests, duration)
end

#wait_readable(duration = nil) ⇒ Object

Wait for the io to become readable.



41
42
43
# File 'lib/async/wrapper.rb', line 41

def wait_readable(duration = nil)
	wait_any(:r, duration)
end

#wait_writable(duration = nil) ⇒ Object

Wait for the io to become writable.



46
47
48
# File 'lib/async/wrapper.rb', line 46

def wait_writable(duration = nil)
	wait_any(:w, duration)
end