Receive a single message from the stream.

        :return: A SLIP-decoded message
        :rtype: bytes
        :raises ProtocolError: when a SLIP protocol error has been encountered.
           A subsequent call to :meth:`recv_msg` (after handling the exception)
           will return the message from the next packet.

def recv_msg(self):
        """Receive a single message from the stream.

        :return: A SLIP-decoded message
        :rtype: bytes
        :raises ProtocolError: when a SLIP protocol error has been encountered.
           A subsequent call to :meth:`recv_msg` (after handling the exception)
           will return the message from the next packet.
        """

        # First check if there are any pending messages
        if self._messages:
            return self._messages.popleft()

        # No pending messages left. If a ProtocolError has occurred
        # it must be re-raised here:
        if self._protocol_error:
            self._handle_pending_protocol_error()

        while not self._messages and not self._stream_closed:
            # As long as no messages are available,
            # flush the internal packet buffer,
            # and try to read data
            try:
                if self._flush_needed:
                    self._flush_needed = False
                    self._messages.extend(self.driver.flush())
                else:
                    data = self.recv_bytes()
                    if data == b'':
                        self._stream_closed = True
                    self._messages.extend(self.driver.receive(data))
            except ProtocolError as pe:
                self._messages.extend(self.driver.messages)
                self._protocol_error = pe
                self._traceback = sys.exc_info()[2]
                break

        if self._messages:
            return self._messages.popleft()

        if self._protocol_error:
            self._handle_pending_protocol_error()
        else:
            return b''