From 4b094b2156818a4fbfe6db7963d514d107b30eeb Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 26 Jan 2015 20:49:16 -0500 Subject: [PATCH] handle tunnel build messages in tunnels thread --- I2NPProtocol.cpp | 7 ++++ Tunnel.cpp | 86 +++++++++++++++++++++++++++--------------------- Tunnel.h | 2 -- 3 files changed, 55 insertions(+), 40 deletions(-) diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 638122b5..9bddbb7b 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -544,6 +544,13 @@ namespace i2p else i2p::context.ProcessDeliveryStatusMessage (msg); break; + case eI2NPVariableTunnelBuild: + case eI2NPVariableTunnelBuildReply: + case eI2NPTunnelBuild: + case eI2NPTunnelBuildReply: + // forward to tunnel thread + i2p::tunnel::tunnels.PostTunnelData (msg); + break; default: HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ()); DeleteI2NPMessage (msg); diff --git a/Tunnel.cpp b/Tunnel.cpp index dd088fa6..75259ba5 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -273,7 +273,6 @@ namespace tunnel { InboundTunnel * tunnel = nullptr; size_t minReceived = 0; - std::unique_lock l(m_InboundTunnelsMutex); for (auto it : m_InboundTunnels) { if (!it.second->IsEstablished ()) continue; @@ -291,7 +290,6 @@ namespace tunnel CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); uint32_t ind = rnd.GenerateWord32 (0, m_OutboundTunnels.size () - 1), i = 0; OutboundTunnel * tunnel = nullptr; - std::unique_lock l(m_OutboundTunnelsMutex); for (auto it: m_OutboundTunnels) { if (it->IsEstablished ()) @@ -369,39 +367,59 @@ namespace tunnel I2NPMessage * msg = m_Queue.GetNextWithTimeout (1000); // 1 sec if (msg) { - uint8_t typeID = msg->GetTypeID (); - uint32_t prevTunnelID = 0; - TunnelBase * prevTunnel = nullptr; + uint32_t prevTunnelID = 0, tunnelID = 0; + TunnelBase * prevTunnel = nullptr, * tunnel = nullptr; do { - uint32_t tunnelID = bufbe32toh (msg->GetPayload ()); - TunnelBase * tunnel = nullptr; - if (tunnelID == prevTunnelID) - tunnel = prevTunnel; - else if (prevTunnel) - prevTunnel->FlushTunnelDataMsgs (); + uint8_t typeID = msg->GetTypeID (); + switch (typeID) + { + case eI2NPTunnelData: + case eI2NPTunnelGateway: + { + tunnelID = bufbe32toh (msg->GetPayload ()); + if (tunnelID == prevTunnelID) + tunnel = prevTunnel; + else if (prevTunnel) + prevTunnel->FlushTunnelDataMsgs (); - if (!tunnel && typeID == eI2NPTunnelData) - tunnel = GetInboundTunnel (tunnelID); - if (!tunnel) - tunnel = GetTransitTunnel (tunnelID); - if (tunnel) - { - if (typeID == eI2NPTunnelData) - tunnel->HandleTunnelDataMsg (msg); - else // tunnel gateway assumed - HandleTunnelGatewayMsg (tunnel, msg); + if (!tunnel && typeID == eI2NPTunnelData) + tunnel = GetInboundTunnel (tunnelID); + if (!tunnel) + tunnel = GetTransitTunnel (tunnelID); + if (tunnel) + { + if (typeID == eI2NPTunnelData) + tunnel->HandleTunnelDataMsg (msg); + else // tunnel gateway assumed + HandleTunnelGatewayMsg (tunnel, msg); + } + else + { + LogPrint (eLogWarning, "Tunnel ", tunnelID, " not found"); + DeleteI2NPMessage (msg); + } + break; + } + case eI2NPVariableTunnelBuild: + case eI2NPVariableTunnelBuildReply: + case eI2NPTunnelBuild: + case eI2NPTunnelBuildReply: + { + HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ()); + DeleteI2NPMessage (msg); + break; + } + default: + { + LogPrint (eLogError, "Unexpected messsage type ", (int)typeID); + DeleteI2NPMessage (msg); + } } - else - { - LogPrint ("Tunnel ", tunnelID, " not found"); - DeleteI2NPMessage (msg); - } - + msg = m_Queue.Get (); if (msg) { - typeID = msg->GetTypeID (); prevTunnelID = tunnelID; prevTunnel = tunnel; } @@ -517,10 +535,7 @@ namespace tunnel if (pool) pool->TunnelExpired (tunnel); } - { - std::unique_lock l(m_OutboundTunnelsMutex); - it = m_OutboundTunnels.erase (it); - } + it = m_OutboundTunnels.erase (it); delete tunnel; } else @@ -562,10 +577,7 @@ namespace tunnel if (pool) pool->TunnelExpired (tunnel); } - { - std::unique_lock l(m_InboundTunnelsMutex); - it = m_InboundTunnels.erase (it); - } + it = m_InboundTunnels.erase (it); delete tunnel; } else @@ -664,7 +676,6 @@ namespace tunnel void Tunnels::AddOutboundTunnel (OutboundTunnel * newTunnel) { - std::unique_lock l(m_OutboundTunnelsMutex); m_OutboundTunnels.push_back (newTunnel); auto pool = newTunnel->GetTunnelPool (); if (pool && pool->IsActive ()) @@ -675,7 +686,6 @@ namespace tunnel void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel) { - std::unique_lock l(m_InboundTunnelsMutex); m_InboundTunnels[newTunnel->GetTunnelID ()] = newTunnel; auto pool = newTunnel->GetTunnelPool (); if (!pool) diff --git a/Tunnel.h b/Tunnel.h index 2cd9c0fb..892f7430 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -166,9 +166,7 @@ namespace tunnel std::thread * m_Thread; std::map m_PendingInboundTunnels; // by replyMsgID std::map m_PendingOutboundTunnels; // by replyMsgID - std::mutex m_InboundTunnelsMutex; std::map m_InboundTunnels; - std::mutex m_OutboundTunnelsMutex; std::list m_OutboundTunnels; std::mutex m_TransitTunnelsMutex; std::map m_TransitTunnels;