From d01d0332095e2a6af7c0dffc869f34a405a5e8bb Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 25 Nov 2015 11:51:35 -0500 Subject: [PATCH] eliminate session creation collision --- NTCPSession.cpp | 29 +++++++++++++++-------------- NTCPSession.h | 5 ++--- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 9de41727..90733d73 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -793,27 +793,28 @@ namespace transport } } - void NTCPServer::AddNTCPSession (std::shared_ptr session) + bool NTCPServer::AddNTCPSession (std::shared_ptr session) { - if (session && session->GetRemoteIdentity ()) + if (!session || !session->GetRemoteIdentity ()) return false; + auto& ident = session->GetRemoteIdentity ()->GetIdentHash (); + auto it = m_NTCPSessions.find (ident); + if (it != m_NTCPSessions.end ()) { - std::unique_lock l(m_NTCPSessionsMutex); - m_NTCPSessions[session->GetRemoteIdentity ()->GetIdentHash ()] = session; + LogPrint (eLogWarning, "NTCP session to ", ident.ToBase64 (), " already exists"); + return false; } + m_NTCPSessions.insert (std::pair >(ident, session)); + return true; } void NTCPServer::RemoveNTCPSession (std::shared_ptr session) { if (session && session->GetRemoteIdentity ()) - { - std::unique_lock l(m_NTCPSessionsMutex); m_NTCPSessions.erase (session->GetRemoteIdentity ()->GetIdentHash ()); - } } std::shared_ptr NTCPServer::FindNTCPSession (const i2p::data::IdentHash& ident) { - std::unique_lock l(m_NTCPSessionsMutex); auto it = m_NTCPSessions.find (ident); if (it != m_NTCPSessions.end ()) return it->second; @@ -896,12 +897,12 @@ namespace transport void NTCPServer::Connect (const boost::asio::ip::address& address, int port, std::shared_ptr conn) { LogPrint (eLogInfo, "Connecting to ", address ,":", port); - m_Service.post([conn, this]() - { - this->AddNTCPSession (conn); - }); - conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port), - std::bind (&NTCPServer::HandleConnect, this, std::placeholders::_1, conn)); + m_Service.post([=]() + { + if (this->AddNTCPSession (conn)) + conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port), + std::bind (&NTCPServer::HandleConnect, this, std::placeholders::_1, conn)); + }); } void NTCPServer::HandleConnect (const boost::system::error_code& ecode, std::shared_ptr conn) diff --git a/NTCPSession.h b/NTCPSession.h index 13e1656f..b8efa532 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -143,7 +143,7 @@ namespace transport void Start (); void Stop (); - void AddNTCPSession (std::shared_ptr session); + bool AddNTCPSession (std::shared_ptr session); void RemoveNTCPSession (std::shared_ptr session); std::shared_ptr FindNTCPSession (const i2p::data::IdentHash& ident); void Connect (const boost::asio::ip::address& address, int port, std::shared_ptr conn); @@ -166,8 +166,7 @@ namespace transport boost::asio::io_service m_Service; boost::asio::io_service::work m_Work; boost::asio::ip::tcp::acceptor * m_NTCPAcceptor, * m_NTCPV6Acceptor; - std::mutex m_NTCPSessionsMutex; - std::map > m_NTCPSessions; + std::map > m_NTCPSessions; // access from m_Thread only std::map m_BanList; // IP -> ban expiration time in seconds public: