/* delete[] mess */ Well there's your mem leak.

This commit is contained in:
MarcEricMartel 2023-12-08 04:55:00 -05:00
parent 7d46536acc
commit 34976ef7ec
4 changed files with 76 additions and 79 deletions

View File

@ -1167,8 +1167,7 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) {
return pck;
}
std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
std::vector<char*> lsPck;
void netprot::recvPacks(SOCKET sock, Buffer* buf, std::vector<char*>* 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<char*> 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<char*> netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from) {
std::vector<char*> 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<char*> 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<char*>* 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;

View File

@ -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 <class T> void sendPack(SOCKET sock, T* pack, Buffer* buf);
template <class T> void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad);
std::vector<char*> recvPacks(SOCKET sock, Buffer* buf);
std::vector<char*> recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from);
void recvPacks(SOCKET sock, Buffer* buf, std::vector<char*>* lsPck);
void recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, std::vector<char*>* lsPck);
/* Templates */

View File

@ -51,7 +51,7 @@ void Connection::getPacks(SOCKET sock) {
std::vector<char*> 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)) {

View File

@ -227,6 +227,7 @@ void Server::Run() {
std::vector<Bullet*> bullets;
std::vector<std::vector<Bullet*>::iterator> bullit;
std::vector<BulletAdd*> netbull;
std::vector<char*> lsPck;
Chat* startchat = new Chat();
startchat->src_id = 0;
@ -257,10 +258,9 @@ void Server::Run() {
/* In */
std::vector<char*> 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<Chat>(m_sock_udp, &end, &m_buf, conn->getAddr());