I read the news today, oh boy.

This commit is contained in:
MarcEricMartel
2023-10-28 11:18:01 -04:00
parent c23d1b2120
commit 42d612ad33
3 changed files with 75 additions and 12 deletions

View File

@@ -801,6 +801,40 @@ bool netprot::emptyPack(netprot::Packet pck) {
}
}
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;
while (true) {
while (!pck_received) {
int bytes = recv(sock, &buf->ptr[len], buf->len - len, NULL);
if (bytes < 0)
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;
}
}
}
template <>
void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) {
switch (pack->type) {
@@ -838,6 +872,7 @@ void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, u
return;
case PACKET_TYPE::CHAT:
sendPack<Chat>(sock, (Chat*)pack->ptr, buf, buflen);
return;
case PACKET_TYPE::ERRLOG:
sendPack<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen);
return;