From: <tz...@us...> - 2007-07-20 18:50:50
|
Revision: 2180 http://svn.sourceforge.net/freeorion/revision/?rev=2180&view=rev Author: tzlaine Date: 2007-07-20 11:50:53 -0700 (Fri, 20 Jul 2007) Log Message: ----------- Addressed TODOs in the server and server's networking code about possible iterator invalidation in certain loops. The invalidation was real, and has been corrected. Modified Paths: -------------- branches/networking-reimplementation/FreeOrion/network/ServerNetworking.cpp branches/networking-reimplementation/FreeOrion/server/ServerFSM.cpp Modified: branches/networking-reimplementation/FreeOrion/network/ServerNetworking.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/network/ServerNetworking.cpp 2007-07-20 18:23:50 UTC (rev 2179) +++ branches/networking-reimplementation/FreeOrion/network/ServerNetworking.cpp 2007-07-20 18:50:53 UTC (rev 2180) @@ -279,9 +279,13 @@ void ServerNetworking::Disconnect(PlayerConnectionPtr player_connection) { DisconnectImpl(player_connection); } -// TODO: Are these iterators invalidated in this loop? Why does this work? void ServerNetworking::DisconnectAll() -{ std::for_each(m_player_connections.begin(), m_player_connections.end(), boost::bind(&ServerNetworking::DisconnectImpl, this, _1)); } +{ + for (const_iterator it = m_player_connections.begin(); it != m_player_connections.end(); ) { + PlayerConnectionPtr player_connection = *it++; + DisconnectImpl(player_connection); + } +} ServerNetworking::iterator ServerNetworking::begin() { return m_player_connections.begin(); } Modified: branches/networking-reimplementation/FreeOrion/server/ServerFSM.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/server/ServerFSM.cpp 2007-07-20 18:23:50 UTC (rev 2179) +++ branches/networking-reimplementation/FreeOrion/server/ServerFSM.cpp 2007-07-20 18:50:53 UTC (rev 2180) @@ -310,11 +310,10 @@ SDL_Delay(1000); // HACK! Add a delay here so the messages can propagate; setting socket linger does not appear to work - // TODO: Are these iterators invalidated in this loop? Why does this work? - for (ServerNetworking::const_established_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ++it) { - if ((*it)->ID() != message.SendingPlayer()) { - server.m_networking.Disconnect((*it)->ID()); - } + for (ServerNetworking::const_established_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ) { + PlayerConnectionPtr player_connection = *it++; + if (player_connection->ID() != message.SendingPlayer()) + server.m_networking.Disconnect(player_connection); } return discard_event(); |