diff --git a/SQCSim-common/define.h b/SQCSim-common/define.h index 05d90da..a5441d4 100644 --- a/SQCSim-common/define.h +++ b/SQCSim-common/define.h @@ -7,7 +7,7 @@ #define SRV_PORT 10000 #define CLI_PORT 10001 -#define BUFFER_LENGTH 150 +#define BUFFER_LENGTH 1500 #define CHUNK_SIZE_X 4 #define CHUNK_SIZE_Y 64 diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index ab80618..fac6913 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -4,38 +4,38 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) { *buf[0] = (char)netprot::PACKET_TYPE::INPUT; uint64_t time = in->timestamp; - uint8_t time8[sizeof(uint64_t)] = {(uint8_t)((time >> 56) & 0xFF), + 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)}; + (uint8_t)((time >> 8) & 0xFF), + (uint8_t)(time & 0xFF) }; memcpy(*buf + 1, time8, sizeof(uint64_t)); uint64_t sid = in->sid; - uint8_t sid8[sizeof(uint64_t)] = {(uint8_t)((sid >> 56) & 0xFF), + 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)}; + (uint8_t)((sid >> 8) & 0xFF), + (uint8_t)(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.forward & 0b10000000 | keys.backward & 0b01000000 | - keys.left & 0b00100000 | - keys.right & 0b00010000 | - keys.jump & 0b00001000 | - keys.shoot & 0b00000100 | - keys.block & 0b00000010 ; + keys.left & 0b00100000 | + keys.right & 0b00010000 | + keys.jump & 0b00001000 | + keys.shoot & 0b00000100 | + keys.block & 0b00000010; memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t)); @@ -54,7 +54,7 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) { (uint8_t)((vec[2] >> 24) & 0xFF), (uint8_t)((vec[2] >> 16) & 0xFF), (uint8_t)((vec[2] >> 8) & 0xFF), - (uint8_t)(vec[2] & 0xFF)}; + (uint8_t)(vec[2] & 0xFF) }; memcpy(*buf + sizeof(uint64_t) * 2 + 2, vec8, sizeof(uint32_t) * 3); @@ -104,7 +104,7 @@ void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) { (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), @@ -118,11 +118,11 @@ void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) { 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 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; @@ -169,8 +169,8 @@ void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) { (uint8_t)((tid >> 32) & 0xFF), (uint8_t)((tid >> 24) & 0xFF), (uint8_t)((tid >> 16) & 0xFF), - (uint8_t)((tid >> 8) & 0xFF), - (uint8_t)(tid & 0xFF) + (uint8_t)((tid >> 8) & 0xFF), + (uint8_t)(tid & 0xFF) }; memcpy(*buf + namesize + 2, tid8, sizeof(uint64_t)); @@ -182,18 +182,18 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) { *buf[0] = (char)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)] = { + 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) + (uint8_t)((sid >> 8) & 0xFF), + (uint8_t)(sid & 0xFF) }; memcpy(*buf + namesize + 2, sid8, sizeof(uint64_t)); @@ -206,8 +206,8 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) { (uint8_t)((tid >> 32) & 0xFF), (uint8_t)((tid >> 24) & 0xFF), (uint8_t)((tid >> 16) & 0xFF), - (uint8_t)((tid >> 8) & 0xFF), - (uint8_t)(tid & 0xFF) + (uint8_t)((tid >> 8) & 0xFF), + (uint8_t)(tid & 0xFF) }; memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t)); @@ -229,8 +229,8 @@ void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) { (uint8_t)((id >> 32) & 0xFF), (uint8_t)((id >> 24) & 0xFF), (uint8_t)((id >> 16) & 0xFF), - (uint8_t)((id >> 8) & 0xFF), - (uint8_t)(id & 0xFF) + (uint8_t)((id >> 8) & 0xFF), + (uint8_t)(id & 0xFF) }; memcpy(*buf + namesize + 2, id8, sizeof(uint64_t)); @@ -243,8 +243,8 @@ void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) { (uint8_t)((tid >> 32) & 0xFF), (uint8_t)((tid >> 24) & 0xFF), (uint8_t)((tid >> 16) & 0xFF), - (uint8_t)((tid >> 8) & 0xFF), - (uint8_t)(tid & 0xFF) + (uint8_t)((tid >> 8) & 0xFF), + (uint8_t)(tid & 0xFF) }; memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t)); @@ -263,8 +263,8 @@ void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) { (uint8_t)((game >> 32) & 0xFF), (uint8_t)((game >> 24) & 0xFF), (uint8_t)((game >> 16) & 0xFF), - (uint8_t)((game >> 8) & 0xFF), - (uint8_t)(game & 0xFF) + (uint8_t)((game >> 8) & 0xFF), + (uint8_t)(game & 0xFF) }; memcpy(*buf + 1, seed8, sizeof(uint64_t)); @@ -272,27 +272,27 @@ void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) { game = ginfo->countdown; uint8_t count8[sizeof(uint64_t)] = { (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) + (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)] = { - (uint8_t)((game >> 56) & 0xFF), + (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) + (uint8_t)((game >> 8) & 0xFF), + (uint8_t)(game & 0xFF) }; memcpy(*buf + sizeof(uint64_t) + 1, gtype8, sizeof(uint64_t)); @@ -311,8 +311,8 @@ void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) { (uint8_t)((src >> 32) & 0xFF), (uint8_t)((src >> 24) & 0xFF), (uint8_t)((src >> 16) & 0xFF), - (uint8_t)((src >> 8) & 0xFF), - (uint8_t)(src & 0xFF) + (uint8_t)((src >> 8) & 0xFF), + (uint8_t)(src & 0xFF) }; memcpy(*buf + 1, src8, sizeof(uint64_t)); @@ -325,8 +325,8 @@ void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) { (uint8_t)((dst >> 32) & 0xFF), (uint8_t)((dst >> 24) & 0xFF), (uint8_t)((dst >> 16) & 0xFF), - (uint8_t)((dst >> 8) & 0xFF), - (uint8_t)(dst & 0xFF) + (uint8_t)((dst >> 8) & 0xFF), + (uint8_t)(dst & 0xFF) }; memcpy(*buf + 1 + sizeof(uint64_t), dst8, sizeof(uint64_t)); @@ -339,10 +339,10 @@ void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) { (uint8_t)((dstteam >> 32) & 0xFF), (uint8_t)((dstteam >> 24) & 0xFF), (uint8_t)((dstteam >> 16) & 0xFF), - (uint8_t)((dstteam >> 8) & 0xFF), - (uint8_t)(dstteam & 0xFF) + (uint8_t)((dstteam >> 8) & 0xFF), + (uint8_t)(dstteam & 0xFF) }; - + memcpy(*buf + 1 + sizeof(uint64_t) * 2, dstt8, sizeof(uint64_t)); size_t messize = std::strlen(chat->mess) + 1; @@ -366,8 +366,8 @@ void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) { -bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) { - if (buflen <= sizeof(Input)) +bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) { + if (*buflen <= sizeof(Input)) return false; uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; @@ -395,13 +395,13 @@ bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) { uint8_t keys = 0; memcpy(&keys, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint8_t)); - 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; + 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; 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(uint8_t) * 12); @@ -417,19 +417,21 @@ bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) { (uint32_t)subvec[8] << 24 | (uint32_t)subvec[9] << 16 | (uint32_t)subvec[10] << 8 | - (uint32_t)subvec[11]}; + (uint32_t)subvec[11] }; memcpy(&in->direction, vec, sizeof(uint32_t) * 3); + *buflen = sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 3; + return true; } -bool netprot::Deserialize(Output* out, char* buf, const uint32_t buflen) { +bool netprot::Deserialize(Output* out, char* buf, uint32_t *buflen) { return false; } -bool netprot::Deserialize(Sync* sync, char* buf, const uint32_t buflen) { - if (buflen <= sizeof(Sync)) +bool netprot::Deserialize(Sync* sync, char* buf, uint32_t *buflen) { + if (*buflen <= sizeof(Sync)) return false; uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; @@ -487,11 +489,13 @@ bool netprot::Deserialize(Sync* sync, char* buf, const uint32_t buflen) { memcpy(&sync->position, vec, sizeof(uint32_t) * 3); + *buflen = sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + 2; + return true; } -bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) { - if (buflen <= sizeof(LoginInfo)) +bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t *buflen) { + if (*buflen <= sizeof(LoginInfo)) return false; size_t namesize = std::strlen(buf) + 1; @@ -513,11 +517,13 @@ bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) { (uint64_t)diff[6] << 8 | (uint64_t)diff[7]; + *buflen = namesize + sizeof(uint64_t) + 2; + return true; } -bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) { - if (buflen <= sizeof(LoginInfo)) +bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t *buflen) { + if (*buflen <= sizeof(LoginInfo)) return false; size_t namesize = std::strlen(buf) + 1; @@ -550,11 +556,13 @@ bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) { (uint64_t)diff[6] << 8 | (uint64_t)diff[7]; + *buflen = namesize + sizeof(uint64_t) * 2 + 2; + return true; } -bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) { - if (buflen <= sizeof(PlayerInfo)) +bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t *buflen) { + if (*buflen <= sizeof(PlayerInfo)) return false; size_t namesize = std::strlen(buf) + 1; @@ -587,11 +595,13 @@ bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) { (uint64_t)diff[6] << 8 | (uint64_t)diff[7]; + *buflen = namesize + sizeof(uint64_t) * 2 + 2; + return true; } -bool netprot::Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen) { - if (buflen <= sizeof(GameInfo)) +bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t *buflen) { + if (*buflen <= sizeof(GameInfo)) return false; uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; @@ -628,11 +638,13 @@ bool netprot::Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen) { (uint64_t)diff[6] << 8 | (uint64_t)diff[7]; + *buflen = sizeof(uint64_t) * 3 + 1; + return true; } -bool netprot::Deserialize(Chat* chat, char* buf, const uint32_t buflen) { - if (buflen <= sizeof(Chat)) +bool netprot::Deserialize(Chat* chat, char* buf, uint32_t *buflen) { + if (*buflen <= sizeof(Chat)) return false; uint8_t src[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; @@ -677,11 +689,14 @@ bool netprot::Deserialize(Chat* chat, char* buf, const uint32_t buflen) { return false; memcpy(&chat->mess, &buf[1 + sizeof(uint64_t) * 3], messsize); + + *buflen = messsize + sizeof(uint64_t) * 3 + 2; + return true; } -bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) { - if (buflen <= sizeof(ErrorLog)) +bool netprot::Deserialize(ErrorLog* errlog, char* buf, uint32_t *buflen) { + if (*buflen <= sizeof(ErrorLog)) return false; size_t messsize = std::strlen(buf) + 1; @@ -692,18 +707,20 @@ bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) { memcpy(&errlog->mess, &buf[1], messsize); memcpy(&errlog->is_fatal, &buf[1 + messsize], sizeof(bool)); + *buflen = messsize + sizeof(uint64_t) * 3 + 2; + return true; } netprot::PACKET_TYPE netprot::getType(char* buf, const uint32_t buflen) { - if (buflen < 1 || - buf[0] >= (char)netprot::PACKET_TYPE::LAST_PACK || + if (buflen < 1 || + buf[0] >= (char)netprot::PACKET_TYPE::LAST_PACK || buf[0] <= (char)netprot::PACKET_TYPE::ERR) return netprot::PACKET_TYPE::ERR; return (netprot::PACKET_TYPE)buf[0]; } -netprot::Packet netprot::getPack(char* buf, uint32_t buflen) { +netprot::Packet netprot::getPack(char* buf, uint32_t *buflen) { Packet pck = { nullptr, PACKET_TYPE::ERR }; Input* in = nullptr; Output* out = nullptr; @@ -713,7 +730,7 @@ netprot::Packet netprot::getPack(char* buf, uint32_t buflen) { ErrorLog* errlog = nullptr; LoginInfo* loginf = nullptr; - switch (getType(buf, buflen)) { + switch (getType(buf, *buflen)) { case PACKET_TYPE::INPUT: in = new Input(); if (netprot::Deserialize(in, buf, buflen)) { @@ -769,8 +786,8 @@ netprot::Packet netprot::getPack(char* buf, uint32_t buflen) { return pck; } -netprot::Packet netprot::getPack(netprot::Buffer* buf) { - return netprot::getPack(buf->ptr, buf->len); +netprot::Packet netprot::getPack(netprot::Buffer* buf) { + return netprot::getPack(buf->ptr, &buf->len); } bool netprot::emptyPack(netprot::Packet pck) { @@ -808,138 +825,148 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) { return pck; } -std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { - std::vector lsPck; +std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { + std::vector lsPck; int len = 0, end = 0; bool pck_received = false; + char * cursor = nullptr, * next = buf->ptr, * last = buf->ptr; + const char footer[sizeof(uint64_t) + sizeof(uint8_t)] = { '\0', '\0', '\0', '\0', '\0' }; while (true) { - while (!pck_received) { - int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0); - if (bytes < 0) // si recv() retourne -1; ça veut dire qu'il y a plus rien a lire. - 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. - + int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0); + if (bytes <= 0) // si recv() retourne -1 ou 0; ça veut dire qu'il y a plus rien a lire qui n'a pas déjà été traité. + return lsPck; + len += bytes; + end = len; + for (int x = 0; x < len; ++x) + std::putchar(buf->ptr[x]); + std::puts(""); - //buf->ptr + while (true) { + int cmp = 0; - //end = len; - //pck_received = true; - } - char* cursor = nullptr; + if (cursor) + end -= (cursor - buf->ptr); - cursor = &buf->ptr[end]; + if (end < 0) + break; - lsPck.push_back(getPack(buf)); + cursor = (char*)memchr(next, '\0', end); + next = cursor + 1; - 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 = false; + if (cursor) { + cmp = memcmp(cursor, &footer, sizeof(uint64_t) + sizeof(uint8_t)); + if (cmp == 0) { + lsPck.push_back(last); + cursor += sizeof(uint64_t) + sizeof(uint8_t) + 1; + last = cursor; + next = cursor + 1; + } + } + else { + cursor = &buf->ptr[len]; + next = cursor + 1; + break; + }; } } + } template <> void netprot::sendPack(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) { switch (pack->type) { - case PACKET_TYPE::INPUT: - sendPack(sock, (Input*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::OUTPUT: - sendPack(sock, (Output*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::SYNC: - sendPack(sock, (Sync*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::TEAMINF: - sendPack(sock, (TeamInfo*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::PLAYINF: - sendPack(sock, (PlayerInfo*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::LOGINF: - sendPack(sock, (LoginInfo*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::CHUNKMOD: - //sendPack(sock, (ChunkMod*)pack->ptr, buf, buflen); - case PACKET_TYPE::PLAYERMOD: - //sendPack(sock, (PlayerMod*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::PICKUPMOD: - //sendPack(sock, (PickupMod*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::GAMEINFO: - sendPack(sock, (GameInfo*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::ENDINFO: - //sendPack(sock, (EndInfo*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::CHAT: - sendPack(sock, (Chat*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::ERRLOG: - sendPack(sock, (ErrorLog*)pack->ptr, buf, buflen); - return; - case PACKET_TYPE::LAST_PACK: [[fallthrough]]; - case PACKET_TYPE::ERR: [[fallthrough]]; - default: - return; - } + case PACKET_TYPE::INPUT: + sendPack(sock, (Input*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::OUTPUT: + sendPack(sock, (Output*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::SYNC: + sendPack(sock, (Sync*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::TEAMINF: + sendPack(sock, (TeamInfo*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::PLAYINF: + sendPack(sock, (PlayerInfo*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::LOGINF: + sendPack(sock, (LoginInfo*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::CHUNKMOD: + //sendPack(sock, (ChunkMod*)pack->ptr, buf, buflen); + case PACKET_TYPE::PLAYERMOD: + //sendPack(sock, (PlayerMod*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::PICKUPMOD: + //sendPack(sock, (PickupMod*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::GAMEINFO: + sendPack(sock, (GameInfo*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::ENDINFO: + //sendPack(sock, (EndInfo*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::CHAT: + sendPack(sock, (Chat*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::ERRLOG: + sendPack(sock, (ErrorLog*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::LAST_PACK: [[fallthrough]]; + case PACKET_TYPE::ERR: [[fallthrough]]; + default: + return; + } } template <> void netprot::sendPackTo(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) { switch (pack->type) { - case PACKET_TYPE::INPUT: - sendPackTo(sock, (Input*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::OUTPUT: - sendPackTo(sock, (Output*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::SYNC: - sendPackTo(sock, (Sync*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::TEAMINF: - sendPackTo(sock, (TeamInfo*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::PLAYINF: - sendPackTo(sock, (PlayerInfo*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::LOGINF: - sendPackTo(sock, (LoginInfo*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::CHUNKMOD: - //sendPackTo(sock, (ChunkMod*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::PLAYERMOD: - //sendPackTo(sock, (PlayerMod*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::PICKUPMOD: - //sendPackTo(sock, (PickupMod*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::GAMEINFO: - sendPackTo(sock, (GameInfo*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::ENDINFO: - //sendPackTo(sock, (EndInfo*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::CHAT: - sendPackTo(sock, (Chat*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::ERRLOG: - sendPackTo(sock, (ErrorLog*)pack->ptr, buf, buflen, sockad); - return; - case PACKET_TYPE::LAST_PACK: [[fallthrough]]; - case PACKET_TYPE::ERR: [[fallthrough]]; - default: - return; - } + case PACKET_TYPE::INPUT: + sendPackTo(sock, (Input*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::OUTPUT: + sendPackTo(sock, (Output*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::SYNC: + sendPackTo(sock, (Sync*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::TEAMINF: + sendPackTo(sock, (TeamInfo*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::PLAYINF: + sendPackTo(sock, (PlayerInfo*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::LOGINF: + sendPackTo(sock, (LoginInfo*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::CHUNKMOD: + //sendPackTo(sock, (ChunkMod*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::PLAYERMOD: + //sendPackTo(sock, (PlayerMod*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::PICKUPMOD: + //sendPackTo(sock, (PickupMod*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::GAMEINFO: + sendPackTo(sock, (GameInfo*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::ENDINFO: + //sendPackTo(sock, (EndInfo*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::CHAT: + sendPackTo(sock, (Chat*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::ERRLOG: + sendPackTo(sock, (ErrorLog*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::LAST_PACK: [[fallthrough]]; + case PACKET_TYPE::ERR: [[fallthrough]]; + default: + return; + } } diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index 987e5fe..57b865f 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -135,19 +135,19 @@ namespace netprot { void Serialize(Chat* chat, char* buf[], uint32_t* buflen); // cli/srv void Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen); // srv - bool Deserialize(Input* in, char* buf, const uint32_t buflen); // srv - bool Deserialize(Output* out, char* buf, const uint32_t buflen); // cli - bool Deserialize(Sync* sync, char* buf, const uint32_t buflen); // cli - bool Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen); // cli/srv - bool Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen); // cli/srv - bool Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen); // cli - bool Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen); // cli - bool Deserialize(Chat* chat, char* buf, const uint32_t buflen); // srv/cli - bool Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen); // srv + bool Deserialize(Input* in, char* buf, uint32_t* buflen); // srv + bool Deserialize(Output* out, char* buf, uint32_t* buflen); // cli + bool Deserialize(Sync* sync, char* buf, uint32_t* buflen); // cli + bool Deserialize(TeamInfo* tinfo, char* buf, uint32_t* buflen); // cli/srv + bool Deserialize(LoginInfo* linfo, char* buf, uint32_t* buflen); // cli/srv + bool Deserialize(PlayerInfo* pinfo, char* buf, uint32_t* buflen); // cli + bool Deserialize(GameInfo* ginfo, char* buf, uint32_t* buflen); // cli + bool Deserialize(Chat* chat, char* buf, uint32_t* buflen); // srv/cli + bool Deserialize(ErrorLog* errlog, char* buf, uint32_t* buflen); // srv PACKET_TYPE getType(char* buf, uint32_t buflen); - Packet getPack(char* buf, uint32_t buflen); + Packet getPack(char* buf, uint32_t *buflen); Packet getPack(Buffer* buf); bool emptyPack(Packet pck); @@ -160,7 +160,7 @@ namespace netprot { template void sendPack(SOCKET sock, T* pack, Buffer* buf); template void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad); - std::vector recvPacks(SOCKET sock, Buffer* buf); + std::vector recvPacks(SOCKET sock, Buffer* buf); template void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) { diff --git a/SQCSim2021/connector.cpp b/SQCSim2021/connector.cpp index 3611a37..aa2040e 100644 --- a/SQCSim2021/connector.cpp +++ b/SQCSim2021/connector.cpp @@ -71,42 +71,46 @@ int Connector::Connect(const char* srv_addr, std::string name) { bool ready = false; int errors = 0; - std::vector lsPck; + std::vector lsPck; while (!ready) { lsPck = netprot::recvPacks(m_sock_tcp, &bf); for (auto& pck : lsPck) { - bool bypass_delete = false; + uint32_t bsize = bf.len - (pck - bf.ptr); netprot::PlayerInfo* pl = nullptr; - switch (pck.type) { + switch (netprot::getType(pck, 1)) { using enum netprot::PACKET_TYPE; case LOGINF: - m_loginfo = netprot::LoginInfo((netprot::LoginInfo*)pck.ptr); + if (!netprot::Deserialize(&m_loginfo, pck, &bsize)) + ++errors; break; case GAMEINFO: - m_gameinfo = netprot::GameInfo((netprot::GameInfo*)pck.ptr); + if (!netprot::Deserialize(&m_gameinfo, pck, &bsize)) + ++errors; break; case PLAYINF: - pl = (netprot::PlayerInfo*)pck.ptr; - m_players[pl->id] = pl; - bypass_delete = true; + pl = new netprot::PlayerInfo(); + if (!netprot::Deserialize(pl, pck, &bsize)) + ++errors; + else m_players[pl->id] = pl; break; case TEAMINF: // TODO: Faire dequoi avec TeamInfo si on fini par avoir des teams. break; case SYNC: - m_origin = netprot::Sync((netprot::Sync*)pck.ptr); + if (!netprot::Deserialize(&m_origin, pck, &bsize)) + ++errors; ready = true; break; default: errors++; break; } - if (!bypass_delete) - netprot::emptyPack(pck); } + if (lsPck.empty()) + errors++; lsPck.clear(); - if (errors > 100) + if (errors > 1000000) return 4; } return 0;