Sha256: dcdbd57f9aa59064dbddfc5c72fa4338340dd8df5a09911fdeebeecfc49c38f3

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 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 connection_completed
      EventMachine::Hiredis.logger.info("Connected to Redis")
      @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
      EventMachine::Hiredis.logger.info("Disconnected from Redis")
      emit(:closed)
    end

    def send_command(sym, *args)
      send_data(command(sym, *args))
    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

2 entries across 2 versions & 1 rubygems

Version Path
em-hiredis-0.1.1 lib/em-hiredis/connection.rb
em-hiredis-0.1.0 lib/em-hiredis/connection.rb