def _process_recv_buffer(self):
        """
        Process the :attr:`~pants.stream.Stream._recv_buffer`, passing
        chunks of data to :meth:`~pants.stream.Stream.on_read`.
        """
        while self._recv_buffer:
            delimiter = self.read_delimiter

            if delimiter is None:
                data = self._recv_buffer
                self._recv_buffer = ""
                self._safely_call(self.on_read, data)

            elif isinstance(delimiter, (int, long)):
                if len(self._recv_buffer) < delimiter:
                    break
                data = self._recv_buffer[:delimiter]
                self._recv_buffer = self._recv_buffer[delimiter:]
                self._safely_call(self.on_read, data)

            elif isinstance(delimiter, basestring):
                mark = self._recv_buffer.find(delimiter)
                if mark == -1:
                    break
                data = self._recv_buffer[:mark]
                self._recv_buffer = self._recv_buffer[mark + len(delimiter):]
                self._safely_call(self.on_read, data)

            elif isinstance(delimiter, Struct):
                if len(self._recv_buffer) < delimiter.size:
                    break
                data = self._recv_buffer[:delimiter.size]
                self._recv_buffer = self._recv_buffer[delimiter.size:]

                # Safely unpack it. This should *probably* never error.
                try:
                    data = delimiter.unpack(data)
                except struct.error:
                    log.exception("Unable to unpack data on %r." % self)
                    self.close()
                    break

                # Unlike most on_read calls, this one sends every variable of
                # the parsed data as its own argument.
                self._safely_call(self.on_read, *data)

            elif isinstance(delimiter, _NetStruct):
                if not self._netstruct_iter:
                    # We need to get started.
                    self._netstruct_iter = delimiter.iter_unpack()
                    self._netstruct_needed = next(self._netstruct_iter)

                if len(self._recv_buffer) < self._netstruct_needed:
                    break

                data = self._netstruct_iter.send(
                    self._recv_buffer[:self._netstruct_needed])
                self._recv_buffer = self._recv_buffer[self._netstruct_needed:]

                if isinstance(data, (int,long)):
                    self._netstruct_needed = data
                    continue

                # Still here? Then we've got our object. Delete the NetStruct
                # state and send the data.
                self._netstruct_needed = None
                self._netstruct_iter = None

                self._safely_call(self.on_read, *data)

            elif isinstance(delimiter, RegexType):
                # Depending on regex_search, we could do this two ways.
                if self.regex_search:
                    match = delimiter.search(self._recv_buffer)
                    if not match:
                        break

                    data = self._recv_buffer[:match.start()]
                    self._recv_buffer = self._recv_buffer[match.end():]

                else:
                    # Require the match to be at the beginning.
                    data = delimiter.match(self._recv_buffer)
                    if not data:
                        break

                    self._recv_buffer = self._recv_buffer[data.end():]

                # Send either the string or the match object.
                self._safely_call(self.on_read, data)

            else:
                # The safeguards in the read delimiter property should
                # prevent this from happening unless people start
                # getting too crafty for their own good.
                err = InvalidReadDelimiterError("Invalid read delimiter on %r." % self)
                self._safely_call(self.on_error, err)
                break

            if self._closed or not self.connected:
                break
dataset/ETHPy150Open ecdavis/pants/pants/stream.py/Stream._process_recv_buffer/original