2019-02-12 13:12:11 +03:00
|
|
|
#include "clientprotocol.h"
|
2019-02-13 13:03:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include <QVariantMap>
|
2019-02-14 13:58:51 +03:00
|
|
|
#include <typeinfo>
|
2019-02-13 22:03:29 +03:00
|
|
|
#include "snakeitem.h"
|
|
|
|
#include "player.h"
|
2019-02-13 13:03:40 +03:00
|
|
|
|
|
|
|
#define DEFAULT_GAME_PORT 7777
|
|
|
|
|
|
|
|
namespace ClientProtocol {
|
|
|
|
|
|
|
|
|
|
|
|
Header::Header() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Header::isValid() const {
|
|
|
|
|
2019-02-13 22:03:29 +03:00
|
|
|
if (sizeof (*this) != 2) {
|
2019-02-13 13:03:40 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (command) {
|
|
|
|
case ping: {
|
|
|
|
|
2019-02-13 22:03:29 +03:00
|
|
|
if (type > 1 || size > 0)
|
2019-02-13 13:03:40 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:03:29 +03:00
|
|
|
case item: {
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Request: return size == 36; // key sha256 (32byte) + id item 4
|
2019-02-14 13:58:51 +03:00
|
|
|
case Responke: return size < (snakeSize + sizeof (Class));
|
2019-02-13 22:03:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case login: {
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Request: return size == 96; // key sha256 (32byte) + maxsize of name of gmail (64)
|
|
|
|
case Responke: return size < MAX_SIZE_PLAYER && size > MIN_SIZE_PLAYER;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case playerData: {
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Request: return size == 96; // key sha256 (32byte) + maxsize of name of gmail (64)
|
|
|
|
case Responke: return size < MAX_SIZE_PLAYER && size > MIN_SIZE_PLAYER;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-13 13:03:40 +03:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Header::reset() {
|
|
|
|
size = 0;
|
|
|
|
command = undefined;
|
|
|
|
type = Responke;
|
|
|
|
}
|
|
|
|
|
|
|
|
Package::Package() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Package::isValid() const {
|
|
|
|
if (!hdr.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hdr.size == data.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap Package::parse() const {
|
|
|
|
if (!isValid())
|
|
|
|
return QVariantMap();
|
|
|
|
|
|
|
|
QVariantMap res;
|
|
|
|
|
2019-02-14 13:58:51 +03:00
|
|
|
res["command"] = hdr.command;
|
|
|
|
res["type"] = hdr.type;
|
|
|
|
res["status"] = true;
|
|
|
|
|
2019-02-13 13:03:40 +03:00
|
|
|
switch (hdr.command) {
|
|
|
|
case ping: {
|
|
|
|
if (hdr.type == Responke) {
|
|
|
|
res["res"] = "Pong";
|
|
|
|
} else {
|
|
|
|
res["value"] = "Ping";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-02-14 13:58:51 +03:00
|
|
|
case item: {
|
|
|
|
|
|
|
|
if (hdr.type == Responke) {
|
|
|
|
|
|
|
|
QDataStream stream(data);
|
|
|
|
unsigned char type;
|
|
|
|
stream >> type;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case SnakeData:{
|
|
|
|
if (!SnakeItem::read(stream, res)) {
|
|
|
|
res["status"] = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res["hash"] = data.left(32);
|
|
|
|
res["id"] = data.right(4).toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case login: {
|
|
|
|
|
|
|
|
if (hdr.type == Responke) {
|
|
|
|
|
|
|
|
QDataStream stream(data);
|
|
|
|
|
|
|
|
if (!Player::read(stream, res)) {
|
|
|
|
res["status"] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
QDataStream stream(data);
|
|
|
|
QString gmail;
|
|
|
|
QByteArray hash;
|
|
|
|
|
|
|
|
stream >> gmail;
|
|
|
|
stream >> hash;
|
|
|
|
|
|
|
|
res["gmail"] = gmail;
|
|
|
|
res["hash"] = hash;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case playerData: {
|
2019-02-13 22:03:29 +03:00
|
|
|
|
2019-02-14 13:58:51 +03:00
|
|
|
if (hdr.type == Responke) {
|
|
|
|
|
|
|
|
QDataStream stream(data);
|
|
|
|
if (!Player::read(stream, res)) {
|
|
|
|
res["status"] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
QDataStream stream(data);
|
|
|
|
QString gmail;
|
|
|
|
QByteArray hash;
|
|
|
|
|
|
|
|
stream >> gmail;
|
|
|
|
stream >> hash;
|
|
|
|
|
|
|
|
res["gmail"] = gmail;
|
|
|
|
res["hash"] = hash;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-02-13 13:03:40 +03:00
|
|
|
default:
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|