From e461982a316ea8dcc9363c6654a85cdc585cebc5 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 9 Jun 2015 11:00:37 -0400 Subject: [PATCH] support multiple transport sessions to the same peer --- Transports.cpp | 40 ++++++++++++++++++---------------------- Transports.h | 8 +++++++- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Transports.cpp b/Transports.cpp index f871607e..f288d113 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -239,7 +239,7 @@ namespace transport try { auto r = netdb.FindRouter (ident); - it = m_Peers.insert (std::pair(ident, { 0, r, nullptr, + it = m_Peers.insert (std::pair(ident, { 0, r, {}, i2p::util::GetSecondsSinceEpoch () })).first; connected = ConnectToPeer (ident, it->second); } @@ -254,8 +254,8 @@ namespace transport return; } } - if (it->second.session) - it->second.session->SendI2NPMessages (msgs); + if (!it->second.sessions.empty ()) + it->second.sessions.front ()->SendI2NPMessages (msgs); else { for (auto it1: msgs) @@ -309,7 +309,7 @@ namespace transport } } LogPrint (eLogError, "No NTCP and SSU addresses available"); - if (peer.session) peer.session->Done (); + peer.Done (); m_Peers.erase (ident); return false; } @@ -436,20 +436,12 @@ namespace transport auto it = m_Peers.find (ident); if (it != m_Peers.end ()) { - if (!it->second.session) - { - it->second.session = session; - session->SendI2NPMessages (it->second.delayedMessages); - it->second.delayedMessages.clear (); - } - else - { - LogPrint (eLogError, "Session for ", ident.ToBase64 ().substr (0, 4), " already exists"); - session->Done (); - } + it->second.sessions.push_back (session); + session->SendI2NPMessages (it->second.delayedMessages); + it->second.delayedMessages.clear (); } else // incoming connection - m_Peers.insert (std::make_pair (ident, Peer{ 0, nullptr, session, i2p::util::GetSecondsSinceEpoch () })); + m_Peers.insert (std::make_pair (ident, Peer{ 0, nullptr, { session }, i2p::util::GetSecondsSinceEpoch () })); }); } @@ -459,12 +451,16 @@ namespace transport { auto ident = session->GetRemoteIdentity ().GetIdentHash (); auto it = m_Peers.find (ident); - if (it != m_Peers.end () && (!it->second.session || it->second.session == session)) + if (it != m_Peers.end ()) { - if (it->second.delayedMessages.size () > 0) - ConnectToPeer (ident, it->second); - else - m_Peers.erase (it); + it->second.sessions.remove (session); + if (it->second.sessions.empty ()) // TODO: why? + { + if (it->second.delayedMessages.size () > 0) + ConnectToPeer (ident, it->second); + else + m_Peers.erase (it); + } } }); } @@ -482,7 +478,7 @@ namespace transport auto ts = i2p::util::GetSecondsSinceEpoch (); for (auto it = m_Peers.begin (); it != m_Peers.end (); ) { - if (!it->second.session && ts > it->second.creationTime + SESSION_CREATION_TIMEOUT) + if (it->second.sessions.empty () && ts > it->second.creationTime + SESSION_CREATION_TIMEOUT) { LogPrint (eLogError, "Session to peer ", it->first.ToBase64 (), " has not been created in ", SESSION_CREATION_TIMEOUT, " seconds"); it = m_Peers.erase (it); diff --git a/Transports.h b/Transports.h index 11f05190..ec04f6c7 100644 --- a/Transports.h +++ b/Transports.h @@ -60,10 +60,16 @@ namespace transport { int numAttempts; std::shared_ptr router; - std::shared_ptr session; + std::list > sessions; uint64_t creationTime; std::vector delayedMessages; + void Done () + { + for (auto it: sessions) + it->Done (); + } + ~Peer () { for (auto it :delayedMessages)