Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
package.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2024 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#include "package.h"
9#include <crc/crchash.h>
10#include <QDataStream>
11
12namespace QH {
13
17
18bool Package::isValid() const {
19 if (!hdr.isValid()) {
20 return false;
21 }
22
23 if (hdr.size != static_cast<unsigned int>(data.size())) {
24 return false;
25 }
26
27 if (hdr.size > maximumSize())
28 return false;
29
30 return calcHash() == hdr.hash;
31}
32
34 hdr.reset();
35 data.clear();
36}
37
38QString Package::toString() const {
39 return QString("Pakcage description: %0."
40 " Data description: Data size - %1, Data: %2").
41 arg(hdr.toString()).arg(data.size()).arg(QString(data.toHex().toUpper()));
42}
43
44unsigned int Package::calcHash() const {
45 auto tmp = data + QByteArray::number(hdr.command);
46 return qa_common::hash32(tmp.constData(), tmp.size());
47}
48
49unsigned int Package::maximumSize() {
50 return 1024 * 1024;
51}
52
53QDataStream &Package::fromStream(QDataStream &stream) {
54 reset();
55 stream.readRawData(reinterpret_cast<char*>(&hdr), sizeof(Header));
56
57 char * buf = static_cast<char*>(malloc(hdr.size));
58 stream.readRawData(buf, hdr.size);
59 data.clear();
60 data.insert(0, buf, hdr.size);
61
62 free(buf);
63
64 return stream;
65}
66
67QDataStream &Package::toStream(QDataStream &stream) const {
68 stream.writeRawData(reinterpret_cast<const char*>(&hdr),
69 sizeof (hdr));
70
71 stream.writeRawData(data.data(),
72 data.size());
73
74 return stream;
75}
76
77}
unsigned int calcHash() const
calcHash This method recalc hash sum for this pacakge.
Definition package.cpp:44
QDataStream & fromStream(QDataStream &stream) override
fromStream This method should be read all bytes from the stream object and full the current object.
Definition package.cpp:53
QByteArray data
data This is source data of package.
Definition package.h:33
virtual bool isValid() const
isValid This method validation a current package. Default implementation is checked a header and comp...
Definition package.cpp:18
Header hdr
hdr This is header of package. For more information see the Header struct.
Definition package.h:29
QString toString() const
toString This method convert a package information to a string label.
Definition package.cpp:38
virtual void reset()
reset This method reset all data and set for package invalid status.
Definition package.cpp:33
static unsigned int maximumSize()
maximumSize This method return maximu size of pacakge. If pacakge large the maximum size then package...
Definition package.cpp:49
QDataStream & toStream(QDataStream &stream) const override
fromStream This method should be write all members of the current object to the stream object.
Definition package.cpp:67
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13
The Header struct 32 bytes.
Definition header.h:19
bool isValid() const
isValid This method check header size and compare commands.
Definition header.cpp:18
unsigned int hash
hash This is unique id of a package. id calc with CRC32 function for Qt implementation.
Definition header.h:38
QString toString() const
toString This method return string of header of package.
Definition header.cpp:36
unsigned int size
size This is size of package data (exclude header size).
Definition header.h:33
void reset()
reset This method reset all data and set for header invalid status.
Definition header.cpp:25
unsigned short command
command of package for more information see the AbstractData::toPackage method.
Definition header.h:23