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 @@ -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<i2p::tunnel::TunnelPool> 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 @@ -121,7 +121,7 @@ namespace client
std::map<i2p::data::IdentHash, i2p::data::LeaseSet *> m_RemoteLeaseSets;
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;
bool m_IsPublic;
uint32_t m_PublishReplyToken;

21
Tunnel.cpp

@ -191,7 +191,7 @@ namespace tunnel @@ -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 @@ -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 @@ -280,15 +277,15 @@ namespace 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);
m_Pools.push_back (pool);
return pool;
}
void Tunnels::DeleteTunnelPool (TunnelPool * pool)
void Tunnels::DeleteTunnelPool (std::shared_ptr<TunnelPool> pool)
{
if (pool)
{
@ -297,14 +294,10 @@ namespace tunnel @@ -297,14 +294,10 @@ namespace tunnel
std::unique_lock<std::mutex> 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<TunnelPool> pool)
{
if (pool)
{
@ -547,8 +540,8 @@ namespace tunnel @@ -547,8 +540,8 @@ namespace tunnel
std::unique_lock<std::mutex> 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 ();

19
Tunnel.h

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

8
TunnelPool.cpp

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

3
TunnelPool.h

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

Loading…
Cancel
Save