diff --git a/Garlic.cpp b/Garlic.cpp index 09fd27b3..1bd5cc3f 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -100,7 +100,7 @@ namespace garlic if (ts >= it->second->tagsCreationTime + OUTGOING_TAGS_EXPIRATION_TIMEOUT) { if (m_Owner) - m_Owner->RemoveCreatedSession (it->first); + m_Owner->RemoveDeliveryStatusSession (it->first); delete it->second; it = m_UnconfirmedTagsMsgs.erase (it); } @@ -553,10 +553,13 @@ namespace garlic std::shared_ptr GarlicDestination::GetRoutingSession ( std::shared_ptr destination, bool attachLeaseSet) { - auto it = m_Sessions.find (destination->GetIdentHash ()); - std::shared_ptr session; - if (it != m_Sessions.end ()) - session = it->second; + GarlicRoutingSessionPtr session; + { + std::unique_lock l(m_SessionsMutex); + auto it = m_Sessions.find (destination->GetIdentHash ()); + if (it != m_Sessions.end ()) + session = it->second; + } if (!session) { session = std::make_shared (this, destination, @@ -582,25 +585,25 @@ namespace garlic } } - void GarlicDestination::RemoveCreatedSession (uint32_t msgID) + void GarlicDestination::RemoveDeliveryStatusSession (uint32_t msgID) { - m_CreatedSessions.erase (msgID); + m_DeliveryStatusSessions.erase (msgID); } - void GarlicDestination::DeliveryStatusSent (std::shared_ptr session, uint32_t msgID) + void GarlicDestination::DeliveryStatusSent (GarlicRoutingSessionPtr session, uint32_t msgID) { - m_CreatedSessions[msgID] = session; + m_DeliveryStatusSessions[msgID] = session; } void GarlicDestination::HandleDeliveryStatusMessage (std::shared_ptr msg) { uint32_t msgID = bufbe32toh (msg->GetPayload ()); { - auto it = m_CreatedSessions.find (msgID); - if (it != m_CreatedSessions.end ()) + auto it = m_DeliveryStatusSessions.find (msgID); + if (it != m_DeliveryStatusSessions.end ()) { it->second->MessageConfirmed (msgID); - m_CreatedSessions.erase (it); + m_DeliveryStatusSessions.erase (it); LogPrint (eLogDebug, "Garlic: message ", msgID, " acknowledged"); } } diff --git a/Garlic.h b/Garlic.h index 45903f3f..1bfcab7c 100644 --- a/Garlic.h +++ b/Garlic.h @@ -118,7 +118,8 @@ namespace garlic // for HTTP only size_t GetNumOutgoingTags () const { return m_SessionTags.size (); }; }; - + using GarlicRoutingSessionPtr = std::shared_ptr; + class GarlicDestination: public i2p::data::LocalDestination { public: @@ -129,13 +130,13 @@ namespace garlic void SetNumTags (int numTags) { m_NumTags = numTags; }; std::shared_ptr GetRoutingSession (std::shared_ptr destination, bool attachLeaseSet); void CleanupRoutingSessions (); - void RemoveCreatedSession (uint32_t msgID); + void RemoveDeliveryStatusSession (uint32_t msgID); std::shared_ptr WrapMessage (std::shared_ptr destination, std::shared_ptr 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 (std::shared_ptr session, uint32_t msgID); + void DeliveryStatusSent (GarlicRoutingSessionPtr session, uint32_t msgID); virtual void ProcessGarlicMessage (std::shared_ptr msg); virtual void ProcessDeliveryStatusMessage (std::shared_ptr msg); @@ -161,12 +162,12 @@ namespace garlic // outgoing sessions int m_NumTags; 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_DeliveryStatusSessions; // msgID -> session public: