class WebSocket::Driver::Hybi

def parse(chunk)

def parse(chunk)
  @reader.put(chunk)
  buffer = true
  while buffer
    case @stage
      when 0 then
        buffer = @reader.read(1)
        parse_opcode(buffer.getbyte(0)) if buffer
      when 1 then
        buffer = @reader.read(1)
        parse_length(buffer.getbyte(0)) if buffer
      when 2 then
        buffer = @reader.read(@frame.length_bytes)
        parse_extended_length(buffer) if buffer
      when 3 then
        buffer = @reader.read(4)
        if buffer
          @stage = 4
          @frame.masking_key = buffer
        end
      when 4 then
        buffer = @reader.read(@frame.length)
        if buffer
          @stage = 0
          emit_frame(buffer)
        end
      else
        buffer = nil
    end
  end
end