pass TunnelConfig as shared_ptr

pull/189/head
orignal 9 years ago
parent 2442d0e910
commit 4c91d08cea

@ -17,14 +17,13 @@ namespace i2p
namespace tunnel
{
Tunnel::Tunnel (TunnelConfig * config):
Tunnel::Tunnel (std::shared_ptr<const TunnelConfig> config):
m_Config (config), m_Pool (nullptr), m_State (eTunnelStatePending), m_IsRecreated (false)
{
}
Tunnel::~Tunnel ()
{
delete m_Config;
}
void Tunnel::Build (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> outboundTunnel)
@ -567,7 +566,7 @@ namespace tunnel
if (!inboundTunnel || !router) return;
LogPrint ("Creating one hop outbound tunnel...");
CreateTunnel<OutboundTunnel> (
new TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> > { router },
std::make_shared<TunnelConfig> (std::vector<std::shared_ptr<const i2p::data::RouterInfo> > { router },
inboundTunnel->GetTunnelConfig ())
);
}
@ -623,7 +622,7 @@ namespace tunnel
auto router = i2p::data::netdb.GetRandomRouter ();
LogPrint ("Creating one hop inbound tunnel...");
CreateTunnel<InboundTunnel> (
new TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> > { router })
std::make_shared<TunnelConfig> (std::vector<std::shared_ptr<const i2p::data::RouterInfo> > { router })
);
}
}
@ -673,7 +672,7 @@ namespace tunnel
}
template<class TTunnel>
std::shared_ptr<TTunnel> Tunnels::CreateTunnel (TunnelConfig * config, std::shared_ptr<OutboundTunnel> outboundTunnel)
std::shared_ptr<TTunnel> Tunnels::CreateTunnel (std::shared_ptr<TunnelConfig> config, std::shared_ptr<OutboundTunnel> outboundTunnel)
{
auto newTunnel = std::make_shared<TTunnel> (config);
uint32_t replyMsgID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
@ -724,7 +723,7 @@ namespace tunnel
void Tunnels::CreateZeroHopsInboundTunnel ()
{
CreateTunnel<InboundTunnel> (
new TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >
std::make_shared<TunnelConfig> (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >
{
i2p::context.GetSharedRouterInfo ()
}));

@ -45,12 +45,12 @@ namespace tunnel
{
public:
Tunnel (TunnelConfig * config);
Tunnel (std::shared_ptr<const TunnelConfig> config);
~Tunnel ();
void Build (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> outboundTunnel = nullptr);
TunnelConfig * GetTunnelConfig () const { return m_Config; }
std::shared_ptr<const TunnelConfig> GetTunnelConfig () const { return m_Config; }
TunnelState GetState () const { return m_State; };
void SetState (TunnelState state) { m_State = state; };
bool IsEstablished () const { return m_State == eTunnelStateEstablished; };
@ -71,7 +71,7 @@ namespace tunnel
private:
TunnelConfig * m_Config;
std::shared_ptr<const TunnelConfig> m_Config;
std::shared_ptr<TunnelPool> m_Pool; // pool, tunnel belongs to, or null
TunnelState m_State;
bool m_IsRecreated;
@ -81,7 +81,7 @@ namespace tunnel
{
public:
OutboundTunnel (TunnelConfig * config): Tunnel (config), m_Gateway (this) {};
OutboundTunnel (std::shared_ptr<const TunnelConfig> config): Tunnel (config), m_Gateway (this) {};
void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg);
void SendTunnelDataMsg (const std::vector<TunnelMessageBlock>& msgs); // multiple messages
@ -103,7 +103,7 @@ namespace tunnel
{
public:
InboundTunnel (TunnelConfig * config): Tunnel (config), m_Endpoint (true) {};
InboundTunnel (std::shared_ptr<const TunnelConfig> config): Tunnel (config), m_Endpoint (true) {};
void HandleTunnelDataMsg (I2NPMessage * msg);
size_t GetNumReceivedBytes () const { return m_Endpoint.GetNumReceivedBytes (); };
@ -138,7 +138,7 @@ namespace tunnel
void PostTunnelData (I2NPMessage * msg);
void PostTunnelData (const std::vector<I2NPMessage *>& msgs);
template<class TTunnel>
std::shared_ptr<TTunnel> CreateTunnel (TunnelConfig * config, std::shared_ptr<OutboundTunnel> outboundTunnel = nullptr);
std::shared_ptr<TTunnel> CreateTunnel (std::shared_ptr<TunnelConfig> config, std::shared_ptr<OutboundTunnel> outboundTunnel = nullptr);
void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<InboundTunnel> tunnel);
void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> tunnel);
std::shared_ptr<TunnelPool> CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops, int numInboundTunnels, int numOutboundTunnels);

@ -84,7 +84,7 @@ namespace tunnel
}
}
void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID)
void CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID) const
{
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
htobe32buf (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID);
@ -113,7 +113,7 @@ namespace tunnel
TunnelConfig (std::vector<std::shared_ptr<const i2p::data::RouterInfo> > peers,
const TunnelConfig * replyTunnelConfig = nullptr) // replyTunnelConfig=nullptr means inbound
std::shared_ptr<const TunnelConfig> replyTunnelConfig = nullptr) // replyTunnelConfig=nullptr means inbound
{
TunnelHopConfig * prev = nullptr;
for (auto it: peers)
@ -189,9 +189,9 @@ namespace tunnel
s << ":me";
}
TunnelConfig * Invert () const
std::shared_ptr<TunnelConfig> Invert () const
{
TunnelConfig * newConfig = new TunnelConfig ();
auto newConfig = new TunnelConfig ();
TunnelHopConfig * hop = m_FirstHop, * nextNewHop = nullptr;
while (hop)
{
@ -214,10 +214,10 @@ namespace tunnel
hop = hop->next;
}
return newConfig;
return std::shared_ptr<TunnelConfig>(newConfig);
}
TunnelConfig * Clone (const TunnelConfig * replyTunnelConfig = nullptr) const
std::shared_ptr<TunnelConfig> Clone (std::shared_ptr<const TunnelConfig> replyTunnelConfig = nullptr) const
{
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > peers;
TunnelHopConfig * hop = m_FirstHop;
@ -226,7 +226,7 @@ namespace tunnel
peers.push_back (hop->router);
hop = hop->next;
}
return new TunnelConfig (peers, replyTunnelConfig);
return std::make_shared<TunnelConfig> (peers, replyTunnelConfig);
}
private:

@ -320,7 +320,7 @@ namespace tunnel
hops.push_back (hop);
}
std::reverse (hops.begin (), hops.end ());
auto tunnel = tunnels.CreateTunnel<InboundTunnel> (new TunnelConfig (hops), outboundTunnel);
auto tunnel = tunnels.CreateTunnel<InboundTunnel> (std::make_shared<TunnelConfig> (hops), outboundTunnel);
tunnel->SetTunnelPool (shared_from_this ());
}
@ -368,7 +368,7 @@ namespace tunnel
}
auto tunnel = tunnels.CreateTunnel<OutboundTunnel> (
new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ()));
std::make_shared<TunnelConfig> (hops, inboundTunnel->GetTunnelConfig ()));
tunnel->SetTunnelPool (shared_from_this ());
}
else

Loading…
Cancel
Save