From 8dba751929d7cd61b382babc44a962f7489d20bf Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 28 Aug 2019 15:54:18 +0900 Subject: [PATCH] Some fixes Fix id of sent packet stored in network byte order instead of native order. Fix failing to send packet not stopping storing sent packet data. --- cpp_impl/src/UDPC_Defines.hpp | 4 ++-- cpp_impl/src/UDPConnection.cpp | 15 +++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cpp_impl/src/UDPC_Defines.hpp b/cpp_impl/src/UDPC_Defines.hpp index 0325b1e..40e53e6 100644 --- a/cpp_impl/src/UDPC_Defines.hpp +++ b/cpp_impl/src/UDPC_Defines.hpp @@ -145,9 +145,9 @@ struct Context { struct sockaddr_in socketInfo; std::chrono::steady_clock::time_point lastUpdated; - // ipv4 address and port to ConnectionData + // ipv4 address and port (as ConnectionIdentifier) to ConnectionData std::unordered_map conMap; - // id to ipv4 address + // id to ipv4 address and port (as ConnectionIdentifier) std::unordered_map idMap; std::default_random_engine rng_engine; diff --git a/cpp_impl/src/UDPConnection.cpp b/cpp_impl/src/UDPConnection.cpp index a082498..15104a8 100644 --- a/cpp_impl/src/UDPConnection.cpp +++ b/cpp_impl/src/UDPConnection.cpp @@ -78,8 +78,11 @@ UDPC::ConnectionData::ConnectionData() void UDPC::ConnectionData::cleanupSentPkts() { uint32_t id; while(sentPkts.size() > UDPC_SENT_PKTS_MAX_SIZE) { - id = ntohl(*((uint32_t*)(sentPkts.front().data + 8))); - sentInfoMap.erase(id); + id = *((uint32_t*)(sentPkts.front().data + 8)); + auto iter = sentInfoMap.find(id); + assert(iter != sentInfoMap.end() + && "Sent packet must have correspoding entry in sentInfoMap"); + sentInfoMap.erase(iter); sentPkts.pop_front(); } } @@ -367,6 +370,7 @@ void UDPC_update(void *ctx) { sizeof(struct sockaddr_in)); if(sentBytes != 20) { // TODO log fail of sending connection-initiate-packet + continue; } } else { // is server, initiate connection to client @@ -396,6 +400,7 @@ void UDPC_update(void *ctx) { sizeof(struct sockaddr_in)); if(sentBytes != 20) { // TODO log fail send init connection packet as server + continue; } } continue; @@ -432,6 +437,7 @@ void UDPC_update(void *ctx) { sizeof(struct sockaddr_in)); if(sentBytes != 20) { // TODO log fail send heartbeat packet + continue; } UDPC_PacketInfo pInfo{{0}, 0, 0, 0, 0, 0, 0}; @@ -439,7 +445,7 @@ void UDPC_update(void *ctx) { pInfo.receiver = iter->first.getAddr(); pInfo.senderPort = c->socketInfo.sin_port; pInfo.receiverPort = iter->second.port; - *((uint32_t*)(pInfo.data + 8)) = htonl(iter->second.lseq - 1); + *((uint32_t*)(pInfo.data + 8)) = iter->second.lseq - 1; iter->second.sentPkts.push_back(std::move(pInfo)); iter->second.cleanupSentPkts(); @@ -485,6 +491,7 @@ void UDPC_update(void *ctx) { sizeof(struct sockaddr_in)); if(sentBytes != 20 + pInfo.dataSize) { // TODO log fail send packet + continue; } if((pInfo.flags & 0x4) == 0) { @@ -509,7 +516,7 @@ void UDPC_update(void *ctx) { sentPInfo.receiver = iter->first.getAddr(); sentPInfo.senderPort = c->socketInfo.sin_port; sentPInfo.receiverPort = iter->second.port; - *((uint32_t*)(sentPInfo.data + 8)) = htonl(iter->second.lseq - 1); + *((uint32_t*)(sentPInfo.data + 8)) = iter->second.lseq - 1; iter->second.sentPkts.push_back(std::move(pInfo)); iter->second.cleanupSentPkts();