From 88a8553608f8e7cbfbe9ba5e7d5140c1a77f6803 Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Wed, 1 Nov 2023 06:12:38 -0400 Subject: [PATCH] *guttural scream* --- SQCSim-common/netprotocol.cpp | 51 +++++++++++++++++------------------ SQCSim-common/netprotocol.h | 10 ++++--- SQCSim-srv/server.cpp | 9 ++++--- SQCSim2021/connector.cpp | 5 ++++ 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index 0c0d126..901f976 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -531,25 +531,7 @@ bool netprot::Deserialize(Output* out, char* buf, uint32_t *buflen) { memcpy(&out->direction, vec, sizeof(uint32_t) * 3); - uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 }; memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 3], sizeof(uint8_t) * 12); - uint32_t vec[3] = { - (uint32_t)subvec[0] << 24 | - (uint32_t)subvec[1] << 16 | - (uint32_t)subvec[2] << 8 | - (uint32_t)subvec[3], - (uint32_t)subvec[4] << 24 | - (uint32_t)subvec[5] << 16 | - (uint32_t)subvec[6] << 8 | - (uint32_t)subvec[7], - (uint32_t)subvec[8] << 24 | - (uint32_t)subvec[9] << 16 | - (uint32_t)subvec[10] << 8 | - (uint32_t)subvec[11] }; - - memcpy(&out->direction, vec, sizeof(uint32_t) * 3); - - memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 6], sizeof(uint8_t) * 12); uint32_t vec2[3] = { (uint32_t)subvec[0] << 24 | (uint32_t)subvec[1] << 16 | @@ -971,9 +953,8 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) { std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { std::vector lsPck; int len = 0, end = 0; - bool pck_received = false; char * cursor = nullptr, * next = buf->ptr, * last = buf->ptr; - const char footer[sizeof(uint64_t) + sizeof(uint8_t)] = { '\0', '\0', '\0', '\0', '\0' }; + const char footer[sizeof(uint32_t)] = { '\0', '\r', '\0', '\n' }; while (true) { int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0); @@ -982,6 +963,23 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { len += bytes; end = len; + for (int x = 0; x < len; ++x) + switch (buf->ptr[x]) { + case '\0': + std::putchar('n'); + break; + case '\r': + std::putchar('i'); + break; + case '\n': + std::putchar('a'); + break; + default: + std::putchar(buf->ptr[x]); + break; + } + std::puts(""); + while (true) { int cmp = 0; @@ -991,14 +989,16 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { if (end < 0) break; - cursor = (char*)memchr(next, '\0', end); - next = cursor + 1; + cursor = (char*)memchr(next, '\r', end); if (cursor) { - cmp = memcmp(cursor, &footer, sizeof(uint64_t) + sizeof(uint8_t)); + cursor--; + next = cursor + 1; + + cmp = memcmp(cursor, footer, sizeof(uint32_t)); if (cmp == 0) { lsPck.push_back(last); - cursor += sizeof(uint64_t) + sizeof(uint8_t); + cursor += sizeof(uint32_t); last = cursor; next = cursor + 1; } @@ -1010,9 +1010,6 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { }; } } - for (int x = 0; x < buf->len; ++x) - std::putchar(buf->ptr[x]); - std::puts(""); } template <> diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index c5c0777..8d51ee3 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -183,19 +183,21 @@ namespace netprot { template void sendPack(SOCKET sock, T* pack, Buffer* buf) { + const char footer[sizeof(uint32_t)] = { '\0', '\r', '\0', '\n' }; 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); + memcpy(&buf->ptr[buf->len], footer, sizeof(uint32_t)); + buf->len += sizeof(uint32_t); send(sock, buf->ptr, buf->len, 0); buf->rstLen(); } template void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) { + const char footer[sizeof(uint32_t)] = { '\0', '\r', '\0', '\n' }; 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); + memcpy(&buf->ptr[buf->len], footer, sizeof(uint32_t)); + buf->len += sizeof(uint32_t); sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr)); buf->rstLen(); } diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index 7588ced..efca9a9 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -140,21 +140,24 @@ int Server::Ready() { LoginInfo* log = (LoginInfo*)pck.ptr; log->sid = getUniqueId(); - log->tid = 0; // TODO: À changer si on implemente un mode en equipe. + log->tid = 1145389380; // TODO: À changer si on implemente un mode en equipe. Log(str.append(" Nom: ").append(log->name), false, false); str.clear(); Log(str.append(log->name).append(" SID: [").append(std::to_string(log->sid).append("]")), false, false); - sendPack(sock, log, &m_buf.ptr, &m_buf.len); + //using namespace std::chrono_literals; + //std::this_thread::sleep_for(100ms); + + sendPack(sock, log, &m_buf); play.id = getUniqueId(); strcpy(play.name, log->name); play.tid = log->tid; - sendPack(sock, &m_game, &m_buf.ptr, &m_buf.len); + sendPack(sock, &m_game, &m_buf); std::unique_ptr conn = std::make_unique(sock, sockad, *log, play); for (auto& [key, player] : m_players) { diff --git a/SQCSim2021/connector.cpp b/SQCSim2021/connector.cpp index d38d523..5be2f1a 100644 --- a/SQCSim2021/connector.cpp +++ b/SQCSim2021/connector.cpp @@ -69,6 +69,11 @@ int Connector::Connect(const char* srv_addr, std::string name) { netprot::sendPack(m_sock_tcp, &log, &bf); + memset(bf.ptr, '\0', BUFFER_LENGTH); + + using namespace std::chrono_literals; + std::this_thread::sleep_for(100ms); + bool ready = false; int errors = 0; std::vector lsPck;