Sha256: 6be76565edc4b03e90e6ae8a4c32a03ae70e6d9a5cfff84f8c9c3d5512bf4b6e

Contents?: true

Size: 938 Bytes

Versions: 6

Compression:

Stored size: 938 Bytes

Contents

module Oxblood
  # @private
  class BufferedIO
    def initialize(socket)
      @socket = socket
      @buffer = String.new
    end

    def gets(separator, timeout)
      crlf = nil

      while (crlf = @buffer.index(separator)) == nil
        @buffer << _read_from_socket(1024, timeout)
      end

      @buffer.slice!(0, crlf + separator.bytesize)
    end

    def read(nbytes, timeout)
      result = @buffer.slice!(0, nbytes)

      while result.bytesize < nbytes
        result << _read_from_socket(nbytes - result.bytesize, timeout)
      end

      result
    end

    private

    def _read_from_socket(nbytes, timeout)
      begin
        @socket.read_nonblock(nbytes)
      rescue Errno::EWOULDBLOCK, Errno::EAGAIN
        if IO.select([@socket], nil, nil, timeout)
          retry
        else
          raise Connection::TimeoutError
        end
      end

    rescue EOFError
      raise Errno::ECONNRESET
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
oxblood-0.1.0.dev6 lib/oxblood/buffered_io.rb
oxblood-0.1.0.dev5 lib/oxblood/buffered_io.rb
oxblood-0.1.0.dev4 lib/oxblood/buffered_io.rb
oxblood-0.1.0.dev3 lib/oxblood/buffered_io.rb
oxblood-0.1.0.dev2 lib/oxblood/buffered_io.rb
oxblood-0.1.0.dev1 lib/oxblood/buffered_io.rb