From cf6e58cd95c6e34ba19968f8b852a370ad1c01a5 Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Tue, 24 Oct 2023 09:17:21 -0400 Subject: [PATCH] =?UTF-8?q?erratas=20dans=20la=20s=C3=A9rialisation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SQCSim-common/netprotocol.cpp | 12 ++++++------ SQCSim-common/netprotocol.h | 9 +++++++++ SQCSim2021/engine.cpp | 8 ++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index b71662c..62ac48d 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -37,7 +37,7 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) { keys.shoot & 0b00000100 | keys.block & 0b00000010 ; - memcpy(*buf + sizeof(uint64_t) + 2, &keys8, sizeof(uint8_t)); + memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t)); uint32_t vec[3]; memcpy(vec, &in->direction, sizeof(Vector3f)); // Pour dénaturer les floats. @@ -56,13 +56,13 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) { (vec[2] >> 8) & 0xFF, vec[2] & 0xFF}; - memcpy(*buf + sizeof(uint64_t) + 3, vec8, sizeof(uint32_t) * 3); + memcpy(*buf + sizeof(uint64_t) * 2 + 2, vec8, sizeof(uint32_t) * 3); - *buflen = sizeof(uint64_t) + 3 + sizeof(uint32_t) * 3; + *buflen = sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 3; } void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) { - + } void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) { @@ -308,7 +308,7 @@ bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) { (uint64_t)diff[7]; uint8_t keys = 0; - memcpy(&keys, &buf[2 + sizeof(uint64_t)], sizeof(uint8_t)); + memcpy(&keys, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint8_t)); in->keys.forward = keys && 0b10000000; in->keys.backward = keys && 0b01000000; in->keys.left = keys && 0b00100000; @@ -318,7 +318,7 @@ bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) { in->keys.block = keys && 0b00000010; uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 }; - memcpy(subvec, &buf[3 + sizeof(uint64_t)], sizeof(uint8_t) * 12); + memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2], sizeof(uint8_t) * 12); uint32_t vec[3] = { (uint32_t)subvec[0] << 24 | (uint32_t)subvec[1] << 16 | diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index 7c41418..72bc26e 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -118,6 +118,7 @@ namespace netprot { PacketType getType(char* buf, uint32_t buflen); // srv/cli template void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen); + template void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad); template void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) { @@ -126,6 +127,14 @@ namespace netprot { *buflen = BUFFER_LENGTH; } + template + void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) { + sockaddr_in addr = *sockad; + netprot::Serialize(pack, buf, buflen); + sendto(sock, *buf, *buflen, 0, (sockaddr*)&addr, sizeof(addr)); + *buflen = BUFFER_LENGTH; + } + }; #endif diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index d544415..7f4747d 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -783,7 +783,7 @@ void Engine::Render(float elapsedTime) { static char* buf = new char[BUFFER_LENGTH]; uint32_t buflen = BUFFER_LENGTH; netprot::Input input; - sockaddr_in addr = m_conn.m_srvsockaddr; + //sockaddr_in addr = m_conn.m_srvsockaddr; input.direction = m_player.GetDirection(); input.sid = m_conn.getId(); @@ -796,10 +796,10 @@ void Engine::Render(float elapsedTime) { input.keys.block = m_mouseR; input.keys.shoot = m_mouseL; - netprot::Serialize(&input, &buf, &buflen); - sendto(m_conn.m_sock_udp, buf, buflen, 0, (sockaddr*)&addr, sizeof(addr)); + //netprot::Serialize(&input, &buf, &buflen); + //sendto(m_conn.m_sock_udp, buf, buflen, 0, (sockaddr*)&addr, sizeof(addr)); - //netprot::sendPackTo(m_conn.m_sock_udp, &input, &buf, &buflen, &m_conn.m_srvsockaddr); + netprot::sendPackTo(m_conn.m_sock_udp, &input, &buf, &buflen, &m_conn.m_srvsockaddr); } } else if (m_gamestate == GameState::MAIN_MENU || m_gamestate == GameState::OPTIONS)