diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 491e239e..5c1883c1 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -20,8 +20,9 @@ namespace transport { NTCPSession::NTCPSession (NTCPServer& server, std::shared_ptr 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 () diff --git a/NTCPSession.h b/NTCPSession.h index 66f38538..c264b7b0 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -54,6 +54,7 @@ namespace transport NTCPSession (NTCPServer& server, std::shared_ptr 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; diff --git a/SSUSession.cpp b/SSUSession.cpp index 7aedef07..1df785e6 100644 --- a/SSUSession.cpp +++ b/SSUSession.cpp @@ -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 () diff --git a/SSUSession.h b/SSUSession.h index 35cffbe5..2f323c3f 100644 --- a/SSUSession.h +++ b/SSUSession.h @@ -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); diff --git a/TransportSession.h b/TransportSession.h index 1acc9493..1067f763 100644 --- a/TransportSession.h +++ b/TransportSession.h @@ -62,7 +62,7 @@ namespace transport } virtual ~TransportSession () { delete m_DHKeysPair; }; - virtual void Terminate () = 0; + virtual void Done () = 0; std::shared_ptr GetRemoteRouter () { return m_RemoteRouter; }; const i2p::data::IdentityEx& GetRemoteIdentity () { return m_RemoteIdentity; }; diff --git a/Transports.cpp b/Transports.cpp index 0285fe1b..afa3de5c 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -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 diff --git a/Transports.h b/Transports.h index e265f344..c9b882c5 100644 --- a/Transports.h +++ b/Transports.h @@ -61,7 +61,7 @@ namespace transport ~Peer () { for (auto it :delayedMessages) - i2p::DeleteI2NPMessage (it); + i2p::DeleteI2NPMessage (it); } };