From 06ad40f1c84ada4bcf1a84814293c63958504f66 Mon Sep 17 00:00:00 2001 From: EasyMoney322 <20065717+easymoney322@users.noreply.github.com> Date: Tue, 27 Jun 2023 23:52:06 +0500 Subject: [PATCH] Performance improvmements. Removed timestamps and some unused code. Added lifetime info in update messages --- GlobalVars.hpp | 2 +- NetFunctions.cpp | 50 ++++++++++-------------------------------------- NetFunctions.hpp | 3 --- 3 files changed, 11 insertions(+), 44 deletions(-) diff --git a/GlobalVars.hpp b/GlobalVars.hpp index ce13536..2bda800 100644 --- a/GlobalVars.hpp +++ b/GlobalVars.hpp @@ -42,12 +42,12 @@ extern struct ProtoPort std::string proto{ "UDP" }; uint_fast16_t portnumin{ 0 }; uint_fast16_t portnumout{ 0 }; + uint_fast32_t maptime{ 1 }; std::string UpdateText{ "" }; }; extern struct ProlongationStruct { pcpp::Packet Packet; - uint_fast64_t TimestampOfNextUpdate; std::chrono::time_point chronotimepoint; ProtoPort ProtocolAndPort; uint_fast64_t PacketID{ 0 }; diff --git a/NetFunctions.cpp b/NetFunctions.cpp index 33deaff..82bd55e 100644 --- a/NetFunctions.cpp +++ b/NetFunctions.cpp @@ -325,19 +325,6 @@ uint_fast8_t PrintPayloadFromPacket(pcpp::Packet packet) } -uint_fast64_t CreateTimestampOfNextUpdate(pcpp::PayloadLayer payload) -{ - uint8_t* pdata = payload.getPayload(); - uint_fast32_t* packetlifetime = (uint_fast32_t*)&pdata[8]; - uint_fast32_t hostorder = ntohl(*packetlifetime); - hostorder /= 2; //Time till refresh in seconds - uint_fast64_t systime = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); //Current time - uint_fast64_t timeofrefresh = systime + hostorder; //Time of required update - - return timeofrefresh; -} - - std::chrono::time_point CreateTimepointOfNextUpdate(pcpp::PayloadLayer payload) { uint8_t* pdata = payload.getPayload(); @@ -352,31 +339,14 @@ std::chrono::time_point CreateTimepointOfNextUpdate( } - -uint_fast64_t CreateTimestampOfNextUpdate(pcpp::Packet packet) -{ - uint8_t* ppayload = packet.getLayerOfType()->getPayload(); - uint_fast64_t retval = CreateTimestampOfNextUpdate(*ppayload); - return retval; -} - -uint_fast64_t CreateTimestampOfNextUpdate(uint32_t lifetime) -{ - - uint_fast64_t secondstillupdate = lifetime / 2; - uint_fast64_t systime = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); //Current time - uint_fast64_t timeofrefresh = systime + secondstillupdate; //Time of required update - - return timeofrefresh; -} - ProtoPort GetProtoAndPortFromPayloadLayer(pcpp::PayloadLayer lspayload) { uint8_t* pdata = lspayload.getPayload(); - uint8_t opmode = pdata[1]; + uint_fast8_t opmode = pdata[1]; std::string retstr; - uint16_t retint = 0; - uint16_t retext = 0; + uint_fast16_t retint = 0; + uint_fast16_t retext = 0; + uint_fast32_t maptime = 1; if (1 == opmode) { retstr = "UDP"; @@ -392,10 +362,12 @@ ProtoPort GetProtoAndPortFromPayloadLayer(pcpp::PayloadLayer lspayload) uint_fast16_t* portnumint = (uint_fast16_t*)&pdata[4]; //4,5 = INT retint = ntohs(*portnumint); - uint_fast16_t* portnumext = (uint_fast16_t*)&pdata[6]; //4,5 = INT + uint_fast16_t* portnumext = (uint_fast16_t*)&pdata[6]; //6,7 = EXT retext = ntohs(*portnumext); - std::string Updstr = "Updated mapping for " + retstr + '-' + std::to_string(retint) + '-' + std::to_string(retext) + '.'; - return { retstr,retint, retext, Updstr }; + uint_fast32_t* maplifetime = (uint_fast32_t*)&pdata[8]; + maptime = ntohl(*maplifetime); + std::string Updstr = "Updated mapping for " + retstr + '-' + std::to_string(retint) + '-' + std::to_string(retext) + '-' + std::to_string(maptime) + '.'; + return { retstr,retint, retext, maptime, Updstr }; } ProtoPort GetProtoAndPortFromPacket(pcpp::Packet packet) @@ -478,7 +450,6 @@ uint_fast8_t SendPacketWrap(pcpp::Packet &lspacket, WinDev lsOut) { alreadyexists = true; std::chrono::time_point UpdateTime = CreateTimepointOfNextUpdate(lspayload); - ProlongationList.at(sp).TimestampOfNextUpdate = CreateTimestampOfNextUpdate(lspacket); ProlongationList.at(sp).chronotimepoint = UpdateTime; std::time_t datetimerefr = std::chrono::system_clock::to_time_t(UpdateTime); std::string str1 = ProlongationList.at(sp).ProtocolAndPort.UpdateText; @@ -501,8 +472,7 @@ uint_fast8_t SendPacketWrap(pcpp::Packet &lspacket, WinDev lsOut) if (false == alreadyexists) { ProtoPort lsportstruc = GetProtoAndPortFromPayloadLayer(lspayload); - std::string Updstr= "Updated mapping for " + lsportstruc.proto + '-' + std::to_string(lsportstruc.portnumin) + '-' + std::to_string(lsportstruc.portnumout) + '.'; - ProlongationList.push_back({ lspacket, CreateTimestampOfNextUpdate(mappinglifetime),CreateTimepointOfNextUpdate(lspayload),lsportstruc}); + ProlongationList.push_back({ lspacket ,CreateTimepointOfNextUpdate(lspayload),lsportstruc}); } } } diff --git a/NetFunctions.hpp b/NetFunctions.hpp index 965bbef..74550a7 100644 --- a/NetFunctions.hpp +++ b/NetFunctions.hpp @@ -28,9 +28,6 @@ uint_fast8_t PrintPayloadFromPacket(pcpp::Packet packet); uint_fast8_t RemoveCreatedMappings(std::vector & packetvector, WinDev lsOut); uint_fast8_t SendPacketWrap(pcpp::Packet& lspacket, WinDev lsOut); uint_fast8_t WatchList(); -uint_fast64_t CreateTimestampOfNextUpdate(pcpp::Packet packet); -uint_fast64_t CreateTimestampOfNextUpdate(pcpp::PayloadLayer payload); -uint_fast64_t CreateTimestampOfNextUpdate(uint32_t lifetime); std::chrono::time_point CreateTimepointOfNextUpdate(pcpp::PayloadLayer payload);