Tidy away local port in CServerCoreInfo etc

This commit is contained in:
Peter L Jones 2020-04-12 13:26:21 +01:00
parent 8fd59dd93f
commit 84a95686bb
5 changed files with 64 additions and 87 deletions

View file

@ -197,9 +197,9 @@ CONNECTION LESS MESSAGES
- PROTMESSID_CLM_REGISTER_SERVER: Register a server, providing server - PROTMESSID_CLM_REGISTER_SERVER: Register a server, providing server
information information
+--------------+ ... +------------------------------+ ...
| 2 bytes port | ... | 2 bytes server internal port | ...
+--------------+ ... +------------------------------+ ...
... -----------------+----------------------------------+ ... ... -----------------+----------------------------------+ ...
... 2 bytes country | 1 byte maximum connected clients | ... ... 2 bytes country | 1 byte maximum connected clients | ...
... -----------------+----------------------------------+ ... ... -----------------+----------------------------------+ ...
@ -225,6 +225,9 @@ CONNECTION LESS MESSAGES
is permanent online. is permanent online.
- "server interal address" represents the IPv4 address as a dotted quad to - "server interal address" represents the IPv4 address as a dotted quad to
be used by clients with the same external IP address as the server. be used by clients with the same external IP address as the server.
NOTE: In the PROTMESSID_CLM_SERVER_LIST list, this field will be empty
as only the initial IP address should be used by the client. Where
necessary, that value will contain the server internal address.
- PROTMESSID_CLM_UNREGISTER_SERVER: Unregister a server - PROTMESSID_CLM_UNREGISTER_SERVER: Unregister a server
@ -1415,7 +1418,7 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr,
// size of current message body // size of current message body
const int iEntrLen = const int iEntrLen =
2 /* port number */ + 2 /* server internal port number */ +
2 /* country */ + 2 /* country */ +
1 /* maximum number of connected clients */ + 1 /* maximum number of connected clients */ +
1 /* is permanent flag */ + 1 /* is permanent flag */ +
@ -1428,7 +1431,7 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr,
// port number (2 bytes) // port number (2 bytes)
PutValOnStream ( vecData, iPos, PutValOnStream ( vecData, iPos,
static_cast<uint32_t> ( ServerInfo.iLocalPortNumber ), 2 ); static_cast<uint32_t> ( LInetAddr.iPort ), 2 );
// country (2 bytes) // country (2 bytes)
PutValOnStream ( vecData, iPos, PutValOnStream ( vecData, iPos,
@ -1472,7 +1475,7 @@ bool CProtocol::EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr,
} }
// port number (2 bytes) // port number (2 bytes)
RecServerInfo.iLocalPortNumber = LInetAddr.iPort =
static_cast<quint16> ( GetValFromStream ( vecData, iPos, 2 ) ); static_cast<quint16> ( GetValFromStream ( vecData, iPos, 2 ) );
// country (2 bytes) // country (2 bytes)
@ -1563,9 +1566,9 @@ void CProtocol::CreateCLServerListMes ( const CHostAddress& InetAddr,
for ( int i = 0; i < iNumServers; i++ ) for ( int i = 0; i < iNumServers; i++ )
{ {
// convert server list strings to utf-8 // convert server list strings to utf-8
const QByteArray strUTF8Name = vecServerInfo[i].strName.toUtf8(); const QByteArray strUTF8Name = vecServerInfo[i].strName.toUtf8();
const QByteArray strUTF8LHostAddr = vecServerInfo[i].LHostAddr.InetAddr.toString().toUtf8(); const QByteArray strUTF8Empty = QString("").toUtf8();
const QByteArray strUTF8City = vecServerInfo[i].strCity.toUtf8(); const QByteArray strUTF8City = vecServerInfo[i].strCity.toUtf8();
// size of current list entry // size of current list entry
const int iCurListEntrLen = const int iCurListEntrLen =
@ -1575,17 +1578,19 @@ void CProtocol::CreateCLServerListMes ( const CHostAddress& InetAddr,
1 /* maximum number of connected clients */ + 1 /* maximum number of connected clients */ +
1 /* is permanent flag */ + 1 /* is permanent flag */ +
2 /* name utf-8 string size */ + strUTF8Name.size() + 2 /* name utf-8 string size */ + strUTF8Name.size() +
2 /* server internal address utf-8 string size */ + strUTF8LHostAddr.size() + 2 /* empty string */ +
2 /* city utf-8 string size */ + strUTF8City.size(); 2 /* city utf-8 string size */ + strUTF8City.size();
// make space for new data // make space for new data
vecData.Enlarge ( iCurListEntrLen ); vecData.Enlarge ( iCurListEntrLen );
// IP address (4 bytes) // IP address (4 bytes)
// note the Server List manager has put the internal details in HostAddr where required
PutValOnStream ( vecData, iPos, static_cast<uint32_t> ( PutValOnStream ( vecData, iPos, static_cast<uint32_t> (
vecServerInfo[i].HostAddr.InetAddr.toIPv4Address() ), 4 ); vecServerInfo[i].HostAddr.InetAddr.toIPv4Address() ), 4 );
// port number (2 bytes) // port number (2 bytes)
// note the Server List manager has put the internal details in HostAddr where required
PutValOnStream ( vecData, iPos, PutValOnStream ( vecData, iPos,
static_cast<uint32_t> ( vecServerInfo[i].HostAddr.iPort ), 2 ); static_cast<uint32_t> ( vecServerInfo[i].HostAddr.iPort ), 2 );
@ -1604,8 +1609,8 @@ void CProtocol::CreateCLServerListMes ( const CHostAddress& InetAddr,
// name // name
PutStringUTF8OnStream ( vecData, iPos, strUTF8Name ); PutStringUTF8OnStream ( vecData, iPos, strUTF8Name );
// topic // empty string
PutStringUTF8OnStream ( vecData, iPos, strUTF8LHostAddr ); PutStringUTF8OnStream ( vecData, iPos, strUTF8Empty );
// city // city
PutStringUTF8OnStream ( vecData, iPos, strUTF8City ); PutStringUTF8OnStream ( vecData, iPos, strUTF8City );
@ -1661,23 +1666,12 @@ bool CProtocol::EvaluateCLServerListMes ( const CHostAddress& InetAddr,
return true; // return error code return true; // return error code
} }
// server internal address // empty
QString strLHostAddr; QString strEmpty;
if ( GetStringFromStream ( vecData, if ( GetStringFromStream ( vecData,
iPos, iPos,
MAX_LEN_IP_ADDRESS, MAX_LEN_IP_ADDRESS,
strLHostAddr ) ) strEmpty ) )
{
return true; // return error code
}
CHostAddress LInetAddr;
if ( strLHostAddr.isEmpty() )
{
// old central server, empty "topic", default to localhost
LInetAddr.InetAddr.setAddress ( QHostAddress::LocalHost );
}
else if ( !LInetAddr.InetAddr.setAddress ( strLHostAddr ) )
{ {
return true; // return error code return true; // return error code
} }
@ -1695,8 +1689,7 @@ bool CProtocol::EvaluateCLServerListMes ( const CHostAddress& InetAddr,
// add server information to vector // add server information to vector
vecServerInfo.Add ( vecServerInfo.Add (
CServerInfo ( CHostAddress ( QHostAddress ( iIpAddr ), iPort ), CServerInfo ( CHostAddress ( QHostAddress ( iIpAddr ), iPort ),
LInetAddr, CHostAddress ( QHostAddress ( iIpAddr ), iPort ),
iPort,
strName, strName,
eCountry, eCountry,
strCity, strCity,

View file

@ -31,8 +31,7 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum,
const int iNumChannels, const int iNumChannels,
const bool bNCentServPingServerInList, const bool bNCentServPingServerInList,
CProtocol* pNConLProt ) CProtocol* pNConLProt )
: iPortNumber ( iNPortNum ), : iNumPredefinedServers ( 0 ),
iNumPredefinedServers ( 0 ),
eCentralServerAddressType ( AT_MANUAL ), // must be AT_MANUAL for the "no GUI" case eCentralServerAddressType ( AT_MANUAL ), // must be AT_MANUAL for the "no GUI" case
bCentServPingServerInList ( bNCentServPingServerInList ), bCentServPingServerInList ( bNCentServPingServerInList ),
pConnLessProtocol ( pNConLProt ) pConnLessProtocol ( pNConLProt )
@ -40,7 +39,7 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum,
// set the central server address // set the central server address
SetCentralServerAddress ( sNCentServAddr ); SetCentralServerAddress ( sNCentServAddr );
// set the server internal address // set the server internal address, including internal port number
SlaveCurLocalHostAddress = CHostAddress( NetworkUtil::GetLocalAddress().InetAddr, iNPortNum ); SlaveCurLocalHostAddress = CHostAddress( NetworkUtil::GetLocalAddress().InetAddr, iNPortNum );
// prepare the server info information // prepare the server info information
@ -66,7 +65,6 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum,
// we have a permanent server. // we have a permanent server.
CServerListEntry ThisServerListEntry ( CHostAddress(), CServerListEntry ThisServerListEntry ( CHostAddress(),
SlaveCurLocalHostAddress, SlaveCurLocalHostAddress,
iPortNumber,
"", "",
QLocale::system().country(), QLocale::system().country(),
"", "",
@ -115,7 +113,6 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum,
// registered via the command line are permanent servers // registered via the command line are permanent servers
CServerListEntry NewServerListEntry ( CHostAddress(), CServerListEntry NewServerListEntry ( CHostAddress(),
CHostAddress(), CHostAddress(),
0, // port number not used
"", "",
QLocale::AnyCountry, QLocale::AnyCountry,
"", "",
@ -322,49 +319,49 @@ void CServerListManager::CentralServerRegisterServer ( const CHostAddress& In
{ {
const int iCurServerListSize = ServerList.size(); const int iCurServerListSize = ServerList.size();
// check for maximum allowed number of servers in the server list // define invalid index used as a flag
if ( iCurServerListSize < MAX_NUM_SERVERS_IN_SERVER_LIST ) 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++ )
{ {
// define invalid index used as a flag if ( ServerList[iIdx].HostAddr == InetAddr )
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++ )
{ {
if ( ServerList[iIdx].HostAddr == InetAddr ) // store entry index
{ iSelIdx = iIdx;
// store entry index
iSelIdx = iIdx;
// entry found, leave for-loop // entry found, leave for-loop
continue; continue;
}
} }
}
// if server is not yet registered, we have to create a new entry // if server is not yet registered, we have to create a new entry
if ( iSelIdx == ciInvalidIdx ) if ( iSelIdx == ciInvalidIdx )
{
// check for maximum allowed number of servers in the server list
if ( iCurServerListSize < MAX_NUM_SERVERS_IN_SERVER_LIST )
{ {
// create a new server list entry and init with received data // create a new server list entry and init with received data
ServerList.append ( CServerListEntry ( InetAddr, LInetAddr, ServerInfo ) ); ServerList.append ( CServerListEntry ( InetAddr, LInetAddr, ServerInfo ) );
} }
else }
else
{
// do not update the information in the predefined servers
if ( iSelIdx > iNumPredefinedServers )
{ {
// do not update the information in the predefined servers // update all data and call update registration function
if ( iSelIdx > iNumPredefinedServers ) ServerList[iSelIdx].LHostAddr = LInetAddr;
{ ServerList[iSelIdx].strName = ServerInfo.strName;
// update all data and call update registration function ServerList[iSelIdx].eCountry = ServerInfo.eCountry;
ServerList[iSelIdx].LHostAddr = LInetAddr; ServerList[iSelIdx].strCity = ServerInfo.strCity;
ServerList[iSelIdx].strName = ServerInfo.strName; ServerList[iSelIdx].iMaxNumClients = ServerInfo.iMaxNumClients;
ServerList[iSelIdx].eCountry = ServerInfo.eCountry; ServerList[iSelIdx].bPermanentOnline = ServerInfo.bPermanentOnline;
ServerList[iSelIdx].strCity = ServerInfo.strCity;
ServerList[iSelIdx].iMaxNumClients = ServerInfo.iMaxNumClients;
ServerList[iSelIdx].bPermanentOnline = ServerInfo.bPermanentOnline;
ServerList[iSelIdx].UpdateRegistration(); ServerList[iSelIdx].UpdateRegistration();
}
} }
} }
} }
@ -431,11 +428,7 @@ void CServerListManager::CentralServerQueryServerList ( const CHostAddress& Inet
// otherwise, use the supplied details // otherwise, use the supplied details
if ( iIdx > iNumPredefinedServers ) if ( iIdx > iNumPredefinedServers )
{ {
vecServerInfo[iIdx].HostAddr.InetAddr = vecServerInfo[iIdx].HostAddr = ServerList[iIdx].LHostAddr;
ServerList[iIdx].LHostAddr.InetAddr;
vecServerInfo[iIdx].HostAddr.iPort =
ServerList[iIdx].iLocalPortNumber;
} }
} }
else else

View file

@ -76,7 +76,6 @@ public:
CServerListEntry() : CServerListEntry() :
CServerInfo ( CHostAddress(), CServerInfo ( CHostAddress(),
CHostAddress(), CHostAddress(),
0,
"", "",
QLocale::AnyCountry, QLocale::AnyCountry,
"", "",
@ -85,7 +84,6 @@ public:
CServerListEntry ( const CHostAddress& NHAddr, CServerListEntry ( const CHostAddress& NHAddr,
const CHostAddress& NLHAddr, const CHostAddress& NLHAddr,
const quint16 NLocPort,
const QString& NsName, const QString& NsName,
const QLocale::Country& NeCountry, const QLocale::Country& NeCountry,
const QString& NsCity, const QString& NsCity,
@ -93,7 +91,6 @@ public:
const bool NbPermOnline) const bool NbPermOnline)
: CServerInfo ( NHAddr, : CServerInfo ( NHAddr,
NLHAddr, NLHAddr,
NLocPort,
NsName, NsName,
NeCountry, NeCountry,
NsCity, NsCity,
@ -105,7 +102,6 @@ public:
const CServerCoreInfo& NewCoreServerInfo ) const CServerCoreInfo& NewCoreServerInfo )
: CServerInfo ( NHAddr, : CServerInfo ( NHAddr,
NLHAddr, NLHAddr,
NewCoreServerInfo.iLocalPortNumber,
NewCoreServerInfo.strName, NewCoreServerInfo.strName,
NewCoreServerInfo.eCountry, NewCoreServerInfo.eCountry,
NewCoreServerInfo.strCity, NewCoreServerInfo.strCity,

View file

@ -45,6 +45,9 @@ public:
sAddress ( sNewAddress ), sAddress ( sNewAddress ),
iPort ( iNewPort ) iPort ( iNewPort )
{ {
sLAddress = GenRandomIPv4Address().toString();
iLPort = static_cast<quint16> ( GenRandomIntInRange ( -2, 10000 ) );
// bind socket (try 100 port numbers) // bind socket (try 100 port numbers)
quint16 iPortIncrement = 0; // start value: port nubmer plus ten quint16 iPortIncrement = 0; // start value: port nubmer plus ten
bool bSuccess = false; // initialization for while loop bool bSuccess = false; // initialization for while loop
@ -99,6 +102,8 @@ protected:
QString sAddress; QString sAddress;
quint16 iPort; quint16 iPort;
QString sLAddress;
quint16 iLPort;
QTimer Timer; QTimer Timer;
CProtocol Protocol; CProtocol Protocol;
QUdpSocket UdpSocket; QUdpSocket UdpSocket;
@ -111,7 +116,7 @@ public slots:
CServerCoreInfo ServerInfo; CServerCoreInfo ServerInfo;
CVector<CServerInfo> vecServerInfo ( 1 ); CVector<CServerInfo> vecServerInfo ( 1 );
CHostAddress CurHostAddress ( QHostAddress ( sAddress ), iPort ); CHostAddress CurHostAddress ( QHostAddress ( sAddress ), iPort );
CHostAddress CurLocalAddress ( GenRandomIPv4Address(), iPort ); CHostAddress CurLocalAddress ( QHostAddress ( sLAddress ), iLPort );
CChannelCoreInfo ChannelCoreInfo; CChannelCoreInfo ChannelCoreInfo;
ELicenceType eLicenceType; ELicenceType eLicenceType;
@ -212,7 +217,6 @@ public slots:
ServerInfo.eCountry = ServerInfo.eCountry =
static_cast<QLocale::Country> ( GenRandomIntInRange ( 0, 100 ) ); static_cast<QLocale::Country> ( GenRandomIntInRange ( 0, 100 ) );
ServerInfo.iLocalPortNumber = GenRandomIntInRange ( -2, 10000 );
ServerInfo.iMaxNumClients = GenRandomIntInRange ( -2, 10000 ); ServerInfo.iMaxNumClients = GenRandomIntInRange ( -2, 10000 );
ServerInfo.strCity = GenRandomString(); ServerInfo.strCity = GenRandomString();
ServerInfo.strName = GenRandomString(); ServerInfo.strName = GenRandomString();
@ -235,7 +239,6 @@ public slots:
vecServerInfo[0].HostAddr = CurHostAddress; vecServerInfo[0].HostAddr = CurHostAddress;
vecServerInfo[0].LHostAddr = CurLocalAddress; vecServerInfo[0].LHostAddr = CurLocalAddress;
vecServerInfo[0].iLocalPortNumber = GenRandomIntInRange ( -2, 10000 );
vecServerInfo[0].iMaxNumClients = GenRandomIntInRange ( -2, 10000 ); vecServerInfo[0].iMaxNumClients = GenRandomIntInRange ( -2, 10000 );
vecServerInfo[0].strCity = GenRandomString(); vecServerInfo[0].strCity = GenRandomString();
vecServerInfo[0].strName = GenRandomString(); vecServerInfo[0].strName = GenRandomString();

View file

@ -870,7 +870,6 @@ class CServerCoreInfo
{ {
public: public:
CServerCoreInfo() : CServerCoreInfo() :
iLocalPortNumber ( 0 ),
strName ( "" ), strName ( "" ),
eCountry ( QLocale::AnyCountry ), eCountry ( QLocale::AnyCountry ),
strCity ( "" ), strCity ( "" ),
@ -878,22 +877,17 @@ public:
bPermanentOnline ( false ) {} bPermanentOnline ( false ) {}
CServerCoreInfo ( CServerCoreInfo (
const quint16 NLocPort,
const QString& NsName, const QString& NsName,
const QLocale::Country& NeCountry, const QLocale::Country& NeCountry,
const QString& NsCity, const QString& NsCity,
const int NiMaxNumClients, const int NiMaxNumClients,
const bool NbPermOnline) : const bool NbPermOnline) :
iLocalPortNumber ( NLocPort ),
strName ( NsName ), strName ( NsName ),
eCountry ( NeCountry ), eCountry ( NeCountry ),
strCity ( NsCity ), strCity ( NsCity ),
iMaxNumClients ( NiMaxNumClients ), iMaxNumClients ( NiMaxNumClients ),
bPermanentOnline ( NbPermOnline ) {} bPermanentOnline ( NbPermOnline ) {}
// local port number of the server
quint16 iLocalPortNumber;
// name of the server // name of the server
QString strName; QString strName;
@ -922,14 +916,12 @@ public:
CServerInfo ( CServerInfo (
const CHostAddress& NHAddr, const CHostAddress& NHAddr,
const CHostAddress& NLAddr, const CHostAddress& NLAddr,
const quint16 NLocPort,
const QString& NsName, const QString& NsName,
const QLocale::Country& NeCountry, const QLocale::Country& NeCountry,
const QString& NsCity, const QString& NsCity,
const int NiMaxNumClients, const int NiMaxNumClients,
const bool NbPermOnline) : const bool NbPermOnline) :
CServerCoreInfo ( NLocPort, CServerCoreInfo ( NsName,
NsName,
NeCountry, NeCountry,
NsCity, NsCity,
NiMaxNumClients, NiMaxNumClients,