From ebb5c53c3a1a7ca100461eedd49f98531b4c3929 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 19 Jan 2015 22:28:13 -0500 Subject: [PATCH] use shared_ptr for TunnelPool --- Destination.h | 4 ++-- Tunnel.cpp | 21 +++++++-------------- Tunnel.h | 19 ++++++++++--------- TunnelPool.cpp | 8 ++++---- TunnelPool.h | 3 ++- 5 files changed, 25 insertions(+), 30 deletions(-) diff --git a/Destination.h b/Destination.h index 14580e6b..ffb96d2a 100644 --- a/Destination.h +++ b/Destination.h @@ -61,7 +61,7 @@ namespace client virtual void Stop (); bool IsRunning () const { return m_IsRunning; }; boost::asio::io_service& GetService () { return m_Service; }; - i2p::tunnel::TunnelPool * GetTunnelPool () { return m_Pool; }; + std::shared_ptr GetTunnelPool () { return m_Pool; }; bool IsReady () const { return m_LeaseSet && m_LeaseSet->HasNonExpiredLeases (); }; const i2p::data::LeaseSet * FindLeaseSet (const i2p::data::IdentHash& ident); bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr); @@ -121,7 +121,7 @@ namespace client std::map m_RemoteLeaseSets; std::map m_LeaseSetRequests; - i2p::tunnel::TunnelPool * m_Pool; + std::shared_ptr m_Pool; i2p::data::LeaseSet * m_LeaseSet; bool m_IsPublic; uint32_t m_PublishReplyToken; diff --git a/Tunnel.cpp b/Tunnel.cpp index a569a329..b6e93eb5 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -191,7 +191,7 @@ namespace tunnel Tunnels tunnels; - Tunnels::Tunnels (): m_IsRunning (false), m_Thread (nullptr), m_ExploratoryPool (nullptr) + Tunnels::Tunnels (): m_IsRunning (false), m_Thread (nullptr) { } @@ -213,9 +213,6 @@ namespace tunnel delete it.second; m_PendingTunnels.clear ();*/ - for (auto& it: m_Pools) - delete it; - m_Pools.clear (); } InboundTunnel * Tunnels::GetInboundTunnel (uint32_t tunnelID) @@ -280,15 +277,15 @@ namespace tunnel return tunnel; } - TunnelPool * Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops) + std::shared_ptr Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops) { - auto pool = new TunnelPool (localDestination, numInboundHops, numOutboundHops); + auto pool = std::make_shared (localDestination, numInboundHops, numOutboundHops); std::unique_lock l(m_PoolsMutex); m_Pools.push_back (pool); return pool; } - void Tunnels::DeleteTunnelPool (TunnelPool * pool) + void Tunnels::DeleteTunnelPool (std::shared_ptr pool) { if (pool) { @@ -297,14 +294,10 @@ namespace tunnel std::unique_lock l(m_PoolsMutex); m_Pools.remove (pool); } - for (auto it: m_PendingTunnels) - if (it.second->GetTunnelPool () == pool) - it.second->SetTunnelPool (nullptr); - delete pool; } } - void Tunnels::StopTunnelPool (TunnelPool * pool) + void Tunnels::StopTunnelPool (std::shared_ptr pool) { if (pool) { @@ -547,8 +540,8 @@ namespace tunnel std::unique_lock l(m_PoolsMutex); for (auto it: m_Pools) { - TunnelPool * pool = it; - if (pool->IsActive ()) + auto pool = it; + if (pool && pool->IsActive ()) { pool->CreateTunnels (); pool->TestTunnels (); diff --git a/Tunnel.h b/Tunnel.h index 99f44976..527e8827 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "Queue.h" #include "TunnelConfig.h" #include "TunnelPool.h" @@ -54,8 +55,8 @@ namespace tunnel bool IsEstablished () const { return m_State == eTunnelStateEstablished; }; bool IsFailed () const { return m_State == eTunnelStateFailed; }; - TunnelPool * GetTunnelPool () const { return m_Pool; }; - void SetTunnelPool (TunnelPool * pool) { m_Pool = pool; }; + std::shared_ptr GetTunnelPool () const { return m_Pool; }; + void SetTunnelPool (std::shared_ptr pool) { m_Pool = pool; }; bool HandleTunnelBuildResponse (uint8_t * msg, size_t len); @@ -67,7 +68,7 @@ namespace tunnel private: TunnelConfig * m_Config; - TunnelPool * m_Pool; // pool, tunnel belongs to, or null + std::shared_ptr m_Pool; // pool, tunnel belongs to, or null TunnelState m_State; }; @@ -121,7 +122,7 @@ namespace tunnel Tunnel * GetPendingTunnel (uint32_t replyMsgID); InboundTunnel * GetNextInboundTunnel (); OutboundTunnel * GetNextOutboundTunnel (); - TunnelPool * GetExploratoryPool () const { return m_ExploratoryPool; }; + std::shared_ptr GetExploratoryPool () const { return m_ExploratoryPool; }; TransitTunnel * GetTransitTunnel (uint32_t tunnelID); int GetTransitTunnelsExpirationTimeout (); void AddTransitTunnel (TransitTunnel * tunnel); @@ -130,9 +131,9 @@ namespace tunnel void PostTunnelData (I2NPMessage * msg); template TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0); - TunnelPool * CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops); - void DeleteTunnelPool (TunnelPool * pool); - void StopTunnelPool (TunnelPool * pool); + std::shared_ptr CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops); + void DeleteTunnelPool (std::shared_ptr pool); + void StopTunnelPool (std::shared_ptr pool); private: @@ -158,8 +159,8 @@ namespace tunnel std::mutex m_TransitTunnelsMutex; std::map m_TransitTunnels; std::mutex m_PoolsMutex; - std::list m_Pools; - TunnelPool * m_ExploratoryPool; + std::list> m_Pools; + std::shared_ptr m_ExploratoryPool; i2p::util::Queue m_Queue; public: diff --git a/TunnelPool.cpp b/TunnelPool.cpp index fd176841..2482d670 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -300,7 +300,7 @@ namespace tunnel } std::reverse (hops.begin (), hops.end ()); auto * tunnel = tunnels.CreateTunnel (new TunnelConfig (hops), outboundTunnel); - tunnel->SetTunnelPool (this); + tunnel->SetTunnelPool (shared_from_this ()); } void TunnelPool::RecreateInboundTunnel (InboundTunnel * tunnel) @@ -310,7 +310,7 @@ namespace tunnel outboundTunnel = tunnels.GetNextOutboundTunnel (); LogPrint ("Re-creating destination inbound tunnel..."); auto * newTunnel = tunnels.CreateTunnel (tunnel->GetTunnelConfig ()->Clone (), outboundTunnel); - newTunnel->SetTunnelPool (this); + newTunnel->SetTunnelPool (shared_from_this()); } void TunnelPool::CreateOutboundTunnel () @@ -333,7 +333,7 @@ namespace tunnel auto * tunnel = tunnels.CreateTunnel ( new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ())); - tunnel->SetTunnelPool (this); + tunnel->SetTunnelPool (shared_from_this ()); } else LogPrint ("Can't create outbound tunnel. No inbound tunnels found"); @@ -349,7 +349,7 @@ namespace tunnel LogPrint ("Re-creating destination outbound tunnel..."); auto * newTunnel = tunnels.CreateTunnel ( tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ())); - newTunnel->SetTunnelPool (this); + newTunnel->SetTunnelPool (shared_from_this ()); } else LogPrint ("Can't re-create outbound tunnel. No inbound tunnels found"); diff --git a/TunnelPool.h b/TunnelPool.h index b2d75950..2bc645b3 100644 --- a/TunnelPool.h +++ b/TunnelPool.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "Identity.h" #include "LeaseSet.h" #include "RouterInfo.h" @@ -22,7 +23,7 @@ namespace tunnel class InboundTunnel; class OutboundTunnel; - class TunnelPool // per local destination + class TunnelPool: public std::enable_shared_from_this // per local destination { public: