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

View file

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

View file

@ -26,10 +26,12 @@
/* Implementation *************************************************************/ /* Implementation *************************************************************/
CServerListManager::CServerListManager ( const QString& sNCentServAddr, CServerListManager::CServerListManager ( const quint16 iNPortNum,
const QString& sNCentServAddr,
const QString& strServerInfo, const QString& strServerInfo,
CProtocol* pNConLProt ) CProtocol* pNConLProt )
: iNumPredefinedServers ( 0 ), : iPortNumber ( iNPortNum ),
iNumPredefinedServers ( 0 ),
bUseDefaultCentralServerAddress ( false ), bUseDefaultCentralServerAddress ( false ),
pConnLessProtocol ( pNConLProt ) pConnLessProtocol ( pNConLProt )
{ {
@ -58,6 +60,7 @@ CServerListManager::CServerListManager ( const QString& sNCentServAddr,
// itself for his server list // itself for his server list
CServerListEntry ThisServerListEntry ( CServerListEntry ThisServerListEntry (
CHostAddress(), CHostAddress(),
iPortNumber,
"", "",
"", "",
QLocale::system().country(), QLocale::system().country(),
@ -105,6 +108,7 @@ CServerListManager::CServerListManager ( const QString& sNCentServAddr,
// create a new server list entry // create a new server list entry
CServerListEntry NewServerListEntry ( CServerListEntry NewServerListEntry (
CHostAddress(), CHostAddress(),
0, // port number not used
"", "",
"", "",
QLocale::AnyCountry, QLocale::AnyCountry,
@ -258,13 +262,13 @@ void CServerListManager::CentralServerRegisterServer ( const CHostAddress& In
// define invalid index used as a flag // define invalid index used as a flag
const int ciInvalidIdx = -1; 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 // 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 == InetAddr.InetAddr ) if ( ServerList[iIdx].HostAddr == InetAddr )
{ {
// store entry index // store entry index
iSelIdx = iIdx; iSelIdx = iIdx;
@ -283,7 +287,6 @@ void CServerListManager::CentralServerRegisterServer ( const CHostAddress& In
else else
{ {
// update all data and call update registration function // update all data and call update registration function
ServerList[iSelIdx].HostAddr = InetAddr; // because of port number
ServerList[iSelIdx].strName = ServerInfo.strName; ServerList[iSelIdx].strName = ServerInfo.strName;
ServerList[iSelIdx].strTopic = ServerInfo.strTopic; ServerList[iSelIdx].strTopic = ServerInfo.strTopic;
ServerList[iSelIdx].eCountry = ServerInfo.eCountry; 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 // check if the address of the client which is requesting the
// list is the same address as one server in the list -> in this // list is the same address as one server in the list -> in this
// case he has to connect to the local host address, // 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
if ( vecServerInfo[iIdx].HostAddr.InetAddr == InetAddr.InetAddr ) if ( vecServerInfo[iIdx].HostAddr.InetAddr == InetAddr.InetAddr )
{ {
vecServerInfo[iIdx].HostAddr.InetAddr = vecServerInfo[iIdx].HostAddr.InetAddr =
QHostAddress ( QHostAddress::LocalHost ); 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 else
{ {

View file

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

View file

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