From a0de60e179e60ced49c76b4da3e8ca8536111b4b Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 16 Jun 2015 10:14:14 -0400 Subject: [PATCH] use share_ptr for garlic messages --- Destination.cpp | 7 +++---- Destination.h | 6 +++--- Garlic.cpp | 11 ++++------- Garlic.h | 8 ++++---- I2NPProtocol.cpp | 21 ++++++++++++--------- RouterContext.cpp | 4 ++-- RouterContext.h | 4 ++-- TunnelPool.cpp | 11 ++--------- TunnelPool.h | 4 ++-- 9 files changed, 34 insertions(+), 42 deletions(-) diff --git a/Destination.cpp b/Destination.cpp index 63c89fff..27f2564c 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -215,12 +215,12 @@ namespace client return true; } - void ClientDestination::ProcessGarlicMessage (I2NPMessage * msg) + void ClientDestination::ProcessGarlicMessage (std::shared_ptr msg) { m_Service.post (std::bind (&ClientDestination::HandleGarlicMessage, this, msg)); } - void ClientDestination::ProcessDeliveryStatusMessage (I2NPMessage * msg) + void ClientDestination::ProcessDeliveryStatusMessage (std::shared_ptr msg) { m_Service.post (std::bind (&ClientDestination::HandleDeliveryStatusMessage, this, msg)); } @@ -343,7 +343,7 @@ namespace client LogPrint ("Request for ", key.ToBase64 (), " not found"); } - void ClientDestination::HandleDeliveryStatusMessage (I2NPMessage * msg) + void ClientDestination::HandleDeliveryStatusMessage (std::shared_ptr msg) { uint32_t msgID = bufbe32toh (msg->GetPayload () + DELIVERY_STATUS_MSGID_OFFSET); if (msgID == m_PublishReplyToken) @@ -351,7 +351,6 @@ namespace client LogPrint (eLogDebug, "Publishing confirmed"); m_ExcludedFloodfills.clear (); m_PublishReplyToken = 0; - i2p::DeleteI2NPMessage (msg); } else i2p::garlic::GarlicDestination::HandleDeliveryStatusMessage (msg); diff --git a/Destination.h b/Destination.h index 1f48ded0..c41ee9ca 100644 --- a/Destination.h +++ b/Destination.h @@ -99,8 +99,8 @@ namespace client // override GarlicDestination bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); - void ProcessGarlicMessage (I2NPMessage * msg); - void ProcessDeliveryStatusMessage (I2NPMessage * msg); + void ProcessGarlicMessage (std::shared_ptr msg); + void ProcessDeliveryStatusMessage (std::shared_ptr msg); void SetLeaseSetUpdated (); // I2CP @@ -114,7 +114,7 @@ namespace client void HandlePublishConfirmationTimer (const boost::system::error_code& ecode); void HandleDatabaseStoreMessage (const uint8_t * buf, size_t len); void HandleDatabaseSearchReplyMessage (const uint8_t * buf, size_t len); - void HandleDeliveryStatusMessage (I2NPMessage * msg); + void HandleDeliveryStatusMessage (std::shared_ptr msg); void RequestLeaseSet (const i2p::data::IdentHash& dest, RequestComplete requestComplete); bool SendLeaseSetRequest (const i2p::data::IdentHash& dest, std::shared_ptr nextFloodfill, LeaseSetRequest * request); diff --git a/Garlic.cpp b/Garlic.cpp index bbfaed91..8bdfb10c 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -362,14 +362,13 @@ namespace garlic return true; } - void GarlicDestination::HandleGarlicMessage (I2NPMessage * msg) + void GarlicDestination::HandleGarlicMessage (std::shared_ptr msg) { uint8_t * buf = msg->GetPayload (); uint32_t length = bufbe32toh (buf); if (length > msg->GetLength ()) { LogPrint (eLogError, "Garlic message length ", length, " exceeds I2NP message length ", msg->GetLength ()); - DeleteI2NPMessage (msg); return; } buf += 4; // length @@ -406,7 +405,6 @@ namespace garlic else LogPrint (eLogError, "Failed to decrypt garlic"); } - DeleteI2NPMessage (msg); // cleanup expired tags uint32_t ts = i2p::util::GetSecondsSinceEpoch (); @@ -588,7 +586,7 @@ namespace garlic m_CreatedSessions[msgID] = session; } - void GarlicDestination::HandleDeliveryStatusMessage (I2NPMessage * msg) + void GarlicDestination::HandleDeliveryStatusMessage (std::shared_ptr msg) { uint32_t msgID = bufbe32toh (msg->GetPayload ()); { @@ -600,7 +598,6 @@ namespace garlic LogPrint (eLogInfo, "Garlic message ", msgID, " acknowledged"); } } - DeleteI2NPMessage (msg); } void GarlicDestination::SetLeaseSetUpdated () @@ -610,12 +607,12 @@ namespace garlic it.second->SetLeaseSetUpdated (); } - void GarlicDestination::ProcessGarlicMessage (I2NPMessage * msg) + void GarlicDestination::ProcessGarlicMessage (std::shared_ptr msg) { HandleGarlicMessage (msg); } - void GarlicDestination::ProcessDeliveryStatusMessage (I2NPMessage * msg) + void GarlicDestination::ProcessDeliveryStatusMessage (std::shared_ptr msg) { HandleDeliveryStatusMessage (msg); } diff --git a/Garlic.h b/Garlic.h index 489e532b..d5330735 100644 --- a/Garlic.h +++ b/Garlic.h @@ -133,8 +133,8 @@ namespace garlic virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread void DeliveryStatusSent (std::shared_ptr session, uint32_t msgID); - virtual void ProcessGarlicMessage (I2NPMessage * msg); - virtual void ProcessDeliveryStatusMessage (I2NPMessage * msg); + virtual void ProcessGarlicMessage (std::shared_ptr msg); + virtual void ProcessDeliveryStatusMessage (std::shared_ptr msg); virtual void SetLeaseSetUpdated (); virtual std::shared_ptr GetLeaseSet () = 0; // TODO @@ -143,8 +143,8 @@ namespace garlic protected: - void HandleGarlicMessage (I2NPMessage * msg); - void HandleDeliveryStatusMessage (I2NPMessage * msg); + void HandleGarlicMessage (std::shared_ptr msg); + void HandleDeliveryStatusMessage (std::shared_ptr msg); private: diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 104fdef1..3948afec 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -537,20 +537,20 @@ namespace i2p i2p::tunnel::tunnels.PostTunnelData (msg); break; case eI2NPGarlic: + { LogPrint ("Garlic"); + auto sharedMsg = ToSharedI2NPMessage (msg); if (msg->from) { if (msg->from->GetTunnelPool ()) - msg->from->GetTunnelPool ()->ProcessGarlicMessage (msg); + msg->from->GetTunnelPool ()->ProcessGarlicMessage (sharedMsg); else - { LogPrint (eLogInfo, "Local destination for garlic doesn't exist anymore"); - DeleteI2NPMessage (msg); - } } else - i2p::context.ProcessGarlicMessage (msg); - break; + i2p::context.ProcessGarlicMessage (sharedMsg); + break; + } case eI2NPDatabaseStore: case eI2NPDatabaseSearchReply: case eI2NPDatabaseLookup: @@ -558,12 +558,15 @@ namespace i2p i2p::data::netdb.PostI2NPMsg (msg); break; case eI2NPDeliveryStatus: + { LogPrint ("DeliveryStatus"); + auto sharedMsg = ToSharedI2NPMessage (msg); if (msg->from && msg->from->GetTunnelPool ()) - msg->from->GetTunnelPool ()->ProcessDeliveryStatus (msg); + msg->from->GetTunnelPool ()->ProcessDeliveryStatus (sharedMsg); else - i2p::context.ProcessDeliveryStatusMessage (msg); - break; + i2p::context.ProcessDeliveryStatusMessage (sharedMsg); + break; + } case eI2NPVariableTunnelBuild: case eI2NPVariableTunnelBuildReply: case eI2NPTunnelBuild: diff --git a/RouterContext.cpp b/RouterContext.cpp index 495f1181..c9328b5c 100644 --- a/RouterContext.cpp +++ b/RouterContext.cpp @@ -299,13 +299,13 @@ namespace i2p i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from)); } - void RouterContext::ProcessGarlicMessage (I2NPMessage * msg) + void RouterContext::ProcessGarlicMessage (std::shared_ptr msg) { std::unique_lock l(m_GarlicMutex); i2p::garlic::GarlicDestination::ProcessGarlicMessage (msg); } - void RouterContext::ProcessDeliveryStatusMessage (I2NPMessage * msg) + void RouterContext::ProcessDeliveryStatusMessage (std::shared_ptr msg) { std::unique_lock l(m_GarlicMutex); i2p::garlic::GarlicDestination::ProcessDeliveryStatusMessage (msg); diff --git a/RouterContext.h b/RouterContext.h index 840d75ad..2689d025 100644 --- a/RouterContext.h +++ b/RouterContext.h @@ -78,8 +78,8 @@ namespace i2p void HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr from); // override GarlicDestination - void ProcessGarlicMessage (I2NPMessage * msg); - void ProcessDeliveryStatusMessage (I2NPMessage * msg); + void ProcessGarlicMessage (std::shared_ptr msg); + void ProcessDeliveryStatusMessage (std::shared_ptr msg); private: diff --git a/TunnelPool.cpp b/TunnelPool.cpp index 478d6f11..0f0eb709 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -260,18 +260,15 @@ namespace tunnel } } - void TunnelPool::ProcessGarlicMessage (I2NPMessage * msg) + void TunnelPool::ProcessGarlicMessage (std::shared_ptr msg) { if (m_LocalDestination) m_LocalDestination->ProcessGarlicMessage (msg); else - { LogPrint (eLogWarning, "Local destination doesn't exist. Dropped"); - DeleteI2NPMessage (msg); - } } - void TunnelPool::ProcessDeliveryStatus (I2NPMessage * msg) + void TunnelPool::ProcessDeliveryStatus (std::shared_ptr msg) { const uint8_t * buf = msg->GetPayload (); uint32_t msgID = bufbe32toh (buf); @@ -288,17 +285,13 @@ namespace tunnel it->second.second->SetState (eTunnelStateEstablished); LogPrint ("Tunnel test ", it->first, " successive. ", i2p::util::GetMillisecondsSinceEpoch () - timestamp, " milliseconds"); m_Tests.erase (it); - DeleteI2NPMessage (msg); } else { if (m_LocalDestination) m_LocalDestination->ProcessDeliveryStatusMessage (msg); else - { LogPrint (eLogWarning, "Local destination doesn't exist. Dropped"); - DeleteI2NPMessage (msg); - } } } diff --git a/TunnelPool.h b/TunnelPool.h index 2232a787..2d4203ef 100644 --- a/TunnelPool.h +++ b/TunnelPool.h @@ -47,8 +47,8 @@ namespace tunnel std::shared_ptr GetNewOutboundTunnel (std::shared_ptr old) const; void TestTunnels (); - void ProcessGarlicMessage (I2NPMessage * msg); - void ProcessDeliveryStatus (I2NPMessage * msg); + void ProcessGarlicMessage (std::shared_ptr msg); + void ProcessDeliveryStatus (std::shared_ptr msg); bool IsActive () const { return m_IsActive; }; void SetActive (bool isActive) { m_IsActive = isActive; };