SQCSimulator2023/SQCSim-common/netprotocol.cpp

941 lines
27 KiB
C++
Raw Permalink Normal View History

2023-09-25 16:30:03 -04:00
#include "netprotocol.h"
void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::INPUT;
2023-09-25 16:30:03 -04:00
2023-09-30 10:27:05 -04:00
uint64_t time = in->timestamp;
2023-10-27 09:28:34 -04:00
uint8_t time8[sizeof(uint64_t)] = {(uint8_t)((time >> 56) & 0xFF),
(uint8_t)((time >> 48) & 0xFF),
(uint8_t)((time >> 40) & 0xFF),
(uint8_t)((time >> 32) & 0xFF),
(uint8_t)((time >> 24) & 0xFF),
(uint8_t)((time >> 16) & 0xFF),
(uint8_t)((time >> 8 ) & 0xFF),
(uint8_t)(time & 0xFF)};
2023-09-30 10:27:05 -04:00
memcpy(*buf + 1, time8, sizeof(uint64_t));
uint64_t sid = in->sid;
2023-10-27 09:28:34 -04:00
uint8_t sid8[sizeof(uint64_t)] = {(uint8_t)((sid >> 56) & 0xFF),
(uint8_t)((sid >> 48) & 0xFF),
(uint8_t)((sid >> 40) & 0xFF),
(uint8_t)((sid >> 32) & 0xFF),
(uint8_t)((sid >> 24) & 0xFF),
(uint8_t)((sid >> 16) & 0xFF),
(uint8_t)((sid >> 8 ) & 0xFF),
(uint8_t)( sid & 0xFF)};
2023-09-30 10:27:05 -04:00
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 ;
2023-10-24 09:17:21 -04:00
memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t));
2023-09-30 10:27:05 -04:00
uint32_t vec[3];
2023-10-27 09:28:34 -04:00
memcpy(vec, &in->direction, sizeof(Vector3f)); // Pour d<>naturer les floats.
2023-09-30 10:27:05 -04:00
uint8_t vec8[3 * sizeof(uint32_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF)};
2023-09-30 10:27:05 -04:00
2023-10-24 09:17:21 -04:00
memcpy(*buf + sizeof(uint64_t) * 2 + 2, vec8, sizeof(uint32_t) * 3);
2023-09-30 10:27:05 -04:00
2023-10-24 09:17:21 -04:00
*buflen = sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 3;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::OUTPUT;
2023-10-24 10:43:30 -04:00
2023-10-27 15:07:38 -04:00
uint64_t time = out->timestamp;
uint8_t time8[sizeof(uint64_t)] = { (uint8_t)((time >> 56) & 0xFF),
(uint8_t)((time >> 48) & 0xFF),
(uint8_t)((time >> 40) & 0xFF),
(uint8_t)((time >> 32) & 0xFF),
(uint8_t)((time >> 24) & 0xFF),
(uint8_t)((time >> 16) & 0xFF),
(uint8_t)((time >> 8) & 0xFF),
(uint8_t)(time & 0xFF) };
memcpy(*buf + 1, time8, sizeof(uint64_t));
2023-10-24 10:43:30 -04:00
2023-10-27 15:07:38 -04:00
uint64_t sid = out->id;
uint8_t sid8[sizeof(uint64_t)] = { (uint8_t)((sid >> 56) & 0xFF),
(uint8_t)((sid >> 48) & 0xFF),
(uint8_t)((sid >> 40) & 0xFF),
(uint8_t)((sid >> 32) & 0xFF),
(uint8_t)((sid >> 24) & 0xFF),
(uint8_t)((sid >> 16) & 0xFF),
(uint8_t)((sid >> 8) & 0xFF),
(uint8_t)(sid & 0xFF) };
memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t));
// TODO: Finir ca.
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::SYNC;
2023-10-27 15:07:38 -04:00
uint64_t time = sync->timestamp;
uint8_t stamp8[sizeof(uint64_t)] = { (uint8_t)((time >> 56) & 0xFF),
(uint8_t)((time >> 48) & 0xFF),
(uint8_t)((time >> 40) & 0xFF),
(uint8_t)((time >> 32) & 0xFF),
(uint8_t)((time >> 24) & 0xFF),
(uint8_t)((time >> 16) & 0xFF),
(uint8_t)((time >> 8) & 0xFF),
(uint8_t)(time & 0xFF) };
memcpy(*buf + 1, stamp8, sizeof(uint64_t));
uint64_t sid = sync->sid;
uint8_t sid8[sizeof(uint64_t)] = { (uint8_t)((sid >> 56) & 0xFF),
(uint8_t)((sid >> 48) & 0xFF),
(uint8_t)((sid >> 40) & 0xFF),
(uint8_t)((sid >> 32) & 0xFF),
(uint8_t)((sid >> 24) & 0xFF),
(uint8_t)((sid >> 16) & 0xFF),
(uint8_t)((sid >> 8) & 0xFF),
(uint8_t)(sid & 0xFF) };
memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t));
uint32_t timer = sync->timer;
uint8_t time8[sizeof(uint32_t)] = {(uint8_t)((timer >> 24) & 0xFF),
(uint8_t)((timer >> 16) & 0xFF),
(uint8_t)((timer >> 8) & 0xFF),
(uint8_t)(timer & 0xFF) };
memcpy(*buf + sizeof(uint64_t) * 2 + 1, time8, sizeof(uint32_t));
uint16_t ammo = sync->ammo;
uint8_t ammo8[sizeof(uint16_t)] = { (uint8_t)((ammo >> 8) & 0xFF),
(uint8_t)(ammo & 0xFF) };
memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) + 1, ammo8, sizeof(uint16_t));
memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t) + 1, &sync->hp, sizeof(uint8_t));
uint32_t vec[3];
memcpy(vec, &sync->position, sizeof(Vector3f)); // Pour d<>naturer les floats.
uint8_t vec8[3 * sizeof(uint32_t)] = {
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF) };
2023-10-24 10:43:30 -04:00
2023-10-27 15:07:38 -04:00
memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t) + 2, vec8, sizeof(uint32_t) * 3);
2023-10-24 10:43:30 -04:00
2023-10-27 15:07:38 -04:00
*buflen = sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + 2;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::TEAMINF;
2023-09-25 16:30:03 -04:00
2023-09-29 12:04:08 -04:00
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)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((tid >> 56) & 0xFF),
(uint8_t)((tid >> 48) & 0xFF),
(uint8_t)((tid >> 40) & 0xFF),
(uint8_t)((tid >> 32) & 0xFF),
(uint8_t)((tid >> 24) & 0xFF),
(uint8_t)((tid >> 16) & 0xFF),
(uint8_t)((tid >> 8) & 0xFF),
(uint8_t)(tid & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + namesize + 2, tid8, sizeof(uint64_t));
*buflen = namesize + sizeof(uint64_t) + 2;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::LOGINF;
2023-09-25 16:30:03 -04:00
2023-09-27 15:44:56 -04:00
size_t namesize = std::strlen(linfo->name) + 1;
2023-09-27 11:24:41 -04:00
memcpy(*buf + 1, &linfo->name, namesize);
2023-09-25 16:30:03 -04:00
uint64_t sid = linfo->sid;
uint8_t sid8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((sid >> 56) & 0xFF),
(uint8_t)((sid >> 48) & 0xFF),
(uint8_t)((sid >> 40) & 0xFF),
(uint8_t)((sid >> 32) & 0xFF),
(uint8_t)((sid >> 24) & 0xFF),
(uint8_t)((sid >> 16) & 0xFF),
(uint8_t)((sid >> 8) & 0xFF),
(uint8_t)(sid & 0xFF)
2023-09-27 11:24:41 -04:00
};
2023-09-25 16:30:03 -04:00
memcpy(*buf + namesize + 2, sid8, sizeof(uint64_t));
uint64_t tid = linfo->tid;
uint8_t tid8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((tid >> 56) & 0xFF),
(uint8_t)((tid >> 48) & 0xFF),
(uint8_t)((tid >> 40) & 0xFF),
(uint8_t)((tid >> 32) & 0xFF),
(uint8_t)((tid >> 24) & 0xFF),
(uint8_t)((tid >> 16) & 0xFF),
(uint8_t)((tid >> 8) & 0xFF),
(uint8_t)(tid & 0xFF)
};
2023-09-25 16:30:03 -04:00
memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
2023-09-25 16:30:03 -04:00
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::PLAYINF;
2023-09-29 12:04:08 -04:00
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)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((id >> 56) & 0xFF),
(uint8_t)((id >> 48) & 0xFF),
(uint8_t)((id >> 40) & 0xFF),
(uint8_t)((id >> 32) & 0xFF),
(uint8_t)((id >> 24) & 0xFF),
(uint8_t)((id >> 16) & 0xFF),
(uint8_t)((id >> 8) & 0xFF),
(uint8_t)(id & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + namesize + 2, id8, sizeof(uint64_t));
uint64_t tid = pinfo->tid;
uint8_t tid8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((tid >> 56) & 0xFF),
(uint8_t)((tid >> 48) & 0xFF),
(uint8_t)((tid >> 40) & 0xFF),
(uint8_t)((tid >> 32) & 0xFF),
(uint8_t)((tid >> 24) & 0xFF),
(uint8_t)((tid >> 16) & 0xFF),
(uint8_t)((tid >> 8) & 0xFF),
(uint8_t)(tid & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
2023-09-25 16:30:03 -04:00
2023-09-29 12:04:08 -04:00
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::GAMEINFO;
uint64_t game = ginfo->seed;
uint8_t seed8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((game >> 56) & 0xFF),
(uint8_t)((game >> 48) & 0xFF),
(uint8_t)((game >> 40) & 0xFF),
(uint8_t)((game >> 32) & 0xFF),
(uint8_t)((game >> 24) & 0xFF),
(uint8_t)((game >> 16) & 0xFF),
(uint8_t)((game >> 8) & 0xFF),
(uint8_t)(game & 0xFF)
};
memcpy(*buf + 1, seed8, sizeof(uint64_t));
game = ginfo->countdown;
uint8_t count8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((game >> 56) & 0xFF),
(uint8_t)((game >> 48) & 0xFF),
(uint8_t)((game >> 40) & 0xFF),
(uint8_t)((game >> 32) & 0xFF),
(uint8_t)((game >> 24) & 0xFF),
(uint8_t)((game >> 16) & 0xFF),
(uint8_t)((game >> 8) & 0xFF),
(uint8_t)(game & 0xFF)
};
memcpy(*buf + sizeof(uint64_t) + 1, count8, sizeof(uint64_t));
game = ginfo->countdown;
uint8_t gtype8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((game >> 56) & 0xFF),
(uint8_t)((game >> 48) & 0xFF),
(uint8_t)((game >> 40) & 0xFF),
(uint8_t)((game >> 32) & 0xFF),
(uint8_t)((game >> 24) & 0xFF),
(uint8_t)((game >> 16) & 0xFF),
(uint8_t)((game >> 8) & 0xFF),
(uint8_t)(game & 0xFF)
};
memcpy(*buf + sizeof(uint64_t) + 1, gtype8, sizeof(uint64_t));
2023-09-25 16:30:03 -04:00
*buflen = sizeof(uint64_t) * 3 + 1;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::CHAT;
2023-09-29 12:04:08 -04:00
uint64_t src = chat->src_id;
uint8_t src8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((src >> 56) & 0xFF),
(uint8_t)((src >> 48) & 0xFF),
(uint8_t)((src >> 40) & 0xFF),
(uint8_t)((src >> 32) & 0xFF),
(uint8_t)((src >> 24) & 0xFF),
(uint8_t)((src >> 16) & 0xFF),
(uint8_t)((src >> 8) & 0xFF),
(uint8_t)(src & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + 1, src8, sizeof(uint64_t));
uint64_t dst = chat->dest_id;
uint8_t dst8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((dst >> 56) & 0xFF),
(uint8_t)((dst >> 48) & 0xFF),
(uint8_t)((dst >> 40) & 0xFF),
(uint8_t)((dst >> 32) & 0xFF),
(uint8_t)((dst >> 24) & 0xFF),
(uint8_t)((dst >> 16) & 0xFF),
(uint8_t)((dst >> 8) & 0xFF),
(uint8_t)(dst & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + 1 + sizeof(uint64_t), dst8, sizeof(uint64_t));
uint64_t dstteam = chat->dest_id;
uint8_t dstt8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((dstteam >> 56) & 0xFF),
(uint8_t)((dstteam >> 48) & 0xFF),
(uint8_t)((dstteam >> 40) & 0xFF),
(uint8_t)((dstteam >> 32) & 0xFF),
(uint8_t)((dstteam >> 24) & 0xFF),
(uint8_t)((dstteam >> 16) & 0xFF),
(uint8_t)((dstteam >> 8) & 0xFF),
(uint8_t)(dstteam & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + 1 + sizeof(uint64_t) * 2, dstt8, sizeof(uint64_t));
2023-09-25 16:30:03 -04:00
2023-09-29 12:04:08 -04:00
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;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::ERRLOG;
2023-09-29 12:04:08 -04:00
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;
2023-09-25 16:30:03 -04:00
}
bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) {
2023-10-24 07:32:16 -04:00
if (buflen <= sizeof(Input))
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[1], sizeof(uint64_t));
in->timestamp =
(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[1 + sizeof(uint64_t)], sizeof(uint64_t));
in->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];
uint8_t keys = 0;
2023-10-24 09:17:21 -04:00
memcpy(&keys, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint8_t));
2023-10-27 09:28:34 -04:00
in->keys.forward = keys & 0b10000000;
in->keys.backward = keys & 0b01000000;
in->keys.left = keys & 0b00100000;
in->keys.right = keys & 0b00010000;
in->keys.jump = keys & 0b00001000;
in->keys.shoot = keys & 0b00000100;
in->keys.block = keys & 0b00000010;
2023-10-24 07:32:16 -04:00
uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
2023-10-24 09:17:21 -04:00
memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2], sizeof(uint8_t) * 12);
2023-10-24 07:32:16 -04:00
uint32_t vec[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11]};
memcpy(&in->direction, vec, sizeof(uint32_t) * 3);
return true;
2023-09-25 16:30:03 -04:00
}
bool netprot::Deserialize(Output* out, char* buf, const uint32_t buflen) {
return false;
}
bool netprot::Deserialize(Sync* sync, char* buf, const uint32_t buflen) {
2023-10-27 15:07:38 -04:00
if (buflen <= sizeof(Sync))
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[1], sizeof(uint64_t));
sync->timestamp =
(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[1 + sizeof(uint64_t)], sizeof(uint64_t));
sync->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[1 + sizeof(uint64_t) * 2], sizeof(uint32_t));
sync->timer =
(uint32_t)diff[0] << 24 |
(uint32_t)diff[1] << 16 |
(uint32_t)diff[2] << 8 |
(uint32_t)diff[3];
memcpy(diff, &buf[1 + sizeof(uint64_t) * 2 + sizeof(uint32_t)], sizeof(uint16_t));
sync->ammo =
(uint16_t)diff[0] << 8 |
(uint16_t)diff[1];
memcpy(&sync->hp, &buf[1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t)], sizeof(uint8_t));
uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t)], sizeof(uint8_t) * 12);
uint32_t vec[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11] };
memcpy(&sync->position, vec, sizeof(uint32_t) * 3);
return true;
2023-09-25 16:30:03 -04:00
}
bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) {
2023-09-29 12:04:08 -04:00
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;
2023-09-25 16:30:03 -04:00
}
bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) {
2023-09-29 12:04:08 -04:00
if (buflen <= sizeof(LoginInfo))
2023-09-25 16:30:03 -04:00
return false;
2023-09-27 15:44:56 -04:00
size_t namesize = std::strlen(buf) + 1;
2023-09-25 16:30:03 -04:00
2023-09-27 15:44:56 -04:00
if (namesize > 32)
return false;
2023-09-25 16:30:03 -04:00
2023-09-27 15:44:56 -04:00
memcpy(&linfo->name, &buf[1], namesize);
2023-09-25 16:30:03 -04:00
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
2023-09-27 11:24:41 -04:00
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];
2023-09-25 16:30:03 -04:00
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];
2023-09-25 16:30:03 -04:00
return true;
2023-09-25 16:30:03 -04:00
}
bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) {
2023-09-29 12:04:08 -04:00
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;
2023-09-25 16:30:03 -04:00
}
bool netprot::Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen) {
2023-09-29 12:04:08 -04:00
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;
2023-09-25 16:30:03 -04:00
}
bool netprot::Deserialize(Chat* chat, char* buf, const uint32_t buflen) {
2023-09-29 12:04:08 -04:00
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;
2023-09-25 16:30:03 -04:00
}
bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) {
2023-09-29 12:04:08 -04:00
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;
2023-09-25 16:30:03 -04:00
}
2023-10-27 09:28:34 -04:00
netprot::PACKET_TYPE netprot::getType(char* buf, const uint32_t buflen) {
2023-09-25 16:30:03 -04:00
if (buflen < 1 ||
2023-10-27 09:28:34 -04:00
buf[0] >= (char)netprot::PACKET_TYPE::LAST_PACK ||
buf[0] <= (char)netprot::PACKET_TYPE::ERR)
2023-09-25 16:30:03 -04:00
return netprot::PACKET_TYPE::ERR;
2023-10-27 09:28:34 -04:00
return (netprot::PACKET_TYPE)buf[0];
2023-09-25 16:30:03 -04:00
}
2023-10-24 10:43:30 -04:00
netprot::Packet netprot::getPack(char* buf, uint32_t buflen) {
Packet pck = { nullptr, PACKET_TYPE::ERR };
Input* in = nullptr;
Output* out = nullptr;
Sync* sync = nullptr;
Chat* chat = nullptr;
GameInfo* ginfo = nullptr;
ErrorLog* errlog = nullptr;
LoginInfo* loginf = nullptr;
2023-10-24 10:43:30 -04:00
switch (getType(buf, buflen)) {
case PACKET_TYPE::INPUT:
in = new Input();
if (netprot::Deserialize(in, buf, buflen)) {
pck.type = PACKET_TYPE::INPUT;
pck.ptr = (void*)in;
}
2023-10-24 10:43:30 -04:00
break;
case PACKET_TYPE::OUTPUT:
out = new Output();
if (netprot::Deserialize(out, buf, buflen)) {
pck.type = PACKET_TYPE::OUTPUT;
pck.ptr = (void*)out;
}
2023-10-24 10:43:30 -04:00
break;
case PACKET_TYPE::SYNC:
sync = new Sync();
if (netprot::Deserialize(sync, buf, buflen)) {
pck.type = PACKET_TYPE::SYNC;
pck.ptr = (void*)sync;
}
2023-10-24 10:43:30 -04:00
break;
case PACKET_TYPE::CHAT:
chat = new Chat();
if (netprot::Deserialize(chat, buf, buflen)) {
pck.type = PACKET_TYPE::CHAT;
pck.ptr = (void*)chat;
}
2023-10-24 10:43:30 -04:00
break;
case PACKET_TYPE::GAMEINFO:
ginfo = new GameInfo();
if (netprot::Deserialize(ginfo, buf, buflen)) {
pck.type = PACKET_TYPE::GAMEINFO;
pck.ptr = (void*)ginfo;
}
2023-10-24 10:43:30 -04:00
break;
case PACKET_TYPE::ERRLOG:
errlog = new ErrorLog();
if (netprot::Deserialize(errlog, buf, buflen)) {
pck.type = PACKET_TYPE::ERRLOG;
pck.ptr = (void*)errlog;
}
break;
case PACKET_TYPE::LOGINF:
loginf = new LoginInfo();
if (netprot::Deserialize(loginf, buf, buflen)) {
pck.type = PACKET_TYPE::LOGINF;
pck.ptr = (void*)loginf;
}
2023-10-24 10:43:30 -04:00
break;
default:
break;
}
return pck;
}
2023-10-27 09:28:34 -04:00
netprot::Packet netprot::getPack(netprot::Buffer* buf) {
return netprot::getPack(buf->ptr, buf->len);
}
bool netprot::emptyPack(netprot::Packet pck) {
switch (pck.type) {
case PACKET_TYPE::INPUT:
delete (Input*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::OUTPUT:
delete (Output*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::SYNC:
delete (Sync*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::CHAT:
delete (Chat*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::GAMEINFO:
delete (GameInfo*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::ERRLOG:
delete (ErrorLog*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::LOGINF:
delete (LoginInfo*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
default:
return false;
}
}
2023-10-27 12:02:29 -04:00
2023-10-29 14:54:36 -04:00
netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) {
Packet pck;
pck.ptr = ptr;
pck.type = type;
return pck;
}
2023-10-28 11:18:01 -04:00
std::vector<netprot::Packet> netprot::recvPacks(SOCKET sock, Buffer* buf) {
std::vector<Packet> lsPck;
int len = 0, end = 0;
bool pck_received = false;
char* cursor = nullptr;
2023-10-28 11:18:01 -04:00
while (true) {
while (!pck_received) {
int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0);
2023-10-28 12:59:19 -04:00
if (bytes < 0) // si recv() retourne -1; ça veut dire qu'il y a plus rien a lire.
2023-10-28 11:18:01 -04:00
return lsPck;
len += bytes;
// TODO: Voir si on trouve un footer (5 '\0' d'affilee).
// Si oui, mettre l'index du debut du footer dans
// la variable "end" et mettre pck_received a true.
}
cursor = &buf->ptr[end];
lsPck.push_back(getPack(buf));
while (*cursor == '\0') {
cursor++;
end++;
}
if (cursor) { // mettre le début du prochain paquet au début du buffer.
memcpy(buf->ptr, cursor, buf->len - end); // et voir s'il y a en un autre
len = 0;
pck_received = true;
}
}
}
2023-10-27 12:02:29 -04:00
template <>
void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) {
switch (pack->type) {
case PACKET_TYPE::INPUT:
sendPack<Input>(sock, (Input*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::OUTPUT:
sendPack<Output>(sock, (Output*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::SYNC:
sendPack<Sync>(sock, (Sync*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::TEAMINF:
sendPack<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::PLAYINF:
sendPack<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::LOGINF:
sendPack<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::CHUNKMOD:
//sendPack<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, buflen);
case PACKET_TYPE::PLAYERMOD:
//sendPack<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::PICKUPMOD:
//sendPack<PickupMod>(sock, (PickupMod*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::GAMEINFO:
sendPack<GameInfo>(sock, (GameInfo*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::ENDINFO:
//sendPack<EndInfo>(sock, (EndInfo*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::CHAT:
sendPack<Chat>(sock, (Chat*)pack->ptr, buf, buflen);
2023-10-28 11:18:01 -04:00
return;
2023-10-27 12:02:29 -04:00
case PACKET_TYPE::ERRLOG:
sendPack<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::LAST_PACK: [[fallthrough]];
case PACKET_TYPE::ERR: [[fallthrough]];
default:
return;
}
}
template <>
void netprot::sendPackTo<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) {
switch (pack->type) {
case PACKET_TYPE::INPUT:
sendPackTo<Input>(sock, (Input*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::OUTPUT:
sendPackTo<Output>(sock, (Output*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::SYNC:
sendPackTo<Sync>(sock, (Sync*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::TEAMINF:
sendPackTo<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::PLAYINF:
sendPackTo<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::LOGINF:
sendPackTo<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::CHUNKMOD:
//sendPackTo<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::PLAYERMOD:
//sendPackTo<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::PICKUPMOD:
//sendPackTo<PickupMod>(sock, (PickupMod*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::GAMEINFO:
sendPackTo<GameInfo>(sock, (GameInfo*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::ENDINFO:
//sendPackTo<EndInfo>(sock, (EndInfo*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::CHAT:
sendPackTo<Chat>(sock, (Chat*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::ERRLOG:
sendPackTo<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen, sockad);
return;
case PACKET_TYPE::LAST_PACK: [[fallthrough]];
case PACKET_TYPE::ERR: [[fallthrough]];
default:
return;
}
}