diff --git a/source/router/server.cc b/source/router/server.cc index e198f4425..bd9276c9b 100644 --- a/source/router/server.cc +++ b/source/router/server.cc @@ -276,27 +276,46 @@ bool Server::stopSession(Session::SessionId session_id) //-------------------------------------------------------------------------------------------------- void Server::onHostSessionWithId(SessionHost* session) { + if (!session) + { + LOG(LS_ERROR) << "Invalid session pointer"; + return; + } + for (auto it = sessions_.begin(); it != sessions_.end();) { - if (it->get()->sessionType() == proto::ROUTER_SESSION_HOST) + Session* other_session_ptr = it->get(); + + if (!other_session_ptr || other_session_ptr->sessionType() != proto::ROUTER_SESSION_HOST) + { + ++it; + continue; + } + + SessionHost* other_session = reinterpret_cast(other_session_ptr); + if (other_session == session) + { + ++it; + continue; + } + + bool is_found = false; + + for (const auto& host_id : session->hostIdList()) { - SessionHost* other_session = reinterpret_cast(it->get()); - if (other_session != session) + if (other_session->hasHostId(host_id)) { - for (const auto& host_id : session->hostIdList()) - { - if (other_session->hasHostId(host_id)) - { - LOG(LS_INFO) << "Detected previous connection with ID " << host_id; + LOG(LS_INFO) << "Detected previous connection with ID " << host_id; - it = sessions_.erase(it); - continue; - } - } + is_found = true; + break; } } - ++it; + if (is_found) + it = sessions_.erase(it); + else + ++it; } }