From b269bda52b2f30454335f58bf577636144aba03c Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 22 Jan 2015 15:31:34 -0500 Subject: [PATCH] shared_ptr for GarlicRouting Session --- Garlic.cpp | 15 ++++++--------- Garlic.h | 10 +++++----- Streaming.cpp | 4 ++-- Streaming.h | 2 +- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/Garlic.cpp b/Garlic.cpp index ff773978..a0a6747e 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -197,7 +197,7 @@ namespace garlic { (*numCloves)++; m_UnconfirmedTagsMsgs[msgID] = newTags; - m_Owner->DeliveryStatusSent (this, msgID); + m_Owner->DeliveryStatusSent (shared_from_this (), msgID); } else LogPrint ("DeliveryStatus clove was not created"); @@ -306,9 +306,6 @@ namespace garlic GarlicDestination::~GarlicDestination () { - for (auto it: m_Sessions) - delete it.second; - m_Sessions.clear (); } void GarlicDestination::AddSessionKey (const uint8_t * key, const uint8_t * tag) @@ -503,23 +500,23 @@ namespace garlic } } - GarlicRoutingSession * GarlicDestination::GetRoutingSession ( + std::shared_ptr GarlicDestination::GetRoutingSession ( const i2p::data::RoutingDestination& destination, int numTags) { auto it = m_Sessions.find (destination.GetIdentHash ()); - GarlicRoutingSession * session = nullptr; + std::shared_ptr session; if (it != m_Sessions.end ()) session = it->second; if (!session) { - session = new GarlicRoutingSession (this, &destination, numTags); + session = std::make_shared (this, &destination, numTags); std::unique_lock l(m_SessionsMutex); m_Sessions[destination.GetIdentHash ()] = session; } return session; } - void GarlicDestination::DeliveryStatusSent (GarlicRoutingSession * session, uint32_t msgID) + void GarlicDestination::DeliveryStatusSent (std::shared_ptr session, uint32_t msgID) { m_CreatedSessions[msgID] = session; } @@ -533,7 +530,7 @@ namespace garlic { it->second->TagsConfirmed (msgID); m_CreatedSessions.erase (it); - LogPrint ("Garlic message ", msgID, " acknowledged"); + LogPrint (eLogInfo, "Garlic message ", msgID, " acknowledged"); } } DeleteI2NPMessage (msg); diff --git a/Garlic.h b/Garlic.h index 88d47280..dc18ade3 100644 --- a/Garlic.h +++ b/Garlic.h @@ -54,7 +54,7 @@ namespace garlic }; class GarlicDestination; - class GarlicRoutingSession + class GarlicRoutingSession: public std::enable_shared_from_this { struct UnconfirmedTags { @@ -105,13 +105,13 @@ namespace garlic GarlicDestination (): m_LastTagsCleanupTime (0) {}; ~GarlicDestination (); - GarlicRoutingSession * GetRoutingSession (const i2p::data::RoutingDestination& destination, int numTags); + std::shared_ptr GetRoutingSession (const i2p::data::RoutingDestination& destination, int numTags); I2NPMessage * WrapMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg, bool attachLeaseSet = false); void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread - void DeliveryStatusSent (GarlicRoutingSession * session, uint32_t msgID); + void DeliveryStatusSent (std::shared_ptr session, uint32_t msgID); virtual void ProcessGarlicMessage (I2NPMessage * msg); virtual void ProcessDeliveryStatusMessage (I2NPMessage * msg); @@ -135,12 +135,12 @@ namespace garlic // outgoing sessions std::mutex m_SessionsMutex; - std::map m_Sessions; + std::map > m_Sessions; // incoming std::map> m_Tags; uint32_t m_LastTagsCleanupTime; // DeliveryStatus - std::map m_CreatedSessions; // msgID -> session + std::map > m_CreatedSessions; // msgID -> session }; } } diff --git a/Streaming.cpp b/Streaming.cpp index 79296fe4..7c0e32f8 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -15,7 +15,7 @@ namespace stream const i2p::data::LeaseSet& remote, int port): m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_IsOpen (false), m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local), - m_RemoteLeaseSet (&remote), m_RoutingSession (nullptr), m_CurrentOutboundTunnel (nullptr), + m_RemoteLeaseSet (&remote), m_CurrentOutboundTunnel (nullptr), m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service), m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (port) { @@ -26,7 +26,7 @@ namespace stream Stream::Stream (boost::asio::io_service& service, StreamingDestination& local): m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_IsOpen (false), m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local), - m_RemoteLeaseSet (nullptr), m_RoutingSession (nullptr), m_CurrentOutboundTunnel (nullptr), + m_RemoteLeaseSet (nullptr), m_CurrentOutboundTunnel (nullptr), m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service), m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (0) { diff --git a/Streaming.h b/Streaming.h index 944e40de..9797a9ba 100644 --- a/Streaming.h +++ b/Streaming.h @@ -142,7 +142,7 @@ namespace stream StreamingDestination& m_LocalDestination; i2p::data::IdentityEx m_RemoteIdentity; const i2p::data::LeaseSet * m_RemoteLeaseSet; - i2p::garlic::GarlicRoutingSession * m_RoutingSession; + std::shared_ptr m_RoutingSession; i2p::data::Lease m_CurrentRemoteLease; i2p::tunnel::OutboundTunnel * m_CurrentOutboundTunnel; std::queue m_ReceiveQueue;