Class: Rev::IO

Inherits:
Object
  • Object
show all
Extended by:
Meta
Defined in:
lib/rev/io.rb

Overview

A buffered I/O class witch fits into the Rev Watcher framework. It provides both an observer which reads data as it’s received from the wire and a buffered write watcher which stores data and writes it out each time the socket becomes writable.

This class is primarily meant as a base class for other streams which need non-blocking writing, and is used to implement Rev’s Socket class and its associated subclasses.

Direct Known Subclasses

Socket

Defined Under Namespace

Classes: Watcher

Constant Summary collapse

INPUT_SIZE =

Maximum number of bytes to consume at once

16384

Instance Method Summary collapse

Methods included from Meta

event_callback, watcher_delegate

Constructor Details

#initialize(io) ⇒ IO

Returns a new instance of IO.



22
23
24
25
26
27
# File 'lib/rev/io.rb', line 22

def initialize(io)
  @_io = io
  @_write_buffer  ||= Rev::Buffer.new
  @_read_watcher  = Watcher.new(io, self, :r)
  @_write_watcher = Watcher.new(io, self, :w)
end

Instance Method Details

#attach(loop) ⇒ Object

Attach to the event loop



34
# File 'lib/rev/io.rb', line 34

def attach(loop); @_read_watcher.attach loop; schedule_write if !@_write_buffer.empty?; self; end

#attached?Boolean

Is the watcher attached?

Returns:

  • (Boolean)


46
# File 'lib/rev/io.rb', line 46

def attached?; @_read_watcher.attached?; end

#closeObject

Close the IO stream



87
88
89
90
91
92
93
94
# File 'lib/rev/io.rb', line 87

def close
  detach if attached?
  detach_write_watcher
  @_io.close unless @_io.closed?

  on_close
  nil
end

#closed?Boolean

Is the IO object closed?

Returns:

  • (Boolean)


97
98
99
# File 'lib/rev/io.rb', line 97

def closed?
  @_io.nil? or @_io.closed?
end

#detachObject

Detach from the event loop



37
# File 'lib/rev/io.rb', line 37

def detach; @_read_watcher.detach; self; end

#disableObject

Disable the watcher



43
# File 'lib/rev/io.rb', line 43

def disable; @_read_watcher.disable; self; end

#enableObject

Enable the watcher



40
# File 'lib/rev/io.rb', line 40

def enable; @_read_watcher.enable; self; end

#enabled?Boolean

Is the watcher enabled?

Returns:

  • (Boolean)


49
# File 'lib/rev/io.rb', line 49

def enabled?; @_read_watcher.enabled?; end

#evloopObject

Obtain the event loop associated with this object



52
# File 'lib/rev/io.rb', line 52

def evloop; @_read_watcher.evloop; end

#on_closeObject

Called whenever the IO object hits EOF



67
# File 'lib/rev/io.rb', line 67

def on_close; end

#on_read(data) ⇒ Object

Called whenever the IO object receives data



59
# File 'lib/rev/io.rb', line 59

def on_read(data); end

#on_write_completeObject

Called whenever a write completes and the output buffer is empty



63
# File 'lib/rev/io.rb', line 63

def on_write_complete; end

#output_buffer_sizeObject

Number of bytes are currently in the output buffer



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

def output_buffer_size
  @_write_buffer.size
end

#write(data) ⇒ Object

Write data in a buffered, non-blocking manner



75
76
77
78
79
# File 'lib/rev/io.rb', line 75

def write(data)
  @_write_buffer << data
  schedule_write
  data.size
end