Heart/ClientProtocol/clientprotocol.cpp

62 lines
954 B
C++
Raw Normal View History

2019-10-08 13:03:13 +03:00
#include "clientprotocol.h"
#include <QDataStream>
#include <QVariantMap>
namespace ClientProtocol {
2019-10-09 17:58:58 +03:00
BaseHeader::BaseHeader() {
2019-10-08 13:03:13 +03:00
reset();
}
2019-10-09 17:58:58 +03:00
bool BaseHeader::isValid() const {
2019-10-08 13:03:13 +03:00
if (sizeof (*this) != 4) {
return false;
}
2019-10-08 18:02:25 +03:00
return true;
2019-10-08 13:03:13 +03:00
}
2019-10-09 17:58:58 +03:00
void BaseHeader::reset() {
2019-10-08 13:03:13 +03:00
size = 0;
2019-10-08 18:02:25 +03:00
command = 0;
triggerCommnad = 0;
2019-10-08 13:03:13 +03:00
}
2019-10-09 17:58:58 +03:00
BasePackage::BasePackage() {
2019-10-08 13:03:13 +03:00
reset();
}
2019-10-09 17:58:58 +03:00
bool BasePackage::isValid() const {
2019-10-08 13:03:13 +03:00
if (!hdr.isValid()) {
return false;
}
if (data.size() && hdr.command != data.at(0)) {
return false;
}
return hdr.size == static_cast<unsigned int> (data.size());
}
2019-10-09 17:58:58 +03:00
QByteArray BasePackage::toBytes() const {
2019-10-08 13:03:13 +03:00
QByteArray res;
2019-10-09 17:58:58 +03:00
res.append(reinterpret_cast<char*>(const_cast<BaseHeader*>(&hdr)),
2019-10-08 13:03:13 +03:00
sizeof (hdr));
res.append(data);
return res;
}
2019-10-09 17:58:58 +03:00
void BasePackage::reset() {
2019-10-08 13:03:13 +03:00
hdr.reset();
data.clear();
}
bool initClientProtockol() {
return true;
}
}