From c287fb58bdb33b2ee902b160a6f6a785ddeb313a Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 6 Jul 2017 16:12:06 -0400 Subject: [PATCH] reference counter for destinations --- libi2pd/Destination.cpp | 2 +- libi2pd/Destination.h | 8 +++++++- libi2pd_client/I2PService.cpp | 8 ++++++++ libi2pd_client/I2PService.h | 2 +- libi2pd_client/SAM.cpp | 2 ++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/libi2pd/Destination.cpp b/libi2pd/Destination.cpp index 31447e79..d45fdf47 100644 --- a/libi2pd/Destination.cpp +++ b/libi2pd/Destination.cpp @@ -694,7 +694,7 @@ namespace client ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map * params): LeaseSetDestination (isPublic, params), - m_Keys (keys), m_DatagramDestination (nullptr), + m_Keys (keys), m_DatagramDestination (nullptr), m_RefCounter (0), m_ReadyChecker(GetService()) { if (isPublic) diff --git a/libi2pd/Destination.h b/libi2pd/Destination.h index 4b22e043..ef7437fb 100644 --- a/libi2pd/Destination.h +++ b/libi2pd/Destination.h @@ -181,6 +181,11 @@ namespace client const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; }; void Sign (const uint8_t * buf, int len, uint8_t * signature) const { m_Keys.Sign (buf, len, signature); }; + // ref counter + int Acquire () { return ++m_RefCounter; }; + int Release () { return --m_RefCounter; }; + int GetRefCounter () const { return m_RefCounter; }; + // streaming std::shared_ptr CreateStreamingDestination (int port, bool gzip = true); // additional std::shared_ptr GetStreamingDestination (int port = 0) const; @@ -223,7 +228,8 @@ namespace client std::shared_ptr m_StreamingDestination; // default std::map > m_StreamingDestinationsByPorts; - i2p::datagram::DatagramDestination * m_DatagramDestination; + i2p::datagram::DatagramDestination * m_DatagramDestination; + int m_RefCounter; // how many clients(tunnels) use this destination boost::asio::deadline_timer m_ReadyChecker; diff --git a/libi2pd_client/I2PService.cpp b/libi2pd_client/I2PService.cpp index efcf61aa..856ea8ab 100644 --- a/libi2pd_client/I2PService.cpp +++ b/libi2pd_client/I2PService.cpp @@ -13,13 +13,21 @@ namespace client m_LocalDestination (localDestination ? localDestination : i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE)) { + m_LocalDestination->Acquire (); } I2PService::I2PService (i2p::data::SigningKeyType kt): m_LocalDestination (i2p::client::context.CreateNewLocalDestination (false, kt)) { + m_LocalDestination->Acquire (); } + I2PService::~I2PService () + { + ClearHandlers (); + if (m_LocalDestination) m_LocalDestination->Release (); + } + void I2PService::CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port) { assert(streamRequestComplete); i2p::data::IdentHash identHash; diff --git a/libi2pd_client/I2PService.h b/libi2pd_client/I2PService.h index 096443db..8ea29815 100644 --- a/libi2pd_client/I2PService.h +++ b/libi2pd_client/I2PService.h @@ -19,7 +19,7 @@ namespace client public: I2PService (std::shared_ptr localDestination = nullptr); I2PService (i2p::data::SigningKeyType kt); - virtual ~I2PService () { ClearHandlers (); } + virtual ~I2PService (); inline void AddHandler (std::shared_ptr conn) { diff --git a/libi2pd_client/SAM.cpp b/libi2pd_client/SAM.cpp index 778e0745..7fcf8fbf 100644 --- a/libi2pd_client/SAM.cpp +++ b/libi2pd_client/SAM.cpp @@ -921,6 +921,7 @@ namespace client } if (localDestination) { + localDestination->Acquire (); auto session = std::make_shared(localDestination); std::unique_lock l(m_SessionsMutex); auto ret = m_Sessions.insert (std::make_pair(id, session)); @@ -945,6 +946,7 @@ namespace client } if (session) { + session->localDestination->Release (); session->localDestination->StopAcceptingStreams (); session->CloseStreams (); }