From a9a36a3bae3292429c42567c3551e94fcce77897 Mon Sep 17 00:00:00 2001 From: freeeyes Date: Fri, 22 Mar 2024 09:14:54 +0800 Subject: [PATCH] Fix deadlock problem. --- PSS_ASIO/UdpSession/UdpServer.cpp | 25 ++++++++++++++++++++----- PSS_ASIO/UdpSession/UdpServer.h | 9 +++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/PSS_ASIO/UdpSession/UdpServer.cpp b/PSS_ASIO/UdpSession/UdpServer.cpp index dcd3175..74b9688 100644 --- a/PSS_ASIO/UdpSession/UdpServer.cpp +++ b/PSS_ASIO/UdpSession/UdpServer.cpp @@ -211,11 +211,15 @@ void CUdpServer::close() void CUdpServer::close_server() { - std::lock_guard lock(udp_session_mutex_); + vector udp_session_ip_info_list; + CUdp_Session_IP_Info udp_session_ip_info; + udp_session_mutex_.lock(); if (!socket_.is_open()) { + udp_session_mutex_.unlock(); return; } + //释放所有udp资源 for (const auto& session_info : udp_id_2_endpoint_list_) { @@ -228,15 +232,26 @@ void CUdpServer::close_server() remote_ip_info.m_strClientIP = session_info.second->send_endpoint.address().to_string(); remote_ip_info.m_u2Port = session_info.second->send_endpoint.port(); - App_WorkThreadLogic::instance()->delete_thread_session(connect_id, - shared_from_this(), - remote_ip_info, - io_type_); + //插入列表 + udp_session_ip_info.connect_id_ = connect_id; + udp_session_ip_info.remote_ip_info_ = remote_ip_info; + udp_session_ip_info_list.emplace_back(udp_session_ip_info); } udp_id_2_endpoint_list_.clear(); udp_endpoint_2_id_list_.clear(); socket_.close(); + udp_session_mutex_.unlock(); + + //发送断开消息 + for (const auto& udp_session_ip_info_cell : udp_session_ip_info_list) + { + App_WorkThreadLogic::instance()->delete_thread_session(udp_session_ip_info_cell.connect_id_, + shared_from_this(), + udp_session_ip_info_cell.remote_ip_info_, + io_type_); + } + PSS_LOGGER_DEBUG("[CUdpServer::close_server]close [{0}:{1}]", server_ip_, server_port_); } diff --git a/PSS_ASIO/UdpSession/UdpServer.h b/PSS_ASIO/UdpSession/UdpServer.h index f47dcc6..e5b22c5 100644 --- a/PSS_ASIO/UdpSession/UdpServer.h +++ b/PSS_ASIO/UdpSession/UdpServer.h @@ -31,6 +31,15 @@ class CUdp_Session_Connect_Id bool is_new_ = true; }; + +//用于关闭所有的链接 +class CUdp_Session_IP_Info +{ +public: + uint32 connect_id_ = 0; + _ClientIPInfo remote_ip_info_; +}; + class CUdp_Session_Info { public: