Class: Ione::Io::BaseConnection
- Inherits:
-
Object
- Object
- Ione::Io::BaseConnection
- Defined in:
- lib/ione/io/base_connection.rb
Overview
Direct Known Subclasses
Constant Summary collapse
- CONNECTING_STATE =
0
- CONNECTED_STATE =
1
- DRAINING_STATE =
2
- CLOSED_STATE =
3
Instance Attribute Summary collapse
- #host ⇒ Object readonly
- #port ⇒ Object readonly
Instance Method Summary collapse
-
#close(cause = nil) ⇒ true, false
Closes the connection.
-
#closed? ⇒ Boolean
Returns true if the connection is closed.
-
#connected? ⇒ Boolean
Returns true if the connection is connected.
-
#drain ⇒ Ione::Future
Wait for the connection's buffers to empty and then close it.
-
#on_closed {|error, nil| ... } ⇒ Object
Register to receive a notification when the socket is closed, both for expected and unexpected reasons.
-
#on_data {|String| ... } ⇒ Object
Register to receive notifications when new data is read from the socket.
- #to_s ⇒ Object
-
#write(bytes = nil) {|buffer| ... } ⇒ Object
Write bytes to the socket.
Instance Attribute Details
#host ⇒ Object (readonly)
12 13 14 |
# File 'lib/ione/io/base_connection.rb', line 12 def host @host end |
#port ⇒ Object (readonly)
12 13 14 |
# File 'lib/ione/io/base_connection.rb', line 12 def port @port end |
Instance Method Details
#close(cause = nil) ⇒ true, false
Closes the connection
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ione/io/base_connection.rb', line 31 def close(cause=nil) @lock.synchronize do return false if @state == CLOSED_STATE @state = CLOSED_STATE @writable = false end if @io begin @io.close @io = nil rescue SystemCallError, IOError # nothing to do, the socket was most likely already closed end end if cause && !cause.is_a?(IoError) cause = ConnectionClosedError.new(cause.) end if cause @closed_promise.fail(cause) else @closed_promise.fulfill(self) end true end |
#closed? ⇒ Boolean
Returns true if the connection is closed
80 81 82 |
# File 'lib/ione/io/base_connection.rb', line 80 def closed? @state == CLOSED_STATE end |
#connected? ⇒ Boolean
Returns true if the connection is connected
75 76 77 |
# File 'lib/ione/io/base_connection.rb', line 75 def connected? @state == CONNECTED_STATE end |
#drain ⇒ Ione::Future
Wait for the connection's buffers to empty and then close it.
This method is almost always preferable to #close.
63 64 65 66 67 |
# File 'lib/ione/io/base_connection.rb', line 63 def drain @state = DRAINING_STATE close unless @writable @closed_promise.future end |
#on_closed {|error, nil| ... } ⇒ Object
Register to receive a notification when the socket is closed, both for expected and unexpected reasons.
Errors raised by the callback will be ignored.
118 119 120 121 |
# File 'lib/ione/io/base_connection.rb', line 118 def on_closed(&listener) @closed_promise.future.on_value { listener.call(nil) } @closed_promise.future.on_failure { |e| listener.call(e) } end |
#on_data {|String| ... } ⇒ Object
Register to receive notifications when new data is read from the socket.
You should only call this method in your protocol handler constructor.
Only one callback can be registered, if you register multiple times only the last one will receive notifications. This is not meant as a general event system, it's just for protocol handlers to receive data from their connection. If you want multiple listeners you need to implement that yourself in your protocol handler.
It is very important that you don't do any heavy lifting in the callback since it is called from the IO reactor thread, and as long as the callback is working the reactor can't handle any IO and no other callbacks can be called.
Errors raised by the callback will be ignored.
107 108 109 |
# File 'lib/ione/io/base_connection.rb', line 107 def on_data(&listener) @data_listener = listener end |
#to_s ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/ione/io/base_connection.rb', line 184 def to_s state_constant_name = self.class.constants.find do |name| name.to_s.end_with?('_STATE') && self.class.const_get(name) == @state end state = state_constant_name.to_s.rpartition('_').first %(#<#{self.class.name} #{state} #{@host}:#{@port}>) end |
#write(bytes = nil) {|buffer| ... } ⇒ Object
Write bytes to the socket.
You can either pass in bytes (as a string or as a ByteBuffer
), or you
can use the block form of this method to get access to the connection's
internal buffer.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ione/io/base_connection.rb', line 131 def write(bytes=nil) if @state == CONNECTED_STATE || @state == CONNECTING_STATE @lock.lock begin if block_given? yield @write_buffer elsif bytes @write_buffer.append(bytes) end @writable = !@write_buffer.empty? ensure @lock.unlock end @unblocker.unblock end end |