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.
|
|
|
|
|
|
|
|
#include "online/servers_manager.hpp"
|
|
|
|
#include "config/user_config.hpp"
|
2015-11-09 04:51:00 -05:00
|
|
|
#include "network/stk_host.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
|
|
|
|
2014-08-04 11:41:47 -04:00
|
|
|
namespace Online
|
|
|
|
{
|
2015-11-06 00:50:58 -05:00
|
|
|
static ServersManager* manager_singleton(NULL);
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
ServersManager* ServersManager::get()
|
|
|
|
{
|
|
|
|
if (manager_singleton == NULL)
|
|
|
|
manager_singleton = new ServersManager();
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
return manager_singleton;
|
|
|
|
} // get
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
void ServersManager::deallocate()
|
|
|
|
{
|
|
|
|
delete manager_singleton;
|
|
|
|
manager_singleton = NULL;
|
|
|
|
} // deallocate
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
// ===========================================================-=============
|
|
|
|
ServersManager::ServersManager()
|
|
|
|
{
|
|
|
|
m_last_load_time.setAtomic(0.0f);
|
|
|
|
m_joined_server.setAtomic(NULL);
|
|
|
|
} // ServersManager
|
2013-07-22 16:00:08 -04:00
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
ServersManager::~ServersManager()
|
|
|
|
{
|
|
|
|
cleanUpServers();
|
|
|
|
MutexLocker(m_joined_server);
|
|
|
|
delete m_joined_server.getData();
|
|
|
|
} // ~ServersManager
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
// ------------------------------------------------------------------------
|
2015-11-09 16:31:07 -05:00
|
|
|
/** Removes all stored server information. After this call the list of
|
|
|
|
* servers can be refreshed.
|
|
|
|
*/
|
2015-11-06 00:50:58 -05:00
|
|
|
void ServersManager::cleanUpServers()
|
|
|
|
{
|
2015-11-09 16:31:07 -05:00
|
|
|
if(m_joined_server.getAtomic()!=NULL)
|
|
|
|
{
|
|
|
|
// m_joinsed_server is a pointer into the m_server structure,
|
|
|
|
// we can not modify this data structure while this pointer exists.
|
|
|
|
Log::warn("ServersManager", "Server cleanUp while being already "
|
|
|
|
"connected to a server.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
m_sorted_servers.lock();
|
|
|
|
m_sorted_servers.getData().clearAndDeleteAll();
|
|
|
|
m_sorted_servers.unlock();
|
|
|
|
m_mapped_servers.lock();
|
|
|
|
m_mapped_servers.getData().clear();
|
|
|
|
m_mapped_servers.unlock();
|
|
|
|
} // cleanUpServers
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
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. */
|
|
|
|
class WANRefreshRequest : public XMLRequest
|
2013-07-21 13:41:02 -04:00
|
|
|
{
|
2015-11-09 04:51:00 -05:00
|
|
|
public:
|
|
|
|
WANRefreshRequest() : XMLRequest(/*manage_memory*/false,
|
|
|
|
/*priority*/100) {}
|
|
|
|
// --------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
virtual void callback()
|
2013-07-12 19:12:31 -04:00
|
|
|
{
|
2015-11-06 00:50:58 -05:00
|
|
|
ServersManager::get()->refresh(isSuccess(), getXMLData());
|
|
|
|
} // 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-09 04:51:00 -05:00
|
|
|
XMLRequest* request = new WANRefreshRequest();
|
2015-11-06 00:50:58 -05:00
|
|
|
request->setApiURL(API::SERVER_PATH, "get-all");
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
XMLRequest* ServersManager::getLANRefreshRequest() const
|
|
|
|
{
|
|
|
|
/** 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.
|
|
|
|
*/
|
|
|
|
class LANRefreshRequest : public XMLRequest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** High priority for this request. */
|
|
|
|
LANRefreshRequest() : XMLRequest(false, 100) {m_success = false;}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
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
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
virtual void operation()
|
|
|
|
{
|
|
|
|
Network *broadcast = new Network(1, 1, 0, 0);
|
|
|
|
|
|
|
|
NetworkString s(std::string("stk-server"));
|
|
|
|
TransportAddress broadcast_address(-1, 2757);
|
|
|
|
broadcast->sendRawPacket(s.getBytes(), s.size(), broadcast_address);
|
|
|
|
|
|
|
|
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;
|
|
|
|
while(StkTime::getRealTime() - start_time < DURATION)
|
|
|
|
{
|
|
|
|
TransportAddress sender;
|
|
|
|
int len = broadcast->receiveRawPacket(buffer, LEN, &sender, 1);
|
|
|
|
if(len>0)
|
|
|
|
{
|
|
|
|
NetworkString s(buffer, len);
|
|
|
|
uint8_t name_len = s.getUInt8(0);
|
|
|
|
std::string name = s.getString(1, name_len);
|
|
|
|
irr::core::stringw name_w =
|
|
|
|
StringUtils::utf8_to_wide(name.c_str());
|
|
|
|
uint8_t max_players = s.getUInt8(1+name_len );
|
|
|
|
uint8_t players = s.getUInt8(1+name.size()+1);
|
|
|
|
ServersManager::get()
|
|
|
|
->addServer(new Server(name_w, /*lan*/true,
|
2015-11-09 16:31:07 -05:00
|
|
|
max_players, players,
|
|
|
|
sender) );
|
2015-11-09 04:51:00 -05:00
|
|
|
m_success = true;
|
|
|
|
} // if received_data
|
|
|
|
} // while still waiting
|
|
|
|
} // 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
|
|
|
|
* requests. The current list of servers is also cleared/
|
|
|
|
*/
|
|
|
|
XMLRequest* ServersManager::getRefreshRequest(bool request_now)
|
|
|
|
{
|
|
|
|
if (StkTime::getRealTime() - m_last_load_time.getAtomic()
|
|
|
|
< SERVER_REFRESH_INTERVAL)
|
|
|
|
{
|
|
|
|
// Avoid too frequent refreshing
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
if(m_joined_server.getAtomic()!=NULL)
|
|
|
|
{
|
|
|
|
// m_joinsed_server is a pointer into the m_server structure,
|
|
|
|
// we can not modify this data structure while this pointer exists.
|
|
|
|
Log::warn("ServersManager", "Server refresh while being already "
|
|
|
|
"connected to a server.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-09 04:51:00 -05:00
|
|
|
cleanUpServers();
|
|
|
|
XMLRequest *request = STKHost::isWAN() ? getWANRefreshRequest()
|
|
|
|
: getLANRefreshRequest();
|
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
if (request_now)
|
|
|
|
RequestManager::get()->addRequest(request);
|
|
|
|
|
|
|
|
return request;
|
2015-11-09 16:09:05 -05:00
|
|
|
} // getRefreshRequest
|
2015-11-06 00:50:58 -05:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
/** Callback from the refresh request.
|
|
|
|
* \param success If the refresh was successful.
|
|
|
|
* \param input The XML data describing the server.
|
|
|
|
*/
|
|
|
|
void ServersManager::refresh(bool success, const XMLNode *input)
|
|
|
|
{
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
Log::error("Server Manager", "Could not refresh server list");
|
|
|
|
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
|
|
|
{
|
2015-11-06 00:50:58 -05:00
|
|
|
addServer(new Server(*servers_xml->getNode(i), /*is_lan*/false));
|
|
|
|
}
|
|
|
|
m_last_load_time.setAtomic((float)StkTime::getRealTime());
|
|
|
|
} // refresh
|
2013-07-31 14:03:11 -04:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
const Server* ServersManager::getQuickPlay() const
|
|
|
|
{
|
|
|
|
if (m_sorted_servers.getData().size() > 0)
|
|
|
|
return getServerBySort(0);
|
2013-07-23 12:34:38 -04:00
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
return NULL;
|
|
|
|
} // getQuickPlay
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Sets a pointer to the server to which this client is connected. From now
|
|
|
|
* on the list of servers must not be modified (else this pointer might
|
|
|
|
* become invalid).
|
|
|
|
*/
|
2015-11-06 00:50:58 -05:00
|
|
|
void ServersManager::setJoinedServer(uint32_t id)
|
|
|
|
{
|
2015-11-09 16:31:07 -05:00
|
|
|
{
|
|
|
|
MutexLocker(m_joined_server);
|
|
|
|
delete m_joined_server.getData();
|
|
|
|
}
|
2013-07-23 12:34:38 -04:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
{
|
|
|
|
MutexLocker(m_mapped_servers);
|
|
|
|
m_joined_server.getData() = m_mapped_servers.getData().at(id);
|
|
|
|
}
|
2015-11-06 00:50:58 -05:00
|
|
|
} // setJoinedServer
|
2013-07-23 12:34:38 -04:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Unsets the server to which this client is connected.
|
|
|
|
*/
|
2015-11-06 00:50:58 -05:00
|
|
|
void ServersManager::unsetJoinedServer()
|
|
|
|
{
|
|
|
|
MutexLocker(m_joined_server);
|
|
|
|
delete m_joined_server.getData();
|
|
|
|
m_joined_server.getData() = NULL;
|
|
|
|
} // unsetJoinedServer
|
2014-08-04 11:41:47 -04:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
void ServersManager::addServer(Server *server)
|
|
|
|
{
|
|
|
|
m_sorted_servers.lock();
|
|
|
|
m_sorted_servers.getData().push_back(server);
|
|
|
|
m_sorted_servers.unlock();
|
2013-07-23 12:34:38 -04:00
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
m_mapped_servers.lock();
|
|
|
|
m_mapped_servers.getData()[server->getServerId()] = server;
|
|
|
|
m_mapped_servers.unlock();
|
|
|
|
} // addServer
|
2013-07-23 12:34:38 -04:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
int ServersManager::getNumServers() const
|
|
|
|
{
|
|
|
|
MutexLocker(m_sorted_servers);
|
|
|
|
return m_sorted_servers.getData().size();
|
|
|
|
} // getNumServers
|
2013-07-23 12:34:38 -04:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
const Server* ServersManager::getServerBySort(int index) const
|
|
|
|
{
|
|
|
|
MutexLocker(m_sorted_servers);
|
|
|
|
return m_sorted_servers.getData().get(index);
|
|
|
|
} // getServerBySort
|
2013-07-24 22:22:53 -04:00
|
|
|
|
2015-11-06 00:50:58 -05:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
const Server* ServersManager::getServerByID(uint32_t id) const
|
|
|
|
{
|
|
|
|
MutexLocker(m_mapped_servers);
|
|
|
|
return m_mapped_servers.getData().at(id);
|
|
|
|
} // getServerByID
|
2013-07-24 22:22:53 -04:00
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
Server* ServersManager::getJoinedServer() const
|
|
|
|
{
|
|
|
|
return m_joined_server.getAtomic();
|
|
|
|
} // getJoinedServer
|
|
|
|
|
2015-11-09 16:31:07 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-11-06 00:50:58 -05:00
|
|
|
void ServersManager::sort(bool sort_desc)
|
|
|
|
{
|
|
|
|
MutexLocker(m_sorted_servers);
|
|
|
|
m_sorted_servers.getData().insertionSort(0, sort_desc);
|
|
|
|
} // sort
|
2013-07-24 22:22:53 -04:00
|
|
|
|
2013-07-19 17:20:32 -04:00
|
|
|
} // namespace Online
|