From d971dff59373fde05325993afca3e66f5b9cb2c9 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 12 Jan 2015 22:53:35 -0500 Subject: [PATCH] introduced Peer --- NTCPSession.cpp | 3 +++ SSUSession.cpp | 2 ++ Transports.cpp | 30 ++++++++++++++++++++++++++++++ Transports.h | 20 +++++++++++++++++++- 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 435d0ace..c0ba4451 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -77,6 +77,7 @@ namespace transport { m_IsEstablished = false; m_Socket.close (); + transports.PeerDisconnected (shared_from_this ()); int numDelayed = 0; for (auto it :m_DelayedMessages) { @@ -106,6 +107,8 @@ namespace transport SendTimeSyncMessage (); SendI2NPMessage (CreateDatabaseStoreMsg ()); // we tell immediately who we are + transports.PeerConnected (shared_from_this ()); + if (!m_DelayedMessages.empty ()) { for (auto it :m_DelayedMessages) diff --git a/SSUSession.cpp b/SSUSession.cpp index 11044921..4618624b 100644 --- a/SSUSession.cpp +++ b/SSUSession.cpp @@ -756,6 +756,7 @@ namespace transport void SSUSession::Close () { SendSesionDestroyed (); + transports.PeerDisconnected (shared_from_this ()); if (!m_DelayedMessages.empty ()) { for (auto it :m_DelayedMessages) @@ -773,6 +774,7 @@ namespace transport m_DHKeysPair = nullptr; } SendI2NPMessage (CreateDatabaseStoreMsg ()); + transports.PeerConnected (shared_from_this ()); if (!m_DelayedMessages.empty ()) { for (auto it :m_DelayedMessages) diff --git a/Transports.cpp b/Transports.cpp index f36cfd2a..b8e35bcb 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -139,6 +139,7 @@ namespace transport void Transports::Stop () { + m_Peers.clear (); if (m_SSUServer) { m_SSUServer->Stop (); @@ -291,6 +292,35 @@ namespace transport { m_DHKeysPairSupplier.Return (pair); } + + void Transports::PeerConnected (std::shared_ptr session) + { + m_Service.post([session, this]() + { + auto ident = session->GetRemoteIdentity ().GetIdentHash (); + auto it = m_Peers.find (ident); + if (it != m_Peers.end ()) + { + it->second.session = session; + for (auto it1: it->second.delayedMessages) + session->SendI2NPMessage (it1); + } + /* else // incoming connection + m_Peers[ident] = { nullptr, session };*/ + }); + } + + void Transports::PeerDisconnected (std::shared_ptr session) + { + m_Service.post([session, this]() + { + auto ident = session->GetRemoteIdentity ().GetIdentHash (); + auto it = m_Peers.find (ident); + if (it != m_Peers.end ()) + m_Peers.erase (it); + // TODO:: check for delayed messages + }); + } } } diff --git a/Transports.h b/Transports.h index 07f2afd3..6ab9330e 100644 --- a/Transports.h +++ b/Transports.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -50,6 +51,19 @@ namespace transport CryptoPP::AutoSeededRandomPool m_Rnd; }; + struct Peer + { + std::shared_ptr router; + std::shared_ptr session; + std::list delayedMessages; + + ~Peer () + { + for (auto it :delayedMessages) + i2p::DeleteI2NPMessage (it); + } + }; + class Transports { public: @@ -66,6 +80,9 @@ namespace transport void SendMessage (const i2p::data::IdentHash& ident, i2p::I2NPMessage * msg); void CloseSession (std::shared_ptr router); + + void PeerConnected (std::shared_ptr session); + void PeerDisconnected (std::shared_ptr session); private: @@ -86,7 +103,8 @@ namespace transport NTCPServer * m_NTCPServer; SSUServer * m_SSUServer; - + std::map m_Peers; + DHKeysPairSupplier m_DHKeysPairSupplier; public: