add local port number in server registering struct to allow a connection to the local host server with different port than the default one

This commit is contained in:
Volker Fischer 2011-05-04 18:18:31 +00:00
parent 97d32de751
commit dfa5296de0
5 changed files with 54 additions and 19 deletions

View file

@ -174,9 +174,12 @@ CONNECTION LESS MESSAGES
- PROTMESSID_CLM_REGISTER_SERVER: Register a server, providing server
information
+-----------------+----------------------------------+ ...
| 2 bytes country | 1 byte maximum connected clients | ...
+-----------------+----------------------------------+ ...
+--------------+ ...
| 2 bytes port | ...
+--------------+ ...
... -----------------+----------------------------------+ ...
... 2 bytes country | 1 byte maximum connected clients | ...
... -----------------+----------------------------------+ ...
... ---------------------+------------------+ ...
... 1 byte is permanent | 2 bytes number n | ...
... ---------------------+------------------+ ...
@ -1224,6 +1227,7 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr,
// size of current message body
const int iEntrLen =
2 /* port number */ +
2 /* country */ +
1 /* maximum number of connected clients */ +
1 /* is permanent flag */ +
@ -1234,6 +1238,10 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr,
// build data vector
CVector<uint8_t> vecData ( iEntrLen );
// port number (2 bytes)
PutValOnStream ( vecData, iPos,
static_cast<uint32_t> ( ServerInfo.iLocalPortNumber ), 2 );
// country (2 bytes)
PutValOnStream ( vecData, iPos,
static_cast<uint32_t> ( ServerInfo.eCountry ), 2 );
@ -1267,12 +1275,16 @@ bool CProtocol::EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr,
const int iDataLen = vecData.Size();
CServerCoreInfo RecServerInfo;
// check size (the first 4 bytes)
if ( iDataLen < 4 )
// check size (the first 6 bytes)
if ( iDataLen < 6 )
{
return true; // return error code
}
// port number (2 bytes)
RecServerInfo.iLocalPortNumber =
static_cast<int> ( GetValFromStream ( vecData, iPos, 2 ) );
// country (2 bytes)
RecServerInfo.eCountry =
static_cast<QLocale::Country> ( GetValFromStream ( vecData, iPos, 2 ) );
@ -1467,6 +1479,7 @@ bool CProtocol::EvaluateCLServerListMes ( const CHostAddress& InetAddr,
// add server information to vector
vecServerInfo.Add (
CServerInfo ( CHostAddress ( QHostAddress ( iIpAddr ), iPort ),
iPort,
strName,
strTopic,
eCountry,

View file

@ -173,7 +173,8 @@ CServer::CServer ( const QString& strLoggingFileName,
const QString& strServerInfo ) :
Socket ( this, iPortNumber ),
bWriteStatusHTMLFile ( false ),
ServerListManager ( strCentralServer,
ServerListManager ( iPortNumber,
strCentralServer,
strServerInfo,
&ConnLessProtocol )
{

View file

@ -26,10 +26,12 @@
/* Implementation *************************************************************/
CServerListManager::CServerListManager ( const QString& sNCentServAddr,
CServerListManager::CServerListManager ( const quint16 iNPortNum,
const QString& sNCentServAddr,
const QString& strServerInfo,
CProtocol* pNConLProt )
: iNumPredefinedServers ( 0 ),
: iPortNumber ( iNPortNum ),
iNumPredefinedServers ( 0 ),
bUseDefaultCentralServerAddress ( false ),
pConnLessProtocol ( pNConLProt )
{
@ -58,6 +60,7 @@ CServerListManager::CServerListManager ( const QString& sNCentServAddr,
// itself for his server list
CServerListEntry ThisServerListEntry (
CHostAddress(),
iPortNumber,
"",
"",
QLocale::system().country(),
@ -105,6 +108,7 @@ CServerListManager::CServerListManager ( const QString& sNCentServAddr,
// create a new server list entry
CServerListEntry NewServerListEntry (
CHostAddress(),
0, // port number not used
"",
"",
QLocale::AnyCountry,
@ -258,13 +262,13 @@ void CServerListManager::CentralServerRegisterServer ( const CHostAddress& In
// define invalid index used as a flag
const int ciInvalidIdx = -1;
// Check if server is already registered. Use IP number to identify
// Check if server is already registered. Use address to identify
// a server. 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 == InetAddr.InetAddr )
if ( ServerList[iIdx].HostAddr == InetAddr )
{
// store entry index
iSelIdx = iIdx;
@ -283,7 +287,6 @@ void CServerListManager::CentralServerRegisterServer ( const CHostAddress& In
else
{
// update all data and call update registration function
ServerList[iSelIdx].HostAddr = InetAddr; // because of port number
ServerList[iSelIdx].strName = ServerInfo.strName;
ServerList[iSelIdx].strTopic = ServerInfo.strTopic;
ServerList[iSelIdx].eCountry = ServerInfo.eCountry;
@ -344,16 +347,17 @@ void CServerListManager::CentralServerQueryServerList ( const CHostAddress& Inet
{
// check if the address of the client which is requesting the
// list is the same address as one server in the list -> in this
// case he has to connect to the local host address,
// unfortunately, the port number is not known (because of NAT
// port translating) so we assume that it is the default llcon
// port number
// case he has to connect to the local host address
if ( vecServerInfo[iIdx].HostAddr.InetAddr == InetAddr.InetAddr )
{
vecServerInfo[iIdx].HostAddr.InetAddr =
QHostAddress ( QHostAddress::LocalHost );
vecServerInfo[iIdx].HostAddr.iPort = LLCON_DEFAULT_PORT_NUMBER;
// take the local port number instead of the received port
// number since some NAT (network address translation) might
// have changed the port
vecServerInfo[iIdx].HostAddr.iPort =
ServerList[iIdx].iLocalPortNumber;
}
else
{

View file

@ -76,6 +76,7 @@ class CServerListEntry : public CServerInfo
public:
CServerListEntry() :
CServerInfo ( CHostAddress(),
0,
"",
"",
QLocale::AnyCountry,
@ -84,6 +85,7 @@ public:
false ) { UpdateRegistration(); }
CServerListEntry ( const CHostAddress& NHAddr,
const quint16 NLocPort,
const QString& NsName,
const QString& NsTopic,
const QLocale::Country& NeCountry,
@ -91,6 +93,7 @@ public:
const int NiMaxNumClients,
const bool NbPermOnline)
: CServerInfo ( NHAddr,
NLocPort,
NsName,
NsTopic,
NeCountry,
@ -101,6 +104,7 @@ public:
CServerListEntry ( const CHostAddress& NHAddr,
const CServerCoreInfo& NewCoreServerInfo )
: CServerInfo ( NHAddr,
NewCoreServerInfo.iLocalPortNumber,
NewCoreServerInfo.strName,
NewCoreServerInfo.strTopic,
NewCoreServerInfo.eCountry,
@ -121,7 +125,8 @@ class CServerListManager : public QObject
Q_OBJECT
public:
CServerListManager ( const QString& sNCentServAddr,
CServerListManager ( const quint16 iNPortNum,
const QString& sNCentServAddr,
const QString& strServerInfo,
CProtocol* pNConLProt );
@ -176,7 +181,10 @@ protected:
QTimer TimerPollList;
QTimer TimerRegistering;
QMutex Mutex;
QList<CServerListEntry> ServerList;
quint16 iPortNumber;
QString strCentralServerAddress;
int iNumPredefinedServers;
bool bEnabled;

View file

@ -483,6 +483,7 @@ class CServerCoreInfo
{
public:
CServerCoreInfo() :
iLocalPortNumber ( 0 ),
strName ( "" ),
strTopic ( "" ),
eCountry ( QLocale::AnyCountry ),
@ -491,12 +492,14 @@ public:
bPermanentOnline ( false ) {}
CServerCoreInfo (
const quint16 NLocPort,
const QString& NsName,
const QString& NsTopic,
const QLocale::Country& NeCountry,
const QString& NsCity,
const int NiMaxNumClients,
const bool NbPermOnline) :
iLocalPortNumber ( NLocPort ),
strName ( NsName ),
strTopic ( NsTopic ),
eCountry ( NeCountry ),
@ -505,6 +508,9 @@ public:
bPermanentOnline ( NbPermOnline ) {}
public:
// local port number of the server
quint16 iLocalPortNumber;
// name of the server
QString strName;
@ -529,7 +535,8 @@ class CServerInfo : public CServerCoreInfo
{
public:
CServerInfo() :
CServerCoreInfo ( "",
CServerCoreInfo ( 0,
"",
"",
QLocale::AnyCountry,
"",
@ -538,13 +545,15 @@ public:
CServerInfo (
const CHostAddress& NHAddr,
const quint16 NLocPort,
const QString& NsName,
const QString& NsTopic,
const QLocale::Country& NeCountry,
const QString& NsCity,
const int NiMaxNumClients,
const bool NbPermOnline) :
CServerCoreInfo ( NsName,
CServerCoreInfo ( NLocPort,
NsName,
NsTopic,
NeCountry,
NsCity,