Browse Source

use shared_ptr for TunnelPool

pull/151/head
orignal 10 years ago
parent
commit
ebb5c53c3a
  1. 4
      Destination.h
  2. 21
      Tunnel.cpp
  3. 19
      Tunnel.h
  4. 8
      TunnelPool.cpp
  5. 3
      TunnelPool.h

4
Destination.h

@ -61,7 +61,7 @@ namespace client
virtual void Stop (); virtual void Stop ();
bool IsRunning () const { return m_IsRunning; }; bool IsRunning () const { return m_IsRunning; };
boost::asio::io_service& GetService () { return m_Service; }; boost::asio::io_service& GetService () { return m_Service; };
i2p::tunnel::TunnelPool * GetTunnelPool () { return m_Pool; }; std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () { return m_Pool; };
bool IsReady () const { return m_LeaseSet && m_LeaseSet->HasNonExpiredLeases (); }; bool IsReady () const { return m_LeaseSet && m_LeaseSet->HasNonExpiredLeases (); };
const i2p::data::LeaseSet * FindLeaseSet (const i2p::data::IdentHash& ident); const i2p::data::LeaseSet * FindLeaseSet (const i2p::data::IdentHash& ident);
bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr); bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr);
@ -121,7 +121,7 @@ namespace client
std::map<i2p::data::IdentHash, i2p::data::LeaseSet *> m_RemoteLeaseSets; std::map<i2p::data::IdentHash, i2p::data::LeaseSet *> m_RemoteLeaseSets;
std::map<i2p::data::IdentHash, LeaseSetRequest *> m_LeaseSetRequests; std::map<i2p::data::IdentHash, LeaseSetRequest *> m_LeaseSetRequests;
i2p::tunnel::TunnelPool * m_Pool; std::shared_ptr<i2p::tunnel::TunnelPool> m_Pool;
i2p::data::LeaseSet * m_LeaseSet; i2p::data::LeaseSet * m_LeaseSet;
bool m_IsPublic; bool m_IsPublic;
uint32_t m_PublishReplyToken; uint32_t m_PublishReplyToken;

21
Tunnel.cpp

@ -191,7 +191,7 @@ namespace tunnel
Tunnels tunnels; 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; delete it.second;
m_PendingTunnels.clear ();*/ m_PendingTunnels.clear ();*/
for (auto& it: m_Pools)
delete it;
m_Pools.clear ();
} }
InboundTunnel * Tunnels::GetInboundTunnel (uint32_t tunnelID) InboundTunnel * Tunnels::GetInboundTunnel (uint32_t tunnelID)
@ -280,15 +277,15 @@ namespace tunnel
return tunnel; return tunnel;
} }
TunnelPool * Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops) std::shared_ptr<TunnelPool> Tunnels::CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOutboundHops)
{ {
auto pool = new TunnelPool (localDestination, numInboundHops, numOutboundHops); auto pool = std::make_shared<TunnelPool> (localDestination, numInboundHops, numOutboundHops);
std::unique_lock<std::mutex> l(m_PoolsMutex); std::unique_lock<std::mutex> l(m_PoolsMutex);
m_Pools.push_back (pool); m_Pools.push_back (pool);
return pool; return pool;
} }
void Tunnels::DeleteTunnelPool (TunnelPool * pool) void Tunnels::DeleteTunnelPool (std::shared_ptr<TunnelPool> pool)
{ {
if (pool) if (pool)
{ {
@ -297,14 +294,10 @@ namespace tunnel
std::unique_lock<std::mutex> l(m_PoolsMutex); std::unique_lock<std::mutex> l(m_PoolsMutex);
m_Pools.remove (pool); 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<TunnelPool> pool)
{ {
if (pool) if (pool)
{ {
@ -547,8 +540,8 @@ namespace tunnel
std::unique_lock<std::mutex> l(m_PoolsMutex); std::unique_lock<std::mutex> l(m_PoolsMutex);
for (auto it: m_Pools) for (auto it: m_Pools)
{ {
TunnelPool * pool = it; auto pool = it;
if (pool->IsActive ()) if (pool && pool->IsActive ())
{ {
pool->CreateTunnels (); pool->CreateTunnels ();
pool->TestTunnels (); pool->TestTunnels ();

19
Tunnel.h

@ -8,6 +8,7 @@
#include <string> #include <string>
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <memory>
#include "Queue.h" #include "Queue.h"
#include "TunnelConfig.h" #include "TunnelConfig.h"
#include "TunnelPool.h" #include "TunnelPool.h"
@ -54,8 +55,8 @@ namespace tunnel
bool IsEstablished () const { return m_State == eTunnelStateEstablished; }; bool IsEstablished () const { return m_State == eTunnelStateEstablished; };
bool IsFailed () const { return m_State == eTunnelStateFailed; }; bool IsFailed () const { return m_State == eTunnelStateFailed; };
TunnelPool * GetTunnelPool () const { return m_Pool; }; std::shared_ptr<TunnelPool> GetTunnelPool () const { return m_Pool; };
void SetTunnelPool (TunnelPool * pool) { m_Pool = pool; }; void SetTunnelPool (std::shared_ptr<TunnelPool> pool) { m_Pool = pool; };
bool HandleTunnelBuildResponse (uint8_t * msg, size_t len); bool HandleTunnelBuildResponse (uint8_t * msg, size_t len);
@ -67,7 +68,7 @@ namespace tunnel
private: private:
TunnelConfig * m_Config; TunnelConfig * m_Config;
TunnelPool * m_Pool; // pool, tunnel belongs to, or null std::shared_ptr<TunnelPool> m_Pool; // pool, tunnel belongs to, or null
TunnelState m_State; TunnelState m_State;
}; };
@ -121,7 +122,7 @@ namespace tunnel
Tunnel * GetPendingTunnel (uint32_t replyMsgID); Tunnel * GetPendingTunnel (uint32_t replyMsgID);
InboundTunnel * GetNextInboundTunnel (); InboundTunnel * GetNextInboundTunnel ();
OutboundTunnel * GetNextOutboundTunnel (); OutboundTunnel * GetNextOutboundTunnel ();
TunnelPool * GetExploratoryPool () const { return m_ExploratoryPool; }; std::shared_ptr<TunnelPool> GetExploratoryPool () const { return m_ExploratoryPool; };
TransitTunnel * GetTransitTunnel (uint32_t tunnelID); TransitTunnel * GetTransitTunnel (uint32_t tunnelID);
int GetTransitTunnelsExpirationTimeout (); int GetTransitTunnelsExpirationTimeout ();
void AddTransitTunnel (TransitTunnel * tunnel); void AddTransitTunnel (TransitTunnel * tunnel);
@ -130,9 +131,9 @@ namespace tunnel
void PostTunnelData (I2NPMessage * msg); void PostTunnelData (I2NPMessage * msg);
template<class TTunnel> template<class TTunnel>
TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0); TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0);
TunnelPool * CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops); std::shared_ptr<TunnelPool> CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops);
void DeleteTunnelPool (TunnelPool * pool); void DeleteTunnelPool (std::shared_ptr<TunnelPool> pool);
void StopTunnelPool (TunnelPool * pool); void StopTunnelPool (std::shared_ptr<TunnelPool> pool);
private: private:
@ -158,8 +159,8 @@ namespace tunnel
std::mutex m_TransitTunnelsMutex; std::mutex m_TransitTunnelsMutex;
std::map<uint32_t, TransitTunnel *> m_TransitTunnels; std::map<uint32_t, TransitTunnel *> m_TransitTunnels;
std::mutex m_PoolsMutex; std::mutex m_PoolsMutex;
std::list<TunnelPool *> m_Pools; std::list<std::shared_ptr<TunnelPool>> m_Pools;
TunnelPool * m_ExploratoryPool; std::shared_ptr<TunnelPool> m_ExploratoryPool;
i2p::util::Queue<I2NPMessage> m_Queue; i2p::util::Queue<I2NPMessage> m_Queue;
public: public:

8
TunnelPool.cpp

@ -300,7 +300,7 @@ namespace tunnel
} }
std::reverse (hops.begin (), hops.end ()); std::reverse (hops.begin (), hops.end ());
auto * tunnel = tunnels.CreateTunnel<InboundTunnel> (new TunnelConfig (hops), outboundTunnel); auto * tunnel = tunnels.CreateTunnel<InboundTunnel> (new TunnelConfig (hops), outboundTunnel);
tunnel->SetTunnelPool (this); tunnel->SetTunnelPool (shared_from_this ());
} }
void TunnelPool::RecreateInboundTunnel (InboundTunnel * tunnel) void TunnelPool::RecreateInboundTunnel (InboundTunnel * tunnel)
@ -310,7 +310,7 @@ namespace tunnel
outboundTunnel = tunnels.GetNextOutboundTunnel (); outboundTunnel = tunnels.GetNextOutboundTunnel ();
LogPrint ("Re-creating destination inbound tunnel..."); LogPrint ("Re-creating destination inbound tunnel...");
auto * newTunnel = tunnels.CreateTunnel<InboundTunnel> (tunnel->GetTunnelConfig ()->Clone (), outboundTunnel); auto * newTunnel = tunnels.CreateTunnel<InboundTunnel> (tunnel->GetTunnelConfig ()->Clone (), outboundTunnel);
newTunnel->SetTunnelPool (this); newTunnel->SetTunnelPool (shared_from_this());
} }
void TunnelPool::CreateOutboundTunnel () void TunnelPool::CreateOutboundTunnel ()
@ -333,7 +333,7 @@ namespace tunnel
auto * tunnel = tunnels.CreateTunnel<OutboundTunnel> ( auto * tunnel = tunnels.CreateTunnel<OutboundTunnel> (
new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ())); new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ()));
tunnel->SetTunnelPool (this); tunnel->SetTunnelPool (shared_from_this ());
} }
else else
LogPrint ("Can't create outbound tunnel. No inbound tunnels found"); LogPrint ("Can't create outbound tunnel. No inbound tunnels found");
@ -349,7 +349,7 @@ namespace tunnel
LogPrint ("Re-creating destination outbound tunnel..."); LogPrint ("Re-creating destination outbound tunnel...");
auto * newTunnel = tunnels.CreateTunnel<OutboundTunnel> ( auto * newTunnel = tunnels.CreateTunnel<OutboundTunnel> (
tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ())); tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ()));
newTunnel->SetTunnelPool (this); newTunnel->SetTunnelPool (shared_from_this ());
} }
else else
LogPrint ("Can't re-create outbound tunnel. No inbound tunnels found"); LogPrint ("Can't re-create outbound tunnel. No inbound tunnels found");

3
TunnelPool.h

@ -6,6 +6,7 @@
#include <vector> #include <vector>
#include <utility> #include <utility>
#include <mutex> #include <mutex>
#include <memory>
#include "Identity.h" #include "Identity.h"
#include "LeaseSet.h" #include "LeaseSet.h"
#include "RouterInfo.h" #include "RouterInfo.h"
@ -22,7 +23,7 @@ namespace tunnel
class InboundTunnel; class InboundTunnel;
class OutboundTunnel; class OutboundTunnel;
class TunnelPool // per local destination class TunnelPool: public std::enable_shared_from_this<TunnelPool> // per local destination
{ {
public: public:

Loading…
Cancel
Save