From: <tz...@us...> - 2007-07-07 05:54:14
|
Revision: 2128 http://svn.sourceforge.net/freeorion/revision/?rev=2128&view=rev Author: tzlaine Date: 2007-07-06 22:54:17 -0700 (Fri, 06 Jul 2007) Log Message: ----------- Added tons of instrumentation, and implemented and debugged starting a new SP game, and saving a game. Modified Paths: -------------- branches/networking-reimplementation/FreeOrion/UI/InGameMenu.cpp branches/networking-reimplementation/FreeOrion/client/human/HumanClientApp.cpp branches/networking-reimplementation/FreeOrion/client/human/HumanClientApp.h branches/networking-reimplementation/FreeOrion/client/human/chmain.cpp branches/networking-reimplementation/FreeOrion/combat/CombatSystem.cpp branches/networking-reimplementation/FreeOrion/network/ClientNetworking.cpp branches/networking-reimplementation/FreeOrion/network/Message.cpp branches/networking-reimplementation/FreeOrion/network/Message.h branches/networking-reimplementation/FreeOrion/network/ServerNetworking.cpp branches/networking-reimplementation/FreeOrion/network/ServerNetworking.h branches/networking-reimplementation/FreeOrion/server/ServerApp.cpp branches/networking-reimplementation/FreeOrion/server/ServerApp.h branches/networking-reimplementation/FreeOrion/server/ServerFSM.cpp branches/networking-reimplementation/FreeOrion/server/ServerFSM.h branches/networking-reimplementation/FreeOrion/server/dmain.cpp branches/networking-reimplementation/FreeOrion/universe/Universe.cpp Modified: branches/networking-reimplementation/FreeOrion/UI/InGameMenu.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/UI/InGameMenu.cpp 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/UI/InGameMenu.cpp 2007-07-07 05:54:17 UTC (rev 2128) @@ -100,11 +100,8 @@ try { FileDlg dlg(GetOptionsDB().Get<std::string>("save-dir"), "", true, false, save_file_types); dlg.Run(); - std::string filename; if (!dlg.Result().empty()) { - filename = *dlg.Result().begin(); - Message response; - HumanClientApp::GetApp()->Networking().SendSynchronousMessage(HostSaveGameMessage(HumanClientApp::GetApp()->PlayerID(), filename), response); + HumanClientApp::GetApp()->SaveGame(*dlg.Result().begin()); CloseClicked(); } } catch (const FileDlg::BadInitialDirectory& e) { Modified: branches/networking-reimplementation/FreeOrion/client/human/HumanClientApp.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/client/human/HumanClientApp.cpp 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/client/human/HumanClientApp.cpp 2007-07-07 05:54:17 UTC (rev 2128) @@ -94,7 +94,8 @@ false, "freeorion"), m_single_player_game(true), m_game_started(false), - m_turns_since_autosave(0) + m_turns_since_autosave(0), + m_in_save_game_cycle(false) { #ifdef ENABLE_CRASH_BACKTRACE signal(SIGSEGV, SigHandler); @@ -177,6 +178,25 @@ SetPlayerName(""); } +void HumanClientApp::SaveGame(const std::string& filename) +{ + // Note that SendSynchronousMessage() will not work in this situation, since the save game cycle is send request, + // receive request for save game data, then send save game data. Synchronous messages are designed to be send one + // message, immediately get a response back. So, we effectively block here by polling the networking message queue + // until we get the final message (at which point m_in_save_game_cycle == false). + m_in_save_game_cycle = true; + Networking().SendMessage(HostSaveGameMessage(PlayerID(), filename)); + while (m_in_save_game_cycle) { + if (!Networking().Connected()) { + HandleServerDisconnect(); + } else if (Networking().MessageAvailable()) { + Message msg; + Networking().GetMessage(msg); + HandleMessage(msg); + } + } +} + void HumanClientApp::EndGame() { m_game_started = false; @@ -396,14 +416,11 @@ GG::Wnd::SetDefaultBrowseInfoWnd(default_browse_info_wnd); } -void HumanClientApp::HandleSystemEvents(int& last_mouse_event_time) +void HumanClientApp::HandleSystemEvents() { // handle events SDL_Event event; while (0 < FE_PollEvent(&event)) { - if (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP || event.type == SDL_MOUSEMOTION) - last_mouse_event_time = Ticks(); - bool send_to_gg = false; EventType gg_event = MOUSEMOVE; GG::Key key = GGKeyFromSDLKey(event.key.keysym); @@ -447,15 +464,21 @@ HandleGGEvent(gg_event, key, key_mods, mouse_pos, mouse_rel); else HandleNonGGEvent(event); + } - // now check for a single network message - if (!Networking().Connected()) { - HandleServerDisconnect(); - } else if (Networking().MessageAvailable()) { - Message msg; - Networking().GetMessage(msg); - HandleMessage(msg); - } + // now check for a single network message + if (!Networking().Connected()) { + HandleServerDisconnect(); + } else if (Networking().MessageAvailable()) { + Logger().debugStream() << "Message available. Retrieving ..."; + Message msg; + Networking().GetMessage(msg); + Logger().debugStream() << "Message Received. Handling ..."; + HandleMessage(msg); +#if 0 + } else if (!Networking().MessageAvailable()) { + Logger().debugStream() << "No message available ..."; +#endif } } @@ -548,6 +571,7 @@ SaveGameUIData ui_data; ClientUI::GetClientUI()->GetSaveGameUIData(ui_data); Networking().SendMessage(ClientSaveDataMessage(PlayerID(), Orders(), ui_data)); + m_in_save_game_cycle = false; break; } @@ -572,10 +596,12 @@ Autosave(false); +#if 0 // TODO: re-evaluate whether this break is necessary now // if this is the last turn, the TCP message handling inherent in Autosave()'s synchronous message may have // processed an end-of-game message, in which case we need *not* to execute these last two lines below if (!m_game_started || !Networking().Connected()) break; +#endif m_ui->ScreenMap(); m_ui->InitTurn(CurrentTurn()); @@ -732,8 +758,7 @@ fs::remove(save_dir / *rit); } - Message response; - Networking().SendSynchronousMessage(HostSaveGameMessage(PlayerID(), (save_dir / save_filename).native_file_string()), response); + SaveGame((save_dir / save_filename).native_file_string()); } } Modified: branches/networking-reimplementation/FreeOrion/client/human/HumanClientApp.h =================================================================== --- branches/networking-reimplementation/FreeOrion/client/human/HumanClientApp.h 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/client/human/HumanClientApp.h 2007-07-07 05:54:17 UTC (rev 2128) @@ -42,6 +42,7 @@ void FreeServer(); ///< frees (relinquishes ownership and control of) any running server process already started by this client; performs no cleanup of other processes, such as AIs void KillServer(); ///< kills any running server process already started by this client; performs no cleanup of other processes, such as AIs + void SaveGame(const std::string& filename); ///< saves the current game; blocks until all save-related network traffic is resolved. void EndGame(); ///< kills the server (if appropriate) and ends the current game, leaving the application in its start state void SetLobby(MultiplayerLobbyWnd* lobby); ///< registers a lobby dialog so that Messages can reach it; passing 0 unsets the lobby dialog @@ -86,7 +87,7 @@ virtual void GLInit(); virtual void Initialize(); - virtual void HandleSystemEvents(int& last_mouse_event_time); + virtual void HandleSystemEvents(); virtual void HandleNonGGEvent(const SDL_Event& event); virtual void RenderBegin(); @@ -99,12 +100,13 @@ void HandleServerDisconnect(); void Autosave(bool new_game); ///< autosaves the current game, iff autosaves are enabled, and m_turns_since_autosave % autosaves.turns == 0 - Process m_server_process; ///< the server process (when hosting a game or playing single player); will be empty when playing multiplayer as a non-host player - boost::shared_ptr<ClientUI> m_ui; ///< the one and only ClientUI object! - std::string m_save_filename; ///< the name under which the current game has been saved - bool m_single_player_game; ///< true when this game is a single-player game - bool m_game_started; ///< true when a game is currently in progress - int m_turns_since_autosave; ///< the number of turns that have elapsed since the last autosave + Process m_server_process; ///< the server process (when hosting a game or playing single player); will be empty when playing multiplayer as a non-host player + boost::shared_ptr<ClientUI> m_ui; ///< the one and only ClientUI object! + std::string m_save_filename; ///< the name under which the current game has been saved + bool m_single_player_game; ///< true when this game is a single-player game + bool m_game_started; ///< true when a game is currently in progress + int m_turns_since_autosave; ///< the number of turns that have elapsed since the last autosave + bool m_in_save_game_cycle; ///< true during SaveGame()'s send-request, receive-save-game-data-request, send-save-game-data cycle }; #endif // _HumanClientApp_h_ Modified: branches/networking-reimplementation/FreeOrion/client/human/chmain.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/client/human/chmain.cpp 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/client/human/chmain.cpp 2007-07-07 05:54:17 UTC (rev 2128) @@ -1,4 +1,4 @@ -#include "HumanClientAppSoundFMOD.h" +#include "HumanClientApp.h" #include "../../util/OptionsDB.h" #include "../../util/Directories.h" #include "../../util/XMLDoc.h" @@ -60,7 +60,7 @@ return 1; } - HumanClientAppSoundFMOD app; + HumanClientApp app; try { app(); // run app (intialization and main process loop) Modified: branches/networking-reimplementation/FreeOrion/combat/CombatSystem.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/combat/CombatSystem.cpp 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/combat/CombatSystem.cpp 2007-07-07 05:54:17 UTC (rev 2128) @@ -36,7 +36,7 @@ { std::ostringstream stream; doc.WriteDoc(stream); - for (ServerNetworking::const_iterator it = ServerApp::GetApp()->Networking().begin(); it != ServerApp::GetApp()->Networking().end(); ++it) { + for (ServerNetworking::const_iterator it = ServerApp::GetApp()->Networking().established_begin(); it != ServerApp::GetApp()->Networking().established_end(); ++it) { (*it)->SendMessage(Message(msg_type, -1, (*it)->ID(), module, stream.str())); } } Modified: branches/networking-reimplementation/FreeOrion/network/ClientNetworking.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/network/ClientNetworking.cpp 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/network/ClientNetworking.cpp 2007-07-07 05:54:17 UTC (rev 2128) @@ -1,6 +1,7 @@ #include "ClientNetworking.h" #include "Networking.h" +#include "../util/AppInterface.h" #include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/erase.hpp> @@ -14,6 +15,8 @@ using namespace Networking; namespace { + const bool TRACE_EXECUTION = false; + /** A simple client that broadcasts UDP datagrams on the local network for FreeOrion servers, and reports any it finds. */ class ServerDiscoverer @@ -136,6 +139,7 @@ for (tcp::resolver::iterator it = resolver.resolve(query); it != end_it; ++it) { m_socket.close(); boost::asio::deadline_timer timer(m_io_service); + if (TRACE_EXECUTION) Logger().debugStream() << "ClientNetworking::ConnectToServer : attempting to connect to server at " << ip_address; m_socket.async_connect(*it, boost::bind(&ClientNetworking::HandleConnection, this, &it, &timer, @@ -147,6 +151,7 @@ m_io_service.reset(); if (Connected()) { m_socket.set_option(boost::asio::socket_base::linger(true, SOCKET_LINGER_TIME)); + if (TRACE_EXECUTION) Logger().debugStream() << "ClientNetworking::ConnectToServer : starting networking thread"; boost::thread(boost::bind(&ClientNetworking::NetworkingThread, this)); break; } @@ -163,6 +168,7 @@ void ClientNetworking::SendMessage(const Message& message) { assert(Connected()); + if (TRACE_EXECUTION) Logger().debugStream() << "ClientNetworking::SendMessage() : sending message " << message; m_io_service.post(boost::bind(&ClientNetworking::SendMessageImpl, this, message)); } @@ -170,12 +176,15 @@ { assert(MessageAvailable()); m_incoming_messages.PopFront(message); + if (TRACE_EXECUTION) Logger().debugStream() << "ClientNetworking::GetMessage() : received message " << message; } void ClientNetworking::SendSynchronousMessage(const Message& message, Message& response_message) { + if (TRACE_EXECUTION) Logger().debugStream() << "ClientNetworking::SendSynchronousMessage : sending message " << message; SendMessage(message); m_incoming_messages.EraseFirstSynchronousResponse(response_message); // note that this is a blocking operation + if (TRACE_EXECUTION) Logger().debugStream() << "ClientNetworking::SendSynchronousMessage : received response message " << response_message; } void ClientNetworking::HandleConnection(tcp::resolver::iterator* it, boost::asio::deadline_timer* timer, const boost::system::error_code& error) @@ -183,12 +192,14 @@ if (error) { // TODO: Log connection error. if (!m_cancel_retries) { + if (TRACE_EXECUTION) Logger().debugStream() << "ClientNetworking::HandleConnection : connection error ... retrying"; m_socket.async_connect(**it, boost::bind(&ClientNetworking::HandleConnection, this, it, timer, boost::asio::placeholders::error)); } } else { + if (TRACE_EXECUTION) Logger().debugStream() << "ClientNetworking::HandleConnection : connected"; timer->cancel(); boost::mutex::scoped_lock lock(m_mutex); m_connected = true; @@ -203,9 +214,9 @@ if (error.code() == boost::asio::error::eof || error.code() == boost::asio::error::connection_reset || error.code() == boost::asio::error::operation_aborted) { - std::cout << "ClientNetworking::NetworkingThread() : Networking thread will be terminated due to disconnect exception \"" << error.what() << "\"" << std::endl; // TODO: log the closing of this thread + Logger().errorStream() << "ClientNetworking::NetworkingThread() : Networking thread will be terminated due to disconnect exception \"" << error.what() << "\""; } else { - std::cout << "ClientNetworking::NetworkingThread() : Networking thread will be terminated due to unhandled exception \"" << error.what() << "\"" << std::endl; // TODO: log the closing of this thread + Logger().errorStream() << "ClientNetworking::NetworkingThread() : Networking thread will be terminated due to unhandled exception \"" << error.what() << "\""; } } @@ -226,7 +237,7 @@ } catch (const boost::system::system_error& error) { HandleException(error); } - std::cout << "ClientNetworking::NetworkingThread() : Networking thread terminated." << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "ClientNetworking::NetworkingThread() : Networking thread terminated."; m_incoming_messages.Clear(); m_outgoing_messages.clear(); m_io_service.reset(); Modified: branches/networking-reimplementation/FreeOrion/network/Message.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/network/Message.cpp 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/network/Message.cpp 2007-07-07 05:54:17 UTC (rev 2128) @@ -396,9 +396,9 @@ return Message(Message::LOAD_GAME, sender, -1, Message::CORE, filename); } -Message ServerSaveGameMessage(int receiver, bool done/* = false*/) +Message ServerSaveGameMessage(int receiver) { - return Message(Message::SAVE_GAME, -1, receiver, done ? Message::CLIENT_SYNCHRONOUS_RESPONSE : Message::CORE, ""); + return Message(Message::SAVE_GAME, -1, receiver, Message::CORE, ""); } Message ServerLoadGameMessage(int receiver, const OrderSet& orders, const SaveGameUIData* ui_data) Modified: branches/networking-reimplementation/FreeOrion/network/Message.h =================================================================== --- branches/networking-reimplementation/FreeOrion/network/Message.h 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/network/Message.h 2007-07-07 05:54:17 UTC (rev 2128) @@ -209,7 +209,7 @@ Message HostLoadGameMessage(int sender, const std::string& filename); /** creates a SAVE_GAME data request message. This message should only be sent by the server to get game data from a client.*/ -Message ServerSaveGameMessage(int receiver, bool done = false); +Message ServerSaveGameMessage(int receiver); /** creates a LOAD_GAME data message. This message should only be sent by the server to provide saved game data to a client.*/ Message ServerLoadGameMessage(int receiver, const OrderSet& orders, const SaveGameUIData* ui_data); Modified: branches/networking-reimplementation/FreeOrion/network/ServerNetworking.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/network/ServerNetworking.cpp 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/network/ServerNetworking.cpp 2007-07-07 05:54:17 UTC (rev 2128) @@ -1,6 +1,7 @@ #include "ServerNetworking.h" #include "Networking.h" +#include "../util/AppInterface.h" #include <boost/bind.hpp> #include <boost/iterator/filter_iterator.hpp> @@ -10,6 +11,10 @@ using boost::asio::ip::udp; using namespace Networking; +namespace { + const bool TRACE_EXECUTION = false; +} + /** A simple server that listens for FreeOrion-server-discovery UDP datagrams on the local network and sends out responses to them. */ class DiscoveryServer @@ -45,6 +50,13 @@ private: int m_id; }; + + struct PlayerIDLess + { + typedef bool result_type; + bool operator()(const PlayerConnectionPtr& lhs, const PlayerConnectionPtr& rhs) + { return lhs->ID() < rhs->ID(); } + }; } //////////////////////////////////////////////////////////////////////////////// @@ -84,10 +96,14 @@ { AsyncReadMessage(); } void PlayerConnection::SendMessage(const Message& message) -{ WriteMessage(m_socket, message); } +{ + if (TRACE_EXECUTION) Logger().debugStream() << "PlayerConnection(@ " << this << ")::SendMessage(): sending message " << MessageTypeStr(message.Type()) << " " << message.SendingPlayer() << " --> " << message.ReceivingPlayer(); + WriteMessage(m_socket, message); +} void PlayerConnection::EstablishPlayer(int id, const std::string& player_name, bool host) { + if (TRACE_EXECUTION) Logger().debugStream() << "PlayerConnection(@ " << this << ")::EstablishPlayer(" << id << ", " << player_name << ", " << host << ")"; assert(m_ID == INVALID_PLAYER_ID && m_player_name == ""); assert(0 <= id); assert(player_name != ""); @@ -96,10 +112,11 @@ m_host = host; } -PlayerConnectionPtr PlayerConnection::NewConnection(boost::asio::io_service& io_service, - boost::function<void (const Message&, PlayerConnectionPtr)> nonplayer_message_callback, - boost::function<void (const Message&, PlayerConnectionPtr)> player_message_callback, - boost::function<void (PlayerConnectionPtr)> disconnected_callback) +PlayerConnectionPtr +PlayerConnection::NewConnection(boost::asio::io_service& io_service, + boost::function<void (const Message&, PlayerConnectionPtr)> nonplayer_message_callback, + boost::function<void (const Message&, PlayerConnectionPtr)> player_message_callback, + boost::function<void (PlayerConnectionPtr)> disconnected_callback) { return PlayerConnectionPtr(new PlayerConnection(io_service, nonplayer_message_callback, player_message_callback, disconnected_callback)); } void PlayerConnection::HandleMessageBodyRead(boost::system::error_code error, std::size_t bytes_transferred) @@ -109,10 +126,11 @@ error == boost::asio::error::connection_reset) m_disconnected_callback(shared_from_this()); else - std::cout << "PlayerConnection::HandleMessageBodyRead(): error \"" << error << "\"" << std::endl; + Logger().errorStream() << "PlayerConnection::HandleMessageBodyRead(): error \"" << error << "\""; } else { assert(static_cast<int>(bytes_transferred) <= m_incoming_header_buffer[4]); if (static_cast<int>(bytes_transferred) == m_incoming_header_buffer[4]) { + if (TRACE_EXECUTION) Logger().debugStream() << "PlayerConnection::HandleMessageBodyRead(): received message " << m_incoming_message; if (EstablishedPlayer()) m_player_message_callback(m_incoming_message, shared_from_this()); else @@ -129,7 +147,7 @@ error == boost::asio::error::connection_reset) m_disconnected_callback(shared_from_this()); else - std::cout << "PlayerConnection::HandleMessageHeaderRead(): error \"" << error << "\"" << std::endl; + Logger().errorStream() << "PlayerConnection::HandleMessageHeaderRead(): error \"" << error << "\""; } else { assert(static_cast<int>(bytes_transferred) <= HEADER_SIZE); if (static_cast<int>(bytes_transferred) == HEADER_SIZE) { @@ -193,41 +211,59 @@ ServerNetworking::~ServerNetworking() { delete m_discovery_server; } -ServerNetworking::const_iterator ServerNetworking::GetPlayer(int id) const -{ return std::find_if(begin(), end(), PlayerID(id)); } - bool ServerNetworking::empty() const { return m_player_connections.empty(); } std::size_t ServerNetworking::size() const { return m_player_connections.size(); } -ServerNetworking::const_iterator ServerNetworking::begin() const +std::size_t ServerNetworking::NumPlayers() const +{ return std::distance(established_begin(), established_end()); } + +ServerNetworking::const_iterator ServerNetworking::GetPlayer(int id) const +{ return std::find_if(established_begin(), established_end(), PlayerID(id)); } + +ServerNetworking::const_iterator ServerNetworking::established_begin() const { return const_iterator(EstablishedPlayer(), m_player_connections.begin(), m_player_connections.end()); } -ServerNetworking::const_iterator ServerNetworking::end() const +ServerNetworking::const_iterator ServerNetworking::established_end() const { return const_iterator(EstablishedPlayer(), m_player_connections.end(), m_player_connections.end()); } -ServerNetworking::const_reverse_iterator ServerNetworking::rbegin() const +ServerNetworking::const_reverse_iterator ServerNetworking::established_rbegin() const { return const_reverse_iterator(EstablishedPlayer(), m_player_connections.rbegin(), m_player_connections.rend()); } -ServerNetworking::const_reverse_iterator ServerNetworking::rend() const +ServerNetworking::const_reverse_iterator ServerNetworking::established_rend() const { return const_reverse_iterator(EstablishedPlayer(), m_player_connections.rend(), m_player_connections.rend()); } +int ServerNetworking::GreatestPlayerID() const +{ + // Return the max ID, but be careful not to return INVALID_PLAYER_ID -- the implicit max ID when none are defined is + // the predefined host id. + PlayerConnections::const_iterator it = std::max_element(m_player_connections.begin(), m_player_connections.end(), PlayerIDLess()); + int retval = it == m_player_connections.end() ? HOST_PLAYER_ID : (*it)->ID(); + if (retval == PlayerConnection::INVALID_PLAYER_ID) + retval = HOST_PLAYER_ID; + return retval; +} + void ServerNetworking::SendMessage(const Message& message, PlayerConnectionPtr player_connection) -{ player_connection->SendMessage(message); } +{ + if (TRACE_EXECUTION) Logger().debugStream() << "ServerNetworking::SendMessage : sending message " << message; + player_connection->SendMessage(message); +} void ServerNetworking::SendMessage(const Message& message) { iterator it = GetPlayer(message.ReceivingPlayer()); - assert(it != end()); + assert(it != established_end()); + if (TRACE_EXECUTION) Logger().debugStream() << "ServerNetworking::SendMessage : sending message " << message; (*it)->SendMessage(message); } void ServerNetworking::Disconnect(int id) { iterator it = GetPlayer(id); - assert(it != end()); + assert(it != established_end()); Disconnect(*it); } @@ -238,18 +274,18 @@ { std::for_each(m_player_connections.begin(), m_player_connections.end(), boost::bind(&ServerNetworking::DisconnectImpl, this, _1)); } ServerNetworking::iterator ServerNetworking::GetPlayer(int id) -{ return std::find_if(begin(), end(), PlayerID(id)); } +{ return std::find_if(established_begin(), established_end(), PlayerID(id)); } -ServerNetworking::iterator ServerNetworking::begin() +ServerNetworking::iterator ServerNetworking::established_begin() { return iterator(EstablishedPlayer(), m_player_connections.begin(), m_player_connections.end()); } -ServerNetworking::iterator ServerNetworking::end() +ServerNetworking::iterator ServerNetworking::established_end() { return iterator(EstablishedPlayer(), m_player_connections.end(), m_player_connections.end()); } -ServerNetworking::reverse_iterator ServerNetworking::rbegin() +ServerNetworking::reverse_iterator ServerNetworking::established_rbegin() { return reverse_iterator(EstablishedPlayer(), m_player_connections.rbegin(), m_player_connections.rend()); } -ServerNetworking::reverse_iterator ServerNetworking::rend() +ServerNetworking::reverse_iterator ServerNetworking::established_rend() { return reverse_iterator(EstablishedPlayer(), m_player_connections.rend(), m_player_connections.rend()); } void ServerNetworking::Init() @@ -277,6 +313,7 @@ void ServerNetworking::AcceptConnection(PlayerConnectionPtr player_connection, const boost::system::error_code& error) { if (!error) { + if (TRACE_EXECUTION) Logger().debugStream() << "ServerNetworking::AcceptConnection : connected to new player"; m_player_connections.insert(player_connection); player_connection->Start(); AcceptNextConnection(); @@ -287,6 +324,8 @@ void ServerNetworking::DisconnectImpl(PlayerConnectionPtr player_connection) { + if (TRACE_EXECUTION) + Logger().debugStream() << "ServerNetworking::DisconnectImpl : disconnecting player " << player_connection->ID(); m_player_connections.erase(player_connection); m_disconnected_callback(player_connection); } Modified: branches/networking-reimplementation/FreeOrion/network/ServerNetworking.h =================================================================== --- branches/networking-reimplementation/FreeOrion/network/ServerNetworking.h 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/network/ServerNetworking.h 2007-07-07 05:54:17 UTC (rev 2128) @@ -46,13 +46,15 @@ //@} /** \name Accessors */ //@{ - const_iterator GetPlayer(int id) const; ///< Returns an iterator to the established PlayerConnection object with ID \a id, or end() if none is found. bool empty() const; ///< Returns true if size() == 0. std::size_t size() const; ///< Returns the \a total number of PlayerConnections (not just established ones). - const_iterator begin() const; ///< Returns an iterator to the first \a established PlayerConnection object. - const_iterator end() const; ///< Returns an iterator to the one-past-the-last \a established PlayerConnection object. - const_reverse_iterator rbegin() const; ///< Returns a reverse iterator to the first \a established PlayerConnection object. - const_reverse_iterator rend() const; ///< Returns a reverse iterator to the one-past-the-last \a established PlayerConnection object. + std::size_t NumPlayers() const; ///< Returns the number of established-player PlayerConnections. + const_iterator GetPlayer(int id) const; ///< Returns an iterator to the established PlayerConnection object with ID \a id, or end() if none is found. + const_iterator established_begin() const; ///< Returns an iterator to the first \a established PlayerConnection object. + const_iterator established_end() const; ///< Returns an iterator to the one-past-the-last \a established PlayerConnection object. + const_reverse_iterator established_rbegin() const; ///< Returns a reverse iterator to the first \a established PlayerConnection object. + const_reverse_iterator established_rend() const; ///< Returns a reverse iterator to the one-past-the-last \a established PlayerConnection object. + int GreatestPlayerID() const; ///< Returns the highest player ID of all the established players. //@} /** \name Mutators */ //@{ @@ -72,10 +74,10 @@ void DisconnectAll(); iterator GetPlayer(int id); ///< Returns an iterator to the established PlayerConnection object with ID \a id, or end() if none is found. - iterator begin(); ///< Returns an iterator to the first established PlayerConnection object. - iterator end(); ///< Returns an iterator to the one-past-the-last established PlayerConnection object. - reverse_iterator rbegin(); ///< Returns an iterator to the first established PlayerConnection object. - reverse_iterator rend(); ///< Returns an iterator to the one-past-the-last established PlayerConnection object. + iterator established_begin(); ///< Returns an iterator to the first established PlayerConnection object. + iterator established_end(); ///< Returns an iterator to the one-past-the-last established PlayerConnection object. + reverse_iterator established_rbegin(); ///< Returns an iterator to the first established PlayerConnection object. + reverse_iterator established_rend(); ///< Returns an iterator to the one-past-the-last established PlayerConnection object. //@} private: Modified: branches/networking-reimplementation/FreeOrion/server/ServerApp.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/server/ServerApp.cpp 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/server/ServerApp.cpp 2007-07-07 05:54:17 UTC (rev 2128) @@ -70,11 +70,11 @@ // static member(s) ServerApp* ServerApp::s_app = 0; -ServerApp::ServerApp(int argc, char* argv[]) : +ServerApp::ServerApp() : m_current_combat(0), m_networking(m_io_service, + boost::bind(&ServerApp::HandleNonPlayerMessage, this, _1, _2), boost::bind(&ServerApp::HandleMessage, this, _1, _2), - boost::bind(&ServerApp::HandleNonPlayerMessage, this, _1, _2), boost::bind(&ServerApp::PlayerDisconnected, this, _1)), m_log_category(log4cpp::Category::getRoot()), m_fsm(*this), @@ -99,14 +99,12 @@ m_log_category.setAppender(appender); m_log_category.setAdditivity(true); // ...but allow the addition of others later m_log_category.setPriority(PriorityValue(GetOptionsDB().Get<std::string>("log-level"))); - m_log_category.debug("freeoriond logger initialized."); + + m_fsm.initiate(); } ServerApp::~ServerApp() -{ - m_log_category.debug("Shutting down freeoriond logger..."); - log4cpp::Category::shutdown(); -} +{ log4cpp::Category::shutdown(); } void ServerApp::operator()() { @@ -243,7 +241,23 @@ void ServerApp::NewGameInit(boost::shared_ptr<SinglePlayerSetupData> setup_data) { - // TODO + m_current_turn = BEFORE_FIRST_TURN; // every UniverseObject created before game starts will have m_created_on_turn BEFORE_FIRST_TURN + m_universe.CreateUniverse(setup_data->m_size, setup_data->m_shape, setup_data->m_age, setup_data->m_starlane_freq, setup_data->m_planet_density, setup_data->m_specials_freq, + m_networking.NumPlayers() - m_ai_clients.size(), m_ai_clients.size()); + m_current_turn = 1; // after all game initialization stuff has been created, can set current turn to 1 for start of game + + // TODO: here we add empires to turn sequence map -- according to spec this should be done randomly; for now, it's not + for (ServerNetworking::const_iterator it = m_networking.established_begin(); it != m_networking.established_end(); ++it) { + AddEmpireTurn((*it)->ID()); + } + + for (ServerNetworking::const_iterator it = m_networking.established_begin(); it != m_networking.established_end(); ++it) { + int player_id = (*it)->ID(); + int empire_id = GetPlayerEmpire(player_id)->EmpireID(); + (*it)->SendMessage(GameStartMessage(player_id, true, empire_id, m_current_turn, m_empires, m_universe)); + } + + m_losers.clear(); } void ServerApp::LoadGameInit(boost::shared_ptr<SinglePlayerSetupData> setup_data, const std::vector<PlayerSaveGameData>& player_save_game_data) @@ -255,17 +269,17 @@ { m_current_turn = BEFORE_FIRST_TURN; // every UniverseObject created before game starts will have m_created_on_turn BEFORE_FIRST_TURN m_universe.CreateUniverse(lobby_data->m_size, lobby_data->m_shape, lobby_data->m_age, lobby_data->m_starlane_freq, lobby_data->m_planet_density, lobby_data->m_specials_freq, - m_networking.size() - m_ai_clients.size(), m_ai_clients.size(), lobby_data->m_players); + m_networking.NumPlayers() - m_ai_clients.size(), m_ai_clients.size(), lobby_data->m_players); m_current_turn = 1; // after all game initialization stuff has been created, can set current turn to 1 for start of game m_log_category.debugStream() << "ServerApp::GameInit : Created universe " << " (SERVER_GAME_SETUP)."; // add empires to turn sequence map according to spec this should be done randomly for now it's not - for (ServerNetworking::const_iterator it = m_networking.begin(); it != m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = m_networking.established_begin(); it != m_networking.established_end(); ++it) { AddEmpireTurn((*it)->ID()); } // the universe creation caused the creation of empires. But now we need to assign the empires to players. - for (ServerNetworking::const_iterator it = m_networking.begin(); it != m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = m_networking.established_begin(); it != m_networking.established_end(); ++it) { (*it)->SendMessage(GameStartMessage((*it)->ID(), m_single_player_game, (*it)->ID(), m_current_turn, m_empires, m_universe)); } @@ -293,12 +307,12 @@ std::map<int, int> player_to_empire_ids; std::set<int> already_chosen_empire_ids; unsigned int i = 0; - for (ServerNetworking::const_iterator it = m_networking.begin(); it != m_networking.end(); ++it, ++i) { + for (ServerNetworking::const_iterator it = m_networking.established_begin(); it != m_networking.established_end(); ++it, ++i) { player_to_empire_ids[(*it)->ID()] = lobby_data->m_players[i].m_save_game_empire_id; already_chosen_empire_ids.insert(lobby_data->m_players[i].m_save_game_empire_id); } - for (ServerNetworking::const_iterator it = m_networking.begin(); it != m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = m_networking.established_begin(); it != m_networking.established_end(); ++it) { const int INVALID_EMPIRE_ID = -5000; int empire_id = INVALID_EMPIRE_ID; if (player_to_empire_ids[(*it)->ID()] != -1) { @@ -325,7 +339,7 @@ // the universe is loaded. That means we must do it here. m_universe.RebuildEmpireViewSystemGraphs(); - for (ServerNetworking::const_iterator it = m_networking.begin(); it != m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = m_networking.established_begin(); it != m_networking.established_end(); ++it) { int empire_id = player_to_empire_ids[(*it)->ID()]; (*it)->SendMessage(GameStartMessage((*it)->ID(), m_single_player_game, empire_id, m_current_turn, m_empires, m_universe)); (*it)->SendMessage(ServerLoadGameMessage((*it)->ID(), *player_data_by_empire[empire_id].m_orders, player_data_by_empire[empire_id].m_ui_data.get())); @@ -338,7 +352,7 @@ { Empire* retval = 0; ServerNetworking::const_iterator player_it = m_networking.GetPlayer(player_id); - if (player_it != m_networking.end()) { + if (player_it != m_networking.established_end()) { std::string player_name = (*player_it)->PlayerName(); for (EmpireManager::const_iterator it = Empires().begin(); it != Empires().end(); ++it) { if (it->second->PlayerName() == player_name) { @@ -354,7 +368,7 @@ { int retval = -1; std::string player_name = Empires().Lookup(empire_id)->PlayerName(); - for (ServerNetworking::const_iterator it = m_networking.begin(); it != m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = m_networking.established_begin(); it != m_networking.established_end(); ++it) { if ((*it)->PlayerName() == player_name) { retval = (*it)->ID(); break; @@ -400,7 +414,7 @@ for (std::map<int, OrderSet*>::iterator it = m_turn_sequence.begin(); it != m_turn_sequence.end(); ++it) { // broadcast UI message to all players - for (ServerNetworking::const_iterator player_it = m_networking.begin(); player_it != m_networking.end(); ++player_it) { + for (ServerNetworking::const_iterator player_it = m_networking.established_begin(); player_it != m_networking.established_end(); ++player_it) { (*player_it)->SendMessage(TurnProgressMessage((*player_it)->ID(), Message::PROCESSING_ORDERS, it->first)); } @@ -499,7 +513,7 @@ } // process movement phase - for (ServerNetworking::const_iterator player_it = m_networking.begin(); player_it != m_networking.end(); ++player_it) { + for (ServerNetworking::const_iterator player_it = m_networking.established_begin(); player_it != m_networking.established_end(); ++player_it) { (*player_it)->SendMessage(TurnProgressMessage((*player_it)->ID(), Message::FLEET_MOVEMENT, -1)); } @@ -536,7 +550,7 @@ } // check for combats, and resolve them. - for (ServerNetworking::const_iterator player_it = m_networking.begin(); player_it != m_networking.end(); ++player_it) { + for (ServerNetworking::const_iterator player_it = m_networking.established_begin(); player_it != m_networking.established_end(); ++player_it) { (*player_it)->SendMessage(TurnProgressMessage((*player_it)->ID(), Message::COMBAT, -1)); } @@ -601,7 +615,7 @@ SDL_Delay(1500); // TODO: Put this delay client-side. // process production and growth phase - for (ServerNetworking::const_iterator player_it = m_networking.begin(); player_it != m_networking.end(); ++player_it) { + for (ServerNetworking::const_iterator player_it = m_networking.established_begin(); player_it != m_networking.established_end(); ++player_it) { (*player_it)->SendMessage(TurnProgressMessage((*player_it)->ID(), Message::EMPIRE_PRODUCTION, -1)); } @@ -649,7 +663,7 @@ ++m_current_turn; // indicate that the clients are waiting for their new Universes - for (ServerNetworking::const_iterator player_it = m_networking.begin(); player_it != m_networking.end(); ++player_it) { + for (ServerNetworking::const_iterator player_it = m_networking.established_begin(); player_it != m_networking.established_end(); ++player_it) { (*player_it)->SendMessage(TurnProgressMessage((*player_it)->ID(), Message::DOWNLOADING, -1)); } @@ -658,7 +672,7 @@ for (EmpireManager::const_iterator it = Empires().begin(); it != Empires().end(); ++it) { if (GetUniverse().FindObjects(OwnedVisitor<UniverseObject>(it->first)).empty()) { // when you're out of planets, your game is over std::string player_name = it->second->PlayerName(); - for (ServerNetworking::const_iterator player_it = m_networking.begin(); player_it != m_networking.end(); ++player_it) { + for (ServerNetworking::const_iterator player_it = m_networking.established_begin(); player_it != m_networking.established_end(); ++player_it) { if ((*player_it)->PlayerName() == player_name) { // record this player/empire so we can send out messages about it eliminations[(*player_it)->ID()] = it->first; @@ -677,14 +691,14 @@ } // send new-turn updates to all players - for (ServerNetworking::const_iterator player_it = m_networking.begin(); player_it != m_networking.end(); ++player_it) { + for (ServerNetworking::const_iterator player_it = m_networking.established_begin(); player_it != m_networking.established_end(); ++player_it) { pEmpire = GetPlayerEmpire((*player_it)->ID()); (*player_it)->SendMessage(TurnUpdateMessage((*player_it)->ID(), pEmpire->EmpireID(), m_current_turn, m_empires, m_universe)); } // notify all players of the eliminated players for (std::map<int, int>::iterator it = eliminations.begin(); it != eliminations.end(); ++it) { - for (ServerNetworking::const_iterator player_it = m_networking.begin(); player_it != m_networking.end(); ++player_it) { + for (ServerNetworking::const_iterator player_it = m_networking.established_begin(); player_it != m_networking.established_end(); ++player_it) { (*player_it)->SendMessage(PlayerEliminatedMessage((*player_it)->ID(), Empires().Lookup(it->second)->Name())); } } @@ -700,15 +714,15 @@ } // determine if victory conditions exist - if (m_networking.size() == 1) { // if there is only one player left, that player is the winner + if (m_networking.NumPlayers() == 1) { // if there is only one player left, that player is the winner m_log_category.debugStream() << "ServerApp::ProcessTurns : One player left -- sending victory notification and terminating."; - while (m_networking.size() == 1) { - m_networking.SendMessage(VictoryMessage((*m_networking.begin())->ID())); + while (m_networking.NumPlayers() == 1) { + m_networking.SendMessage(VictoryMessage((*m_networking.established_begin())->ID())); SDL_Delay(100); // TODO: It should be possible to eliminate this by using linger. } m_networking.DisconnectAll(); Exit(0); - } else if (m_ai_IDs.size() == m_networking.size()) { // if there are none but AI players left, we're done + } else if (m_ai_IDs.size() == m_networking.NumPlayers()) { // if there are none but AI players left, we're done m_log_category.debugStream() << "ServerApp::ProcessTurns : No human players left -- server terminating."; m_networking.DisconnectAll(); Exit(0); Modified: branches/networking-reimplementation/FreeOrion/server/ServerApp.h =================================================================== --- branches/networking-reimplementation/FreeOrion/server/ServerApp.h 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/server/ServerApp.h 2007-07-07 05:54:17 UTC (rev 2128) @@ -47,7 +47,7 @@ { public: /** \name Structors */ //@{ - ServerApp(int argc, char* argv[]); + ServerApp(); ~ServerApp(); //@} Modified: branches/networking-reimplementation/FreeOrion/server/ServerFSM.cpp =================================================================== --- branches/networking-reimplementation/FreeOrion/server/ServerFSM.cpp 2007-07-06 05:31:17 UTC (rev 2127) +++ branches/networking-reimplementation/FreeOrion/server/ServerFSM.cpp 2007-07-07 05:54:17 UTC (rev 2128) @@ -14,6 +14,8 @@ namespace { + const bool TRACE_EXECUTION = false; + void RebuildSaveGameEmpireData(std::vector<SaveGameEmpireData>& save_game_empire_data, const std::string& save_game_filename) { save_game_empire_data.clear(); @@ -73,7 +75,7 @@ if (player_connection->Host()) { // if the host dies, there's really nothing else we can do -- the game's over std::string message = player_connection->PlayerName(); - for (ServerNetworking::const_iterator it = m_server.m_networking.begin(); it != m_server.m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = m_server.m_networking.established_begin(); it != m_server.m_networking.established_end(); ++it) { if ((*it)->ID() != id) { (*it)->SendMessage(PlayerDisconnectedMessage((*it)->ID(), message)); (*it)->SendMessage(EndGameMessage(-1, (*it)->ID())); @@ -86,7 +88,7 @@ Logger().debugStream() << "ServerFSM::HandleNonLobbyDisconnection : Lost connection to player #" << boost::lexical_cast<std::string>(id) << ", named \"" << player_connection->PlayerName() << "\"; server terminating."; std::string message = player_connection->PlayerName(); - for (ServerNetworking::const_iterator it = m_server.m_networking.begin(); it != m_server.m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = m_server.m_networking.established_begin(); it != m_server.m_networking.established_end(); ++it) { if ((*it)->ID() != id) { (*it)->SendMessage(PlayerDisconnectedMessage((*it)->ID(), message)); // in the future we may find a way to recover from this, but for now we will immediately send a game ending message as well @@ -107,15 +109,16 @@ //////////////////////////////////////////////////////////// // Idle //////////////////////////////////////////////////////////// -Idle::Idle() -{ std::cout << "Idle" << std::endl; } +Idle::Idle() : + Base() +{ if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) Idle"; } Idle::~Idle() -{ std::cout << "~Idle" << std::endl; } +{ if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) ~Idle"; } boost::statechart::result Idle::react(const HostMPGame& msg) { - std::cout << "Idle.HostMPGame" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) Idle.HostMPGame"; ServerApp& server = context<ServerFSM>().Server(); const Message& message = msg.m_message; PlayerConnectionPtr& player_connection = msg.m_player_connection; @@ -132,12 +135,12 @@ boost::statechart::result Idle::react(const HostSPGame& msg) { - std::cout << "Idle.HostSPGame" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) Idle.HostSPGame"; ServerApp& server = context<ServerFSM>().Server(); const Message& message = msg.m_message; PlayerConnectionPtr& player_connection = msg.m_player_connection; - boost::shared_ptr<SinglePlayerSetupData> setup_data; + boost::shared_ptr<SinglePlayerSetupData> setup_data(new SinglePlayerSetupData); ExtractMessageData(message, *setup_data); int player_id = Networking::HOST_PLAYER_ID; @@ -154,10 +157,11 @@ //////////////////////////////////////////////////////////// // MPLobby //////////////////////////////////////////////////////////// -MPLobby::MPLobby() : +MPLobby::MPLobby(my_context c) : + Base(c), m_lobby_data (new MultiplayerLobbyData(true)) { - std::cout << "MPLobby" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) MPLobby"; ServerApp& server = context<ServerFSM>().Server(); int player_id = Networking::HOST_PLAYER_ID; const PlayerConnectionPtr& player_connection = *server.m_networking.GetPlayer(player_id); @@ -169,18 +173,18 @@ } MPLobby::~MPLobby() -{ std::cout << "~MPLobby" << std::endl; } +{ if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) ~MPLobby"; } boost::statechart::result MPLobby::react(const Disconnection& d) { - std::cout << "MPLobby.Disconnection" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) MPLobby.Disconnection"; ServerApp& server = context<ServerFSM>().Server(); PlayerConnectionPtr& player_connection = d.m_player_connection; int id = player_connection->ID(); // this will not usually happen, since the host process usually owns the server process, and will usually take it down if it fails if (player_connection->Host()) { - for (ServerNetworking::const_iterator it = server.m_networking.begin(); it != server.m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ++it) { if ((*it)->ID() != id) { (*it)->SendMessage(ServerLobbyHostAbortMessage((*it)->ID())); server.m_networking.Disconnect((*it)->ID()); @@ -192,7 +196,7 @@ server.Exit(1); } else { unsigned int i = 0; - for (ServerNetworking::const_iterator it = server.m_networking.begin(); it != server.m_networking.end(); ++it, ++i) { + for (ServerNetworking::const_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ++it, ++i) { if ((*it)->ID() == id) { if (i < m_lobby_data->m_players.size()) m_lobby_data->m_players.erase(m_lobby_data->m_players.begin() + i); // remove the exiting player's PlayerSetupData struct @@ -215,20 +219,20 @@ boost::statechart::result MPLobby::react(const JoinGame& msg) { - std::cout << "MPLobby.JoinGame" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) MPLobby.JoinGame"; ServerApp& server = context<ServerFSM>().Server(); const Message& message = msg.m_message; PlayerConnectionPtr& player_connection = msg.m_player_connection; std::string player_name = message.Text(); - int player_id = (*server.m_networking.rbegin())->ID() + 1; + int player_id = server.m_networking.GreatestPlayerID() + 1; player_connection->EstablishPlayer(player_id, player_name, false); player_connection->SendMessage(JoinAckMessage(player_id)); m_lobby_data->m_players.push_back(PlayerSetupData()); m_lobby_data->m_players.back().m_player_id = player_id; m_lobby_data->m_players.back().m_player_name = player_name; m_lobby_data->m_players.back().m_empire_color = EmpireColors().at(0); - for (ServerNetworking::const_iterator it = server.m_networking.begin(); it != server.m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ++it) { (*it)->SendMessage(ServerLobbyUpdateMessage((*it)->ID(), *m_lobby_data)); } @@ -237,11 +241,11 @@ boost::statechart::result MPLobby::react(const LobbyUpdate& msg) { - std::cout << "MPLobby.LobbyUpdate" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) MPLobby.LobbyUpdate"; ServerApp& server = context<ServerFSM>().Server(); const Message& message = msg.m_message; - MultiplayerLobbyData incoming_lobby_data; + MultiplayerLobbyData incoming_lobby_data(new MultiplayerLobbyData); ExtractMessageData(message, incoming_lobby_data); // NOTE: The client is only allowed to update certain of these, so those are the only ones we'll copy into m_lobby_data. @@ -266,7 +270,7 @@ } m_lobby_data->m_players = incoming_lobby_data.m_players; - for (ServerNetworking::const_iterator it = server.m_networking.begin(); it != server.m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ++it) { if ((*it)->ID() != message.SendingPlayer() || new_save_file_selected) (*it)->SendMessage(ServerLobbyUpdateMessage((*it)->ID(), *m_lobby_data)); } @@ -276,12 +280,12 @@ boost::statechart::result MPLobby::react(const LobbyChat& msg) { - std::cout << "MPLobby.LobbyChat" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) MPLobby.LobbyChat"; ServerApp& server = context<ServerFSM>().Server(); const Message& message = msg.m_message; if (message.ReceivingPlayer() == -1) { // the receiver is everyone (except the sender) - for (ServerNetworking::const_iterator it = server.m_networking.begin(); it != server.m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ++it) { if ((*it)->ID() != message.SendingPlayer()) (*it)->SendMessage(ServerLobbyChatMessage(message.SendingPlayer(), (*it)->ID(), message.Text())); } @@ -294,11 +298,11 @@ boost::statechart::result MPLobby::react(const LobbyHostAbort& msg) { - std::cout << "MPLobby.LobbyHostAbort" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) MPLobby.LobbyHostAbort"; ServerApp& server = context<ServerFSM>().Server(); const Message& message = msg.m_message; - for (ServerNetworking::const_iterator it = server.m_networking.begin(); it != server.m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ++it) { if ((*it)->ID() != message.SendingPlayer()) { (*it)->SendMessage(ServerLobbyHostAbortMessage((*it)->ID())); server.m_networking.Disconnect((*it)->ID()); @@ -310,12 +314,12 @@ boost::statechart::result MPLobby::react(const LobbyNonHostExit& msg) { - std::cout << "MPLobby.LobbyNonHostExit" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) MPLobby.LobbyNonHostExit"; ServerApp& server = context<ServerFSM>().Server(); const Message& message = msg.m_message; unsigned int i = 0; - for (ServerNetworking::const_iterator it = server.m_networking.begin(); it != server.m_networking.end(); ++it, ++i) { + for (ServerNetworking::const_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ++it, ++i) { if ((*it)->ID() == message.SendingPlayer()) { if (i < m_lobby_data->m_players.size()) m_lobby_data->m_players.erase(m_lobby_data->m_players.begin() + i); // remove the exiting player's PlayerSetupData struct @@ -330,7 +334,7 @@ boost::statechart::result MPLobby::react(const StartMPGame& msg) { - std::cout << "MPLobby.StartMPGame" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) MPLobby.StartMPGame"; ServerApp& server = context<ServerFSM>().Server(); const Message& message = msg.m_message; PlayerConnectionPtr& player_connection = msg.m_player_connection; @@ -338,7 +342,7 @@ if (player_connection->Host()) { if (m_lobby_data->m_new_game) { int expected_players = server.m_networking.size() + m_lobby_data->m_AIs.size(); - for (ServerNetworking::const_iterator it = server.m_networking.begin(); it != server.m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ++it) { if ((*it)->ID() != Networking::HOST_PLAYER_ID) (*it)->SendMessage(Message(Message::GAME_START, -1, (*it)->ID(), Message::CLIENT_LOBBY_MODULE, "")); } @@ -355,7 +359,7 @@ assert(m_player_save_game_data[i].m_empire); if (m_lobby_data->m_players[j].m_save_game_empire_id == m_player_save_game_data[i].m_empire->EmpireID()) { ServerNetworking::const_iterator player_it = server.m_networking.GetPlayer(m_lobby_data->m_players[j].m_player_id); - assert(player_it != server.m_networking.end()); + assert(player_it != server.m_networking.established_end()); m_player_save_game_data[i].m_name = (*player_it)->PlayerName(); m_player_save_game_data[i].m_empire->SetPlayerName((*player_it)->PlayerName()); } @@ -363,7 +367,7 @@ Empires().InsertEmpire(m_player_save_game_data[i].m_empire); } - for (ServerNetworking::const_iterator it = server.m_networking.begin(); it != server.m_networking.end(); ++it) { + for (ServerNetworking::const_iterator it = server.m_networking.established_begin(); it != server.m_networking.established_end(); ++it) { if ((*it)->ID() != player_connection->ID()) (*it)->SendMessage(Message(Message::GAME_START, -1, (*it)->ID(), Message::CLIENT_LOBBY_MODULE, "")); } @@ -375,7 +379,7 @@ } } } else { - Logger().errorStream() << "MPLobby.StartMPGame : Player #" << message.SendingPlayer() + Logger().errorStream() << "(ServerFSM) MPLobby.StartMPGame : Player #" << message.SendingPlayer() << " attempted to initiate a game load, but is not the host. Terminating connection."; server.m_networking.Disconnect(player_connection); return discard_event(); @@ -391,11 +395,12 @@ //////////////////////////////////////////////////////////// // WaitingForSPGameJoiners //////////////////////////////////////////////////////////// -WaitingForSPGameJoiners::WaitingForSPGameJoiners() : +WaitingForSPGameJoiners::WaitingForSPGameJoiners(my_context c) : + Base(c), m_setup_data(context<ServerFSM>().m_setup_data), m_num_expected_players(0) { - std::cout << "WaitingForSPGameJoiners" << std::endl; + if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) WaitingForSPGameJoiners"; context<ServerFSM>().m_setup_data.reset(); ServerApp& server = context<ServerFSM>().Server(); @@ -411,17 +416,17 @@ } WaitingForSPGameJoiners::~WaitingForSPGameJoiners() -{ std::cout << "~WaitingForSPGameJoiners" << std::endl; } +{ if (TRACE_EXECUTION) Logger().debugStream() << "(ServerFSM) ~WaitingForSPGameJoiners"; } boost::statechart::result WaitingForSPGameJoiners::react(const JoinGame& msg) { - std::cout << "WaitingForSPGameJoiners.JoinGame" << std::endl; + if (T... [truncated message content] |