Unregister STK server when exiting

This commit is contained in:
Benau 2018-02-23 14:01:20 +08:00
parent 005454ba7b
commit 1023e6580e
14 changed files with 61 additions and 212 deletions

View File

@ -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());

View File

@ -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.

View File

@ -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<TransportAddress> 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; }
// --------------------------------------------------------------------

View File

@ -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<StopServer>();
p->requestStart();
while(p->getState() != PROTOCOL_STATE_TERMINATED)
{
StkTime::sleep(1);
}
main_loop->abort();
return NULL;

View File

@ -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"

View File

@ -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;

View File

@ -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)
{

View File

@ -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();

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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();

View File

@ -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; }
// --------------------------------------------------------------------

View File

@ -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<LobbyProtocol>())
{
m_info_widget->setDefaultColor();
m_info_widget->setText(StringUtils::loadingDots(_("Creating server")),