From 3ae225fb41a1a72ff373a4c79ac026efed19677c Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 25 Mar 2014 21:17:03 -0400 Subject: [PATCH] use existing SSU session if available --- SSU.cpp | 12 ++++++++++++ SSU.h | 1 + Transports.cpp | 44 +++++++++++++++++++++----------------------- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/SSU.cpp b/SSU.cpp index b118c70a..9075d157 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -796,6 +796,18 @@ namespace ssu LogPrint ("SSU receive error: ", ecode.message ()); } + SSUSession * SSUServer::FindSession (const i2p::data::RouterInfo * router) + { + if (!router) return nullptr; + auto address = router->GetSSUAddress (); + if (!address) return nullptr; + auto it = m_Sessions.find (boost::asio::ip::udp::endpoint (address->host, address->port)); + if (it != m_Sessions.end ()) + return it->second; + else + return nullptr; + } + SSUSession * SSUServer::GetSession (const i2p::data::RouterInfo * router) { SSUSession * session = nullptr; diff --git a/SSU.h b/SSU.h index c0c82e3e..c3b2c476 100644 --- a/SSU.h +++ b/SSU.h @@ -126,6 +126,7 @@ namespace ssu void Start (); void Stop (); SSUSession * GetSession (const i2p::data::RouterInfo * router); + SSUSession * FindSession (const i2p::data::RouterInfo * router); void DeleteSession (SSUSession * session); void DeleteAllSessions (); diff --git a/Transports.cpp b/Transports.cpp index 9dc733c4..60de2710 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -156,34 +156,36 @@ namespace i2p void Transports::PostMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg) { auto session = FindNTCPSession (ident); - if (!session) + if (session) + session->SendI2NPMessage (msg); + else { RouterInfo * r = netdb.FindRouter (ident); if (r) { - auto address = r->GetNTCPAddress (); - if (address) - { - session = new i2p::ntcp::NTCPClient (m_Service, address->host, address->port, *r); - AddNTCPSession (session); - } + auto ssuSession = m_SSUServer ? m_SSUServer->FindSession (r) : nullptr; + if (ssuSession) + ssuSession->SendI2NPMessage (msg); else { - // SSU always have lower prioprity than NTCP - // TODO: it shouldn't - LogPrint ("No NTCP addresses available. Trying SSU"); - address = r->GetSSUAddress (); - if (address && m_SSUServer) - { - auto s = m_SSUServer->GetSession (r); + // existing session not found. create new + // try NTCP first + auto address = r->GetNTCPAddress (); + if (address) + { + auto s = new i2p::ntcp::NTCPClient (m_Service, address->host, address->port, *r); + AddNTCPSession (s); + s->SendI2NPMessage (msg); + } + else + { + // then SSU + auto s = m_SSUServer ? m_SSUServer->GetSession (r) : nullptr; if (s) - { s->SendI2NPMessage (msg); - return; - } + else + LogPrint ("No NTCP and SSU addresses available"); } - else - LogPrint ("No SSU addresses available"); } } else @@ -192,10 +194,6 @@ namespace i2p i2p::data::netdb.RequestDestination (ident); } } - if (session) - session->SendI2NPMessage (msg); - else - LogPrint ("Session not found"); } void Transports::DetectExternalIP ()