2012-07-09 14:52:23 +02:00
|
|
|
/******************************************************************************\
|
2020-01-01 15:41:43 +01:00
|
|
|
* Copyright (c) 2004-2020
|
2012-07-09 14:52:23 +02:00
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Volker Fischer
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* 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 2 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 "serverlist.h"
|
|
|
|
|
|
|
|
/* Implementation *************************************************************/
|
|
|
|
CServerListManager::CServerListManager ( const quint16 iNPortNum,
|
|
|
|
const QString& sNCentServAddr,
|
|
|
|
const QString& strServerInfo,
|
|
|
|
const int iNumChannels,
|
2012-07-14 18:18:09 +02:00
|
|
|
const bool bNCentServPingServerInList,
|
2012-07-09 14:52:23 +02:00
|
|
|
CProtocol* pNConLProt )
|
2020-04-30 19:26:04 +02:00
|
|
|
: tsConsoleStream ( *( ( new ConsoleWriterFactory() )->get() ) ),
|
|
|
|
iNumPredefinedServers ( 0 ),
|
2020-05-17 11:00:56 +02:00
|
|
|
eCentralServerAddressType ( AT_CUSTOM ), // must be AT_CUSTOM for the "no GUI" case
|
2020-04-11 14:27:50 +02:00
|
|
|
bCentServPingServerInList ( bNCentServPingServerInList ),
|
2020-04-14 22:00:08 +02:00
|
|
|
pConnLessProtocol ( pNConLProt ),
|
|
|
|
eSvrRegStatus ( SRS_UNREGISTERED ),
|
2020-04-30 19:26:04 +02:00
|
|
|
iSvrRegRetries ( 0 )
|
2012-07-09 14:52:23 +02:00
|
|
|
{
|
|
|
|
// set the central server address
|
|
|
|
SetCentralServerAddress ( sNCentServAddr );
|
|
|
|
|
2020-04-12 14:26:21 +02:00
|
|
|
// set the server internal address, including internal port number
|
2020-04-09 20:06:34 +02:00
|
|
|
SlaveCurLocalHostAddress = CHostAddress( NetworkUtil::GetLocalAddress().InetAddr, iNPortNum );
|
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
// prepare the server info information
|
|
|
|
QStringList slServInfoSeparateParams;
|
|
|
|
int iServInfoNumSplitItems = 0;
|
|
|
|
|
|
|
|
if ( !strServerInfo.isEmpty() )
|
|
|
|
{
|
|
|
|
// split the different parameter strings
|
|
|
|
slServInfoSeparateParams = strServerInfo.split ( ";" );
|
|
|
|
|
|
|
|
// get the number of items in the split list
|
|
|
|
iServInfoNumSplitItems = slServInfoSeparateParams.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
// per definition, the very first entry is this server and this entry will
|
|
|
|
// never be deleted
|
|
|
|
ServerList.clear();
|
|
|
|
|
2016-02-09 16:05:06 +01:00
|
|
|
// Init server list entry (server info for this server) with defaults. Per
|
2012-07-09 14:52:23 +02:00
|
|
|
// definition the client substitudes the IP address of the central server
|
2016-02-09 16:05:06 +01:00
|
|
|
// itself for his server list. If we are the central server, we assume that
|
|
|
|
// we have a permanent server.
|
2012-07-09 14:52:23 +02:00
|
|
|
CServerListEntry ThisServerListEntry ( CHostAddress(),
|
2020-04-09 20:06:34 +02:00
|
|
|
SlaveCurLocalHostAddress,
|
2012-07-09 14:52:23 +02:00
|
|
|
"",
|
|
|
|
QLocale::system().country(),
|
|
|
|
"",
|
|
|
|
iNumChannels,
|
2016-02-09 16:05:06 +01:00
|
|
|
GetIsCentralServer() );
|
2012-07-09 14:52:23 +02:00
|
|
|
|
|
|
|
// parse the server info string according to definition:
|
|
|
|
// [this server name];[this server city]; ...
|
|
|
|
// [this server country as QLocale ID]; ...
|
|
|
|
// per definition, we expect at least three parameters
|
|
|
|
if ( iServInfoNumSplitItems >= 3 )
|
|
|
|
{
|
|
|
|
// [this server name]
|
2020-04-19 07:31:14 +02:00
|
|
|
ThisServerListEntry.strName = slServInfoSeparateParams[0].left ( MAX_LEN_SERVER_NAME );
|
2012-07-09 14:52:23 +02:00
|
|
|
|
|
|
|
// [this server city]
|
2020-04-19 07:31:14 +02:00
|
|
|
ThisServerListEntry.strCity = slServInfoSeparateParams[1].left ( MAX_LEN_SERVER_CITY );
|
2012-07-09 14:52:23 +02:00
|
|
|
|
|
|
|
// [this server country as QLocale ID]
|
|
|
|
const int iCountry = slServInfoSeparateParams[2].toInt();
|
2019-06-20 11:41:29 +02:00
|
|
|
|
|
|
|
if ( !slServInfoSeparateParams[2].isEmpty() && ( iCountry >= 0 ) && ( iCountry <= QLocale::LastCountry ) )
|
2012-07-09 14:52:23 +02:00
|
|
|
{
|
|
|
|
ThisServerListEntry.eCountry = static_cast<QLocale::Country> (
|
|
|
|
iCountry );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// per definition, the first entry in the server list is the own server
|
|
|
|
ServerList.append ( ThisServerListEntry );
|
|
|
|
|
|
|
|
// parse the predefined server infos (if any) according to definition:
|
|
|
|
// [server1 address];[server1 name];[server1 city]; ...
|
|
|
|
// [server1 country as QLocale ID]; ...
|
|
|
|
// [server2 address];[server2 name];[server2 city]; ...
|
|
|
|
// [server2 country as QLocale ID]; ...
|
|
|
|
// ...
|
|
|
|
int iCurUsedServInfoSplitItems = 3; // three items are used for this server
|
|
|
|
|
|
|
|
// we always expect four items per new server, also check for maximum
|
|
|
|
// allowed number of servers in the server list
|
|
|
|
while ( ( iServInfoNumSplitItems - iCurUsedServInfoSplitItems >= 4 ) &&
|
|
|
|
( iNumPredefinedServers <= MAX_NUM_SERVERS_IN_SERVER_LIST ) )
|
|
|
|
{
|
2016-02-09 16:05:06 +01:00
|
|
|
// create a new server list entry, we assume that servers which are
|
|
|
|
// registered via the command line are permanent servers
|
2012-07-09 14:52:23 +02:00
|
|
|
CServerListEntry NewServerListEntry ( CHostAddress(),
|
2020-04-09 20:06:34 +02:00
|
|
|
CHostAddress(),
|
2012-07-09 14:52:23 +02:00
|
|
|
"",
|
|
|
|
QLocale::AnyCountry,
|
|
|
|
"",
|
|
|
|
iNumChannels,
|
|
|
|
true );
|
|
|
|
|
|
|
|
// [server n address]
|
2013-03-24 12:38:00 +01:00
|
|
|
NetworkUtil().ParseNetworkAddress (
|
2012-07-09 14:52:23 +02:00
|
|
|
slServInfoSeparateParams[iCurUsedServInfoSplitItems],
|
|
|
|
NewServerListEntry.HostAddr );
|
|
|
|
|
2020-04-09 20:06:34 +02:00
|
|
|
// [server n server internal address]
|
|
|
|
// Not included in the static server info, so use external address
|
|
|
|
NewServerListEntry.LHostAddr = NewServerListEntry.HostAddr;
|
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
// [server n name]
|
|
|
|
NewServerListEntry.strName =
|
2020-04-19 07:31:14 +02:00
|
|
|
slServInfoSeparateParams[iCurUsedServInfoSplitItems + 1].left ( MAX_LEN_SERVER_NAME );
|
2012-07-09 14:52:23 +02:00
|
|
|
|
|
|
|
// [server n city]
|
|
|
|
NewServerListEntry.strCity =
|
2020-04-19 07:31:14 +02:00
|
|
|
slServInfoSeparateParams[iCurUsedServInfoSplitItems + 2].left ( MAX_LEN_SERVER_CITY );
|
2012-07-09 14:52:23 +02:00
|
|
|
|
|
|
|
// [server n country as QLocale ID]
|
|
|
|
const int iCountry =
|
|
|
|
slServInfoSeparateParams[iCurUsedServInfoSplitItems + 3].toInt();
|
|
|
|
|
|
|
|
if ( ( iCountry >= 0 ) && ( iCountry <= QLocale::LastCountry ) )
|
|
|
|
{
|
|
|
|
NewServerListEntry.eCountry = static_cast<QLocale::Country> (
|
|
|
|
iCountry );
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the new server to the server list
|
|
|
|
ServerList.append ( NewServerListEntry );
|
|
|
|
|
|
|
|
// we have used four items and have created one predefined server
|
|
|
|
// (adjust counters)
|
|
|
|
iCurUsedServInfoSplitItems += 4;
|
|
|
|
iNumPredefinedServers++;
|
|
|
|
}
|
|
|
|
|
2016-02-09 17:09:59 +01:00
|
|
|
// for slave servers start the one shot timer for determining if it is a
|
|
|
|
// permanent server
|
|
|
|
if ( !GetIsCentralServer() )
|
|
|
|
{
|
|
|
|
// 1 minute = 60 * 1000 ms
|
|
|
|
QTimer::singleShot ( SERVLIST_TIME_PERMSERV_MINUTES * 60000,
|
|
|
|
this, SLOT ( OnTimerIsPermanent() ) );
|
|
|
|
}
|
|
|
|
|
2020-04-18 08:39:08 +02:00
|
|
|
// prepare the register server response timer (single shot timer)
|
|
|
|
TimerCLRegisterServerResp.setSingleShot ( true );
|
|
|
|
TimerCLRegisterServerResp.setInterval ( REGISTER_SERVER_TIME_OUT_MS );
|
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
|
|
|
|
// Connections -------------------------------------------------------------
|
|
|
|
QObject::connect ( &TimerPollList, SIGNAL ( timeout() ),
|
|
|
|
this, SLOT ( OnTimerPollList() ) );
|
|
|
|
|
|
|
|
QObject::connect ( &TimerPingServerInList, SIGNAL ( timeout() ),
|
|
|
|
this, SLOT ( OnTimerPingServerInList() ) );
|
|
|
|
|
|
|
|
QObject::connect ( &TimerPingCentralServer, SIGNAL ( timeout() ),
|
|
|
|
this, SLOT ( OnTimerPingCentralServer() ) );
|
|
|
|
|
|
|
|
QObject::connect ( &TimerRegistering, SIGNAL ( timeout() ),
|
|
|
|
this, SLOT ( OnTimerRegistering() ) );
|
2020-04-14 22:00:08 +02:00
|
|
|
|
|
|
|
QObject::connect ( &TimerCLRegisterServerResp, SIGNAL ( timeout() ),
|
|
|
|
this, SLOT ( OnTimerCLRegisterServerResp() ) );
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CServerListManager::SetCentralServerAddress ( const QString sNCentServAddr )
|
|
|
|
{
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
|
|
|
strCentralServerAddress = sNCentServAddr;
|
|
|
|
|
|
|
|
// per definition: If the central server address is empty, the server list
|
|
|
|
// is disabled.
|
|
|
|
// per definition: If we are in server mode and the central server address
|
|
|
|
// is the localhost address, we are in central server mode. For the central
|
|
|
|
// server, the server list is always enabled.
|
|
|
|
if ( !strCentralServerAddress.isEmpty() )
|
|
|
|
{
|
|
|
|
bIsCentralServer =
|
2014-02-23 19:17:46 +01:00
|
|
|
(
|
|
|
|
( !strCentralServerAddress.toLower().compare ( "localhost" ) ||
|
|
|
|
!strCentralServerAddress.compare ( "127.0.0.1" ) ) &&
|
2020-05-17 11:00:56 +02:00
|
|
|
( eCentralServerAddressType == AT_CUSTOM )
|
2014-02-23 19:17:46 +01:00
|
|
|
);
|
2012-07-09 14:52:23 +02:00
|
|
|
|
|
|
|
bEnabled = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bIsCentralServer = false;
|
2014-02-21 22:16:45 +01:00
|
|
|
bEnabled = false;
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerListManager::Update()
|
|
|
|
{
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
|
|
|
if ( bEnabled )
|
|
|
|
{
|
|
|
|
if ( bIsCentralServer )
|
|
|
|
{
|
|
|
|
// start timer for polling the server list if enabled
|
|
|
|
// 1 minute = 60 * 1000 ms
|
|
|
|
TimerPollList.start ( SERVLIST_POLL_TIME_MINUTES * 60000 );
|
|
|
|
|
2012-07-14 18:18:09 +02:00
|
|
|
if ( bCentServPingServerInList )
|
|
|
|
{
|
|
|
|
// start timer for sending ping messages to servers in the list
|
|
|
|
TimerPingServerInList.start ( SERVLIST_UPDATE_PING_SERVERS_MS );
|
|
|
|
}
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// initiate registration right away so that we do not have to wait
|
|
|
|
// for the first time out of the timer until the slave server gets
|
|
|
|
// registered at the central server, note that we have to unlock
|
|
|
|
// the mutex before calling the function since inside this function
|
|
|
|
// the mutex is locked, too
|
|
|
|
locker.unlock();
|
|
|
|
{
|
|
|
|
OnTimerRegistering();
|
|
|
|
}
|
|
|
|
locker.relock();
|
|
|
|
|
2020-04-14 22:00:08 +02:00
|
|
|
// reset the retry counter to zero because update was called
|
|
|
|
iSvrRegRetries = 0;
|
|
|
|
|
|
|
|
// start timer for registration timeout
|
|
|
|
TimerCLRegisterServerResp.start();
|
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
// start timer for registering this server at the central server
|
|
|
|
// 1 minute = 60 * 1000 ms
|
|
|
|
TimerRegistering.start ( SERVLIST_REGIST_INTERV_MINUTES * 60000 );
|
|
|
|
|
|
|
|
// Start timer for ping the central server in short intervals to
|
|
|
|
// keep the port open at the NAT router.
|
|
|
|
// If no NAT is used, we send the messages anyway since they do
|
|
|
|
// not hurt (very low traffic). We also reuse the same update
|
|
|
|
// time as used in the central server for pinging the slave
|
|
|
|
// servers.
|
|
|
|
TimerPingCentralServer.start ( SERVLIST_UPDATE_PING_SERVERS_MS );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// disable service -> stop timer
|
|
|
|
if ( bIsCentralServer )
|
|
|
|
{
|
|
|
|
TimerPollList.stop();
|
2012-07-14 18:18:09 +02:00
|
|
|
|
|
|
|
if ( bCentServPingServerInList )
|
|
|
|
{
|
|
|
|
TimerPingServerInList.stop();
|
|
|
|
}
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-14 22:00:08 +02:00
|
|
|
TimerCLRegisterServerResp.stop();
|
2012-07-09 14:52:23 +02:00
|
|
|
TimerRegistering.stop();
|
|
|
|
TimerPingCentralServer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Central server functionality ***********************************************/
|
|
|
|
void CServerListManager::OnTimerPingServerInList()
|
|
|
|
{
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
|
|
|
const int iCurServerListSize = ServerList.size();
|
|
|
|
|
|
|
|
// send ping to list entries except of the very first one (which is the central
|
|
|
|
// server entry) and the predefined servers
|
|
|
|
for ( int iIdx = 1 + iNumPredefinedServers; iIdx < iCurServerListSize; iIdx++ )
|
|
|
|
{
|
|
|
|
// send empty message to keep NAT port open at slave server
|
|
|
|
pConnLessProtocol->CreateCLEmptyMes ( ServerList[iIdx].HostAddr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerListManager::OnTimerPollList()
|
|
|
|
{
|
2020-04-30 19:26:04 +02:00
|
|
|
CVector<CHostAddress> vecRemovedHostAddr;
|
2020-04-26 14:57:14 +02:00
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
|
|
|
// Check all list entries except of the very first one (which is the central
|
|
|
|
// server entry) and the predefined servers if they are still valid.
|
|
|
|
// Note that we have to use "ServerList.size()" function in the for loop
|
|
|
|
// since we may remove elements from the server list inside the for loop.
|
2020-04-14 22:00:08 +02:00
|
|
|
for ( int iIdx = 1 + iNumPredefinedServers; iIdx < ServerList.size(); )
|
2012-07-09 14:52:23 +02:00
|
|
|
{
|
|
|
|
// 1 minute = 60 * 1000 ms
|
2020-04-18 08:39:08 +02:00
|
|
|
if ( ServerList[iIdx].RegisterTime.elapsed() > ( SERVLIST_TIME_OUT_MINUTES * 60000 ) )
|
2012-07-09 14:52:23 +02:00
|
|
|
{
|
|
|
|
// remove this list entry
|
2020-04-30 19:26:04 +02:00
|
|
|
vecRemovedHostAddr.Add ( ServerList[iIdx].HostAddr );
|
2012-07-09 14:52:23 +02:00
|
|
|
ServerList.removeAt ( iIdx );
|
|
|
|
}
|
2020-04-14 22:00:08 +02:00
|
|
|
else
|
|
|
|
{
|
2020-04-18 08:39:08 +02:00
|
|
|
// move to the next entry (only on else)
|
2020-04-14 22:00:08 +02:00
|
|
|
iIdx++;
|
|
|
|
}
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
2020-04-30 19:26:04 +02:00
|
|
|
|
2020-04-26 14:57:14 +02:00
|
|
|
locker.unlock();
|
2020-04-30 19:26:04 +02:00
|
|
|
|
|
|
|
foreach ( const CHostAddress HostAddr, vecRemovedHostAddr )
|
2020-04-26 14:57:14 +02:00
|
|
|
{
|
2020-04-30 19:26:04 +02:00
|
|
|
tsConsoleStream << "Expired entry for " << HostAddr.toString() << endl;
|
2020-04-26 14:57:14 +02:00
|
|
|
}
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CServerListManager::CentralServerRegisterServer ( const CHostAddress& InetAddr,
|
2020-04-09 20:06:34 +02:00
|
|
|
const CHostAddress& LInetAddr,
|
2012-07-09 14:52:23 +02:00
|
|
|
const CServerCoreInfo& ServerInfo )
|
|
|
|
{
|
|
|
|
if ( bIsCentralServer && bEnabled )
|
|
|
|
{
|
2020-04-26 14:57:14 +02:00
|
|
|
tsConsoleStream << "Requested to register entry for "
|
|
|
|
<< InetAddr.toString() << " (" << LInetAddr.toString() << ")"
|
|
|
|
<< ": " << ServerInfo.strName << endl;
|
|
|
|
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
const int iCurServerListSize = ServerList.size();
|
|
|
|
|
2020-04-12 14:26:21 +02:00
|
|
|
// define invalid index used as a flag
|
|
|
|
const int ciInvalidIdx = -1;
|
|
|
|
|
|
|
|
// Check if server is already registered.
|
|
|
|
// The very first list entry must not be checked since
|
|
|
|
// this is per definition the central server (i.e., this server)
|
|
|
|
int iSelIdx = ciInvalidIdx; // initialize with an illegal value
|
|
|
|
for ( int iIdx = 1; iIdx < iCurServerListSize; iIdx++ )
|
2012-07-09 14:52:23 +02:00
|
|
|
{
|
2020-04-12 14:26:21 +02:00
|
|
|
if ( ServerList[iIdx].HostAddr == InetAddr )
|
2012-07-09 14:52:23 +02:00
|
|
|
{
|
2020-04-12 14:26:21 +02:00
|
|
|
// store entry index
|
|
|
|
iSelIdx = iIdx;
|
2012-07-09 14:52:23 +02:00
|
|
|
|
2020-04-12 14:26:21 +02:00
|
|
|
// entry found, leave for-loop
|
|
|
|
continue;
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
2020-04-12 14:26:21 +02:00
|
|
|
}
|
2012-07-09 14:52:23 +02:00
|
|
|
|
2020-04-12 14:26:21 +02:00
|
|
|
// if server is not yet registered, we have to create a new entry
|
|
|
|
if ( iSelIdx == ciInvalidIdx )
|
|
|
|
{
|
|
|
|
// check for maximum allowed number of servers in the server list
|
|
|
|
if ( iCurServerListSize < MAX_NUM_SERVERS_IN_SERVER_LIST )
|
2012-07-09 14:52:23 +02:00
|
|
|
{
|
|
|
|
// create a new server list entry and init with received data
|
2020-04-09 20:06:34 +02:00
|
|
|
ServerList.append ( CServerListEntry ( InetAddr, LInetAddr, ServerInfo ) );
|
2020-04-14 22:00:08 +02:00
|
|
|
iSelIdx = iCurServerListSize;
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
2020-04-12 14:26:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do not update the information in the predefined servers
|
|
|
|
if ( iSelIdx > iNumPredefinedServers )
|
2012-07-09 14:52:23 +02:00
|
|
|
{
|
2020-04-12 14:26:21 +02:00
|
|
|
// update all data and call update registration function
|
|
|
|
ServerList[iSelIdx].LHostAddr = LInetAddr;
|
|
|
|
ServerList[iSelIdx].strName = ServerInfo.strName;
|
|
|
|
ServerList[iSelIdx].eCountry = ServerInfo.eCountry;
|
|
|
|
ServerList[iSelIdx].strCity = ServerInfo.strCity;
|
|
|
|
ServerList[iSelIdx].iMaxNumClients = ServerInfo.iMaxNumClients;
|
|
|
|
ServerList[iSelIdx].bPermanentOnline = ServerInfo.bPermanentOnline;
|
|
|
|
|
|
|
|
ServerList[iSelIdx].UpdateRegistration();
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 22:00:08 +02:00
|
|
|
|
|
|
|
pConnLessProtocol->CreateCLRegisterServerResp ( InetAddr, iSelIdx == ciInvalidIdx
|
|
|
|
? ESvrRegResult::SRR_CENTRAL_SVR_FULL
|
|
|
|
: ESvrRegResult::SRR_REGISTERED );
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerListManager::CentralServerUnregisterServer ( const CHostAddress& InetAddr )
|
|
|
|
{
|
|
|
|
if ( bIsCentralServer && bEnabled )
|
|
|
|
{
|
2020-04-26 14:57:14 +02:00
|
|
|
tsConsoleStream << "Requested to unregister entry for "
|
|
|
|
<< InetAddr.toString() << endl;
|
|
|
|
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
const int iCurServerListSize = ServerList.size();
|
|
|
|
|
|
|
|
// Find the server to unregister in the list. The very first list entry
|
|
|
|
// must not be checked since this is per definition the central server
|
|
|
|
// (i.e., this server), also the predefined servers must not be checked.
|
|
|
|
for ( int iIdx = 1 + iNumPredefinedServers; iIdx < iCurServerListSize; iIdx++ )
|
|
|
|
{
|
|
|
|
if ( ServerList[iIdx].HostAddr == InetAddr )
|
|
|
|
{
|
|
|
|
// remove this list entry
|
|
|
|
ServerList.removeAt ( iIdx );
|
|
|
|
|
|
|
|
// entry found, leave for-loop (it is important to exit the
|
|
|
|
// for loop since when we remove an item from the server list,
|
|
|
|
// "iCurServerListSize" is not correct anymore and we could get
|
|
|
|
// a segmentation fault)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerListManager::CentralServerQueryServerList ( const CHostAddress& InetAddr )
|
|
|
|
{
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
|
|
|
if ( bIsCentralServer && bEnabled )
|
|
|
|
{
|
|
|
|
const int iCurServerListSize = ServerList.size();
|
|
|
|
|
|
|
|
// allocate memory for the entire list
|
|
|
|
CVector<CServerInfo> vecServerInfo ( iCurServerListSize );
|
|
|
|
|
|
|
|
// copy the list (we have to copy it since the message requires
|
|
|
|
// a vector but the list is actually stored in a QList object and
|
|
|
|
// not in a vector object
|
|
|
|
for ( int iIdx = 0; iIdx < iCurServerListSize; iIdx++ )
|
|
|
|
{
|
|
|
|
// copy list item
|
|
|
|
vecServerInfo[iIdx] = ServerList[iIdx];
|
|
|
|
|
|
|
|
if ( iIdx > 0 )
|
|
|
|
{
|
|
|
|
// check if the address of the client which is requesting the
|
|
|
|
// list is the same address as one server in the list -> in this
|
2020-04-09 20:06:34 +02:00
|
|
|
// case he has to connect to the local host address and port
|
|
|
|
// to allow for NAT.
|
2012-07-09 14:52:23 +02:00
|
|
|
if ( vecServerInfo[iIdx].HostAddr.InetAddr == InetAddr.InetAddr )
|
|
|
|
{
|
2020-04-09 20:06:34 +02:00
|
|
|
// for a predefined server:
|
|
|
|
// - LHostAddr and HostAddr are the same
|
|
|
|
// - no local port number is supplied
|
|
|
|
// otherwise, use the supplied details
|
2012-07-09 14:52:23 +02:00
|
|
|
if ( iIdx > iNumPredefinedServers )
|
|
|
|
{
|
2020-04-12 14:26:21 +02:00
|
|
|
vecServerInfo[iIdx].HostAddr = ServerList[iIdx].LHostAddr;
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// create "send empty message" for all registered servers
|
|
|
|
// (except of the very first list entry since this is this
|
|
|
|
// server (central server) per definition) and also it is
|
|
|
|
// not required to send this message, if the server is on
|
|
|
|
// the same computer
|
|
|
|
pConnLessProtocol->CreateCLSendEmptyMesMes (
|
|
|
|
vecServerInfo[iIdx].HostAddr,
|
|
|
|
InetAddr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send the server list to the client
|
|
|
|
pConnLessProtocol->CreateCLServerListMes ( InetAddr, vecServerInfo );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Slave server functionality *************************************************/
|
2020-04-14 22:00:08 +02:00
|
|
|
void CServerListManager::StoreRegistrationResult ( ESvrRegResult eResult )
|
|
|
|
{
|
|
|
|
// we need the lock since the user might change the server properties at
|
|
|
|
// any time so another response could arrive
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
2020-04-18 08:39:08 +02:00
|
|
|
// we got some response, so stop the retry timer
|
2020-04-14 22:00:08 +02:00
|
|
|
TimerCLRegisterServerResp.stop();
|
|
|
|
|
2020-04-18 08:39:08 +02:00
|
|
|
switch ( eResult )
|
2020-04-14 22:00:08 +02:00
|
|
|
{
|
|
|
|
case ESvrRegResult::SRR_REGISTERED:
|
2020-04-19 00:12:20 +02:00
|
|
|
SetSvrRegStatus ( ESvrRegStatus::SRS_REGISTERED );
|
2020-04-19 12:15:36 +02:00
|
|
|
break;
|
2020-04-18 08:39:08 +02:00
|
|
|
|
2020-04-14 22:00:08 +02:00
|
|
|
case ESvrRegResult::SRR_CENTRAL_SVR_FULL:
|
2020-04-19 00:12:20 +02:00
|
|
|
SetSvrRegStatus ( ESvrRegStatus::SRS_CENTRAL_SVR_FULL );
|
2020-04-19 12:15:36 +02:00
|
|
|
break;
|
2020-04-14 22:00:08 +02:00
|
|
|
|
2020-04-19 12:15:36 +02:00
|
|
|
default:
|
|
|
|
SetSvrRegStatus ( ESvrRegStatus::SRS_UNKNOWN_RESP );
|
|
|
|
break;
|
|
|
|
}
|
2020-04-14 22:00:08 +02:00
|
|
|
}
|
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
void CServerListManager::OnTimerPingCentralServer()
|
|
|
|
{
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
|
|
|
// first check if central server address is valid
|
|
|
|
if ( !( SlaveCurCentServerHostAddress == CHostAddress() ) )
|
|
|
|
{
|
|
|
|
// send empty message to central server to keep NAT port open -> we do
|
|
|
|
// not require any answer from the central server
|
|
|
|
pConnLessProtocol->CreateCLEmptyMes ( SlaveCurCentServerHostAddress );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 22:00:08 +02:00
|
|
|
void CServerListManager::OnTimerCLRegisterServerResp()
|
|
|
|
{
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
|
|
|
if ( eSvrRegStatus == SRS_REQUESTED )
|
|
|
|
{
|
|
|
|
iSvrRegRetries++;
|
2020-04-18 08:39:08 +02:00
|
|
|
|
2020-04-14 22:00:08 +02:00
|
|
|
if ( iSvrRegRetries >= REGISTER_SERVER_RETRY_LIMIT )
|
|
|
|
{
|
2020-04-19 00:12:20 +02:00
|
|
|
SetSvrRegStatus ( SRS_TIME_OUT );
|
2020-04-14 22:00:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
locker.unlock();
|
|
|
|
{
|
|
|
|
OnTimerRegistering();
|
|
|
|
}
|
|
|
|
locker.relock();
|
|
|
|
|
|
|
|
// re-start timer for registration timeout
|
|
|
|
TimerCLRegisterServerResp.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
void CServerListManager::SlaveServerRegisterServer ( const bool bIsRegister )
|
|
|
|
{
|
|
|
|
// we need the lock since the user might change the server properties at
|
|
|
|
// any time
|
|
|
|
QMutexLocker locker ( &Mutex );
|
|
|
|
|
|
|
|
// get the correct central server address
|
|
|
|
const QString strCurCentrServAddr =
|
2020-04-11 14:27:50 +02:00
|
|
|
NetworkUtil::GetCentralServerAddress ( eCentralServerAddressType,
|
|
|
|
strCentralServerAddress );
|
2012-07-09 14:52:23 +02:00
|
|
|
|
|
|
|
// For the slave server, the slave server properties are stored in the
|
|
|
|
// very first item in the server list (which is actually no server list
|
|
|
|
// but just one item long for the slave server).
|
|
|
|
// Note that we always have to parse the server address again since if
|
|
|
|
// it is an URL of a dynamic IP address, the IP address might have
|
|
|
|
// changed in the meanwhile.
|
2013-03-24 12:38:00 +01:00
|
|
|
if ( NetworkUtil().ParseNetworkAddress ( strCurCentrServAddr,
|
|
|
|
SlaveCurCentServerHostAddress ) )
|
2012-07-09 14:52:23 +02:00
|
|
|
{
|
|
|
|
if ( bIsRegister )
|
|
|
|
{
|
|
|
|
// register server
|
2020-04-19 00:12:20 +02:00
|
|
|
SetSvrRegStatus ( SRS_REQUESTED );
|
2020-04-14 22:00:08 +02:00
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
pConnLessProtocol->CreateCLRegisterServerMes ( SlaveCurCentServerHostAddress,
|
2020-04-09 20:06:34 +02:00
|
|
|
SlaveCurLocalHostAddress,
|
2012-07-09 14:52:23 +02:00
|
|
|
ServerList[0] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// unregister server
|
2020-04-19 00:12:20 +02:00
|
|
|
SetSvrRegStatus ( SRS_UNREGISTERED );
|
2020-04-14 22:00:08 +02:00
|
|
|
|
2012-07-09 14:52:23 +02:00
|
|
|
pConnLessProtocol->CreateCLUnregisterServerMes ( SlaveCurCentServerHostAddress );
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 22:00:08 +02:00
|
|
|
else
|
|
|
|
{
|
2020-04-19 00:12:20 +02:00
|
|
|
SetSvrRegStatus ( SRS_BAD_ADDRESS );
|
2020-04-14 22:00:08 +02:00
|
|
|
}
|
2012-07-09 14:52:23 +02:00
|
|
|
}
|
2020-04-19 12:15:36 +02:00
|
|
|
|
2020-04-19 00:12:20 +02:00
|
|
|
void CServerListManager::SetSvrRegStatus ( ESvrRegStatus eNSvrRegStatus )
|
|
|
|
{
|
2020-04-19 12:15:36 +02:00
|
|
|
// output regirstation result/update on the console
|
2020-04-19 20:31:04 +02:00
|
|
|
tsConsoleStream << "Server Registration Status update: " << svrRegStatusToString ( eNSvrRegStatus ) << endl;
|
2020-04-19 12:15:36 +02:00
|
|
|
|
|
|
|
// store the state and inform the GUI about the new status
|
2020-04-19 00:12:20 +02:00
|
|
|
eSvrRegStatus = eNSvrRegStatus;
|
|
|
|
emit SvrRegStatusChanged();
|
|
|
|
}
|