Browse Source

eliminate session creation collision

pull/304/head
orignal 9 years ago
parent
commit
d01d033209
  1. 29
      NTCPSession.cpp
  2. 5
      NTCPSession.h

29
NTCPSession.cpp

@ -793,27 +793,28 @@ namespace transport
} }
} }
void NTCPServer::AddNTCPSession (std::shared_ptr<NTCPSession> session) bool NTCPServer::AddNTCPSession (std::shared_ptr<NTCPSession> 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<std::mutex> l(m_NTCPSessionsMutex); LogPrint (eLogWarning, "NTCP session to ", ident.ToBase64 (), " already exists");
m_NTCPSessions[session->GetRemoteIdentity ()->GetIdentHash ()] = session; return false;
} }
m_NTCPSessions.insert (std::pair<i2p::data::IdentHash, std::shared_ptr<NTCPSession> >(ident, session));
return true;
} }
void NTCPServer::RemoveNTCPSession (std::shared_ptr<NTCPSession> session) void NTCPServer::RemoveNTCPSession (std::shared_ptr<NTCPSession> session)
{ {
if (session && session->GetRemoteIdentity ()) if (session && session->GetRemoteIdentity ())
{
std::unique_lock<std::mutex> l(m_NTCPSessionsMutex);
m_NTCPSessions.erase (session->GetRemoteIdentity ()->GetIdentHash ()); m_NTCPSessions.erase (session->GetRemoteIdentity ()->GetIdentHash ());
}
} }
std::shared_ptr<NTCPSession> NTCPServer::FindNTCPSession (const i2p::data::IdentHash& ident) std::shared_ptr<NTCPSession> NTCPServer::FindNTCPSession (const i2p::data::IdentHash& ident)
{ {
std::unique_lock<std::mutex> l(m_NTCPSessionsMutex);
auto it = m_NTCPSessions.find (ident); auto it = m_NTCPSessions.find (ident);
if (it != m_NTCPSessions.end ()) if (it != m_NTCPSessions.end ())
return it->second; return it->second;
@ -896,12 +897,12 @@ namespace transport
void NTCPServer::Connect (const boost::asio::ip::address& address, int port, std::shared_ptr<NTCPSession> conn) void NTCPServer::Connect (const boost::asio::ip::address& address, int port, std::shared_ptr<NTCPSession> conn)
{ {
LogPrint (eLogInfo, "Connecting to ", address ,":", port); LogPrint (eLogInfo, "Connecting to ", address ,":", port);
m_Service.post([conn, this]() m_Service.post([=]()
{ {
this->AddNTCPSession (conn); if (this->AddNTCPSession (conn))
}); conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port),
conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port), std::bind (&NTCPServer::HandleConnect, this, std::placeholders::_1, conn));
std::bind (&NTCPServer::HandleConnect, this, std::placeholders::_1, conn)); });
} }
void NTCPServer::HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCPSession> conn) void NTCPServer::HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCPSession> conn)

5
NTCPSession.h

@ -143,7 +143,7 @@ namespace transport
void Start (); void Start ();
void Stop (); void Stop ();
void AddNTCPSession (std::shared_ptr<NTCPSession> session); bool AddNTCPSession (std::shared_ptr<NTCPSession> session);
void RemoveNTCPSession (std::shared_ptr<NTCPSession> session); void RemoveNTCPSession (std::shared_ptr<NTCPSession> session);
std::shared_ptr<NTCPSession> FindNTCPSession (const i2p::data::IdentHash& ident); std::shared_ptr<NTCPSession> FindNTCPSession (const i2p::data::IdentHash& ident);
void Connect (const boost::asio::ip::address& address, int port, std::shared_ptr<NTCPSession> conn); void Connect (const boost::asio::ip::address& address, int port, std::shared_ptr<NTCPSession> conn);
@ -166,8 +166,7 @@ namespace transport
boost::asio::io_service m_Service; boost::asio::io_service m_Service;
boost::asio::io_service::work m_Work; boost::asio::io_service::work m_Work;
boost::asio::ip::tcp::acceptor * m_NTCPAcceptor, * m_NTCPV6Acceptor; boost::asio::ip::tcp::acceptor * m_NTCPAcceptor, * m_NTCPV6Acceptor;
std::mutex m_NTCPSessionsMutex; std::map<i2p::data::IdentHash, std::shared_ptr<NTCPSession> > m_NTCPSessions; // access from m_Thread only
std::map<i2p::data::IdentHash, std::shared_ptr<NTCPSession> > m_NTCPSessions;
std::map<boost::asio::ip::address, uint32_t> m_BanList; // IP -> ban expiration time in seconds std::map<boost::asio::ip::address, uint32_t> m_BanList; // IP -> ban expiration time in seconds
public: public:

Loading…
Cancel
Save