Class: Coolio::IO

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

Overview

A buffered I/O class witch fits into the Coolio 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 Coolio’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/cool.io/io.rb', line 22

def initialize(io)
  @_io = io
  @_write_buffer  ||= ::IO::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
35
36
37
38
# File 'lib/cool.io/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)


60
61
62
# File 'lib/cool.io/io.rb', line 60

def attached?
  @_read_watcher.attached?
end

#closeObject

Close the IO stream



102
103
104
105
106
107
108
109
# File 'lib/cool.io/io.rb', line 102

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

  on_close
  nil
end

#closed?Boolean

Is the IO object closed?

Returns:

  • (Boolean)


112
113
114
# File 'lib/cool.io/io.rb', line 112

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

#detachObject

Detach from the event loop



41
42
43
44
45
# File 'lib/cool.io/io.rb', line 41

def detach
  # TODO should these detect write buffers, as well?
  @_read_watcher.detach
  self
end

#disableObject

Disable the watcher



54
55
56
57
# File 'lib/cool.io/io.rb', line 54

def disable
  @_read_watcher.disable
  self
end

#enableObject

Enable the watcher



48
49
50
51
# File 'lib/cool.io/io.rb', line 48

def enable
  @_read_watcher.enable
  self
end

#enabled?Boolean

Is the watcher enabled?

Returns:

  • (Boolean)


65
66
67
# File 'lib/cool.io/io.rb', line 65

def enabled?
  @_read_watcher.enabled?
end

#evloopObject

Obtain the event loop associated with this object



70
71
72
# File 'lib/cool.io/io.rb', line 70

def evloop
  @_read_watcher.evloop
end

#on_closeObject

Called whenever the IO object hits EOF



87
# File 'lib/cool.io/io.rb', line 87

def on_close; end

#on_read(data) ⇒ Object

Called whenever the IO object receives data



79
# File 'lib/cool.io/io.rb', line 79

def on_read(data); end

#on_write_completeObject

Called whenever a write completes and the output buffer is empty



83
# File 'lib/cool.io/io.rb', line 83

def on_write_complete; end

#write(data) ⇒ Object

Write data in a buffered, non-blocking manner



95
96
97
98
99
# File 'lib/cool.io/io.rb', line 95

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