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[0] @peer_addr = socket[1].ip_unpack @close_flag = false @buffer = '' end
Public Instance Methods
close_connection()
click to toggle source
Close the connection @return [nil] nil
# File lib/midori/connection.rb, line 52 def close_connection @socket.close nil end
close_connection_after_writing()
click to toggle source
Close the connection after writing @return [nil] nil
# File lib/midori/connection.rb, line 59 def close_connection_after_writing @close_flag = true nil end
listen()
click to toggle source
Register events of connection @param [Array] socket raw socket
# File lib/midori/connection.rb, line 22 def listen Fiber.schedule do until @socket.closed? receive_data(@socket) if !@buffer.empty? send_buffer elsif @close_flag close_connection end end end end
send_data(data)
click to toggle source
Send message to client @param [Midori::Response | String] data data to send
# File lib/midori/connection.rb, line 37 def send_data(data) @buffer << (data.is_a?(String) ? data : data.to_s) send_buffer nil end
Private Instance Methods
send_buffer()
click to toggle source
Send buffer immediately @return [nil] nil
# File lib/midori/connection.rb, line 45 def send_buffer @socket.write(@buffer) unless @socket.closed? nil end