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
information
+--------------+ ...
| 2 bytes port | ...
+--------------+ ...
+------------------------------+ ...
| 2 bytes server internal port | ...
+------------------------------+ ...
... -----------------+----------------------------------+ ...
... 2 bytes country | 1 byte maximum connected clients | ...
... -----------------+----------------------------------+ ...
@ -225,6 +225,9 @@ CONNECTION LESS MESSAGES
is permanent online.
- "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.
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
@ -1415,7 +1418,7 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr,
// size of current message body
const int iEntrLen =
2 /* port number */ +
2 /* server internal port number */ +
2 /* country */ +
1 /* maximum number of connected clients */ +
1 /* is permanent flag */ +
@ -1428,7 +1431,7 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr,
// port number (2 bytes)
PutValOnStream ( vecData, iPos,
static_cast<uint32_t> ( ServerInfo.iLocalPortNumber ), 2 );
static_cast<uint32_t> ( LInetAddr.iPort ), 2 );
// country (2 bytes)
PutValOnStream ( vecData, iPos,
@ -1472,7 +1475,7 @@ bool CProtocol::EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr,
}
// port number (2 bytes)
RecServerInfo.iLocalPortNumber =
LInetAddr.iPort =
static_cast<quint16> ( GetValFromStream ( vecData, iPos, 2 ) );
// country (2 bytes)
@ -1564,7 +1567,7 @@ void CProtocol::CreateCLServerListMes ( const CHostAddress& InetAddr,
{
// convert server list strings to utf-8
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();
// size of current list entry
@ -1575,17 +1578,19 @@ void CProtocol::CreateCLServerListMes ( const CHostAddress& InetAddr,
1 /* maximum number of connected clients */ +
1 /* is permanent flag */ +
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();
// make space for new data
vecData.Enlarge ( iCurListEntrLen );
// 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> (
vecServerInfo[i].HostAddr.InetAddr.toIPv4Address() ), 4 );
// port number (2 bytes)
// note the Server List manager has put the internal details in HostAddr where required
PutValOnStream ( vecData, iPos,
static_cast<uint32_t> ( vecServerInfo[i].HostAddr.iPort ), 2 );
@ -1604,8 +1609,8 @@ void CProtocol::CreateCLServerListMes ( const CHostAddress& InetAddr,
// name
PutStringUTF8OnStream ( vecData, iPos, strUTF8Name );
// topic
PutStringUTF8OnStream ( vecData, iPos, strUTF8LHostAddr );
// empty string
PutStringUTF8OnStream ( vecData, iPos, strUTF8Empty );
// city
PutStringUTF8OnStream ( vecData, iPos, strUTF8City );
@ -1661,23 +1666,12 @@ bool CProtocol::EvaluateCLServerListMes ( const CHostAddress& InetAddr,
return true; // return error code
}
// server internal address
QString strLHostAddr;
// empty
QString strEmpty;
if ( GetStringFromStream ( vecData,
iPos,
MAX_LEN_IP_ADDRESS,
strLHostAddr ) )
{
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 ) )
strEmpty ) )
{
return true; // return error code
}
@ -1695,8 +1689,7 @@ bool CProtocol::EvaluateCLServerListMes ( const CHostAddress& InetAddr,
// add server information to vector
vecServerInfo.Add (
CServerInfo ( CHostAddress ( QHostAddress ( iIpAddr ), iPort ),
LInetAddr,
iPort,
CHostAddress ( QHostAddress ( iIpAddr ), iPort ),
strName,
eCountry,
strCity,

View file

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

View file

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

View file

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

View file

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