Snake/SnakeServer/ClientProtocol/clientprotocol.cpp

169 lines
3.2 KiB
C++
Raw Normal View History

2019-02-12 13:12:11 +03:00
#include "clientprotocol.h"
2019-04-29 14:39:48 +03:00
#include "gamedata.h"
#include "getitem.h"
#include "login.h"
#include "player.h"
#include "updateplayerdata.h"
2019-02-13 13:03:40 +03:00
2019-02-25 21:38:37 +03:00
#include <QDataStream>
2019-02-13 13:03:40 +03:00
#include <QVariantMap>
2019-04-29 14:39:48 +03:00
#include <factorynetobjects.h>
2019-02-13 13:03:40 +03:00
#define DEFAULT_GAME_PORT 7777
namespace ClientProtocol {
Header::Header() {
reset();
}
bool Header::isValid() const {
2019-04-29 14:39:48 +03:00
if (sizeof (*this) != 4) {
2019-02-13 13:03:40 +03:00
return false;
}
2019-04-29 14:39:48 +03:00
if (static_cast<Command>(command) == Command::Undefined) {
return false;
}
2019-04-29 14:39:48 +03:00
if (static_cast<Type>(type) == Type::Undefined) {
return false;
}
return isValidSize(command, size);
2019-02-13 13:03:40 +03:00
}
void Header::reset() {
size = 0;
2019-04-29 14:39:48 +03:00
command = static_cast<quint8>(Command::Undefined);
type = static_cast<quint8>(Type::Responke);
2019-02-13 13:03:40 +03:00
}
Package::Package() {
reset();
}
bool Package::isValid() const {
if (!hdr.isValid()) {
return false;
}
2019-04-29 14:39:48 +03:00
if (data.size() && hdr.command != data.at(0)) {
return false;
}
2019-03-05 00:48:32 +03:00
return hdr.size == static_cast<unsigned int> (data.size());
2019-02-13 13:03:40 +03:00
}
2019-04-29 14:39:48 +03:00
BaseNetworkObject* Package::parse() const {
2019-02-13 13:03:40 +03:00
if (!isValid())
2019-04-29 14:39:48 +03:00
return nullptr;
2019-04-29 14:39:48 +03:00
auto obj = FactoryNetObjects::build(hdr.command);
2019-04-29 14:39:48 +03:00
if (!obj) {
return nullptr;
2019-02-13 13:03:40 +03:00
}
2019-04-29 14:39:48 +03:00
QDataStream stream(data);
obj->readFromStream(stream);
return obj;
2019-02-15 23:36:59 +03:00
}
2019-02-26 20:24:05 +03:00
2019-04-29 14:39:48 +03:00
bool Package::create(const BaseNetworkObject *obj, Type type) {
if (!obj) {
return false;
}
2019-02-15 23:36:59 +03:00
2019-04-29 14:39:48 +03:00
auto command = obj->getClass();
2019-02-15 23:36:59 +03:00
2019-04-29 14:39:48 +03:00
if (command < 0) {
2019-02-15 23:36:59 +03:00
return false;
}
2019-03-05 00:48:32 +03:00
QDataStream stream(&data, QIODevice::ReadWrite);
2019-04-29 14:39:48 +03:00
obj->writeToStream(stream);
hdr.command = static_cast<quint8>(command);
hdr.type = static_cast<quint8>(type);
hdr.size = static_cast<unsigned short>(data.size());
2019-02-15 23:36:59 +03:00
2019-04-29 14:39:48 +03:00
return isValid();
}
bool Package::create(Command cmd, Type type) {
if (cmd == Command::Undefined) {
2019-03-05 00:48:32 +03:00
return false;
2019-02-15 23:36:59 +03:00
}
2019-04-29 14:39:48 +03:00
hdr.command = static_cast<quint8>(cmd);
hdr.type = static_cast<quint8>(type);
hdr.size = static_cast<unsigned short>(data.size());
2019-02-15 23:36:59 +03:00
return isValid();
2019-02-13 13:03:40 +03:00
}
QByteArray Package::toBytes() const {
QByteArray res;
res.append(reinterpret_cast<char*>(const_cast<Header*>(&hdr)),
sizeof (hdr));
res.append(data);
return res;
}
void Package::reset() {
hdr.reset();
data.clear();
}
2019-04-29 14:39:48 +03:00
bool isValidSize(quint8 type, unsigned int size) {
2019-02-25 21:38:37 +03:00
2019-04-29 14:39:48 +03:00
if (!FactoryNetObjects::isInited()) {
return false;
2019-03-05 00:48:32 +03:00
}
2019-02-25 21:38:37 +03:00
2019-04-29 14:39:48 +03:00
if (!FactoryNetObjects::isRegisteredType(type)) {
return size == 0;
2019-02-25 21:38:37 +03:00
}
2019-04-29 14:39:48 +03:00
return FactoryNetObjects::getSize(type).isValid(size);
2019-02-25 21:38:37 +03:00
}
2019-04-29 14:39:48 +03:00
bool initClientProtockol() {
if (!FactoryNetObjects::regType<Login>(
static_cast<quint8>(Command::Login))) {
2019-03-05 13:38:20 +03:00
return false;
}
2019-04-29 14:39:48 +03:00
if (!FactoryNetObjects::regType<UpdatePlayerData>(
static_cast<quint8>(Command::UpdatePlayerData))) {
return false;
2019-02-25 21:38:37 +03:00
}
2019-04-29 14:39:48 +03:00
if (!FactoryNetObjects::regType<GameData>(
static_cast<quint8>(Command::GameData))) {
2019-04-15 01:26:34 +03:00
return false;
}
2019-04-29 14:39:48 +03:00
if (!FactoryNetObjects::regType<GetItem>(
static_cast<quint8>(Command::GetItem))) {
return false;
}
2019-04-15 01:26:34 +03:00
2019-04-29 14:39:48 +03:00
if (!FactoryNetObjects::regType<Player>(
static_cast<quint8>(Command::Player))) {
return false;
2019-04-15 01:26:34 +03:00
}
return true;
}
2019-02-13 13:03:40 +03:00
}