From 42d612ad336730fc7c13b634d615aa76cc4daa86 Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Sat, 28 Oct 2023 11:18:01 -0400 Subject: [PATCH] I read the news today, oh boy. --- SQCSim-common/netprotocol.cpp | 35 ++++++++++++++++++++++++++++++++ SQCSim-common/netprotocol.h | 14 ++++++++++++- SQCSim2021/connector.cpp | 38 +++++++++++++++++++++++++---------- 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index f324bd0..4da9da7 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -801,6 +801,40 @@ bool netprot::emptyPack(netprot::Packet pck) { } } +std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { + std::vector 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(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) { switch (pack->type) { @@ -838,6 +872,7 @@ void netprot::sendPack(SOCKET sock, Packet* pack, char** buf, u 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; diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index 693f03f..f0395be 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -28,7 +28,7 @@ namespace netprot { struct Packet { // Pour pouvoir recevoir les paquets du recv() sans avoir à les aiguiller dans la même thread. void* ptr = nullptr; // Notez que le pointeur doit être supprimé séparément lorsqu'il n'est plus utile. - PACKET_TYPE type; + PACKET_TYPE type = PACKET_TYPE::ERR; }; struct Keys { @@ -145,10 +145,14 @@ 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); template void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) { netprot::Serialize(pack, buf, buflen); + memset(buf->ptr[buf->len], , '\0', sizeof(uint64_t) + sizeof(uint8_t)); + buf->len += sizeof(uint64_t) + sizeof(uint8_t) send(sock, *buf, *buflen, 0); *buflen = BUFFER_LENGTH; } @@ -157,6 +161,8 @@ namespace netprot { void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) { sockaddr_in addr = *sockad; netprot::Serialize(pack, buf, buflen); + memset(buf->ptr[buf->len], , '\0', sizeof(uint64_t) + sizeof(uint8_t)); + buf->len += sizeof(uint64_t) + sizeof(uint8_t) sendto(sock, *buf, *buflen, 0, (sockaddr*)&addr, sizeof(addr)); *buflen = BUFFER_LENGTH; } @@ -164,6 +170,8 @@ namespace netprot { template void sendPack(SOCKET sock, T* pack, Buffer* buf) { netprot::Serialize(pack, &buf->ptr, &buf->len); + memset(buf->ptr[buf->len], , '\0', sizeof(uint64_t) + sizeof(uint8_t)); + buf->len += sizeof(uint64_t) + sizeof(uint8_t) send(sock, buf->ptr, buf->len, 0); buf->rstLen(); } @@ -172,8 +180,12 @@ namespace netprot { void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) { sockaddr_in addr = *sockad; netprot::Serialize(pack, &buf->ptr, &buf->len); + memset(buf->ptr[buf->len], ,'\0', sizeof(uint64_t) + sizeof(uint8_t)); + buf->len += sizeof(uint64_t) + sizeof(uint8_t) sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr)); buf->rstLen(); } + + }; #endif diff --git a/SQCSim2021/connector.cpp b/SQCSim2021/connector.cpp index f385e77..d0dbdf5 100644 --- a/SQCSim2021/connector.cpp +++ b/SQCSim2021/connector.cpp @@ -54,6 +54,12 @@ int Connector::Connect(const char* srv_addr, std::string name) { if (connect(m_sock_tcp, (sockaddr*)&m_srvsockaddr, sizeof(m_srvsockaddr)) < 0) { std::cout << "Échec de la connexion." << std::endl; + return 2; + } + + if (ioctlsocket(m_sock_tcp, FIONBIO, NULL) < 0) { + std::cout << "Impossible de mettre le socket en mode non-bloquant." << std::endl; + return 3; } char* buf = new char[1500]; @@ -70,17 +76,24 @@ int Connector::Connect(const char* srv_addr, std::string name) { buflen = 150; bool ready = false; int errors = 0; - int bytes = 0; - char* currbuf = buf; + int bytes = 0, ptrbuf = 0; + char* currbuf = new char[150]; netprot::PlayerInfo pinfo{}; while (!ready) { - int len = recv(m_sock_tcp, &buf[bytes], buflen, 0); + int len = 0; + bool is_fullpack = false; - if (len <= 0) - return 8; + do { + len = recv(m_sock_tcp, &buf[bytes], buflen, 0); + bytes += len; + buflen -= bytes; + + for (int i = 0; i < buflen; ++i) { + // TODOTODOTODOSAINT-TROPEZ + } + + } while (!is_fullpack); - bytes += len; - buflen -= bytes; switch (netprot::getType(currbuf, buflen)) { case netprot::PACKET_TYPE::LOGINF: @@ -91,32 +104,35 @@ int Connector::Connect(const char* srv_addr, std::string name) { return 2; } currbuf = buf + sizeof(netprot::LoginInfo) + 2; + //ptrbuf += sizeof(netprot::LoginInfo) + 2; break; case netprot::PACKET_TYPE::GAMEINFO: if (bytes < sizeof(netprot::GameInfo)) break; if (!netprot::Deserialize(&m_gameinfo, currbuf, buflen)) { std::cout << "Packet GameInfo invalide." << std::endl; - return 3; + return 4; } currbuf = buf + sizeof(netprot::GameInfo) + 2; + //ptrbuf += sizeof(netprot::GameInfo) + 2; break; case netprot::PACKET_TYPE::PLAYINF: if (bytes < sizeof(netprot::PlayerInfo)) break; if (!netprot::Deserialize(&pinfo, currbuf, buflen)) { std::cout << "Packet PlayerInfo invalide." << std::endl; - return 3; + return 4; } m_players[pinfo.id] = pinfo; currbuf = buf + sizeof(netprot::PlayerInfo) + 2; + //ptrbuf += sizeof(netprot::PlayerInfo) + 2; break; case netprot::PACKET_TYPE::SYNC: if (bytes < sizeof(netprot::Sync)) break; if (!netprot::Deserialize(&m_origin, currbuf, buflen)) { std::cout << "Packet Sync invalide." << std::endl; - return 3; + return 4; } ready = true; break; @@ -126,7 +142,7 @@ int Connector::Connect(const char* srv_addr, std::string name) { break; } if (errors > 100) - return 1; + return 5; } return 0; }