SQCSimulator2023/SQCSim-common/netprotocol.cpp
2023-09-30 10:27:05 -04:00

507 lines
13 KiB
C++

#include "netprotocol.h"
void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::INPUT;
uint64_t time = in->timestamp;
uint8_t time8[sizeof(uint64_t)] = {(time >> 56) & 0xFF,
(time >> 48) & 0xFF,
(time >> 40) & 0xFF,
(time >> 32) & 0xFF,
(time >> 24) & 0xFF,
(time >> 16) & 0xFF,
(time >> 8 ) & 0xFF,
time & 0xFF};
memcpy(*buf + 1, time8, sizeof(uint64_t));
uint64_t sid = in->sid;
uint8_t sid8[sizeof(uint64_t)] = {(sid >> 56) & 0xFF,
(sid >> 48) & 0xFF,
(sid >> 40) & 0xFF,
(sid >> 32) & 0xFF,
(sid >> 24) & 0xFF,
(sid >> 16) & 0xFF,
(sid >> 8 ) & 0xFF,
sid & 0xFF};
memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t));
Keys keys = in->keys;
uint8_t keys8 = // Reste un bit.
keys.forward & 0b10000000 |
keys.backward & 0b01000000 |
keys.left & 0b00100000 |
keys.right & 0b00010000 |
keys.jump & 0b00001000 |
keys.shoot & 0b00000100 |
keys.block & 0b00000010 ;
memcpy(*buf + sizeof(uint64_t) + 2, &keys8, sizeof(uint8_t));
uint32_t vec[3];
memcpy(vec, &in->direction, sizeof(Vector3f)); // Pour dénaturer les floats.
uint8_t vec8[3 * sizeof(uint32_t)] = {
(vec[0] >> 24) & 0xFF,
(vec[0] >> 16) & 0xFF,
(vec[0] >> 8) & 0xFF,
vec[0] & 0xFF,
(vec[1] >> 24) & 0xFF,
(vec[1] >> 16) & 0xFF,
(vec[1] >> 8) & 0xFF,
vec[1] & 0xFF,
(vec[2] >> 24) & 0xFF,
(vec[2] >> 16) & 0xFF,
(vec[2] >> 8) & 0xFF,
vec[2] & 0xFF};
memcpy(*buf + sizeof(uint64_t) + 3, vec8, sizeof(uint32_t) * 3);
*buflen = sizeof(uint64_t) + 3 + sizeof(uint32_t) * 3;
}
void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) {
}
void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) {
}
void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::TEAMINF;
size_t namesize = std::strlen(tinfo->name) + 1;
memcpy(*buf + 1, &tinfo->name, namesize);
uint64_t tid = tinfo->id;
uint8_t tid8[sizeof(uint64_t)] = {
(tid >> 56) & 0xFF,
(tid >> 48) & 0xFF,
(tid >> 40) & 0xFF,
(tid >> 32) & 0xFF,
(tid >> 24) & 0xFF,
(tid >> 16) & 0xFF,
(tid >> 8) & 0xFF,
tid & 0xFF
};
memcpy(*buf + namesize + 2, tid8, sizeof(uint64_t));
*buflen = namesize + sizeof(uint64_t) + 2;
}
void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::LOGINF;
size_t namesize = std::strlen(linfo->name) + 1;
memcpy(*buf + 1, &linfo->name, namesize);
uint64_t sid = linfo->sid;
uint8_t sid8[sizeof(uint64_t)] = {
(sid >> 56) & 0xFF,
(sid >> 48) & 0xFF,
(sid >> 40) & 0xFF,
(sid >> 32) & 0xFF,
(sid >> 24) & 0xFF,
(sid >> 16) & 0xFF,
(sid >> 8) & 0xFF,
sid & 0xFF
};
memcpy(*buf + namesize + 2, sid8, sizeof(uint64_t));
uint64_t tid = linfo->tid;
uint8_t tid8[sizeof(uint64_t)] = {
(tid >> 56) & 0xFF,
(tid >> 48) & 0xFF,
(tid >> 40) & 0xFF,
(tid >> 32) & 0xFF,
(tid >> 24) & 0xFF,
(tid >> 16) & 0xFF,
(tid >> 8) & 0xFF,
tid & 0xFF
};
memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
}
void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::PLAYINF;
size_t namesize = std::strlen(pinfo->name) + 1;
memcpy(*buf + 1, &pinfo->name, namesize);
uint64_t id = pinfo->id;
uint8_t id8[sizeof(uint64_t)] = {
(id >> 56) & 0xFF,
(id >> 48) & 0xFF,
(id >> 40) & 0xFF,
(id >> 32) & 0xFF,
(id >> 24) & 0xFF,
(id >> 16) & 0xFF,
(id >> 8) & 0xFF,
id & 0xFF
};
memcpy(*buf + namesize + 2, id8, sizeof(uint64_t));
uint64_t tid = pinfo->tid;
uint8_t tid8[sizeof(uint64_t)] = {
(tid >> 56) & 0xFF,
(tid >> 48) & 0xFF,
(tid >> 40) & 0xFF,
(tid >> 32) & 0xFF,
(tid >> 24) & 0xFF,
(tid >> 16) & 0xFF,
(tid >> 8) & 0xFF,
tid & 0xFF
};
memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
}
void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::GAMEINFO;
uint64_t game = ginfo->seed;
uint8_t seed8[sizeof(uint64_t)] = {
(game >> 56) & 0xFF,
(game >> 48) & 0xFF,
(game >> 40) & 0xFF,
(game >> 32) & 0xFF,
(game >> 24) & 0xFF,
(game >> 16) & 0xFF,
(game >> 8) & 0xFF,
game & 0xFF
};
memcpy(*buf + 1, seed8, sizeof(uint64_t));
game = ginfo->countdown;
uint8_t count8[sizeof(uint64_t)] = {
(game >> 56) & 0xFF,
(game >> 48) & 0xFF,
(game >> 40) & 0xFF,
(game >> 32) & 0xFF,
(game >> 24) & 0xFF,
(game >> 16) & 0xFF,
(game >> 8) & 0xFF,
game & 0xFF
};
memcpy(*buf + sizeof(uint64_t) + 1, count8, sizeof(uint64_t));
game = ginfo->countdown;
uint8_t gtype8[sizeof(uint64_t)] = {
(game >> 56) & 0xFF,
(game >> 48) & 0xFF,
(game >> 40) & 0xFF,
(game >> 32) & 0xFF,
(game >> 24) & 0xFF,
(game >> 16) & 0xFF,
(game >> 8) & 0xFF,
game & 0xFF
};
memcpy(*buf + sizeof(uint64_t) + 1, gtype8, sizeof(uint64_t));
*buflen = sizeof(uint64_t) * 3 + 1;
}
void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::CHAT;
uint64_t src = chat->src_id;
uint8_t src8[sizeof(uint64_t)] = {
(src >> 56) & 0xFF,
(src >> 48) & 0xFF,
(src >> 40) & 0xFF,
(src >> 32) & 0xFF,
(src >> 24) & 0xFF,
(src >> 16) & 0xFF,
(src >> 8) & 0xFF,
src & 0xFF
};
memcpy(*buf + 1, src8, sizeof(uint64_t));
uint64_t dst = chat->dest_id;
uint8_t dst8[sizeof(uint64_t)] = {
(dst >> 56) & 0xFF,
(dst >> 48) & 0xFF,
(dst >> 40) & 0xFF,
(dst >> 32) & 0xFF,
(dst >> 24) & 0xFF,
(dst >> 16) & 0xFF,
(dst >> 8) & 0xFF,
dst & 0xFF
};
memcpy(*buf + 1 + sizeof(uint64_t), dst8, sizeof(uint64_t));
uint64_t dstteam = chat->dest_id;
uint8_t dstt8[sizeof(uint64_t)] = {
(dstteam >> 56) & 0xFF,
(dstteam >> 48) & 0xFF,
(dstteam >> 40) & 0xFF,
(dstteam >> 32) & 0xFF,
(dstteam >> 24) & 0xFF,
(dstteam >> 16) & 0xFF,
(dstteam >> 8) & 0xFF,
dstteam & 0xFF
};
memcpy(*buf + 1 + sizeof(uint64_t) * 2, dstt8, sizeof(uint64_t));
size_t messize = std::strlen(chat->mess) + 1;
memcpy(*buf + 1 + sizeof(uint64_t) * 3, &chat->mess, messize);
*buflen = messize + sizeof(uint64_t) * 3 + 2;
}
void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::ERRLOG;
size_t messize = std::strlen(errlog->mess) + 1;
memcpy(*buf + 1, &errlog->mess, messize);
memcpy(*buf + 1 + messize, &errlog->is_fatal, sizeof(bool));
*buflen = messize + sizeof(bool) + 1;
}
bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) {
return false;
}
bool netprot::Deserialize(Output* out, char* buf, const uint32_t buflen) {
return false;
}
bool netprot::Deserialize(Sync* sync, char* buf, const uint32_t buflen) {
return false;
}
bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(LoginInfo))
return false;
size_t namesize = std::strlen(buf) + 1;
if (namesize > 32)
return false;
memcpy(&tinfo->name, &buf[1], namesize);
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[namesize + 1], sizeof(uint64_t));
tinfo->id =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
return true;
}
bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(LoginInfo))
return false;
size_t namesize = std::strlen(buf) + 1;
if (namesize > 32)
return false;
memcpy(&linfo->name, &buf[1], namesize);
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[namesize + 1], sizeof(uint64_t));
linfo->sid =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[namesize + sizeof(uint64_t) + 1], sizeof(uint64_t));
linfo->tid =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
return true;
}
bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(PlayerInfo))
return false;
size_t namesize = std::strlen(buf) + 1;
if (namesize > 32)
return false;
memcpy(&pinfo->name, &buf[1], namesize);
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[namesize + 1], sizeof(uint64_t));
pinfo->id =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[namesize + sizeof(uint64_t) + 1], sizeof(uint64_t));
pinfo->tid =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
return true;
}
bool netprot::Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(GameInfo))
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[1], sizeof(uint64_t));
ginfo->seed =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[sizeof(uint64_t) + 1], sizeof(uint64_t));
ginfo->countdown =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[sizeof(uint64_t) * 2 + 1], sizeof(uint64_t));
ginfo->gameType =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
return true;
}
bool netprot::Deserialize(Chat* chat, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(Chat))
return false;
uint8_t src[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(src, &buf[1], sizeof(uint64_t));
chat->src_id =
(uint64_t)src[0] << 56 |
(uint64_t)src[1] << 48 |
(uint64_t)src[2] << 40 |
(uint64_t)src[3] << 32 |
(uint64_t)src[4] << 24 |
(uint64_t)src[5] << 16 |
(uint64_t)src[6] << 8 |
(uint64_t)src[7];
uint8_t dst[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(dst, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t));
chat->dest_id =
(uint64_t)dst[0] << 56 |
(uint64_t)dst[1] << 48 |
(uint64_t)dst[2] << 40 |
(uint64_t)dst[3] << 32 |
(uint64_t)dst[4] << 24 |
(uint64_t)dst[5] << 16 |
(uint64_t)dst[6] << 8 |
(uint64_t)dst[7];
uint8_t dstt[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(dstt, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint64_t));
chat->dest_team_id =
(uint64_t)dstt[0] << 56 |
(uint64_t)dstt[1] << 48 |
(uint64_t)dstt[2] << 40 |
(uint64_t)dstt[3] << 32 |
(uint64_t)dstt[4] << 24 |
(uint64_t)dstt[5] << 16 |
(uint64_t)dstt[6] << 8 |
(uint64_t)dstt[7];
size_t messsize = std::strlen(buf + sizeof(uint64_t) * 3) + 1;
if (messsize > 140)
return false;
memcpy(&chat->mess, &buf[1 + sizeof(uint64_t) * 3], messsize);
return true;
}
bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) {
if (buflen <= sizeof(ErrorLog))
return false;
size_t messsize = std::strlen(buf) + 1;
if (messsize > 140)
return false;
memcpy(&errlog->mess, &buf[1], messsize);
memcpy(&errlog->is_fatal, &buf[1 + messsize], sizeof(bool));
return true;
}
netprot::PacketType netprot::getType(char* buf, const uint32_t buflen) {
if (buflen < 1 ||
buf[0] >= netprot::PACKET_TYPE::LAST_PACK ||
buf[0] <= netprot::PACKET_TYPE::ERR)
return netprot::PACKET_TYPE::ERR;
return buf[0];
}