support for sending ping messages to servers in the server list to keep to port open in the NAT of the slave server

This commit is contained in:
Volker Fischer 2012-06-05 06:25:19 +00:00
parent e32d80b9db
commit fa30a903a3
3 changed files with 468 additions and 437 deletions

View file

@ -168,6 +168,9 @@ LED bar: lbr
// poll time for server list (to check if entries are time-out) // poll time for server list (to check if entries are time-out)
#define SERVLIST_POLL_TIME_MINUTES 1 // minute #define SERVLIST_POLL_TIME_MINUTES 1 // minute
// time interval for sending ping messages to servers in the server list
#define SERVLIST_UPDATE_PING_SERVERS_MS 59000 // ms
// time until a slave server registers in the server list // time until a slave server registers in the server list
#define SERVLIST_REGIST_INTERV_MINUTES 30 // minutes #define SERVLIST_REGIST_INTERV_MINUTES 30 // minutes

View file

@ -134,8 +134,8 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum,
if ( ( iCountry >= 0 ) && ( iCountry <= QLocale::LastCountry ) ) if ( ( iCountry >= 0 ) && ( iCountry <= QLocale::LastCountry ) )
{ {
NewServerListEntry.eCountry = NewServerListEntry.eCountry = static_cast<QLocale::Country> (
static_cast<QLocale::Country> ( iCountry ); iCountry );
} }
// add the new server to the server list // add the new server to the server list
@ -144,7 +144,6 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum,
// we have used four items and have created one predefined server // we have used four items and have created one predefined server
// (adjust counters) // (adjust counters)
iCurUsedServInfoSplitItems += 4; iCurUsedServInfoSplitItems += 4;
iNumPredefinedServers++; iNumPredefinedServers++;
} }
@ -153,6 +152,9 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum,
QObject::connect ( &TimerPollList, SIGNAL ( timeout() ), QObject::connect ( &TimerPollList, SIGNAL ( timeout() ),
this, SLOT ( OnTimerPollList() ) ); this, SLOT ( OnTimerPollList() ) );
QObject::connect ( &TimerPingServerInList, SIGNAL ( timeout() ),
this, SLOT ( OnTimerPingServerInList() ) );
QObject::connect ( &TimerRegistering, SIGNAL ( timeout() ), QObject::connect ( &TimerRegistering, SIGNAL ( timeout() ),
this, SLOT ( OnTimerRegistering() ) ); this, SLOT ( OnTimerRegistering() ) );
} }
@ -194,6 +196,9 @@ void CServerListManager::Update()
// start timer for polling the server list if enabled // start timer for polling the server list if enabled
// 1 minute = 60 * 1000 ms // 1 minute = 60 * 1000 ms
TimerPollList.start ( SERVLIST_POLL_TIME_MINUTES * 60000 ); TimerPollList.start ( SERVLIST_POLL_TIME_MINUTES * 60000 );
// start timer for sending ping messages to servers in the list
TimerPingServerInList.start ( SERVLIST_UPDATE_PING_SERVERS_MS );
} }
else else
{ {
@ -219,6 +224,7 @@ void CServerListManager::Update()
if ( bIsCentralServer ) if ( bIsCentralServer )
{ {
TimerPollList.stop(); TimerPollList.stop();
TimerPingServerInList.stop();
} }
else else
{ {
@ -229,6 +235,29 @@ void CServerListManager::Update()
/* Central server functionality ***********************************************/ /* 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. Also, do not send the ping to
// servers which use the local port number since no port translation has
// been done and therefore the probability is high that a port forwarding was
// installed by the user of this server.
for ( int iIdx = 1 + iNumPredefinedServers; iIdx < iCurServerListSize; iIdx++ )
{
if ( ServerList[iIdx].HostAddr.iPort != ServerList[iIdx].iLocalPortNumber )
{
// send ping message to keep NAT port open at slave server
pConnLessProtocol->CreateCLPingMes (
ServerList[iIdx].HostAddr,
0 /* dummy */ );
}
}
}
void CServerListManager::OnTimerPollList() void CServerListManager::OnTimerPollList()
{ {
QMutexLocker locker ( &Mutex ); QMutexLocker locker ( &Mutex );
@ -268,7 +297,6 @@ void CServerListManager::CentralServerRegisterServer ( const CHostAddress& In
// a server. The very first list entry must not be checked since // a server. The very first list entry must not be checked since
// this is per definition the central server (i.e., this server) // this is per definition the central server (i.e., this server)
int iSelIdx = ciInvalidIdx; // initialize with an illegal value int iSelIdx = ciInvalidIdx; // initialize with an illegal value
for ( int iIdx = 1; iIdx < iCurServerListSize; iIdx++ ) for ( int iIdx = 1; iIdx < iCurServerListSize; iIdx++ )
{ {
if ( ServerList[iIdx].HostAddr == InetAddr ) if ( ServerList[iIdx].HostAddr == InetAddr )
@ -285,8 +313,6 @@ void CServerListManager::CentralServerRegisterServer ( const CHostAddress& In
if ( iSelIdx == ciInvalidIdx ) if ( iSelIdx == ciInvalidIdx )
{ {
// create a new server list entry and init with received data // create a new server list entry and init with received data
// (note that the "update registration" is called in the
// constructor of the server list entry)
ServerList.append ( CServerListEntry ( InetAddr, ServerInfo ) ); ServerList.append ( CServerListEntry ( InetAddr, ServerInfo ) );
} }
else else
@ -351,7 +377,7 @@ void CServerListManager::CentralServerQueryServerList ( const CHostAddress& Inet
// copy the list (we have to copy it since the message requires // 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 // a vector but the list is actually stored in a QList object and
// not in a vector object) // not in a vector object
for ( int iIdx = 0; iIdx < iCurServerListSize; iIdx++ ) for ( int iIdx = 0; iIdx < iCurServerListSize; iIdx++ )
{ {
// copy list item // copy list item
@ -418,7 +444,6 @@ void CServerListManager::SlaveServerRegisterServer ( const bool bIsRegister )
// it is an URL of a dynamic IP address, the IP address might have // it is an URL of a dynamic IP address, the IP address might have
// changed in the meanwhile. // changed in the meanwhile.
CHostAddress HostAddress; CHostAddress HostAddress;
if ( LlconNetwUtil().ParseNetworkAddress ( strCurCentrServAddr, if ( LlconNetwUtil().ParseNetworkAddress ( strCurCentrServAddr,
HostAddress ) ) HostAddress ) )
{ {

View file

@ -181,6 +181,8 @@ protected:
QTimer TimerPollList; QTimer TimerPollList;
QTimer TimerRegistering; QTimer TimerRegistering;
QTimer TimerPingServerInList;
QMutex Mutex; QMutex Mutex;
QList<CServerListEntry> ServerList; QList<CServerListEntry> ServerList;
@ -196,6 +198,7 @@ protected:
public slots: public slots:
void OnTimerPollList(); void OnTimerPollList();
void OnTimerPingServerInList();
void OnTimerRegistering() { SlaveServerRegisterServer ( true ); } void OnTimerRegistering() { SlaveServerRegisterServer ( true ); }
}; };