Sha256: 504f4f585359d6dffc3c811f1f4a8e89b5cb6e2552cdda93f09eb1a5e7297583
Contents?: true
Size: 1.7 KB
Versions: 2
Compression:
Stored size: 1.7 KB
Contents
## # States of a connection class Midori::Connection include Midori::Server # @!attribute data # @return [String] string buffer of data to send attr_accessor :data # Init Connection with socket # @param [IO] socket raw socket def initialize(socket) @registered = false @socket = socket[0] @peer_addr = socket[1].ip_unpack @monitor = nil @close_flag = false @buffer = '' listen(@socket) end # Register events of connection # @param [Array] socket raw socket 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 message to client # @param [Midori::Response | String] data data to send def send_data(data) @buffer << (data.is_a?(String) ? data : data.to_s) send_buffer nil end # Send buffer immediately # @return [nil] nil private def send_buffer if @monitor.writable? written = @socket.write_nonblock(@buffer) @buffer = @buffer.byteslice(written..-1) end nil rescue IO::EAGAINWaitWritable => _e # :nocov: # Unknown Reason Resource Conflict nil # :nocov: end # Close the connection # @return [nil] nil 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 the connection after writing # @return [nil] nil def close_connection_after_writing @close_flag = true nil end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
midori.rb-0.7.1 | lib/midori/connection.rb |
midori.rb-0.7.0 | lib/midori/connection.rb |