Split data into a sequence of frames.

def frameify(self, state, data):
        """Split data into a sequence of frames."""

        # Pull in any partially-processed data
        data = state.recv_buf + data

        # Loop over the data
        while data:
            if state.frame_len is None:
                # Try to grab a frame length from the data
                if len(data) < self.fmt.size:
                    # Not enough data; try back later
                    break

                # Extract the length
                state.frame_len = self.fmt.unpack(data[:self.fmt.size])[0]
                data = data[self.fmt.size:]

            # Now that we have the frame length, extract the frame
            if len(data) < state.frame_len:
                # Not enough data; try back later
                break

            # OK, we have a full frame...
            frame = data[:state.frame_len]
            data = data[state.frame_len:]
            state.frame_len = None

            # Yield the frame
            try:
                yield frame
            except FrameSwitch:
                break

        # Put any remaining data back into the buffer
        state.recv_buf = data