mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 13:27:17 +01:00
eliminate session creation collision
This commit is contained in:
parent
06c4aca490
commit
d01d033209
|
@ -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);
|
||||
m_NTCPSessions[session->GetRemoteIdentity ()->GetIdentHash ()] = session;
|
||||
LogPrint (eLogWarning, "NTCP session to ", ident.ToBase64 (), " already exists");
|
||||
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)
|
||||
{
|
||||
if (session && session->GetRemoteIdentity ())
|
||||
{
|
||||
std::unique_lock<std::mutex> l(m_NTCPSessionsMutex);
|
||||
m_NTCPSessions.erase (session->GetRemoteIdentity ()->GetIdentHash ());
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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<NTCPSession> 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<NTCPSession> conn)
|
||||
|
|
|
@ -143,7 +143,7 @@ namespace transport
|
|||
void Start ();
|
||||
void Stop ();
|
||||
|
||||
void AddNTCPSession (std::shared_ptr<NTCPSession> session);
|
||||
bool AddNTCPSession (std::shared_ptr<NTCPSession> session);
|
||||
void RemoveNTCPSession (std::shared_ptr<NTCPSession> session);
|
||||
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);
|
||||
|
@ -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<i2p::data::IdentHash, std::shared_ptr<NTCPSession> > m_NTCPSessions;
|
||||
std::map<i2p::data::IdentHash, std::shared_ptr<NTCPSession> > m_NTCPSessions; // access from m_Thread only
|
||||
std::map<boost::asio::ip::address, uint32_t> m_BanList; // IP -> ban expiration time in seconds
|
||||
|
||||
public:
|
||||
|
|
Loading…
Reference in a new issue