1
0
mirror of https://github.com/PurpleI2P/i2pd.git synced 2025-01-22 04:04:16 +00:00

fixed race condition

This commit is contained in:
orignal 2015-01-12 12:15:54 -05:00
parent a15c2c5d86
commit ff856d2f20
2 changed files with 9 additions and 0 deletions

View File

@ -751,17 +751,24 @@ namespace transport
void NTCPServer::AddNTCPSession (std::shared_ptr<NTCPSession> session) void NTCPServer::AddNTCPSession (std::shared_ptr<NTCPSession> session)
{ {
if (session) if (session)
{
std::unique_lock<std::mutex> l(m_NTCPSessionsMutex);
m_NTCPSessions[session->GetRemoteIdentity ().GetIdentHash ()] = session; m_NTCPSessions[session->GetRemoteIdentity ().GetIdentHash ()] = session;
}
} }
void NTCPServer::RemoveNTCPSession (std::shared_ptr<NTCPSession> session) void NTCPServer::RemoveNTCPSession (std::shared_ptr<NTCPSession> session)
{ {
if (session) if (session)
{
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;

View File

@ -6,6 +6,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <thread> #include <thread>
#include <mutex>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <cryptopp/modes.h> #include <cryptopp/modes.h>
#include <cryptopp/aes.h> #include <cryptopp/aes.h>
@ -167,6 +168,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; std::map<i2p::data::IdentHash, std::shared_ptr<NTCPSession> > m_NTCPSessions;
public: public: