class Midori::Connection
States of a connection
Attributes
data[RW]
@!attribute data
@return [String] string buffer of data to send
Public Class Methods
new(socket)
click to toggle source
Init Connection
with socket @param [IO] socket raw socket
# File lib/midori/connection.rb, line 12 def initialize(socket) @registered = false @socket = socket @monitor = nil @close_flag = false @data = '' listen(socket) end
Public Instance Methods
close_connection()
click to toggle source
Close the connection
# File lib/midori/connection.rb, line 50 def close_connection EventLoop.deregister @socket @socket.close end
close_connection_after_writing()
click to toggle source
Close the connection after writing
# File lib/midori/connection.rb, line 56 def close_connection_after_writing @close_flag = true end
listen(socket)
click to toggle source
Register events of connection @param [IO] socket raw socket
# File lib/midori/connection.rb, line 23 def listen(socket) EventLoop.register(socket, :rw) do |monitor| @monitor = monitor if monitor.readable? receive_data(monitor) end if monitor.writable? if !@data == '' # :nocov: # Leave for corner cases monitor.io.write_nonblock(@data) @data = '' # :nocov: elsif @close_flag close_connection end end end end
send_data(data)
click to toggle source
Send message to client @param [String] data data to send
# File lib/midori/connection.rb, line 45 def send_data(data) @monitor.writable? ? @socket.write_nonblock(data) : @data << data end