From a0f43d977248ee5278c0d4398ba4363502896029 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 13 Oct 2014 11:21:57 -0400 Subject: [PATCH] resuse instance of local destination upon restart --- Destination.cpp | 14 +++++++++----- Destination.h | 3 ++- SAM.cpp | 2 +- Tunnel.cpp | 42 +++++++++++++++++++++++++++--------------- Tunnel.h | 1 + TunnelPool.cpp | 9 ++++++--- TunnelPool.h | 6 +++--- 7 files changed, 49 insertions(+), 28 deletions(-) diff --git a/Destination.cpp b/Destination.cpp index 08c461b5..0f9af2c5 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -74,7 +74,8 @@ namespace stream Stop (); for (auto it: m_RemoteLeaseSets) delete it.second; - + if (m_Pool) + i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool); delete m_LeaseSet; } @@ -98,10 +99,7 @@ namespace stream m_Streams.clear (); } if (m_Pool) - { - i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool); - m_Pool = nullptr; - } + i2p::tunnel::tunnels.StopTunnelPool (m_Pool); m_IsRunning = false; m_Service.stop (); if (m_Thread) @@ -110,6 +108,7 @@ namespace stream delete m_Thread; m_Thread = 0; } + m_Service.reset (); } void StreamingDestination::SendTunnelDataMsgs (const std::vector& msgs) @@ -429,6 +428,11 @@ namespace stream if (it != m_Destinations.end ()) { LogPrint ("Local destination ", keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p exists"); + if (!it->second->IsRunning ()) + { + it->second->Start (); + return it->second; + } return nullptr; } auto localDestination = new StreamingDestination (keys, isPublic); diff --git a/Destination.h b/Destination.h index 41ac99f6..e921f4a9 100644 --- a/Destination.h +++ b/Destination.h @@ -25,7 +25,8 @@ namespace stream void Start (); void Stop (); - + bool IsRunning () const { return m_IsRunning; }; + i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; }; Stream * CreateNewOutgoingStream (const i2p::data::LeaseSet& remote); diff --git a/SAM.cpp b/SAM.cpp index 08a62956..8cd811d2 100644 --- a/SAM.cpp +++ b/SAM.cpp @@ -599,7 +599,7 @@ namespace stream for (auto it1 : it->second.sockets) delete it1; it->second.sockets.clear (); - DeleteLocalDestination (it->second.localDestination); + it->second.localDestination->Stop (); m_Sessions.erase (it); } } diff --git a/Tunnel.cpp b/Tunnel.cpp index 04ed9bdc..2da68772 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -299,11 +299,24 @@ namespace tunnel { if (pool) { + StopTunnelPool (pool); + m_Pools.erase (pool->GetLocalDestination ().GetIdentHash ()); + for (auto it: m_PendingTunnels) + if (it.second->GetTunnelPool () == pool) + it.second->SetTunnelPool (nullptr); + delete pool; + } + } + + void Tunnels::StopTunnelPool (TunnelPool * pool) + { + if (pool) + { + pool->SetActive (false); pool->DetachTunnels (); - pool->SetDeleted (); } } - + void Tunnels::AddTransitTunnel (TransitTunnel * tunnel) { std::unique_lock l(m_TransitTunnelsMutex); @@ -536,22 +549,14 @@ namespace tunnel void Tunnels::ManageTunnelPools () { std::unique_lock l(m_PoolsMutex); - for (auto it = m_Pools.begin (); it != m_Pools.end ();) + for (auto it: m_Pools) { - TunnelPool * pool = it->second; - if (!pool->IsDeleted ()) + TunnelPool * pool = it.second; + if (pool->IsActive ()) { pool->CreateTunnels (); pool->TestTunnels (); - it++; } - else - { - it = m_Pools.erase (it); - for (auto it1: m_PendingTunnels) - if (it1.second->GetTunnelPool () == pool) it1.second->SetTunnelPool (nullptr); - delete pool; - } } } @@ -575,8 +580,10 @@ namespace tunnel std::unique_lock l(m_OutboundTunnelsMutex); m_OutboundTunnels.push_back (newTunnel); auto pool = newTunnel->GetTunnelPool (); - if (pool) + if (pool && pool->IsActive ()) pool->TunnelCreated (newTunnel); + else + newTunnel->SetTunnelPool (nullptr); } void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel) @@ -590,7 +597,12 @@ namespace tunnel CreateTunnel (newTunnel->GetTunnelConfig ()->Invert (), GetNextOutboundTunnel ()); } else - pool->TunnelCreated (newTunnel); + { + if (pool->IsActive ()) + pool->TunnelCreated (newTunnel); + else + newTunnel->SetTunnelPool (nullptr); + } } diff --git a/Tunnel.h b/Tunnel.h index c333edb1..919be76e 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -131,6 +131,7 @@ namespace tunnel TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0); TunnelPool * CreateTunnelPool (i2p::garlic::GarlicDestination& localDestination, int numHops); void DeleteTunnelPool (TunnelPool * pool); + void StopTunnelPool (TunnelPool * pool); private: diff --git a/TunnelPool.cpp b/TunnelPool.cpp index 3402f9e2..ac63cf7b 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -12,7 +12,7 @@ namespace tunnel { TunnelPool::TunnelPool (i2p::garlic::GarlicDestination& localDestination, int numHops, int numTunnels): m_LocalDestination (localDestination), m_NumHops (numHops), m_NumTunnels (numTunnels), - m_IsDeleted (false) + m_IsActive (true) { } @@ -27,17 +27,20 @@ namespace tunnel std::unique_lock l(m_InboundTunnelsMutex); for (auto it: m_InboundTunnels) it->SetTunnelPool (nullptr); + m_InboundTunnels.clear (); } { std::unique_lock l(m_OutboundTunnelsMutex); for (auto it: m_OutboundTunnels) it->SetTunnelPool (nullptr); + m_OutboundTunnels.clear (); } + m_Tests.clear (); } void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel) { - if (m_IsDeleted) return; + if (!m_IsActive) return; { std::unique_lock l(m_InboundTunnelsMutex); m_InboundTunnels.insert (createdTunnel); @@ -61,7 +64,7 @@ namespace tunnel void TunnelPool::TunnelCreated (OutboundTunnel * createdTunnel) { - if (m_IsDeleted) return; + if (!m_IsActive) return; std::unique_lock l(m_OutboundTunnelsMutex); m_OutboundTunnels.insert (createdTunnel); } diff --git a/TunnelPool.h b/TunnelPool.h index 0c680aa4..da59866c 100644 --- a/TunnelPool.h +++ b/TunnelPool.h @@ -48,8 +48,8 @@ namespace tunnel void TestTunnels (); void ProcessDeliveryStatus (I2NPMessage * msg); - bool IsDeleted () const { return m_IsDeleted; }; - void SetDeleted () { m_IsDeleted = true; }; + bool IsActive () const { return m_IsActive; }; + void SetActive (bool isActive) { m_IsActive = isActive; }; void DetachTunnels (); private: @@ -72,7 +72,7 @@ namespace tunnel mutable std::mutex m_OutboundTunnelsMutex; std::set m_OutboundTunnels; std::map > m_Tests; - bool m_IsDeleted; + bool m_IsActive; public: