@@ -751,7 +751,7 @@ namespace pcpp
751751 m_OnConnEnd (tcpReassemblyData.connData , reason, m_UserCookie);
752752
753753 tcpReassemblyData.closed = true ; // mark the connection as closed
754- insertIntoCleanupList (flowKey);
754+ scheduleCleanup (flowKey);
755755
756756 PCPP_LOG_DEBUG (" Connection with flow key 0x" << std::hex << flowKey << " is closed" );
757757 }
@@ -781,7 +781,7 @@ namespace pcpp
781781 m_OnConnEnd (tcpReassemblyData.connData , TcpReassemblyConnectionClosedManually, m_UserCookie);
782782
783783 tcpReassemblyData.closed = true ; // mark the connection as closed
784- insertIntoCleanupList (flowKey);
784+ scheduleCleanup (flowKey);
785785
786786 PCPP_LOG_DEBUG (" Connection with flow key 0x" << std::hex << flowKey << " is closed" );
787787 }
@@ -796,18 +796,16 @@ namespace pcpp
796796 return -1 ;
797797 }
798798
799- void TcpReassembly::insertIntoCleanupList (uint32_t flowKey)
799+ void TcpReassembly::scheduleCleanup (uint32_t flowKey)
800800 {
801- // m_CleanupList is a map with key of type time_t (expiration time). The mapped type is a list that stores the
802- // flow keys to be cleared in certain point of time. m_CleanupList.insert inserts an empty list if the container
803- // does not already contain an element with an equivalent key, otherwise this method returns an iterator to the
804- // element that prevents insertion.
805- std::pair<CleanupList::iterator, bool > pair =
806- m_CleanupList.insert (std::make_pair (time (nullptr ) + m_ClosedConnectionDelay, CleanupList::mapped_type ()));
807-
808- // getting the reference to list
809- CleanupList::mapped_type& keysList = pair.first ->second ;
810- keysList.push_front (flowKey);
801+ // m_CleanupMultimap is a multimap with key of type time_t (expiration time) and a value type uint32_t (flow
802+ // key). Due to being a multimap, multiple flow keys can have the same expiration time.
803+ //
804+ // During purge, up to 'maxNumToClean' entries with expiration time smaller than the current time will be
805+ // removed from storage.
806+
807+ auto expireTime = time (nullptr ) + m_ClosedConnectionDelay;
808+ m_CleanupMultimap.emplace (expireTime, flowKey);
811809 }
812810
813811 uint32_t TcpReassembly::purgeClosedConnections (uint32_t maxNumToClean)
@@ -817,25 +815,18 @@ namespace pcpp
817815 if (maxNumToClean == 0 )
818816 maxNumToClean = m_MaxNumToClean;
819817
820- CleanupList::iterator iterTime = m_CleanupList.begin (), iterTimeEnd = m_CleanupList.upper_bound (time (nullptr ));
821- while (iterTime != iterTimeEnd && count < maxNumToClean)
822- {
823- CleanupList::mapped_type& keysList = iterTime->second ;
818+ auto it = m_CleanupMultimap.begin ();
819+ auto itEnd = m_CleanupMultimap.upper_bound (time (nullptr ));
824820
825- for (; !keysList.empty () && count < maxNumToClean; ++count)
826- {
827- CleanupList::mapped_type::const_reference key = keysList.front ();
828- m_ConnectionInfo.erase (key);
829- m_ConnectionList.erase (key);
830- keysList.pop_front ();
831- }
832-
833- if (keysList.empty ())
834- m_CleanupList.erase (iterTime++);
835- else
836- ++iterTime;
821+ for (; it != itEnd && count < maxNumToClean; ++it, ++count)
822+ {
823+ auto flowKey = it->second ;
824+ m_ConnectionInfo.erase (flowKey);
825+ m_ConnectionList.erase (flowKey);
837826 }
838827
828+ m_CleanupMultimap.erase (m_CleanupMultimap.begin (), it);
829+
839830 return count;
840831 }
841832
0 commit comments