From 4c91d08ceabac3477cd0ca0b9ff543d01a674b97 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 6 May 2015 16:17:48 -0400 Subject: [PATCH] pass TunnelConfig as shared_ptr --- Tunnel.cpp | 11 +++++------ Tunnel.h | 12 ++++++------ TunnelConfig.h | 14 +++++++------- TunnelPool.cpp | 4 ++-- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Tunnel.cpp b/Tunnel.cpp index 65638040..59a9de2b 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -17,14 +17,13 @@ namespace i2p namespace tunnel { - Tunnel::Tunnel (TunnelConfig * config): + Tunnel::Tunnel (std::shared_ptr 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) @@ -567,7 +566,7 @@ namespace tunnel if (!inboundTunnel || !router) return; LogPrint ("Creating one hop outbound tunnel..."); CreateTunnel ( - new TunnelConfig (std::vector > { router }, + std::make_shared (std::vector > { router }, inboundTunnel->GetTunnelConfig ()) ); } @@ -623,7 +622,7 @@ namespace tunnel auto router = i2p::data::netdb.GetRandomRouter (); LogPrint ("Creating one hop inbound tunnel..."); CreateTunnel ( - new TunnelConfig (std::vector > { router }) + std::make_shared (std::vector > { router }) ); } } @@ -673,7 +672,7 @@ namespace tunnel } template - std::shared_ptr Tunnels::CreateTunnel (TunnelConfig * config, std::shared_ptr outboundTunnel) + std::shared_ptr Tunnels::CreateTunnel (std::shared_ptr config, std::shared_ptr outboundTunnel) { auto newTunnel = std::make_shared (config); uint32_t replyMsgID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (); @@ -724,7 +723,7 @@ namespace tunnel void Tunnels::CreateZeroHopsInboundTunnel () { CreateTunnel ( - new TunnelConfig (std::vector > + std::make_shared (std::vector > { i2p::context.GetSharedRouterInfo () })); diff --git a/Tunnel.h b/Tunnel.h index adf9a30b..be15aa91 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -45,12 +45,12 @@ namespace tunnel { public: - Tunnel (TunnelConfig * config); + Tunnel (std::shared_ptr config); ~Tunnel (); void Build (uint32_t replyMsgID, std::shared_ptr outboundTunnel = nullptr); - TunnelConfig * GetTunnelConfig () const { return m_Config; } + std::shared_ptr 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 m_Config; std::shared_ptr 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 config): Tunnel (config), m_Gateway (this) {}; void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg); void SendTunnelDataMsg (const std::vector& msgs); // multiple messages @@ -103,7 +103,7 @@ namespace tunnel { public: - InboundTunnel (TunnelConfig * config): Tunnel (config), m_Endpoint (true) {}; + InboundTunnel (std::shared_ptr 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& msgs); template - std::shared_ptr CreateTunnel (TunnelConfig * config, std::shared_ptr outboundTunnel = nullptr); + std::shared_ptr CreateTunnel (std::shared_ptr config, std::shared_ptr outboundTunnel = nullptr); void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr tunnel); void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr tunnel); std::shared_ptr CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops, int numInboundTunnels, int numOutboundTunnels); diff --git a/TunnelConfig.h b/TunnelConfig.h index 49a1d622..76434f9c 100644 --- a/TunnelConfig.h +++ b/TunnelConfig.h @@ -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 > peers, - const TunnelConfig * replyTunnelConfig = nullptr) // replyTunnelConfig=nullptr means inbound + std::shared_ptr 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 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(newConfig); } - TunnelConfig * Clone (const TunnelConfig * replyTunnelConfig = nullptr) const + std::shared_ptr Clone (std::shared_ptr replyTunnelConfig = nullptr) const { std::vector > 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 (peers, replyTunnelConfig); } private: diff --git a/TunnelPool.cpp b/TunnelPool.cpp index 8ed90403..82c66fe6 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -320,7 +320,7 @@ namespace tunnel hops.push_back (hop); } std::reverse (hops.begin (), hops.end ()); - auto tunnel = tunnels.CreateTunnel (new TunnelConfig (hops), outboundTunnel); + auto tunnel = tunnels.CreateTunnel (std::make_shared (hops), outboundTunnel); tunnel->SetTunnelPool (shared_from_this ()); } @@ -368,7 +368,7 @@ namespace tunnel } auto tunnel = tunnels.CreateTunnel ( - new TunnelConfig (hops, inboundTunnel->GetTunnelConfig ())); + std::make_shared (hops, inboundTunnel->GetTunnelConfig ())); tunnel->SetTunnelPool (shared_from_this ()); } else