def read_message(self):
    while True:
        try:
            type_ = self.read_bytes(1)
            size = unpack('!I', self.read_bytes(4))[0]
            if (size < 4):
                raise errors.MessageError('Bad message size: {0}'.format(size))
            if (type_ == messages.WriteFile.message_id):
                f = self.read_string()
                filename = f.decode('utf-8')
                file_length = unpack('!I', self.read_bytes(4))[0]
                size -= (((4 + len(f)) + 1) + 4)
                if (size != file_length):
                    raise errors.MessageError('Bad message size: {0}'.format(size))
                if (filename == ''):
                    if ((file_length % 8) != 0):
                        raise errors.MessageError('Bad RETURNREJECTED data size: {0}'.format(file_length))
                    data = self.read_bytes(file_length)
                    message = messages.WriteFile(filename, file_length, data)
                else:
                    message = messages.WriteFile(filename, file_length)
            else:
                message = BackendMessage.from_type(type_, self.read_bytes((size - 4)))
            self._logger.debug('<= %s', message)
            self.handle_asynchronous_message(message)
            if isinstance(message, messages.ReadyForQuery):
                self.transaction_status = message.transaction_status
        except (SystemError, IOError) as e:
            self.close_socket()
            self._logger.error(e)
            raise_from(errors.ConnectionError(str(e)), e)
        if (not self.is_asynchronous_message(message)):
            break
    return message
def read_message(self):
    while True:
        try:
            type_ = self.read_bytes(1)
            size = unpack('!I', self.read_bytes(4))[0]
            if (size < 4):
                raise errors.MessageError('Bad message size: {0}'.format(size))
            if (type_ == messages.WriteFile.message_id):
                f = self.read_string()
                filename = f.decode('utf-8')
                file_length = unpack('!I', self.read_bytes(4))[0]
                size -= (((4 + len(f)) + 1) + 4)
                if (size != file_length):
                    raise errors.MessageError('Bad message size: {0}'.format(size))
                if (filename == ):
                    if ((file_length % 8) != 0):
                        raise errors.MessageError('Bad RETURNREJECTED data size: {0}'.format(file_length))
                    data = self.read_bytes(file_length)
                    message = messages.WriteFile(filename, file_length, data)
                else:
                    message = messages.WriteFile(filename, file_length)
            else:
                message = BackendMessage.from_type(type_, self.read_bytes((size - 4)))
            self._logger.debug('<= %s', message)
            self.handle_asynchronous_message(message)
            if isinstance(message, messages.ReadyForQuery):
                self.transaction_status = message.transaction_status
        except (SystemError, IOError) as e:
            self.close_socket()
            self._logger.error(e)
            raise_from(errors.ConnectionError(str(e)), e)
        if (not self.is_asynchronous_message(message)):
            break
    return message