# File lib/em-websocket/framing03.rb, line 11
      def process_data(newdata)
        error = false

        while !error && @data.size > 1
          pointer = 0

          more = ((@data.getbyte(pointer) & 0b10000000) == 0b10000000) ^ fin
          # Ignoring rsv1-3 for now
          opcode = @data.getbyte(0) & 0b00001111
          pointer += 1

          # Ignoring rsv4
          length = @data.getbyte(pointer) & 0b01111111
          pointer += 1

          payload_length = case length
          when 127 # Length defined by 8 bytes
            # Check buffer size
            if @data.getbyte(pointer+8-1) == nil
              debug [:buffer_incomplete, @data]
              error = true
              next
            end
            
            # Only using the last 4 bytes for now, till I work out how to
            # unpack 8 bytes. I'm sure 4GB frames will do for now :)
            l = @data[(pointer+4)..(pointer+7)].unpack('N').first
            pointer += 8
            l
          when 126 # Length defined by 2 bytes
            # Check buffer size
            if @data.getbyte(pointer+2-1) == nil
              debug [:buffer_incomplete, @data]
              error = true
              next
            end
            
            l = @data[pointer..(pointer+1)].unpack('n').first
            pointer += 2
            l
          else
            length
          end

          if payload_length > @connection.max_frame_size
            raise WSMessageTooBigError, "Frame length too long (#{payload_length} bytes)"
          end

          # Check buffer size
          if @data.getbyte(pointer+payload_length-1) == nil
            debug [:buffer_incomplete, @data]
            error = true
            next
          end

          # Throw away data up to pointer
          @data.slice!(0...pointer)

          # Read application data
          application_data = @data.slice!(0...payload_length)

          frame_type = opcode_to_type(opcode)

          if frame_type == :continuation && !@frame_type
            raise WSProtocolError, 'Continuation frame not expected'
          end

          if more
            debug [:moreframe, frame_type, application_data]
            @application_data_buffer << application_data
            # The message type is passed in the first frame
            @frame_type ||= frame_type
          else
            # Message is complete
            if frame_type == :continuation
              @application_data_buffer << application_data
              message(@frame_type, '', @application_data_buffer)
              @application_data_buffer = ''
              @frame_type = nil
            else
              message(frame_type, '', application_data)
            end
          end
        end # end while
      end