From 171848adce214afda9224b96d4d7cb1a095e92cd Mon Sep 17 00:00:00 2001 From: Dujeong Lee Date: Fri, 14 Jul 2017 23:23:23 +0900 Subject: [PATCH] Disconnection bug fix --- tx.cpp | 53 +++++++++++++++++++++++++++-------------------------- tx.h | 2 +- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/tx.cpp b/tx.cpp index ce36c14..5192f70 100755 --- a/tx.cpp +++ b/tx.cpp @@ -336,11 +336,8 @@ void TransmissionSession::SendPing() if(TimeSinceLastPongTime.count() > Parameter::CONNECTION_TIMEOUT) { TransmissionSession const * self = this; - std::cout<<"Pong Timeout...Client is disconnected.["<c_Transmission->Disconnect(self->c_IPv4, self->c_Port); - }); - DisconnectThread.detach(); + std::cout<<"Client is disconnected. No response for "<c_Transmission->Disconnect(self->c_Addr); return; } @@ -562,32 +559,36 @@ void Transmission::WaitUntilTxIsCompleted(const DataStructures::AddressType Addr } } -bool Transmission::Disconnect(const DataStructures::AddressType Addr) +void Transmission::Disconnect(const DataStructures::AddressType Addr) { - const DataStructures::SessionKey key = DataStructures::GetSessionKey((sockaddr*)&Addr.Addr, Addr.AddrLength); - TransmissionSession** pp_session = nullptr; - { - std::unique_lock< std::mutex > lock(m_Lock); - - pp_session = m_Sessions.GetPtr(key); - if(pp_session == nullptr) + Transmission* const self = this; + std::thread DisconnectThread = std::thread([self, Addr](){ + const DataStructures::SessionKey key = DataStructures::GetSessionKey((sockaddr*)&Addr.Addr, Addr.AddrLength); + TransmissionSession** pp_session = nullptr; { - return false; + std::unique_lock< std::mutex > lock(self->m_Lock); + + pp_session = self->m_Sessions.GetPtr(key); + if(pp_session == nullptr) + { + return false; + } } - } - do - { - (*pp_session)->m_IsConnected = false; - for(auto i = 0 ; i < Parameter::MAXIMUM_NUMBER_OF_CONCURRENT_RETRANSMISSION*2 ; i++) + do { - (*pp_session)->m_AckList[i] = true; - } - }while(0 < (*pp_session)->m_ConcurrentRetransmissions); - m_Sessions.Remove(key, [](TransmissionSession*&session){ - session->m_Timer.Stop(); - delete session; + (*pp_session)->m_IsConnected = false; + for(auto i = 0 ; i < Parameter::MAXIMUM_NUMBER_OF_CONCURRENT_RETRANSMISSION*2 ; i++) + { + (*pp_session)->m_AckList[i] = true; + } + }while(0 < (*pp_session)->m_ConcurrentRetransmissions); + self->m_Sessions.Remove(key, [](TransmissionSession*&session){ + session->m_Timer.Stop(); + delete session; + }); + return true; }); - return true; + DisconnectThread.detach(); } /* OK */ diff --git a/tx.h b/tx.h index 3c42554..11c4d63 100755 --- a/tx.h +++ b/tx.h @@ -101,7 +101,7 @@ class Transmission bool Send(const DataStructures::AddressType Addr, uint8_t* buffer, uint16_t buffersize/*, bool reqack*/); bool Flush(const DataStructures::AddressType Addr); void WaitUntilTxIsCompleted(const DataStructures::AddressType Addr); - bool Disconnect(const DataStructures::AddressType Addr); + void Disconnect(const DataStructures::AddressType Addr); public: void RxHandler(uint8_t* buffer, uint16_t size, const sockaddr* const sender_addr, const uint32_t sender_addr_len); };