Sha256: a29072e4c8148623551c98854cbc30bb9d1aec06cea2c51bfb829ae0399df267

Contents?: true

Size: 1.25 KB

Versions: 14

Compression:

Stored size: 1.25 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 == ''
          # :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

14 entries across 14 versions & 2 rubygems

Version Path
midori.rb-0.5.1 lib/midori/connection.rb
midori.rb-0.5.0 lib/midori/connection.rb
midori.rb-0.4.4.1 lib/midori/connection.rb
midori.rb-0.4.4 lib/midori/connection.rb
midori.rb-0.4.3 lib/midori/connection.rb
em-midori-0.4.3 lib/midori/connection.rb
em-midori-0.4.2.1 lib/midori/connection.rb
em-midori-0.4.2 lib/midori/connection.rb
em-midori-0.4.1.1 lib/midori/connection.rb
em-midori-0.4.1 lib/midori/connection.rb
em-midori-0.4.0 lib/midori/connection.rb
em-midori-0.3.0 lib/midori/connection.rb
em-midori-0.2.4 lib/midori/connection.rb
em-midori-0.2.3 lib/midori/connection.rb