Sha256: 0d365a8f3b93207698040b2edde713ee7ff5e72435836f91b11a14ae5b299ca2

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 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
    @monitor = nil
    @close_flag = false
    @data = ''
    listen(socket)
  end

  # Register events of connection
  # @param [IO] socket raw socket
  def listen(socket)
    EventLoop.register(socket, :rw) do |monitor|
      @monitor = monitor
      if monitor.readable?
        receive_data(monitor)
      end
      if monitor.writable?
        if !@data.empty?
          # :nocov:
          # Leave for corner cases
          monitor.io.write_nonblock(@data)
          @data = ''
          # :nocov:
        elsif @close_flag
          close_connection
        end
      end
    end
  end

  # Send message to client
  # @param [String] data data to send
  def send_data(data)
    @monitor.writable? ? @socket.write_nonblock(data) : @data << data
  end

  # Close the connection
  def close_connection
    EventLoop.deregister @socket
    @socket.close
  end

  # Close the connection after writing
  def close_connection_after_writing
    @close_flag = true
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
em-midori-0.2.2 lib/midori/connection.rb
em-midori-0.2.1 lib/midori/connection.rb