Class: Coolio::IO
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
Defined Under Namespace
Classes: Watcher
Constant Summary collapse
- INPUT_SIZE =
Maximum number of bytes to consume at once
16384
Instance Method Summary collapse
-
#attach(loop) ⇒ Object
Attach to the event loop.
-
#attached? ⇒ Boolean
Is the watcher attached?.
-
#close ⇒ Object
Close the IO stream.
-
#closed? ⇒ Boolean
Is the IO object closed?.
-
#detach ⇒ Object
Detach from the event loop.
-
#disable ⇒ Object
Disable the watcher.
-
#enable ⇒ Object
Enable the watcher.
-
#enabled? ⇒ Boolean
Is the watcher enabled?.
-
#evloop ⇒ Object
Obtain the event loop associated with this object.
-
#initialize(io) ⇒ IO
constructor
A new instance of IO.
-
#on_close ⇒ Object
Called whenever the IO object hits EOF.
-
#on_read(data) ⇒ Object
Called whenever the IO object receives data.
-
#on_write_complete ⇒ Object
Called whenever a write completes and the output buffer is empty.
-
#write(data) ⇒ Object
Write data in a buffered, non-blocking manner.
Methods included from Meta
event_callback, watcher_delegate
Constructor Details
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?
60 61 62 |
# File 'lib/cool.io/io.rb', line 60 def attached? @_read_watcher.attached? end |
#close ⇒ Object
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?
112 113 114 |
# File 'lib/cool.io/io.rb', line 112 def closed? @_io.nil? or @_io.closed? end |
#detach ⇒ Object
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 |
#disable ⇒ Object
Disable the watcher
54 55 56 57 |
# File 'lib/cool.io/io.rb', line 54 def disable @_read_watcher.disable self end |
#enable ⇒ Object
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?
65 66 67 |
# File 'lib/cool.io/io.rb', line 65 def enabled? @_read_watcher.enabled? end |
#evloop ⇒ Object
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_close ⇒ Object
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_complete ⇒ Object
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 |