From 1023e6580ef6493c7691d2a88cbd3476b2bf8049 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 23 Feb 2018 14:01:20 +0800 Subject: [PATCH] Unregister STK server when exiting --- src/main.cpp | 3 - src/network/network_config.cpp | 13 --- src/network/network_config.hpp | 35 -------- src/network/network_console.cpp | 9 --- src/network/protocols/connect_to_peer.cpp | 1 - src/network/protocols/connect_to_server.cpp | 2 +- src/network/protocols/server_lobby.cpp | 58 +++++++++++++- src/network/protocols/server_lobby.hpp | 4 + src/network/protocols/stop_server.cpp | 89 --------------------- src/network/protocols/stop_server.hpp | 36 --------- src/network/servers_manager.cpp | 3 - src/network/stk_host.cpp | 1 - src/network/stk_host.hpp | 16 ---- src/states_screens/create_server_screen.cpp | 3 +- 14 files changed, 61 insertions(+), 212 deletions(-) delete mode 100644 src/network/protocols/stop_server.cpp delete mode 100644 src/network/protocols/stop_server.hpp diff --git a/src/main.cpp b/src/main.cpp index 03725aee1..8fd337154 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1003,11 +1003,8 @@ int handleCmdLine() if (CommandLine::has("--connect-now", &s)) { TransportAddress ip(s); - TransportAddress me(2130706433/*127.0.0.1*/, - NetworkConfig::get()->getServerDiscoveryPort() ); NetworkConfig::get()->setIsLAN(); NetworkConfig::get()->setIsServer(false); - NetworkConfig::get()->setMyAddress(me); Log::info("main", "Try to connect to server '%s'.", ip.toString().c_str() ); irr::core::stringw name = StringUtils::utf8ToWide(ip.toString()); diff --git a/src/network/network_config.cpp b/src/network/network_config.cpp index fb2b2f8e1..87fb75a91 100644 --- a/src/network/network_config.cpp +++ b/src/network/network_config.cpp @@ -46,21 +46,8 @@ NetworkConfig::NetworkConfig() m_server_discovery_port = 2757; m_server_port = 2758; m_client_port = 2759; - m_my_address.lock(); - m_my_address.getData().clear(); - m_my_address.unlock(); } // NetworkConfig -//----------------------------------------------------------------------------- -/** Stores the public address of this host. - */ -void NetworkConfig::setMyAddress(const TransportAddress& addr) -{ - m_my_address.lock(); - m_my_address.getData().copy(addr); - m_my_address.unlock(); -} // setPublicAddress - // -------------------------------------------------------------------- /** Sets if this instance is a server or client. It also assigns the * private port depending if this is a server or client. diff --git a/src/network/network_config.hpp b/src/network/network_config.hpp index 44b406a1e..80e2d83a7 100644 --- a/src/network/network_config.hpp +++ b/src/network/network_config.hpp @@ -52,11 +52,6 @@ private: /** The password for a server (or to authenticate to a server). */ std::string m_password; - /** This is either this computer's public IP address, or the LAN - * address in case of a LAN game. With lock since it can - * be updated from a separate thread. */ - Synchronised m_my_address; - /** The port number to which the server listens to detect LAN requests. */ uint16_t m_server_discovery_port; @@ -103,7 +98,6 @@ public: } // destroy // ------------------------------------------------------------------------ - void setMyAddress(const TransportAddress& addr); void setIsServer(bool b); // ------------------------------------------------------------------------ /** Sets the port for server discovery. */ @@ -183,35 +177,6 @@ public: return m_server_name; } // getServerName // -------------------------------------------------------------------- - /** Sets if this server is registered with the stk server. */ - void setRegistered(bool registered) - { - assert(isServer()); - m_is_registered = registered; - } // setRegistered - // -------------------------------------------------------------------- - /** Returns if this server is registered with the stk server. */ - bool isRegistered() const - { - assert(isServer()); - return m_is_registered; - } // isRegistered - - // -------------------------------------------------------------------- - /** Returns the IP address of this host. We need to return a copy - * to make sure the address is thread safe (otherwise it could happen - * that e.g. data is taken when the IP address was written, but not - return a; - * yet the port). */ - const TransportAddress getMyAddress() const - { - TransportAddress a; - m_my_address.lock(); - a.copy(m_my_address.getData()); - m_my_address.unlock(); - return a; - } // getMyAddress - // -------------------------------------------------------------------- /** Sets if a client should immediately connect to the first server. */ void setAutoConnect(bool b) { m_auto_connect = b; } // -------------------------------------------------------------------- diff --git a/src/network/network_console.cpp b/src/network/network_console.cpp index 3b419766a..66bf830b1 100644 --- a/src/network/network_console.cpp +++ b/src/network/network_console.cpp @@ -24,7 +24,6 @@ #include "network/stk_host.hpp" #include "network/protocols/client_lobby.hpp" #include "network/protocols/server_lobby.hpp" -#include "network/protocols/stop_server.hpp" #include "network/stk_peer.hpp" #include "utils/log.hpp" #include "utils/time.hpp" @@ -157,14 +156,6 @@ void* NetworkConsole::mainLoop(void* data) Log::info("Console", "Unknown command '%s'.", str.c_str()); } } // while !stop - - auto p = std::make_shared(); - p->requestStart(); - while(p->getState() != PROTOCOL_STATE_TERMINATED) - { - StkTime::sleep(1); - } - main_loop->abort(); return NULL; diff --git a/src/network/protocols/connect_to_peer.cpp b/src/network/protocols/connect_to_peer.cpp index 0b97eab8f..33adbc294 100644 --- a/src/network/protocols/connect_to_peer.cpp +++ b/src/network/protocols/connect_to_peer.cpp @@ -21,7 +21,6 @@ #include "network/event.hpp" #include "network/network_config.hpp" #include "network/protocols/get_peer_address.hpp" -#include "network/protocols/hide_public_address.hpp" #include "network/protocols/request_connection.hpp" #include "network/protocol_manager.hpp" #include "network/stk_host.hpp" diff --git a/src/network/protocols/connect_to_server.cpp b/src/network/protocols/connect_to_server.cpp index 859883992..742178e8f 100644 --- a/src/network/protocols/connect_to_server.cpp +++ b/src/network/protocols/connect_to_server.cpp @@ -347,9 +347,9 @@ void ConnectToServer::waitingAloha(bool is_wan) { Log::info("ConnectToServer", "Server found : %s", sender.toString().c_str()); -#ifndef WIN32 if (!is_wan) { +#ifndef WIN32 // just check if the ip is ours : if so, // then just use localhost (127.0.0.1) struct ifaddrs *ifap, *ifa; diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index 9cefbce78..78d538171 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -99,12 +99,17 @@ ServerLobby::ServerLobby() : LobbyProtocol(NULL) */ ServerLobby::~ServerLobby() { + if (m_server_registered) + { + unregisterServer(); + } } // ~ServerLobby //----------------------------------------------------------------------------- void ServerLobby::setup() { + m_server_registered = false; m_game_setup = STKHost::get()->setupNewGame(); m_game_setup->setNumLocalPlayers(0); // no local players on a server m_next_player_id.setAtomic(0); @@ -316,7 +321,7 @@ void ServerLobby::registerServer() request->addParameter("name", NetworkConfig::get()->getServerName() ); request->addParameter("max_players", UserConfigParams::m_server_max_players ); - Log::info("RegisterServer", "Showing addr %s", addr.toString().c_str()); + Log::info("ServerLobby", "Public server addr %s", addr.toString().c_str()); request->executeNow(); @@ -325,13 +330,13 @@ void ServerLobby::registerServer() if (result->get("success", &rec_success) && rec_success == "yes") { - Log::info("RegisterServer", "Server is now online."); - STKHost::get()->setRegistered(true); + Log::info("ServerLobby", "Server is now online."); + m_server_registered = true; } else { irr::core::stringc error(request->getInfo().c_str()); - Log::error("RegisterServer", "%s", error.c_str()); + Log::error("ServerLobby", "%s", error.c_str()); STKHost::get()->setErrorMessage(_("Failed to register server: %s", error.c_str())); STKHost::get()->requestShutdown(); @@ -339,6 +344,45 @@ void ServerLobby::registerServer() delete request; } // registerServer + +//----------------------------------------------------------------------------- +/** Unregister this server (i.e. its public address) with the STK server, + * currently when karts enter kart selection screen it will be done. + */ +void ServerLobby::unregisterServer() +{ + const TransportAddress &addr = STKHost::get()->getPublicAddress(); + Online::XMLRequest* request = new Online::XMLRequest(); + PlayerManager::setUserDetails(request, "stop", Online::API::SERVER_PATH); + + request->addParameter("address", addr.getIP()); + request->addParameter("port", addr.getPort()); + + Log::info("ServerLobby", "address %s", addr.toString().c_str()); + request->executeNow(); + + const XMLNode * result = request->getXMLData(); + std::string rec_success; + + if (result->get("success", &rec_success)) + { + if (rec_success == "yes") + { + Log::info("ServerLobby", "Server is now unregister."); + } + else + { + Log::error("ServerLobby", "Fail to unregister server."); + } + } + else + { + Log::error("ServerLobby", "Fail to stop server."); + } + delete request; + +} // unregisterServer + //----------------------------------------------------------------------------- /** This function is called when all clients have loaded the world and * are therefore ready to start the race. It signals to all clients @@ -362,6 +406,12 @@ void ServerLobby::signalRaceStartToClients() */ void ServerLobby::startSelection(const Event *event) { + assert(m_server_registered); + if (m_server_registered) + { + unregisterServer(); + m_server_registered = false; + } if (m_state != ACCEPTING_CLIENTS) { diff --git a/src/network/protocols/server_lobby.hpp b/src/network/protocols/server_lobby.hpp index 8e99909ca..6448a3cf9 100644 --- a/src/network/protocols/server_lobby.hpp +++ b/src/network/protocols/server_lobby.hpp @@ -58,6 +58,9 @@ private: bool m_selection_enabled; + /** It indicates if this server is registered with the stk server. */ + std::atomic_bool m_server_registered; + /** Counts how many players are ready to go on. */ int m_player_ready_counter; @@ -80,6 +83,7 @@ private: void registerServer(); void finishedLoadingWorldClient(Event *event); void startedRaceOnClient(Event *event); + void unregisterServer(); public: ServerLobby(); virtual ~ServerLobby(); diff --git a/src/network/protocols/stop_server.cpp b/src/network/protocols/stop_server.cpp deleted file mode 100644 index 8699bb3f7..000000000 --- a/src/network/protocols/stop_server.cpp +++ /dev/null @@ -1,89 +0,0 @@ -// -// SuperTuxKart - a fun racing game with go-kart -// Copyright (C) 2013-2015 SuperTuxKart-Team -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 3 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -#include "network/protocols/stop_server.hpp" - -#include "config/player_manager.hpp" -#include "config/user_config.hpp" -#include "network/network_config.hpp" -#include "online/request_manager.hpp" - -StopServer::StopServer() : Protocol(PROTOCOL_SILENT) -{ -} - -StopServer::~StopServer() -{ -} - -bool StopServer::notifyEventAsynchronous(Event* event) -{ - return true; -} - -void StopServer::setup() -{ - m_state = NONE; -} - -void StopServer::asynchronousUpdate() -{ - if (m_state == NONE) - { - const TransportAddress& addr = NetworkConfig::get()->getMyAddress(); - m_request = new Online::XMLRequest(); - PlayerManager::setUserDetails(m_request, "stop", Online::API::SERVER_PATH); - - m_request->addParameter("address", addr.getIP()); - m_request->addParameter("port", addr.getPort()); - - Log::info("StopServer", "address %s", addr.toString().c_str()); - - Online::RequestManager::get()->addRequest(m_request); - m_state = REQUEST_PENDING; - } - else if (m_state == REQUEST_PENDING && m_request->isDone()) - { - const XMLNode * result = m_request->getXMLData(); - std::string rec_success; - - if(result->get("success", &rec_success)) - { - if(rec_success == "yes") - { - Log::info("StopServer", "Server is now offline."); - } - else - { - Log::error("StopServer", "Fail to stop server."); - } - } - else - { - Log::error("StopServer", "Fail to stop server."); - } - m_state = DONE; - } - else if (m_state == DONE) - { - m_state = EXITING; - delete m_request; - m_request = NULL; - requestTerminate(); - } -} // asynchronousUpdate diff --git a/src/network/protocols/stop_server.hpp b/src/network/protocols/stop_server.hpp deleted file mode 100644 index 6ec1c814e..000000000 --- a/src/network/protocols/stop_server.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef STOP_SERVER_HPP -#define STOP_SERVER_HPP - -#include "network/protocol.hpp" -#include "utils/cpp2011.hpp" - -namespace Online { class XMLRequest; } - -/*! \brief Removes the server info from the database - */ - -class StopServer : public Protocol -{ -private: - Online::XMLRequest* m_request; - enum STATE - { - NONE, - REQUEST_PENDING, - DONE, - EXITING - }; - STATE m_state; -public: - StopServer(); - virtual ~StopServer(); - - virtual bool notifyEventAsynchronous(Event* event) OVERRIDE; - virtual void setup() OVERRIDE; - virtual void asynchronousUpdate() OVERRIDE; - // -------------------------------------------------------------------- - virtual void update(float dt) OVERRIDE {} - -}; - -#endif // STOP_SERVER_HPP diff --git a/src/network/servers_manager.cpp b/src/network/servers_manager.cpp index 074904bb6..b8fef7a2e 100644 --- a/src/network/servers_manager.cpp +++ b/src/network/servers_manager.cpp @@ -190,9 +190,6 @@ Online::XMLRequest* ServersManager::getLANRefreshRequest() const server->setDifficulty((RaceManager::Difficulty)difficulty); server->setRaceMinorMode((RaceManager::MinorRaceModeType)mode); ServersManager::get()->addServer(server); - - TransportAddress me(my_ip, my_port); - NetworkConfig::get()->setMyAddress(me); m_success = true; } // if received_data } // while still waiting diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 3a5c8c024..acc6d4206 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -335,7 +335,6 @@ void STKHost::init() m_shutdown = false; m_network = NULL; m_game_setup = NULL; - m_is_registered = false; m_error_message = ""; m_exit_flag.clear(); diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index 5092df90e..16db2686e 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -95,10 +95,6 @@ private: /** Atomic flag used to stop this thread. */ std::atomic_flag m_exit_flag = ATOMIC_FLAG_INIT; - /** If this is a server, it indicates if this server is registered - * with the stk server. */ - bool m_is_registered; - /** An error message, which is set by a protocol to be displayed * in the GUI. */ irr::core::stringw m_error_message; @@ -242,18 +238,6 @@ public: /** Returns the number of currently connected peers. */ unsigned int getPeerCount() { return (int)m_peers.size(); } // -------------------------------------------------------------------- - /** Sets if this server is registered with the stk server. */ - void setRegistered(bool registered) - { - m_is_registered = registered; - } // setRegistered - // -------------------------------------------------------------------- - /** Returns if this server is registered with the stk server. */ - bool isRegistered() const - { - return m_is_registered; - } // isRegistered - // -------------------------------------------------------------------- /** Sets the global host id of this host. */ void setMyHostId(uint8_t my_host_id) { m_host_id = my_host_id; } // -------------------------------------------------------------------- diff --git a/src/states_screens/create_server_screen.cpp b/src/states_screens/create_server_screen.cpp index ef85f887b..bd8157aac 100644 --- a/src/states_screens/create_server_screen.cpp +++ b/src/states_screens/create_server_screen.cpp @@ -24,6 +24,7 @@ #include "config/player_manager.hpp" #include "config/user_config.hpp" #include "modes/demo_world.hpp" +#include "network/protocols/lobby_protocol.hpp" #include "network/network_config.hpp" #include "network/servers_manager.hpp" #include "network/stk_host.hpp" @@ -148,7 +149,7 @@ void CreateServerScreen::onUpdate(float delta) // Otherwise wait till we get an answer from the server: // ----------------------------------------------------- - if(!STKHost::get()->isRegistered() && !NetworkConfig::get()->isLAN()) + if (!LobbyProtocol::get()) { m_info_widget->setDefaultColor(); m_info_widget->setText(StringUtils::loadingDots(_("Creating server")),