From 7d927b0e282f8735ce34f1656c5b3ab3b5a699e6 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 9 Feb 2016 10:46:27 -0500 Subject: [PATCH] shared_ptr for Lease --- Datagram.cpp | 2 +- I2NPProtocol.cpp | 4 ++-- LeaseSet.cpp | 12 +++++++----- LeaseSet.h | 20 +++++++------------- Streaming.cpp | 17 ++++++++--------- Streaming.h | 2 +- 6 files changed, 26 insertions(+), 31 deletions(-) diff --git a/Datagram.cpp b/Datagram.cpp index b944cf11..9221824d 100644 --- a/Datagram.cpp +++ b/Datagram.cpp @@ -66,7 +66,7 @@ namespace datagram msgs.push_back (i2p::tunnel::TunnelMessageBlock { i2p::tunnel::eDeliveryTypeTunnel, - leases[i].tunnelGateway, leases[i].tunnelID, + leases[i]->tunnelGateway, leases[i]->tunnelID, garlic }); outboundTunnel->SendTunnelDataMsg (msgs); diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index c31a4479..db3330e2 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -265,9 +265,9 @@ namespace i2p auto leases = leaseSet->GetNonExpiredLeases (); if (leases.size () > 0) { - htobe32buf (payload + size, leases[0].tunnelID); + htobe32buf (payload + size, leases[0]->tunnelID); size += 4; // reply tunnelID - memcpy (payload + size, leases[0].tunnelGateway, 32); + memcpy (payload + size, leases[0]->tunnelGateway, 32); size += 32; // reply tunnel gateway } else diff --git a/LeaseSet.cpp b/LeaseSet.cpp index adcbc388..6beec5ab 100644 --- a/LeaseSet.cpp +++ b/LeaseSet.cpp @@ -128,7 +128,9 @@ namespace data m_ExpirationTime = lease.endDate; if (m_StoreLeases) { - m_Leases.push_back (lease); + auto l = std::make_shared(); + *l = lease; + m_Leases.push_back (l); // check if lease's gateway is in our netDb if (!netdb.FindRouter (lease.tunnelGateway)) { @@ -147,13 +149,13 @@ namespace data } } - const std::vector LeaseSet::GetNonExpiredLeases (bool withThreshold) const + const std::vector > LeaseSet::GetNonExpiredLeases (bool withThreshold) const { auto ts = i2p::util::GetMillisecondsSinceEpoch (); - std::vector leases; + std::vector > leases; for (auto& it: m_Leases) { - auto endDate = it.endDate; + auto endDate = it->endDate; if (!withThreshold) endDate -= i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD*1000; if (ts < endDate) @@ -166,7 +168,7 @@ namespace data { auto ts = i2p::util::GetMillisecondsSinceEpoch (); for (auto& it: m_Leases) - if (ts >= it.endDate) return true; + if (ts >= it->endDate) return true; return false; } diff --git a/LeaseSet.h b/LeaseSet.h index 0f437170..027fb948 100644 --- a/LeaseSet.h +++ b/LeaseSet.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "Identity.h" namespace i2p @@ -21,14 +22,6 @@ namespace data IdentHash tunnelGateway; uint32_t tunnelID; uint64_t endDate; - - bool operator< (const Lease& other) const - { - if (endDate != other.endDate) - return endDate > other.endDate; - else - return tunnelID < other.tunnelID; - } }; const int MAX_LS_BUFFER_SIZE = 3072; @@ -48,13 +41,14 @@ namespace data size_t GetBufferLen () const { return m_BufferLen; }; bool IsValid () const { return m_IsValid; }; - // implements RoutingDestination - const IdentHash& GetIdentHash () const { return m_Identity->GetIdentHash (); }; - const std::vector& GetLeases () const { return m_Leases; }; - const std::vector GetNonExpiredLeases (bool withThreshold = true) const; + const std::vector >& GetLeases () const { return m_Leases; }; + const std::vector > GetNonExpiredLeases (bool withThreshold = true) const; bool HasExpiredLeases () const; bool IsExpired () const; uint64_t GetExpirationTime () const { return m_ExpirationTime; }; + + // implements RoutingDestination + const IdentHash& GetIdentHash () const { return m_Identity->GetIdentHash (); }; const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; }; bool IsDestination () const { return true; }; @@ -65,7 +59,7 @@ namespace data private: bool m_IsValid, m_StoreLeases; // we don't need to store leases for floodfill - std::vector m_Leases; + std::vector > m_Leases; uint64_t m_ExpirationTime; // in milliseconds std::shared_ptr m_Identity; uint8_t m_EncryptionKey[256]; diff --git a/Streaming.cpp b/Streaming.cpp index 8e0b223d..7e812167 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -22,7 +22,6 @@ namespace stream { RAND_bytes ((uint8_t *)&m_RecvStreamID, 4); m_RemoteIdentity = remote->GetIdentity (); - m_CurrentRemoteLease.endDate = 0; } Stream::Stream (boost::asio::io_service& service, StreamingDestination& local): @@ -599,9 +598,9 @@ namespace stream } auto ts = i2p::util::GetMillisecondsSinceEpoch (); - if (!m_CurrentRemoteLease.endDate || ts >= m_CurrentRemoteLease.endDate - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD*1000) + if (m_CurrentRemoteLease || ts >= m_CurrentRemoteLease->endDate - i2p::tunnel::TUNNEL_EXPIRATION_THRESHOLD*1000) UpdateCurrentRemoteLease (true); - if (ts < m_CurrentRemoteLease.endDate) + if (m_CurrentRemoteLease && ts < m_CurrentRemoteLease->endDate) { std::vector msgs; for (auto it: packets) @@ -610,7 +609,7 @@ namespace stream msgs.push_back (i2p::tunnel::TunnelMessageBlock { i2p::tunnel::eDeliveryTypeTunnel, - m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID, + m_CurrentRemoteLease->tunnelGateway, m_CurrentRemoteLease->tunnelID, msg }); m_NumSentBytes += it->GetLength (); @@ -725,10 +724,10 @@ namespace stream if (!leases.empty ()) { bool updated = false; - if (expired) + if (expired && m_CurrentRemoteLease) { for (auto it: leases) - if ((it.tunnelGateway == m_CurrentRemoteLease.tunnelGateway) && (it.tunnelID != m_CurrentRemoteLease.tunnelID)) + if ((it->tunnelGateway == m_CurrentRemoteLease->tunnelGateway) && (it->tunnelID != m_CurrentRemoteLease->tunnelID)) { m_CurrentRemoteLease = it; updated = true; @@ -738,7 +737,7 @@ namespace stream if (!updated) { uint32_t i = rand () % leases.size (); - if (m_CurrentRemoteLease.endDate && leases[i].tunnelID == m_CurrentRemoteLease.tunnelID) + if (m_CurrentRemoteLease && leases[i]->tunnelID == m_CurrentRemoteLease->tunnelID) // make sure we don't select previous i = (i + 1) % leases.size (); // if so, pick next m_CurrentRemoteLease = leases[i]; @@ -747,12 +746,12 @@ namespace stream else { m_RemoteLeaseSet = nullptr; - m_CurrentRemoteLease.endDate = 0; + m_CurrentRemoteLease = nullptr; // re-request expired } } else - m_CurrentRemoteLease.endDate = 0; + m_CurrentRemoteLease = nullptr; } std::shared_ptr Stream::CreateDataMessage (const uint8_t * payload, size_t len) diff --git a/Streaming.h b/Streaming.h index 0b240c0b..2f85397a 100644 --- a/Streaming.h +++ b/Streaming.h @@ -172,7 +172,7 @@ namespace stream std::shared_ptr m_RemoteIdentity; std::shared_ptr m_RemoteLeaseSet; std::shared_ptr m_RoutingSession; - i2p::data::Lease m_CurrentRemoteLease; + std::shared_ptr m_CurrentRemoteLease; std::shared_ptr m_CurrentOutboundTunnel; std::queue m_ReceiveQueue; std::set m_SavedPackets;