From 52f806ff945b113b0fde4f415f95a46b94ece005 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 24 Feb 2015 15:40:50 -0500 Subject: [PATCH] use shared_ptr for ClientDestination --- BOB.cpp | 16 ++++++++-------- BOB.h | 12 ++++++------ ClientContext.cpp | 28 ++++++++++++---------------- ClientContext.h | 16 ++++++++-------- I2PService.cpp | 2 +- I2PService.h | 10 +++++----- I2PTunnel.cpp | 4 ++-- I2PTunnel.h | 4 ++-- SAM.cpp | 4 ++-- SAM.h | 4 ++-- 10 files changed, 48 insertions(+), 52 deletions(-) diff --git a/BOB.cpp b/BOB.cpp index 3b31569d..2f881a69 100644 --- a/BOB.cpp +++ b/BOB.cpp @@ -8,7 +8,7 @@ namespace i2p { namespace client { - BOBI2PInboundTunnel::BOBI2PInboundTunnel (int port, ClientDestination * localDestination): + BOBI2PInboundTunnel::BOBI2PInboundTunnel (int port, std::shared_ptr localDestination): BOBI2PTunnel (localDestination), m_Acceptor (localDestination->GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Timer (localDestination->GetService ()) { @@ -142,7 +142,7 @@ namespace client } BOBI2POutboundTunnel::BOBI2POutboundTunnel (const std::string& address, int port, - ClientDestination * localDestination, bool quiet): BOBI2PTunnel (localDestination), + std::shared_ptr localDestination, bool quiet): BOBI2PTunnel (localDestination), m_Endpoint (boost::asio::ip::address::from_string (address), port), m_IsQuiet (quiet) { } @@ -176,7 +176,7 @@ namespace client } } - BOBDestination::BOBDestination (ClientDestination& localDestination): + BOBDestination::BOBDestination (std::shared_ptr localDestination): m_LocalDestination (localDestination), m_OutboundTunnel (nullptr), m_InboundTunnel (nullptr) { @@ -186,7 +186,7 @@ namespace client { delete m_OutboundTunnel; delete m_InboundTunnel; - i2p::client::context.DeleteLocalDestination (&m_LocalDestination); + i2p::client::context.DeleteLocalDestination (m_LocalDestination); } void BOBDestination::Start () @@ -198,7 +198,7 @@ namespace client void BOBDestination::Stop () { StopTunnels (); - m_LocalDestination.Stop (); + m_LocalDestination->Stop (); } void BOBDestination::StopTunnels () @@ -220,13 +220,13 @@ namespace client void BOBDestination::CreateInboundTunnel (int port) { if (!m_InboundTunnel) - m_InboundTunnel = new BOBI2PInboundTunnel (port, &m_LocalDestination); + m_InboundTunnel = new BOBI2PInboundTunnel (port, m_LocalDestination); } void BOBDestination::CreateOutboundTunnel (const std::string& address, int port, bool quiet) { if (!m_OutboundTunnel) - m_OutboundTunnel = new BOBI2POutboundTunnel (address, port, &m_LocalDestination, quiet); + m_OutboundTunnel = new BOBI2POutboundTunnel (address, port, m_LocalDestination, quiet); } BOBCommandSession::BOBCommandSession (BOBCommandChannel& owner): @@ -384,7 +384,7 @@ namespace client LogPrint (eLogDebug, "BOB: start ", m_Nickname); if (!m_CurrentDestination) { - m_CurrentDestination = new BOBDestination (*i2p::client::context.CreateNewLocalDestination (m_Keys, true, &m_Options)); + m_CurrentDestination = new BOBDestination (i2p::client::context.CreateNewLocalDestination (m_Keys, true, &m_Options)); m_Owner.AddDestination (m_Nickname, m_CurrentDestination); } if (m_InPort) diff --git a/BOB.h b/BOB.h index 5858cdb8..cd42c9b8 100644 --- a/BOB.h +++ b/BOB.h @@ -46,7 +46,7 @@ namespace client { public: - BOBI2PTunnel (ClientDestination * localDestination): + BOBI2PTunnel (std::shared_ptr localDestination): I2PService (localDestination) {}; virtual void Start () {}; @@ -67,7 +67,7 @@ namespace client public: - BOBI2PInboundTunnel (int port, ClientDestination * localDestination); + BOBI2PInboundTunnel (int port, std::shared_ptr localDestination); ~BOBI2PInboundTunnel (); void Start (); @@ -96,7 +96,7 @@ namespace client { public: - BOBI2POutboundTunnel (const std::string& address, int port, ClientDestination * localDestination, bool quiet); + BOBI2POutboundTunnel (const std::string& address, int port, std::shared_ptr localDestination, bool quiet); void Start (); void Stop (); @@ -119,7 +119,7 @@ namespace client { public: - BOBDestination (ClientDestination& localDestination); + BOBDestination (std::shared_ptr localDestination); ~BOBDestination (); void Start (); @@ -127,11 +127,11 @@ namespace client void StopTunnels (); void CreateInboundTunnel (int port); void CreateOutboundTunnel (const std::string& address, int port, bool quiet); - const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination.GetPrivateKeys (); }; + const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination->GetPrivateKeys (); }; private: - ClientDestination& m_LocalDestination; + std::shared_ptr m_LocalDestination; BOBI2POutboundTunnel * m_OutboundTunnel; BOBI2PInboundTunnel * m_InboundTunnel; }; diff --git a/ClientContext.cpp b/ClientContext.cpp index a5bb6298..fb6e4791 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -49,7 +49,7 @@ namespace client std::string ircDestination = i2p::util::config::GetArg("-ircdest", ""); if (ircDestination.length () > 0) // ircdest is presented { - ClientDestination * localDestination = nullptr; + std::shared_ptr localDestination = nullptr; std::string ircKeys = i2p::util::config::GetArg("-irckeys", ""); if (ircKeys.length () > 0) localDestination = LoadLocalDestination (ircKeys, false); @@ -146,15 +146,12 @@ namespace client } for (auto it: m_Destinations) - { it.second->Stop (); - delete it.second; - } m_Destinations.clear (); - m_SharedLocalDestination = 0; // deleted through m_Destination + m_SharedLocalDestination = nullptr; } - ClientDestination * ClientContext::LoadLocalDestination (const std::string& filename, bool isPublic) + std::shared_ptr ClientContext::LoadLocalDestination (const std::string& filename, bool isPublic) { i2p::data::PrivateKeys keys; std::string fullPath = i2p::util::filesystem::GetFullPath (filename); @@ -184,7 +181,7 @@ namespace client LogPrint ("New private keys file ", fullPath, " for ", m_AddressBook.ToAddress(keys.GetPublic ().GetIdentHash ()), " created"); } - ClientDestination * localDestination = nullptr; + std::shared_ptr localDestination = nullptr; std::unique_lock l(m_DestinationsMutex); auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ()); if (it != m_Destinations.end ()) @@ -194,25 +191,25 @@ namespace client } else { - localDestination = new ClientDestination (keys, isPublic); + localDestination = std::make_shared (keys, isPublic); m_Destinations[localDestination->GetIdentHash ()] = localDestination; localDestination->Start (); } return localDestination; } - ClientDestination * ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType, + std::shared_ptr ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType, const std::map * params) { i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType); - auto localDestination = new ClientDestination (keys, isPublic, params); + auto localDestination = std::make_shared (keys, isPublic, params); std::unique_lock l(m_DestinationsMutex); m_Destinations[localDestination->GetIdentHash ()] = localDestination; localDestination->Start (); return localDestination; } - void ClientContext::DeleteLocalDestination (ClientDestination * destination) + void ClientContext::DeleteLocalDestination (std::shared_ptr destination) { if (!destination) return; auto it = m_Destinations.find (destination->GetIdentHash ()); @@ -224,11 +221,10 @@ namespace client m_Destinations.erase (it); } d->Stop (); - delete d; } } - ClientDestination * ClientContext::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic, + std::shared_ptr ClientContext::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map * params) { auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ()); @@ -242,14 +238,14 @@ namespace client } return nullptr; } - auto localDestination = new ClientDestination (keys, isPublic, params); + auto localDestination = std::make_shared (keys, isPublic, params); std::unique_lock l(m_DestinationsMutex); m_Destinations[keys.GetPublic ().GetIdentHash ()] = localDestination; localDestination->Start (); return localDestination; } - ClientDestination * ClientContext::FindLocalDestination (const i2p::data::IdentHash& destination) const + std::shared_ptr ClientContext::FindLocalDestination (const i2p::data::IdentHash& destination) const { auto it = m_Destinations.find (destination); if (it != m_Destinations.end ()) @@ -299,7 +295,7 @@ namespace client for (int i = 0; i < numClientTunnels; i++) { - ClientDestination * localDestination = nullptr; + std::shared_ptr localDestination = nullptr; if (keys[i].length () > 0) localDestination = LoadLocalDestination (keys[i], false); auto clientTunnel = new I2PClientTunnel (destinations[i], ports[i], localDestination); diff --git a/ClientContext.h b/ClientContext.h index 6da2cd30..007aa359 100644 --- a/ClientContext.h +++ b/ClientContext.h @@ -37,14 +37,14 @@ namespace client void Start (); void Stop (); - ClientDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; }; - ClientDestination * CreateNewLocalDestination (bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1, + std::shared_ptr GetSharedLocalDestination () const { return m_SharedLocalDestination; }; + std::shared_ptr CreateNewLocalDestination (bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1, const std::map * params = nullptr); // transient - ClientDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true, + std::shared_ptr CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true, const std::map * params = nullptr); - void DeleteLocalDestination (ClientDestination * destination); - ClientDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const; - ClientDestination * LoadLocalDestination (const std::string& filename, bool isPublic); + void DeleteLocalDestination (std::shared_ptr destination); + std::shared_ptr FindLocalDestination (const i2p::data::IdentHash& destination) const; + std::shared_ptr LoadLocalDestination (const std::string& filename, bool isPublic); AddressBook& GetAddressBook () { return m_AddressBook; }; const SAMBridge * GetSAMBridge () const { return m_SamBridge; }; @@ -56,8 +56,8 @@ namespace client private: std::mutex m_DestinationsMutex; - std::map m_Destinations; - ClientDestination * m_SharedLocalDestination; + std::map > m_Destinations; + std::shared_ptr m_SharedLocalDestination; AddressBook m_AddressBook; diff --git a/I2PService.cpp b/I2PService.cpp index 69cee2bc..6b74fe1a 100644 --- a/I2PService.cpp +++ b/I2PService.cpp @@ -10,7 +10,7 @@ namespace client { static const i2p::data::SigningKeyType I2P_SERVICE_DEFAULT_KEY_TYPE = i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256; - I2PService::I2PService (ClientDestination * localDestination): + I2PService::I2PService (std::shared_ptr localDestination): m_LocalDestination (localDestination ? localDestination : i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE)) { diff --git a/I2PService.h b/I2PService.h index a03f16ed..1e4fc10b 100644 --- a/I2PService.h +++ b/I2PService.h @@ -17,7 +17,7 @@ namespace client class I2PService { public: - I2PService (ClientDestination * localDestination = nullptr); + I2PService (std::shared_ptr localDestination = nullptr); I2PService (i2p::data::SigningKeyType kt); virtual ~I2PService () { ClearHandlers (); } @@ -37,8 +37,8 @@ namespace client m_Handlers.clear(); } - inline ClientDestination * GetLocalDestination () { return m_LocalDestination; } - inline void SetLocalDestination (ClientDestination * dest) { m_LocalDestination = dest; } + inline std::shared_ptr GetLocalDestination () { return m_LocalDestination; } + inline void SetLocalDestination (std::shared_ptr dest) { m_LocalDestination = dest; } void CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port = 0); inline boost::asio::io_service& GetService () { return m_LocalDestination->GetService (); } @@ -49,7 +49,7 @@ namespace client virtual const char* GetName() { return "Generic I2P Service"; } private: - ClientDestination * m_LocalDestination; + std::shared_ptr m_LocalDestination; std::unordered_set > m_Handlers; std::mutex m_HandlersMutex; }; @@ -81,7 +81,7 @@ namespace client class TCPIPAcceptor: public I2PService { public: - TCPIPAcceptor (int port, ClientDestination * localDestination = nullptr) : + TCPIPAcceptor (int port, std::shared_ptr localDestination = nullptr) : I2PService(localDestination), m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Timer (GetService ()) {} diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index 8177b18e..15d3b6ad 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -202,7 +202,7 @@ namespace client Done(shared_from_this()); } - I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination): + I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, std::shared_ptr localDestination): TCPIPAcceptor (port,localDestination), m_Destination (destination), m_DestinationIdentHash (nullptr) {} @@ -243,7 +243,7 @@ namespace client return nullptr; } - I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination): + I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, std::shared_ptr localDestination): I2PService (localDestination), m_Endpoint (boost::asio::ip::address::from_string (address), port) { } diff --git a/I2PTunnel.h b/I2PTunnel.h index 51de3b73..d4be85c7 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -65,7 +65,7 @@ namespace client public: - I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination = nullptr); + I2PClientTunnel (const std::string& destination, int port, std::shared_ptr localDestination = nullptr); ~I2PClientTunnel () {} void Start (); @@ -83,7 +83,7 @@ namespace client { public: - I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination); + I2PServerTunnel (const std::string& address, int port, std::shared_ptr localDestination); void Start (); void Stop (); diff --git a/SAM.cpp b/SAM.cpp index 3760fd95..5f4c23af 100644 --- a/SAM.cpp +++ b/SAM.cpp @@ -595,7 +595,7 @@ namespace client LogPrint (eLogWarning, "Datagram size ", len," exceeds buffer"); } - SAMSession::SAMSession (ClientDestination * dest): + SAMSession::SAMSession (std::shared_ptr dest): localDestination (dest) { } @@ -700,7 +700,7 @@ namespace client SAMSession * SAMBridge::CreateSession (const std::string& id, const std::string& destination, const std::map * params) { - ClientDestination * localDestination = nullptr; + std::shared_ptr localDestination = nullptr; if (destination != "") { i2p::data::PrivateKeys keys; diff --git a/SAM.h b/SAM.h index 07583153..5bc1320a 100644 --- a/SAM.h +++ b/SAM.h @@ -128,10 +128,10 @@ namespace client struct SAMSession { - ClientDestination * localDestination; + std::shared_ptr localDestination; std::list > sockets; - SAMSession (ClientDestination * localDestination); + SAMSession (std::shared_ptr dest); ~SAMSession (); void CloseStreams ();