From 6489230e681e000d3242343d4ec994294321b6a6 Mon Sep 17 00:00:00 2001 From: "Francisco Blas (klondike) Izquierdo Riera" Date: Sat, 3 Jan 2015 02:17:01 +0100 Subject: [PATCH] Simplify and merge the identHash caching codepath on I2PClientTunnel --- I2PTunnel.cpp | 45 ++++++++++++++++++++++++--------------------- I2PTunnel.h | 1 + 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index 5a5b88f7..3a2c846c 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -174,11 +174,7 @@ namespace client void I2PClientTunnel::Start () { - i2p::data::IdentHash identHash; - if (i2p::client::context.GetAddressBook ().GetIdentHash (m_Destination, identHash)) - m_DestinationIdentHash = new i2p::data::IdentHash (identHash); - if (!m_DestinationIdentHash) - LogPrint ("I2PTunnel unknown destination ", m_Destination); + GetIdentHash(); m_Acceptor.listen (); Accept (); } @@ -193,6 +189,21 @@ namespace client delete originalIdentHash; } + /* HACK: maybe we should create a caching IdentHash provider in AddressBook */ + const i2p::data::IdentHash * I2PClientTunnel::GetIdentHash () + { + if (!m_DestinationIdentHash) + { + i2p::data::IdentHash identHash; + if (i2p::client::context.GetAddressBook ().GetIdentHash (m_Destination, identHash)) + m_DestinationIdentHash = new i2p::data::IdentHash (identHash); + else + LogPrint ("Remote destination ", m_Destination, " not found"); + } + return m_DestinationIdentHash; + } + + void I2PClientTunnel::Accept () { auto newSocket = new boost::asio::ip::tcp::socket (GetService ()); @@ -204,31 +215,22 @@ namespace client { if (!ecode) { - if (!m_DestinationIdentHash) - { - i2p::data::IdentHash identHash; - if (i2p::client::context.GetAddressBook ().GetIdentHash (m_Destination, identHash)) - m_DestinationIdentHash = new i2p::data::IdentHash (identHash); - } - if (m_DestinationIdentHash) + const i2p::data::IdentHash *identHash = GetIdentHash(); + if (identHash) { // try to get a LeaseSet - m_RemoteLeaseSet = GetLocalDestination ()->FindLeaseSet (*m_DestinationIdentHash); + m_RemoteLeaseSet = GetLocalDestination ()->FindLeaseSet (*identHash); if (m_RemoteLeaseSet && m_RemoteLeaseSet->HasNonExpiredLeases ()) CreateConnection (socket); else { - GetLocalDestination ()->RequestDestination (*m_DestinationIdentHash, + GetLocalDestination ()->RequestDestination (*identHash, std::bind (&I2PClientTunnel::HandleLeaseSetRequestComplete, this, std::placeholders::_1, socket)); } - } + } else - { - LogPrint ("Remote destination ", m_Destination, " not found"); delete socket; - } - Accept (); } else @@ -239,9 +241,10 @@ namespace client { if (success) { - if (m_DestinationIdentHash) + const i2p::data::IdentHash *identHash = GetIdentHash(); + if (identHash) { - m_RemoteLeaseSet = GetLocalDestination ()->FindLeaseSet (*m_DestinationIdentHash); + m_RemoteLeaseSet = GetLocalDestination ()->FindLeaseSet (*identHash); CreateConnection (socket); return; } diff --git a/I2PTunnel.h b/I2PTunnel.h index 070641fd..d1ccbf38 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -88,6 +88,7 @@ namespace client private: + const i2p::data::IdentHash * GetIdentHash (); void Accept (); void HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket); void HandleLeaseSetRequestComplete (bool success, boost::asio::ip::tcp::socket * socket);