From 34976ef7ec938c30eed8013d40fe28af7b3c754a Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Fri, 8 Dec 2023 04:55:00 -0500 Subject: [PATCH] /* delete[] mess */ Well there's your mem leak. --- SQCSim-common/netprotocol.cpp | 140 +++++++++++++++++----------------- SQCSim-common/netprotocol.h | 6 +- SQCSim-srv/connection.cpp | 2 +- SQCSim-srv/server.cpp | 7 +- 4 files changed, 76 insertions(+), 79 deletions(-) diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index 47a8e9e..04f70bc 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -1167,8 +1167,7 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) { return pck; } -std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { - std::vector lsPck; +void netprot::recvPacks(SOCKET sock, Buffer* buf, std::vector* lsPck) { int len = buf->tmp ? buf->tmp - buf->ptr : 0, end = 0; char* cursor = buf->tmp ? buf->tmp : nullptr, @@ -1184,80 +1183,14 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { if (!poll(fds, 1, 0)) { if (ended) buf->tmp = nullptr; - return lsPck; + return; } 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; - - ended = false; - - while (true) { - int cmp = 0; - - if (cursor) - end -= (cursor - buf->ptr); - - if (end < 0) - break; - - cursor = (char*)std::move(memchr(next, '\r', end)); - - if (cursor) { - next = cursor; - cursor--; - - cmp = memcmp(cursor, Footer, sizeof(uint32_t)); - if (cmp == 0) { - lsPck.push_back(std::move(last)); - cursor += sizeof(uint32_t); - last = cursor; - next = cursor + 1; - } - } - else { - buf->tmp = std::move(last); - cursor = std::move(&buf->ptr[len]); - next = cursor + 1; - break; - }; - } - } -} - -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; - 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]; - sockaddr_in sockad = from; - addrlen_t socklen = sizeof(sockad); - - fds[0].fd = sock; - fds[0].events = POLLIN; - - while (true) { - if (!poll(fds, 1, 0)) { - if (ended) - buf->tmp = nullptr; - return lsPck; - } - - int bytes = recvfrom(sock, &buf->ptr[len], buf->len - len, 0, (sockaddr*)&sockad, &socklen); - 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; + return; } len += bytes; end = len; @@ -1281,7 +1214,72 @@ std::vector netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in cmp = memcmp(cursor, Footer, sizeof(uint32_t)); if (cmp == 0) { - lsPck.push_back(last); + lsPck->push_back(last); + cursor += sizeof(uint32_t); + last = cursor; + next = cursor + 1; + } + } + else { + buf->tmp = last; + cursor = &buf->ptr[len]; + next = cursor + 1; + break; + }; + } + } +} + +void netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, std::vector* lsPck) { + 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; + bool ended = true; + struct pollfd fds[1]; + sockaddr_in sockad = from; + addrlen_t socklen = sizeof(sockad); + + fds[0].fd = sock; + fds[0].events = POLLIN; + + while (true) { + if (!poll(fds, 1, 0)) { + if (ended) + buf->tmp = nullptr; + return; + } + + int bytes = recvfrom(sock, &buf->ptr[len], buf->len - len, 0, (sockaddr*)&sockad, &socklen); + 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; + } + len += bytes; + end = len; + + ended = false; + + while (true) { + int cmp = 0; + + if (cursor) + end -= (cursor - buf->ptr); + + if (end < 0) + break; + + cursor = (char*)memchr(next, '\r', end); + + if (cursor) { + next = cursor; + cursor--; + + cmp = memcmp(cursor, Footer, sizeof(uint32_t)); + if (cmp == 0) { + lsPck->push_back(last); cursor += sizeof(uint32_t); last = cursor; next = cursor + 1; diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index 2f12d6e..87a1645 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -129,7 +129,7 @@ namespace netprot { Chat() {} Chat(Chat* cha) : src_id(cha->src_id), dest_id(cha->dest_id), dest_team_id(cha->dest_team_id) { strcpy(mess, 140, cha->mess); } - ~Chat() {/* delete[] mess; */} + ~Chat() { delete[] mess; } }; struct ChunkMod { @@ -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); - std::vector recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from); + void recvPacks(SOCKET sock, Buffer* buf, std::vector* lsPck); + void recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, std::vector* lsPck); /* Templates */ diff --git a/SQCSim-srv/connection.cpp b/SQCSim-srv/connection.cpp index 61961cf..dbe7816 100644 --- a/SQCSim-srv/connection.cpp +++ b/SQCSim-srv/connection.cpp @@ -51,7 +51,7 @@ void Connection::getPacks(SOCKET sock) { std::vector lsPck; Input in; Sync sync; - lsPck = recvPacksFrom(sock, &m_buf, m_addr); + recvPacksFrom(sock, &m_buf, m_addr, &lsPck); for (auto& pck : lsPck) { uint32_t bsize = m_buf.len - (pck - m_buf.ptr); switch (netprot::getType(pck, 1)) { diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index 87e18ea..91a0f23 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -227,6 +227,7 @@ void Server::Run() { std::vector bullets; std::vector::iterator> bullit; std::vector netbull; + std::vector lsPck; Chat* startchat = new Chat(); startchat->src_id = 0; @@ -257,10 +258,9 @@ void Server::Run() { /* In */ - std::vector lsPck; Input in; Chat chat; Sync sync; - lsPck = recvPacks(m_sock_udp, &m_buf); + recvPacks(m_sock_udp, &m_buf, &lsPck); for (auto& pck : lsPck) { uint32_t bsize = m_buf.len - (pck - m_buf.ptr); switch (netprot::getType(pck, 1)) { @@ -370,6 +370,7 @@ void Server::Run() { Chat end; end.src_id = 0; char endmess[] = "Game over, man. Game over."; + strcpy(end.mess, 140, endmess); for (auto& [key, conn] : m_conns) { std::string str = conn->player->GetUsername(); @@ -377,8 +378,6 @@ void Server::Run() { } - strcpy(end.mess, 140, endmess); - for (auto& [key, conn] : m_conns) sendPackTo(m_sock_udp, &end, &m_buf, conn->getAddr());