Sha256: 342adb32b13d8cd7e579bb36add266fbf81dfc02b7d5ed87d0f1bc0067dc7721

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'hiredis/reader'

module EventMachine::Hiredis
  class Connection < EM::Connection
    include EventMachine::Hiredis::EventEmitter

    def initialize(host, port)
      super
      @host, @port = host, port
    end

    def reconnect(host, port)
      super
      @host, @port = host, port
    end

    def connection_completed
      EM::Hiredis.logger.debug("#{to_s}: connection open")
      @reader = ::Hiredis::Reader.new
      emit(:connected)
    end

    def receive_data(data)
      @reader.feed(data)
      until (reply = @reader.gets) == false
        emit(:message, reply)
      end
    end

    def unbind
      EM::Hiredis.logger.debug("#{to_s}: connection unbound")
      emit(:closed)
    end

    def send_command(sym, *args)
      send_data(command(sym, *args))
    end

    def to_s
      "Redis connection #{@host}:#{@port}"
    end

    protected

    COMMAND_DELIMITER = "\r\n"

    def command(*args)
      command = []
      command << "*#{args.size}"

      args.each do |arg|
        arg = arg.to_s
        command << "$#{string_size arg}"
        command << arg
      end

      command.join(COMMAND_DELIMITER) + COMMAND_DELIMITER
    end

    if "".respond_to?(:bytesize)
      def string_size(string)
        string.to_s.bytesize
      end
    else
      def string_size(string)
        string.to_s.size
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cs-em-hiredis-0.1.2 lib/em-hiredis/connection.rb