fixed race condition

This commit is contained in:
orignal 2015-02-06 20:53:48 -05:00
parent 8305fd5f82
commit 5730b15f01
7 changed files with 34 additions and 18 deletions

View File

@ -20,8 +20,9 @@ namespace transport
{
NTCPSession::NTCPSession (NTCPServer& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter):
TransportSession (in_RemoteRouter), m_Server (server), m_Socket (m_Server.GetService ()),
m_TerminationTimer (m_Server.GetService ()), m_IsEstablished (false), m_ReceiveBufferOffset (0),
m_NextMessage (nullptr), m_IsSending (false), m_NumSentBytes (0), m_NumReceivedBytes (0)
m_TerminationTimer (m_Server.GetService ()), m_IsEstablished (false), m_IsTerminated (false),
m_ReceiveBufferOffset (0), m_NextMessage (nullptr), m_IsSending (false),
m_NumSentBytes (0), m_NumReceivedBytes (0)
{
m_DHKeysPair = transports.GetNextDHKeysPair ();
m_Establisher = new Establisher;
@ -30,10 +31,6 @@ namespace transport
NTCPSession::~NTCPSession ()
{
delete m_Establisher;
if (m_NextMessage)
i2p::DeleteI2NPMessage (m_NextMessage);
for (auto it: m_SendQueue)
DeleteI2NPMessage (it);
}
void NTCPSession::CreateAESKey (uint8_t * pubKey, i2p::crypto::AESKey& key)
@ -72,13 +69,30 @@ namespace transport
}
}
void NTCPSession::Done ()
{
m_Server.GetService ().post (std::bind (&NTCPSession::Terminate, shared_from_this ()));
}
void NTCPSession::Terminate ()
{
m_IsEstablished = false;
m_Socket.close ();
transports.PeerDisconnected (shared_from_this ());
m_Server.RemoveNTCPSession (shared_from_this ());
LogPrint (eLogInfo, "NTCP session terminated");
if (!m_IsTerminated)
{
m_IsTerminated = true;
m_IsEstablished = false;
m_Socket.close ();
transports.PeerDisconnected (shared_from_this ());
m_Server.RemoveNTCPSession (shared_from_this ());
for (auto it: m_SendQueue)
DeleteI2NPMessage (it);
m_SendQueue.clear ();
if (m_NextMessage)
{
i2p::DeleteI2NPMessage (m_NextMessage);
m_NextMessage = nullptr;
}
LogPrint (eLogInfo, "NTCP session terminated");
}
}
void NTCPSession::Connected ()

View File

@ -54,6 +54,7 @@ namespace transport
NTCPSession (NTCPServer& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter = nullptr);
~NTCPSession ();
void Terminate ();
void Done ();
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
bool IsEstablished () const { return m_IsEstablished; };
@ -113,7 +114,7 @@ namespace transport
NTCPServer& m_Server;
boost::asio::ip::tcp::socket m_Socket;
boost::asio::deadline_timer m_TerminationTimer;
bool m_IsEstablished;
bool m_IsEstablished, m_IsTerminated;
i2p::crypto::CBCDecryption m_Decryption;
i2p::crypto::CBCEncryption m_Encryption;

View File

@ -759,9 +759,10 @@ namespace transport
transports.PeerDisconnected (shared_from_this ());
}
void SSUSession::Terminate ()
void SSUSession::Done ()
{
m_Server.DeleteSession (shared_from_this ());
boost::asio::io_service& service = IsV6 () ? m_Server.GetServiceV6 () : m_Server.GetService ();
service.post (std::bind (&SSUSession::Failed, shared_from_this ()));
}
void SSUSession::Established ()

View File

@ -63,7 +63,7 @@ namespace transport
void Introduce (uint32_t iTag, const uint8_t * iKey);
void WaitForIntroduction ();
void Close ();
void Terminate ();
void Done ();
boost::asio::ip::udp::endpoint& GetRemoteEndpoint () { return m_RemoteEndpoint; };
bool IsV6 () const { return m_RemoteEndpoint.address ().is_v6 (); };
void SendI2NPMessage (I2NPMessage * msg);

View File

@ -62,7 +62,7 @@ namespace transport
}
virtual ~TransportSession () { delete m_DHKeysPair; };
virtual void Terminate () = 0;
virtual void Done () = 0;
std::shared_ptr<const i2p::data::RouterInfo> GetRemoteRouter () { return m_RemoteRouter; };
const i2p::data::IdentityEx& GetRemoteIdentity () { return m_RemoteIdentity; };

View File

@ -414,7 +414,7 @@ namespace transport
else
{
LogPrint (eLogError, "Session for ", ident.ToBase64 ().substr (0, 4), " already exists");
session->Terminate ();
session->Done ();
}
}
else // incoming connection

View File

@ -61,7 +61,7 @@ namespace transport
~Peer ()
{
for (auto it :delayedMessages)
i2p::DeleteI2NPMessage (it);
i2p::DeleteI2NPMessage (it);
}
};