2013-07-12 19:12:31 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
2015-03-29 20:31:42 -04:00
|
|
|
// Copyright (C) 2013-2015 Glenn De Jonghe
|
2013-07-12 19:12:31 -04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2015-11-12 01:52:04 -05:00
|
|
|
#include "network/servers_manager.hpp"
|
|
|
|
|
2018-04-15 23:57:45 -04:00
|
|
|
#include "config/stk_config.hpp"
|
2013-07-12 19:12:31 -04:00
|
|
|
#include "config/user_config.hpp"
|
2015-11-11 17:53:12 -05:00
|
|
|
#include "network/network.hpp"
|
|
|
|
#include "network/network_config.hpp"
|
|
|
|
#include "network/network_string.hpp"
|
2018-03-04 12:54:44 -05:00
|
|
|
#include "network/server.hpp"
|
2016-11-07 17:52:39 -05:00
|
|
|
#include "network/stk_host.hpp"
|
2015-11-12 01:52:04 -05:00
|
|
|
#include "online/xml_request.hpp"
|
2018-03-04 12:54:44 -05:00
|
|
|
#include "online/request_manager.hpp"
|
2013-07-12 19:12:31 -04:00
|
|
|
#include "utils/translation.hpp"
|
2013-07-21 13:41:02 -04:00
|
|
|
#include "utils/time.hpp"
|
|
|
|
|
2015-10-09 01:47:18 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <irrString.h>
|
|
|
|
#include <string>
|
|
|
|
|
2013-07-21 13:41:02 -04:00
|
|
|
#define SERVER_REFRESH_INTERVAL 5.0f
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2018-03-04 12:54:44 -05:00
|
|
|
static ServersManager* g_manager_singleton(NULL);
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2018-03-04 12:54:44 -05:00
|
|
|
// ============================================================================
|
2015-11-06 00:50:58 -05:00
|
|
|
ServersManager* ServersManager::get()
|
|
|
|
{
|
2018-03-04 12:54:44 -05:00
|
|
|
if (g_manager_singleton == NULL)
|
|
|
|
g_manager_singleton = new ServersManager();
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2018-03-04 12:54:44 -05:00
|
|
|
return g_manager_singleton;
|
2015-11-06 00:50:58 -05:00
|
|
|
} // get
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2018-03-04 12:54:44 -05:00
|
|
|
// ============================================================================
|
2015-11-06 00:50:58 -05:00
|
|
|
void ServersManager::deallocate()
|
|
|
|
{
|
2018-03-04 12:54:44 -05:00
|
|
|
delete g_manager_singleton;
|
|
|
|
g_manager_singleton = NULL;
|
2015-11-06 00:50:58 -05:00
|
|
|
} // deallocate
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2018-03-04 12:54:44 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
ServersManager::ServersManager()
|
|
|
|
{
|
2018-03-04 12:54:44 -05:00
|
|
|
m_last_load_time.store(0.0f);
|
|
|
|
m_list_updated = false;
|
2015-11-06 00:50:58 -05:00
|
|
|
} // ServersManager
|
2013-07-22 16:00:08 -04:00
|
|
|
|
2018-03-04 12:54:44 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
ServersManager::~ServersManager()
|
|
|
|
{
|
|
|
|
} // ~ServersManager
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-09 04:51:00 -05:00
|
|
|
/** Returns a WAN update-list-of-servers request. It queries the
|
|
|
|
* STK server for an up-to-date list of servers.
|
|
|
|
*/
|
2015-11-12 01:52:04 -05:00
|
|
|
Online::XMLRequest* ServersManager::getWANRefreshRequest() const
|
2015-11-06 00:50:58 -05:00
|
|
|
{
|
2015-11-09 04:51:00 -05:00
|
|
|
// ========================================================================
|
|
|
|
/** A small local class that triggers an update of the ServersManager
|
|
|
|
* when the request is finished. */
|
2015-11-12 01:52:04 -05:00
|
|
|
class WANRefreshRequest : public Online::XMLRequest
|
2013-07-21 13:41:02 -04:00
|
|
|
{
|
2015-11-09 04:51:00 -05:00
|
|
|
public:
|
2018-03-04 12:54:44 -05:00
|
|
|
WANRefreshRequest() : Online::XMLRequest(/*manage_memory*/true,
|
2015-11-12 01:52:04 -05:00
|
|
|
/*priority*/100) {}
|
2015-11-09 04:51:00 -05:00
|
|
|
// --------------------------------------------------------------------
|
2018-03-04 12:54:44 -05:00
|
|
|
virtual void afterOperation() OVERRIDE
|
2013-07-12 19:12:31 -04:00
|
|
|
{
|
2018-03-04 12:54:44 -05:00
|
|
|
Online::XMLRequest::afterOperation();
|
|
|
|
ServersManager::get()->setWanServers(isSuccess(), getXMLData());
|
2015-11-06 00:50:58 -05:00
|
|
|
} // callback
|
2015-11-09 04:51:00 -05:00
|
|
|
// --------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
}; // RefreshRequest
|
2015-11-09 04:51:00 -05:00
|
|
|
// ========================================================================
|
2015-11-06 00:50:58 -05:00
|
|
|
|
2015-11-12 01:52:04 -05:00
|
|
|
Online::XMLRequest *request = new WANRefreshRequest();
|
|
|
|
request->setApiURL(Online::API::SERVER_PATH, "get-all");
|
2015-11-06 00:50:58 -05:00
|
|
|
|
2015-11-09 04:51:00 -05:00
|
|
|
return request;
|
|
|
|
} // getWANRefreshRequest
|
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-09 04:51:00 -05:00
|
|
|
/** Returns a LAN update-list-of-servers request. It uses UDP broadcasts
|
|
|
|
* to find LAN servers, and waits for a certain amount of time fr
|
|
|
|
* answers.
|
|
|
|
*/
|
2015-11-12 01:52:04 -05:00
|
|
|
Online::XMLRequest* ServersManager::getLANRefreshRequest() const
|
2015-11-09 04:51:00 -05:00
|
|
|
{
|
|
|
|
/** A simple class that uses LAN broadcasts to find local servers.
|
|
|
|
* It is based on XML request, but actually does not use any of the
|
|
|
|
* XML/HTTP based infrastructure, but implements the same interface.
|
|
|
|
* This way the already existing request thread can be used.
|
|
|
|
*/
|
2015-11-12 01:52:04 -05:00
|
|
|
class LANRefreshRequest : public Online::XMLRequest
|
2015-11-09 04:51:00 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** High priority for this request. */
|
2018-03-04 12:54:44 -05:00
|
|
|
LANRefreshRequest() : XMLRequest(true, 100) {m_success = false;}
|
2015-11-09 04:51:00 -05:00
|
|
|
// --------------------------------------------------------------------
|
|
|
|
virtual ~LANRefreshRequest() {}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/** Get the downloaded XML tree.
|
|
|
|
* \pre request has to be executed.
|
|
|
|
* \return get the complete result from the request reply. */
|
|
|
|
virtual const XMLNode * getXMLData() const
|
|
|
|
{
|
|
|
|
assert(hasBeenExecuted());
|
|
|
|
return NULL;
|
|
|
|
} // getXMLData
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
virtual void prepareOperation() OVERRIDE
|
|
|
|
{
|
|
|
|
} // prepareOperation
|
|
|
|
// --------------------------------------------------------------------
|
2016-02-20 19:21:43 -05:00
|
|
|
virtual void operation() OVERRIDE
|
2015-11-09 04:51:00 -05:00
|
|
|
{
|
2016-11-07 17:52:39 -05:00
|
|
|
ENetAddress addr;
|
|
|
|
addr.host = STKHost::HOST_ANY;
|
|
|
|
addr.port = STKHost::PORT_ANY;
|
|
|
|
Network *broadcast = new Network(1, 1, 0, 0, &addr);
|
2015-11-09 04:51:00 -05:00
|
|
|
|
2016-02-24 04:32:11 -05:00
|
|
|
BareNetworkString s(std::string("stk-server"));
|
2016-10-24 16:53:52 -04:00
|
|
|
TransportAddress broadcast_address(-1,
|
|
|
|
NetworkConfig::get()->getServerDiscoveryPort());
|
2016-02-24 04:32:11 -05:00
|
|
|
broadcast->sendRawPacket(s, broadcast_address);
|
2015-11-09 04:51:00 -05:00
|
|
|
|
|
|
|
Log::info("ServersManager", "Sent broadcast message.");
|
|
|
|
|
|
|
|
const int LEN=2048;
|
|
|
|
char buffer[LEN];
|
|
|
|
// Wait for up to 0.5 seconds to receive an answer from
|
|
|
|
// any local servers.
|
|
|
|
double start_time = StkTime::getRealTime();
|
|
|
|
const double DURATION = 1.0;
|
2018-03-04 12:54:44 -05:00
|
|
|
const auto& servers = ServersManager::get()->getServers();
|
2018-03-16 07:06:20 -04:00
|
|
|
int cur_server_id = (int)servers.size();
|
2018-03-02 02:21:27 -05:00
|
|
|
assert(cur_server_id == 0);
|
2018-03-04 12:54:44 -05:00
|
|
|
std::vector<std::shared_ptr<Server> > servers_now;
|
|
|
|
while (StkTime::getRealTime() - start_time < DURATION)
|
2015-11-09 04:51:00 -05:00
|
|
|
{
|
|
|
|
TransportAddress sender;
|
|
|
|
int len = broadcast->receiveRawPacket(buffer, LEN, &sender, 1);
|
2018-03-06 11:51:59 -05:00
|
|
|
if (len > 0)
|
2015-11-09 04:51:00 -05:00
|
|
|
{
|
2016-02-24 17:48:47 -05:00
|
|
|
BareNetworkString s(buffer, len);
|
2018-04-15 23:57:45 -04:00
|
|
|
int version = s.getUInt8();
|
|
|
|
if (version < stk_config->m_max_server_version ||
|
|
|
|
version > stk_config->m_max_server_version)
|
2018-03-13 02:39:20 -04:00
|
|
|
{
|
|
|
|
Log::verbose("ServersManager", "Skipping a server");
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-26 16:55:21 -05:00
|
|
|
irr::core::stringw name;
|
2016-02-24 17:48:47 -05:00
|
|
|
// bytes_read is the number of bytes read
|
2018-03-06 11:51:59 -05:00
|
|
|
s.decodeStringW(&name);
|
2016-03-12 21:49:43 -05:00
|
|
|
uint8_t max_players = s.getUInt8();
|
|
|
|
uint8_t players = s.getUInt8();
|
2018-03-06 11:51:59 -05:00
|
|
|
uint16_t port = s.getUInt16();
|
2016-03-12 21:49:43 -05:00
|
|
|
uint8_t difficulty = s.getUInt8();
|
2018-03-02 02:21:27 -05:00
|
|
|
uint8_t mode = s.getUInt8();
|
2018-03-06 11:51:59 -05:00
|
|
|
sender.setPort(port);
|
2018-03-13 02:39:20 -04:00
|
|
|
uint8_t password = s.getUInt8();
|
2018-03-04 12:54:44 -05:00
|
|
|
servers_now.emplace_back(std::make_shared<Server>
|
|
|
|
(cur_server_id++, name, max_players, players,
|
2018-03-16 07:06:20 -04:00
|
|
|
difficulty, mode, sender, password == 1));
|
2015-11-09 04:51:00 -05:00
|
|
|
} // if received_data
|
|
|
|
} // while still waiting
|
2018-03-04 12:54:44 -05:00
|
|
|
m_success = true;
|
|
|
|
ServersManager::get()->setLanServers(servers_now);
|
2016-06-27 19:47:56 -04:00
|
|
|
delete broadcast;
|
2015-11-09 04:51:00 -05:00
|
|
|
} // operation
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
/** This function is necessary, otherwise the XML- and HTTP-Request
|
|
|
|
* functions are called, which will cause a crash. */
|
|
|
|
virtual void afterOperation() OVERRIDE {}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
}; // LANRefreshRequest
|
|
|
|
// ========================================================================
|
|
|
|
|
|
|
|
return new LANRefreshRequest();
|
|
|
|
|
|
|
|
} // getLANRefreshRequest
|
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-09 04:51:00 -05:00
|
|
|
/** Factory function to create either a LAN or a WAN update-of-server
|
2018-03-04 12:54:44 -05:00
|
|
|
* requests. The current list of servers is also cleared.
|
2015-11-09 04:51:00 -05:00
|
|
|
*/
|
2018-03-04 12:54:44 -05:00
|
|
|
bool ServersManager::refresh()
|
2015-11-09 04:51:00 -05:00
|
|
|
{
|
2018-03-04 12:54:44 -05:00
|
|
|
if (StkTime::getRealTime() - m_last_load_time.load()
|
2015-11-09 04:51:00 -05:00
|
|
|
< SERVER_REFRESH_INTERVAL)
|
|
|
|
{
|
|
|
|
// Avoid too frequent refreshing
|
2018-03-04 12:54:44 -05:00
|
|
|
return false;
|
2015-11-09 16:31:07 -05:00
|
|
|
}
|
|
|
|
|
2015-11-09 04:51:00 -05:00
|
|
|
cleanUpServers();
|
2018-03-04 12:54:44 -05:00
|
|
|
m_list_updated = false;
|
|
|
|
if (NetworkConfig::get()->isWAN())
|
|
|
|
Online::RequestManager::get()->addRequest(getWANRefreshRequest());
|
|
|
|
else
|
|
|
|
Online::RequestManager::get()->addRequest(getLANRefreshRequest());
|
|
|
|
return true;
|
|
|
|
} // refresh
|
2015-11-06 00:50:58 -05:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2018-03-04 12:54:44 -05:00
|
|
|
/** Callback from the refresh request for wan servers.
|
2015-11-06 00:50:58 -05:00
|
|
|
* \param success If the refresh was successful.
|
|
|
|
* \param input The XML data describing the server.
|
|
|
|
*/
|
2018-03-04 12:54:44 -05:00
|
|
|
void ServersManager::setWanServers(bool success, const XMLNode* input)
|
2015-11-06 00:50:58 -05:00
|
|
|
{
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
Log::error("Server Manager", "Could not refresh server list");
|
2018-03-04 12:54:44 -05:00
|
|
|
m_list_updated = true;
|
2015-11-06 00:50:58 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-07-14 20:25:56 -04:00
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
const XMLNode *servers_xml = input->getNode("servers");
|
|
|
|
for (unsigned int i = 0; i < servers_xml->getNumNodes(); i++)
|
2013-07-18 19:02:18 -04:00
|
|
|
{
|
2018-04-15 23:57:45 -04:00
|
|
|
int version = 0;
|
2018-03-13 02:39:20 -04:00
|
|
|
servers_xml->getNode(i)->get("version", &version);
|
|
|
|
assert(version != 0);
|
2018-04-15 23:57:45 -04:00
|
|
|
if (version < stk_config->m_max_server_version ||
|
|
|
|
version > stk_config->m_max_server_version)
|
2018-03-13 02:39:20 -04:00
|
|
|
{
|
|
|
|
Log::verbose("ServersManager", "Skipping a server");
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-04 12:54:44 -05:00
|
|
|
m_servers.emplace_back(
|
|
|
|
std::make_shared<Server>(*servers_xml->getNode(i)));
|
2015-11-06 00:50:58 -05:00
|
|
|
}
|
2018-03-04 12:54:44 -05:00
|
|
|
m_last_load_time.store((float)StkTime::getRealTime());
|
|
|
|
m_list_updated = true;
|
2015-11-06 00:50:58 -05:00
|
|
|
} // refresh
|