diff options
| author | Christoph Berg <myon@debian.org> | 2020-05-11 22:17:58 +0200 |
|---|---|---|
| committer | Christoph Berg <myon@debian.org> | 2020-05-11 22:17:58 +0200 |
| commit | 32a41b1abe1a4aab0892c9c2051be6ba5b379f1e (patch) | |
| tree | c6e107c1ee4d70610e43a7912e57c720334a08c7 /Network/NetworkMessage.cpp | |
| parent | 08299e8b2bf50ba90daf570f57def529dd29d6e7 (diff) | |
New upstream version 2.2.0~rc1+repackupstream/2.2.0_rc1+repack
Diffstat (limited to 'Network/NetworkMessage.cpp')
| -rw-r--r-- | Network/NetworkMessage.cpp | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/Network/NetworkMessage.cpp b/Network/NetworkMessage.cpp new file mode 100644 index 00000000..5af133c7 --- /dev/null +++ b/Network/NetworkMessage.cpp @@ -0,0 +1,136 @@ +#include "NetworkMessage.hpp" + +#include <exception> + +#include <QString> +#include <QByteArray> +#include <QDebug> + +#include "pimpl_impl.hpp" + +namespace NetworkMessage +{ + Builder::Builder (QIODevice * device, Type type, QString const& id, quint32 schema) + : QDataStream {device} + { + common_initialization (type, id, schema); + } + + Builder::Builder (QByteArray * a, Type type, QString const& id, quint32 schema) + : QDataStream {a, QIODevice::WriteOnly} + { + common_initialization (type, id, schema); + } + + void Builder::common_initialization (Type type, QString const& id, quint32 schema) + { + if (schema <= 1) + { + setVersion (QDataStream::Qt_5_0); // Qt schema version + } +#if QT_VERSION >= 0x050200 + else if (schema <= 2) + { + setVersion (QDataStream::Qt_5_2); // Qt schema version + } +#endif +#if QT_VERSION >= 0x050400 + else if (schema <= 3) + { + setVersion (QDataStream::Qt_5_4); // Qt schema version + } +#endif + else + { + throw std::runtime_error {"Unrecognized message schema"}; + } + + // the following two items assume that the quint32 encoding is + // unchanged over QDataStream versions + *this << magic; + *this << schema; + + *this << static_cast<quint32> (type) << id.toUtf8 (); + } + + class Reader::impl + { + public: + void common_initialization (Reader * parent) + { + quint32 magic; + *parent >> magic; + if (magic != Builder::magic) + { + throw std::runtime_error {"Invalid message format"}; + } + *parent >> schema_; + if (schema_ > Builder::schema_number) + { + throw std::runtime_error {"Unrecognized message schema"}; + } + if (schema_ <= 1) + { + parent->setVersion (QDataStream::Qt_5_0); + } +#if QT_VERSION >= 0x050200 + else if (schema_ <= 2) + { + parent->setVersion (QDataStream::Qt_5_2); + } +#endif +#if QT_VERSION >= 0x050400 + else if (schema_ <= 3) + { + parent->setVersion (QDataStream::Qt_5_4); + } +#endif + quint32 type; + *parent >> type >> id_; + if (type >= maximum_message_type_) + { + qDebug () << "Unrecognized message type:" << type << "from id:" << id_; + type_ = maximum_message_type_; + } + else + { + type_ = static_cast<Type> (type); + } + } + + quint32 schema_; + Type type_; + QByteArray id_; + }; + + Reader::Reader (QIODevice * device) + : QDataStream {device} + { + m_->common_initialization (this); + } + + Reader::Reader (QByteArray const& a) + : QDataStream {a} + { + m_->common_initialization (this); + } + + Reader::~Reader () + { + } + + quint32 Reader::schema () const + { + return m_->schema_; + } + + Type Reader::type () const + { + return static_cast<Type> (m_->type_); + } + + QString Reader::id () const + { + return QString::fromUtf8 (m_->id_); + } +} |
