Class: Midori::Connection
- Inherits:
-
Object
- Object
- Midori::Connection
- Includes:
- Server
- Defined in:
- lib/midori/connection.rb
Overview
States of a connection
Instance Attribute Summary collapse
-
#data ⇒ String
String buffer of data to send.
Attributes included from Server
#api, #eventsource, #request, #websocket
Instance Method Summary collapse
-
#close_connection ⇒ nil
Close the connection.
-
#close_connection_after_writing ⇒ nil
Close the connection after writing.
-
#initialize(socket) ⇒ Connection
constructor
Init Connection with socket.
-
#listen(socket) ⇒ Object
Register events of connection.
-
#send_data(data) ⇒ Object
Send message to client.
Methods included from Server
#call_event, #receive_data, #receive_new_request, #server_initialize, #websocket_request
Constructor Details
#initialize(socket) ⇒ Connection
Init Connection with socket
12 13 14 15 16 17 18 19 20 |
# File 'lib/midori/connection.rb', line 12 def initialize(socket) @registered = false @socket = socket[0] @peer_addr = socket[1].ip_unpack @monitor = nil @close_flag = false @buffer = '' listen(@socket) end |
Instance Attribute Details
#data ⇒ String
Returns string buffer of data to send.
8 9 10 |
# File 'lib/midori/connection.rb', line 8 def data @data end |
Instance Method Details
#close_connection ⇒ nil
Close the connection
63 64 65 66 67 68 |
# File 'lib/midori/connection.rb', line 63 def close_connection EventLoop.remove_timer(@keep_alive_timer) unless @keep_alive_timer.nil? # Be sure to remove timer for memory safety EventLoop.deregister @socket @socket.close nil end |
#close_connection_after_writing ⇒ nil
Close the connection after writing
72 73 74 75 |
# File 'lib/midori/connection.rb', line 72 def close_connection_after_writing @close_flag = true nil end |
#listen(socket) ⇒ Object
Register events of connection
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/midori/connection.rb', line 24 def listen(socket) EventLoop.register(socket, :rw) do |monitor| @monitor = monitor receive_data(monitor) if monitor.readable? if monitor.writable? if !@buffer.empty? send_buffer elsif @close_flag close_connection end end end end |
#send_data(data) ⇒ Object
Send message to client
40 41 42 43 44 |
# File 'lib/midori/connection.rb', line 40 def send_data(data) @buffer << (data.is_a?(String) ? data : data.to_s) send_buffer nil end |