Snake/SnakeServer/ClientProtocol/clientprotocol.h

164 lines
2.9 KiB
C
Raw Normal View History

2019-02-12 13:12:11 +03:00
#ifndef CLIENTPROTOCOL_H
#define CLIENTPROTOCOL_H
#include "clientprotocol_global.h"
2019-02-13 13:03:40 +03:00
#include <QVariantMap>
2019-02-25 21:38:37 +03:00
#include <snakeutils.h>
#include "config.h"
2019-02-13 13:03:40 +03:00
2019-02-12 13:12:11 +03:00
namespace ClientProtocol {
2019-04-29 14:39:48 +03:00
class BaseNetworkObject;
enum class Type: quint8 {
2019-03-05 00:48:32 +03:00
Undefined = 0x00,
Responke = 0x01,
Request = 0x02,
Stream = 0x03,
2019-02-13 13:03:40 +03:00
};
2019-04-29 14:39:48 +03:00
enum class Command: quint8 {
Undefined = 0x00,
Ping = 0x01,
BadRequest = 0x02,
Login = 0x03,
UpdatePlayerData = 0x04,
GameData = 0x05,
GetItem = 0x06,
2019-04-30 16:29:36 +03:00
Player = 0x07,
Snake = 0x08,
2019-05-01 20:13:44 +03:00
Map = 0x09
2019-04-29 14:39:48 +03:00
};
2019-02-26 20:24:05 +03:00
2019-04-29 14:39:48 +03:00
bool isValidSize(quint8 type, unsigned int size);
2019-02-13 13:03:40 +03:00
2019-04-29 14:39:48 +03:00
bool initClientProtockol();
2019-05-01 20:13:44 +03:00
auto cast(const BaseNetworkObject *obj);
2019-02-13 13:03:40 +03:00
/**
2019-04-29 14:39:48 +03:00
* @brief The Header struct 4 byte
2019-02-13 13:03:40 +03:00
*/
2019-03-05 11:11:46 +03:00
#pragma pack(push, 1)
2019-02-28 10:42:36 +03:00
struct CLIENTPROTOCOLSHARED_EXPORT Header {
2019-02-13 13:03:40 +03:00
/**
* @brief size - size of package data (not header)
*/
2019-04-29 14:39:48 +03:00
unsigned short size: 16;
2019-02-13 13:03:40 +03:00
/**
* @brief type of package see Type
*/
2019-04-29 14:39:48 +03:00
quint8 type: 2;
2019-02-13 13:03:40 +03:00
/**
* @brief command of pacage see Command
*/
2019-04-29 14:39:48 +03:00
quint8 command: 6;
2019-02-13 13:03:40 +03:00
/**
* @brief sig
* signed of package (package number)
*/
2019-03-02 13:29:21 +03:00
unsigned char sig : 8;
2019-02-13 13:03:40 +03:00
/**
* @brief Header default constructor
*/
Header();
/**
* @brief isValid
* @return true if header is valid
*/
bool isValid() const;
/**
* @brief reset - reset all data and set for header invalid status
*/
void reset();
};
2019-03-05 11:11:46 +03:00
#pragma pack(pop)
2019-02-13 13:03:40 +03:00
/**
* @brief The Package struct
*/
2019-02-28 10:42:36 +03:00
struct CLIENTPROTOCOLSHARED_EXPORT Package {
2019-02-13 13:03:40 +03:00
/**
* @brief hdr - header of package
*/
Header hdr;
/**
* @brief data - source data of package
*/
QByteArray data;
Package();
/**
* @brief isValid
* @return true if package is valid
*/
2019-05-01 20:13:44 +03:00
virtual bool isValid() const;
2019-02-13 13:03:40 +03:00
/**
* @brief parse
* @return Qmap of package (default key if "value")
*/
2019-04-29 14:39:48 +03:00
BaseNetworkObject * parse() const;
2019-02-15 23:36:59 +03:00
2019-05-02 17:39:56 +03:00
template<class T>
bool parse(T& res) {
auto obj = static_cast<T*>(parse());
if (!obj)
return false;
res = *obj;
return true;
}
2019-02-15 23:36:59 +03:00
/**
* @brief create - fill package
* @param data - data of filled
* @return true if all done
*/
2019-04-29 14:39:48 +03:00
bool create(const BaseNetworkObject *data, Type type);
2019-05-02 17:39:56 +03:00
/**
* @brief create
* @param cmd command of package
* @param type type
* @param data - data of filled
* @return
*/
bool create(Command cmd, Type type, const QByteArray& data);
2019-04-29 14:39:48 +03:00
/**
* @brief create
* @param cmd command of package
* @param type type
* @return
*/
bool create(Command cmd, Type type);
2019-02-13 13:03:40 +03:00
/**
* @brief toBytes
* @return bytes array of packag
*/
QByteArray toBytes() const;
/**
* @brief reset - reset all data and set for package invalid status
*/
void reset();
2019-02-12 13:12:11 +03:00
2019-05-01 20:13:44 +03:00
virtual ~Package() = default;
2019-02-13 13:03:40 +03:00
};
2019-02-15 23:36:59 +03:00
2019-02-12 13:12:11 +03:00
}
#endif // CLIENTPROTOCOL_H