From 7d46536acccb7ecbea9a96cf7c0debc441fa7b0e Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Fri, 8 Dec 2023 04:31:10 -0500 Subject: [PATCH] Can now clean bullets off the forest. --- SQCSim-common/netprotocol.cpp | 122 ++++++++++++++-------------------- SQCSim-common/netprotocol.h | 4 +- SQCSim-srv/server.cpp | 25 ++++--- 3 files changed, 65 insertions(+), 86 deletions(-) diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index 39bd0f5..47a8e9e 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -29,13 +29,13 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) { Keys keys = in->keys; uint8_t keys8 = // Reste un bit. - (keys.forward? 0b10000000: 0) | - (keys.backward? 0b01000000: 0) | - (keys.left? 0b00100000: 0) | - (keys.right? 0b00010000: 0) | - (keys.jump? 0b00001000: 0) | - (keys.shoot? 0b00000100: 0) | - (keys.block? 0b00000010: 0); + (keys.forward ? 0b10000000 : 0) | + (keys.backward ? 0b01000000 : 0) | + (keys.left ? 0b00100000 : 0) | + (keys.right ? 0b00010000 : 0) | + (keys.jump ? 0b00001000 : 0) | + (keys.shoot ? 0b00000100 : 0) | + (keys.block ? 0b00000010 : 0); memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t)); @@ -90,14 +90,14 @@ void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) { States states = out->states; uint8_t states8 = - (states.jumping? 0b10000000: 0) | - (states.shooting? 0b01000000: 0) | - (states.hit? 0b00100000: 0) | - (states.powerup? 0b00010000: 0) | - (states.dead? 0b00001000: 0) | - (states.still? 0b00000100: 0) | - (states.jumpshot? 0b00000010: 0) | - (states.running? 0b00000001: 0); + (states.jumping ? 0b10000000 : 0) | + (states.shooting ? 0b01000000 : 0) | + (states.hit ? 0b00100000 : 0) | + (states.powerup ? 0b00010000 : 0) | + (states.dead ? 0b00001000 : 0) | + (states.still ? 0b00000100 : 0) | + (states.jumpshot ? 0b00000010 : 0) | + (states.running ? 0b00000001 : 0); memcpy(*buf + sizeof(uint64_t) * 2 + 1, &states8, sizeof(uint8_t)); @@ -204,7 +204,7 @@ void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) { memcpy(&hp, &sync->hp, sizeof(float)); - uint8_t hp8[4] = { + uint8_t hp8[4] = { (uint8_t)((hp >> 24) & 0xFF), (uint8_t)((hp >> 16) & 0xFF), (uint8_t)((hp >> 8) & 0xFF), @@ -523,7 +523,7 @@ void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) { -bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) { +bool netprot::Deserialize(Input* in, char* buf, uint32_t* buflen) { if (*buflen <= sizeof(Input)) return false; @@ -538,7 +538,7 @@ bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) { (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 | @@ -583,7 +583,7 @@ bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) { return true; } -bool netprot::Deserialize(Output* out, char* buf, uint32_t *buflen) { +bool netprot::Deserialize(Output* out, char* buf, uint32_t* buflen) { if (*buflen <= sizeof(Output)) return false; @@ -662,7 +662,7 @@ bool netprot::Deserialize(Output* out, char* buf, uint32_t *buflen) { return true; } -bool netprot::Deserialize(Sync* sync, char* buf, uint32_t *buflen) { +bool netprot::Deserialize(Sync* sync, char* buf, uint32_t* buflen) { if (*buflen <= sizeof(Sync)) return false; @@ -725,9 +725,9 @@ bool netprot::Deserialize(Sync* sync, char* buf, uint32_t *buflen) { memcpy(&hp8, &buf[2 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t)], sizeof(uint32_t)); uint32_t hp = (uint32_t)hp8[0] << 24 | - (uint32_t)hp8[1] << 16 | - (uint32_t)hp8[2] << 8 | - (uint32_t)hp8[3]; + (uint32_t)hp8[1] << 16 | + (uint32_t)hp8[2] << 8 | + (uint32_t)hp8[3]; memcpy(&sync->hp, &hp, sizeof(float)); @@ -736,7 +736,7 @@ bool netprot::Deserialize(Sync* sync, char* buf, uint32_t *buflen) { return true; } -bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t *buflen) { +bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t* buflen) { if (*buflen <= sizeof(LoginInfo)) return false; @@ -764,7 +764,7 @@ bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t *buflen) { return true; } -bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t *buflen) { +bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t* buflen) { if (*buflen <= sizeof(LoginInfo)) return false; @@ -803,7 +803,7 @@ bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t *buflen) { return true; } -bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t *buflen) { +bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t* buflen) { if (*buflen <= sizeof(PlayerInfo)) return false; @@ -842,7 +842,7 @@ bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t *buflen) { return true; } -bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t *buflen) { +bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t* buflen) { if (*buflen <= sizeof(GameInfo)) return false; @@ -885,7 +885,7 @@ bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t *buflen) { return true; } -bool netprot::Deserialize(Chat* chat, char* buf, uint32_t *buflen) { +bool netprot::Deserialize(Chat* chat, char* buf, uint32_t* buflen) { if (*buflen <= sizeof(Chat)) return false; @@ -935,7 +935,7 @@ bool netprot::Deserialize(Chat* chat, char* buf, uint32_t *buflen) { strcpy(chat->mess, 140, ciboire); //*buflen = messsize + sizeof(uint64_t) * 3 + 1; - + return true; } @@ -1037,7 +1037,7 @@ bool netprot::Deserialize(BulletAdd* bull, char* buf, uint32_t* buflen) { return true; } -bool netprot::Deserialize(ErrorLog* errlog, char* buf, uint32_t *buflen) { +bool netprot::Deserialize(ErrorLog* errlog, char* buf, uint32_t* buflen) { if (*buflen <= sizeof(ErrorLog)) return false; @@ -1064,7 +1064,7 @@ netprot::PACKET_TYPE netprot::getType(char* buf, const uint32_t buflen) { 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; @@ -1167,13 +1167,13 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) { return pck; } -std::vector netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf) { +std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { std::vector lsPck; - int len = buf->tmp? buf->tmp - buf->ptr: 0, + int len = buf->tmp ? buf->tmp - buf->ptr : 0, end = 0; - char * cursor = buf->tmp ? buf->tmp: nullptr , - * next = buf->tmp ? buf->tmp + 1: buf->ptr, - * last = buf->tmp ? buf->tmp: buf->ptr; + char* cursor = buf->tmp ? buf->tmp : nullptr, + * next = buf->tmp ? buf->tmp + 1 : buf->ptr, + * last = buf->tmp ? buf->tmp : buf->ptr; bool ended = true; struct pollfd fds[1]; @@ -1186,13 +1186,13 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf) buf->tmp = nullptr; return lsPck; } - + 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é. if (ended) buf->tmp = nullptr; return lsPck; - } + } len += bytes; end = len; @@ -1207,7 +1207,7 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf) if (end < 0) break; - cursor = (char*)memchr(next, '\r', end); + cursor = (char*)std::move(memchr(next, '\r', end)); if (cursor) { next = cursor; @@ -1215,25 +1215,15 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf) cmp = memcmp(cursor, Footer, sizeof(uint32_t)); if (cmp == 0) { - if (!outbuf) { - lsPck.push_back(last); - cursor += sizeof(uint32_t); - last = cursor; - next = cursor + 1; - } - else { - memcpy(&outbuf->ptr[cursor - last], last, cursor - last); - lsPck.push_back(&outbuf->ptr[cursor - last]); - cursor += sizeof(uint32_t); - last = cursor; - next = cursor + 1; - } + lsPck.push_back(std::move(last)); + cursor += sizeof(uint32_t); + last = cursor; + next = cursor + 1; } } else { - if (!outbuf) - buf->tmp = last; - cursor = &buf->ptr[len]; + buf->tmp = std::move(last); + cursor = std::move(&buf->ptr[len]); next = cursor + 1; break; }; @@ -1241,7 +1231,7 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf) } } -std::vector netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, Buffer* outbuf) { +std::vector netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from) { std::vector lsPck; int len = buf->tmp ? buf->tmp - buf->ptr : 0, end = 0; @@ -1291,24 +1281,14 @@ std::vector netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in cmp = memcmp(cursor, Footer, sizeof(uint32_t)); if (cmp == 0) { - if (!outbuf) { - lsPck.push_back(last); - cursor += sizeof(uint32_t); - last = cursor; - next = cursor + 1; - } - else { - memcpy(&outbuf->ptr[cursor - last], last, cursor - last); - lsPck.push_back(&outbuf->ptr[cursor - last]); - cursor += sizeof(uint32_t); - last = cursor; - next = cursor + 1; - } + lsPck.push_back(last); + cursor += sizeof(uint32_t); + last = cursor; + next = cursor + 1; } } else { - if (!outbuf) - buf->tmp = last; + buf->tmp = last; cursor = &buf->ptr[len]; next = cursor + 1; break; diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index d8a0b6a..2f12d6e 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -193,8 +193,8 @@ 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, Buffer* oufbuf = nullptr); - std::vector recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, Buffer* oufbuf = nullptr); + std::vector recvPacks(SOCKET sock, Buffer* buf); + std::vector recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from); /* Templates */ diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index e465a6a..87e18ea 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -224,7 +224,8 @@ void Server::Run() { Timestamp last = 0; std::vector chatlog; std::vector chunkdiffs; - std::vector bullets, outbox_bullets; + std::vector bullets; + std::vector::iterator> bullit; std::vector netbull; Chat* startchat = new Chat(); @@ -334,19 +335,21 @@ void Server::Run() { delete bull; } netbull.clear(); - - for (auto& bull : bullets) { + + for (auto bull = bullets.begin(); bull != bullets.end(); ++bull) { ChunkMod* cmod = nullptr; - if (bull->Update(m_world, (1. / 60.), 20, m_players, &cmod)) { + Bullet* bullet = *bull; + if (bullet->Update(m_world, (1. / 60.), 20, m_players, &cmod)) { if (cmod) - chunkdiffs.emplace_back(std::move(cmod)); - outbox_bullets.emplace_back(std::move(bull)); + chunkdiffs.emplace_back(cmod); + bullit.push_back(bull); + delete bullet; } } - for (auto gtfo : outbox_bullets) - delete gtfo; - outbox_bullets.clear(); + for (auto& bull: bullit) + bullets.erase(bull); + bullit.clear(); for (auto& chat : chatlog) { Log(chat->mess, false, false); @@ -362,10 +365,6 @@ void Server::Run() { delete chmo; } chunkdiffs.clear(); - - //for (auto& bull : outbox_bullets) - // delete bull; - //outbox_bullets.clear(); } Chat end;