void Writer::writeMessage(const protocol::Message &msg) { Q_ASSERT(m_file->isOpen()); if(msg.isRecordable()) { // Write Interval message if sufficient time has passed since last message was written if(m_minInterval>0) { qint64 now = QDateTime::currentMSecsSinceEpoch(); qint64 interval = now - m_interval; if(interval >= m_minInterval) { protocol::Interval imsg(0, qMin(qint64(0xffff), interval)); QVarLengthArray<char> ibuf(imsg.length()); int ilen = imsg.serialize(ibuf.data()); m_file->write(ibuf.data(), ilen); } m_interval = now; } // Write the actual message QVarLengthArray<char> buf(msg.length()); int len = msg.serialize(buf.data()); Q_ASSERT(len == buf.length()); m_file->write(buf.data(), len); } }
bool Writer::writeMessage(const protocol::Message &msg) { Q_ASSERT(m_file->isOpen()); if(m_encoding == Encoding::Binary) { QVarLengthArray<char> buf(msg.length()); const int len = msg.serialize(buf.data()); Q_ASSERT(len == buf.length()); if(m_file->write(buf.data(), len) != len) return false; } else { if(msg.type() == protocol::MSG_FILTERED) { // Special case: Filtered messages are // written as comments in the text format. const protocol::Filtered &fm = static_cast<const protocol::Filtered&>(msg); auto wrapped = fm.decodeWrapped(); QString comment; if(wrapped.isNull()) { comment = QStringLiteral("FILTERED: undecodable message type #%1 of length %2") .arg(fm.wrappedType()) .arg(fm.wrappedPayloadLength()); } else { comment = QStringLiteral("FILTERED: ") + wrapped->toString(); } return writeComment(comment); } QByteArray line = msg.toString().toUtf8(); if(m_file->write(line) != line.length()) return false; if(m_file->write("\n", 1) != 1) return false; // Write extra newlines after certain commands to give // the file some visual structure switch(msg.type()) { case protocol::MSG_UNDOPOINT: if(m_file->write("\n", 1) != 1) return false; default: break; } } return true; }