Skip to content

Commit 653cf37

Browse files
authored
Merge pull request #637 from corrados/reduced_serverlist
Reduced serverlist
2 parents 789f58c + b4a0757 commit 653cf37

9 files changed

Lines changed: 200 additions & 34 deletions

File tree

src/client.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ CClient::CClient ( const quint16 iPortNumber,
157157
QObject::connect ( &ConnLessProtocol, &CProtocol::CLServerListReceived,
158158
this, &CClient::CLServerListReceived );
159159

160+
QObject::connect ( &ConnLessProtocol, &CProtocol::CLRedServerListReceived,
161+
this, &CClient::CLRedServerListReceived );
162+
160163
QObject::connect ( &ConnLessProtocol, &CProtocol::CLConnClientsListMesReceived,
161164
this, &CClient::CLConnClientsListMesReceived );
162165

src/client.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ protected slots:
412412
void CLServerListReceived ( CHostAddress InetAddr,
413413
CVector<CServerInfo> vecServerInfo );
414414

415+
void CLRedServerListReceived ( CHostAddress InetAddr,
416+
CVector<CServerInfo> vecServerInfo );
417+
415418
void CLConnClientsListMesReceived ( CHostAddress InetAddr,
416419
CVector<CChannelInfo> vecChanInfo );
417420

src/clientdlg.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
432432
QObject::connect ( pClient, &CClient::CLServerListReceived,
433433
this, &CClientDlg::OnCLServerListReceived );
434434

435+
QObject::connect ( pClient, &CClient::CLRedServerListReceived,
436+
this, &CClientDlg::OnCLRedServerListReceived );
437+
435438
QObject::connect ( pClient, &CClient::CLConnClientsListMesReceived,
436439
this, &CClientDlg::OnCLConnClientsListMesReceived );
437440

src/clientdlg.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ public slots:
201201
CVector<CServerInfo> vecServerInfo )
202202
{ ConnectDlg.SetServerList ( InetAddr, vecServerInfo ); }
203203

204+
void OnCLRedServerListReceived ( CHostAddress InetAddr,
205+
CVector<CServerInfo> vecServerInfo )
206+
{ ConnectDlg.SetServerList ( InetAddr, vecServerInfo, true ); }
207+
204208
void OnCLConnClientsListMesReceived ( CHostAddress InetAddr,
205209
CVector<CChannelInfo> vecChanInfo )
206210
{ ConnectDlg.SetConnClientsList ( InetAddr, vecChanInfo ); }

src/connectdlg.cpp

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ CConnectDlg::CConnectDlg ( CClient* pNCliP,
3030
const bool bNewShowCompleteRegList,
3131
QWidget* parent,
3232
Qt::WindowFlags f )
33-
: QDialog ( parent, f ),
34-
pClient ( pNCliP ),
35-
strCentralServerAddress ( "" ),
36-
strSelectedAddress ( "" ),
37-
strSelectedServerName ( "" ),
38-
bShowCompleteRegList ( bNewShowCompleteRegList ),
39-
bServerListReceived ( false ),
40-
bServerListItemWasChosen ( false ),
41-
bListFilterWasActive ( false ),
42-
bShowAllMusicians ( true )
33+
: QDialog ( parent, f ),
34+
pClient ( pNCliP ),
35+
strCentralServerAddress ( "" ),
36+
strSelectedAddress ( "" ),
37+
strSelectedServerName ( "" ),
38+
bShowCompleteRegList ( bNewShowCompleteRegList ),
39+
bServerListReceived ( false ),
40+
bReducedServerListReceived ( false ),
41+
bServerListItemWasChosen ( false ),
42+
bListFilterWasActive ( false ),
43+
bShowAllMusicians ( true )
4344
{
4445
setupUi ( this );
4546

@@ -216,9 +217,10 @@ void CConnectDlg::showEvent ( QShowEvent* )
216217
void CConnectDlg::RequestServerList()
217218
{
218219
// reset flags
219-
bServerListReceived = false;
220-
bServerListItemWasChosen = false;
221-
bListFilterWasActive = false;
220+
bServerListReceived = false;
221+
bReducedServerListReceived = false;
222+
bServerListItemWasChosen = false;
223+
bListFilterWasActive = false;
222224

223225
// clear current address and name
224226
strSelectedAddress = "";
@@ -267,11 +269,30 @@ void CConnectDlg::OnTimerReRequestServList()
267269
}
268270

269271
void CConnectDlg::SetServerList ( const CHostAddress& InetAddr,
270-
const CVector<CServerInfo>& vecServerInfo )
272+
const CVector<CServerInfo>& vecServerInfo,
273+
const bool bIsReducedServerList )
271274
{
272-
// set flag and disable timer for resend server list request
273-
bServerListReceived = true;
274-
TimerReRequestServList.stop();
275+
// special treatment if a reduced server list was received
276+
if ( bIsReducedServerList )
277+
{
278+
// make sure we only apply the reduced version list once
279+
if ( bReducedServerListReceived )
280+
{
281+
// do nothing
282+
return;
283+
}
284+
else
285+
{
286+
bReducedServerListReceived = true;
287+
}
288+
}
289+
else
290+
{
291+
// set flag and disable timer for resend server list request if full list
292+
// was received (i.e. not the reduced list)
293+
bServerListReceived = true;
294+
TimerReRequestServList.stop();
295+
}
275296

276297
// first clear list
277298
lvwServers->clear();
@@ -745,15 +766,18 @@ void CConnectDlg::SetPingTimeAndNumClientsResult ( const CHostAddress& InetAddr,
745766
}
746767

747768
// update number of clients text
748-
if ( iNumClients >= pCurListViewItem->text ( 5 ).toInt() )
769+
if ( pCurListViewItem->text ( 5 ).toInt() == 0 )
749770
{
750-
pCurListViewItem->
751-
setText ( 2, QString().setNum ( iNumClients ) + " (full)" );
771+
// special case: reduced server list
772+
pCurListViewItem->setText ( 2, QString().setNum ( iNumClients ) );
773+
}
774+
else if ( iNumClients >= pCurListViewItem->text ( 5 ).toInt() )
775+
{
776+
pCurListViewItem->setText ( 2, QString().setNum ( iNumClients ) + " (full)" );
752777
}
753778
else
754779
{
755-
pCurListViewItem->
756-
setText ( 2, QString().setNum ( iNumClients ) + "/" + pCurListViewItem->text ( 5 ) );
780+
pCurListViewItem->setText ( 2, QString().setNum ( iNumClients ) + "/" + pCurListViewItem->text ( 5 ) );
757781
}
758782

759783
// check if the number of child list items matches the number of

src/connectdlg.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class CConnectDlg : public QDialog, private Ui_CConnectDlgBase
6161
bool GetShowAllMusicians() { return bShowAllMusicians; }
6262

6363
void SetServerList ( const CHostAddress& InetAddr,
64-
const CVector<CServerInfo>& vecServerInfo );
64+
const CVector<CServerInfo>& vecServerInfo,
65+
const bool bIsReducedServerList = false );
6566

6667
void SetConnClientsList ( const CHostAddress& InetAddr,
6768
const CVector<CChannelInfo>& vecChanInfo );
@@ -101,6 +102,7 @@ class CConnectDlg : public QDialog, private Ui_CConnectDlgBase
101102
QString strSelectedServerName;
102103
bool bShowCompleteRegList;
103104
bool bServerListReceived;
105+
bool bReducedServerListReceived;
104106
bool bServerListItemWasChosen;
105107
bool bListFilterWasActive;
106108
bool bShowAllMusicians;

src/protocol.cpp

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,18 @@ CONNECTION LESS MESSAGES
338338
of the PROTMESSID_CLM_REGISTER_SERVER message is used
339339
340340
341+
- PROTMESSID_CLM_RED_SERVER_LIST: Reduced server list message (to have less UDP fragmentation)
342+
343+
for each registered server append following data:
344+
345+
+--------------------+------------------------------+ ...
346+
| 4 bytes IP address | 2 bytes server internal port | ...
347+
+--------------------+------------------------------+ ...
348+
... -----------------+----------------------------------+
349+
... 1 byte number n | n bytes UTF-8 string server name |
350+
... -----------------+----------------------------------+
351+
352+
341353
- PROTMESSID_CLM_REQ_SERVER_LIST: Request server list
342354
343355
note: does not have any data -> n = 0
@@ -883,6 +895,10 @@ if ( rand() < ( RAND_MAX / 2 ) ) return false;
883895
EvaluateCLServerListMes ( InetAddr, vecbyMesBodyData );
884896
break;
885897

898+
case PROTMESSID_CLM_RED_SERVER_LIST:
899+
EvaluateCLRedServerListMes ( InetAddr, vecbyMesBodyData );
900+
break;
901+
886902
case PROTMESSID_CLM_REQ_SERVER_LIST:
887903
EvaluateCLReqServerListMes ( InetAddr );
888904
break;
@@ -2323,6 +2339,103 @@ bool CProtocol::EvaluateCLServerListMes ( const CHostAddress& InetAddr,
23232339
return false; // no error
23242340
}
23252341

2342+
void CProtocol::CreateCLRedServerListMes ( const CHostAddress& InetAddr,
2343+
const CVector<CServerInfo> vecServerInfo )
2344+
{
2345+
const int iNumServers = vecServerInfo.Size();
2346+
2347+
// build data vector
2348+
CVector<uint8_t> vecData ( 0 );
2349+
int iPos = 0; // init position pointer
2350+
2351+
for ( int i = 0; i < iNumServers; i++ )
2352+
{
2353+
// convert server list strings to utf-8
2354+
const QByteArray strUTF8Name = vecServerInfo[i].strName.toUtf8();
2355+
2356+
// size of current list entry
2357+
const int iCurListEntrLen =
2358+
4 /* IP address */ +
2359+
2 /* port number */ +
2360+
1 /* name utf-8 string size */ + strUTF8Name.size();
2361+
2362+
// make space for new data
2363+
vecData.Enlarge ( iCurListEntrLen );
2364+
2365+
// IP address (4 bytes)
2366+
// note the Server List manager has put the internal details in HostAddr where required
2367+
PutValOnStream ( vecData, iPos, static_cast<uint32_t> (
2368+
vecServerInfo[i].HostAddr.InetAddr.toIPv4Address() ), 4 );
2369+
2370+
// port number (2 bytes)
2371+
// note the Server List manager has put the internal details in HostAddr where required
2372+
PutValOnStream ( vecData, iPos,
2373+
static_cast<uint32_t> ( vecServerInfo[i].HostAddr.iPort ), 2 );
2374+
2375+
// name (note that the string length indicator is 1 in this special case)
2376+
PutStringUTF8OnStream ( vecData, iPos, strUTF8Name, 1 );
2377+
}
2378+
2379+
CreateAndImmSendConLessMessage ( PROTMESSID_CLM_RED_SERVER_LIST,
2380+
vecData,
2381+
InetAddr );
2382+
}
2383+
2384+
bool CProtocol::EvaluateCLRedServerListMes ( const CHostAddress& InetAddr,
2385+
const CVector<uint8_t>& vecData )
2386+
{
2387+
int iPos = 0; // init position pointer
2388+
const int iDataLen = vecData.Size();
2389+
CVector<CServerInfo> vecServerInfo ( 0 );
2390+
2391+
while ( iPos < iDataLen )
2392+
{
2393+
// check size (the next 6 bytes)
2394+
if ( ( iDataLen - iPos ) < 6 )
2395+
{
2396+
return true; // return error code
2397+
}
2398+
2399+
// IP address (4 bytes)
2400+
const quint32 iIpAddr = static_cast<quint32> ( GetValFromStream ( vecData, iPos, 4 ) );
2401+
2402+
// port number (2 bytes)
2403+
const quint16 iPort = static_cast<quint16> ( GetValFromStream ( vecData, iPos, 2 ) );
2404+
2405+
// server name (note that the string length indicator is 1 in this special case)
2406+
QString strName;
2407+
if ( GetStringFromStream ( vecData,
2408+
iPos,
2409+
MAX_LEN_SERVER_NAME,
2410+
strName,
2411+
1 ) )
2412+
{
2413+
return true; // return error code
2414+
}
2415+
2416+
// add server information to vector
2417+
vecServerInfo.Add (
2418+
CServerInfo ( CHostAddress ( QHostAddress ( iIpAddr ), iPort ),
2419+
CHostAddress ( QHostAddress ( iIpAddr ), iPort ),
2420+
strName,
2421+
QLocale::AnyCountry, // set to any country since the information is not transmitted
2422+
"", // empty city name since the information is not transmitted
2423+
0, // per definition: if max. num. client is zero, we ignore the value in the server list
2424+
false ) ); // assume not permanent since the information is not transmitted
2425+
}
2426+
2427+
// check size: all data is read, the position must now be at the end
2428+
if ( iPos != iDataLen )
2429+
{
2430+
return true; // return error code
2431+
}
2432+
2433+
// invoke message action
2434+
emit CLRedServerListReceived ( InetAddr, vecServerInfo );
2435+
2436+
return false; // no error
2437+
}
2438+
23262439
void CProtocol::CreateCLReqServerListMes ( const CHostAddress& InetAddr )
23272440
{
23282441
CreateAndImmSendConLessMessage ( PROTMESSID_CLM_REQ_SERVER_LIST,
@@ -2883,21 +2996,22 @@ uint32_t CProtocol::GetValFromStream ( const CVector<uint8_t>& vecIn,
28832996
bool CProtocol::GetStringFromStream ( const CVector<uint8_t>& vecIn,
28842997
int& iPos,
28852998
const int iMaxStringLen,
2886-
QString& strOut )
2999+
QString& strOut,
3000+
const int iNumberOfBytsLen )
28873001
{
28883002
/*
28893003
note: iPos is automatically incremented in this function
28903004
*/
28913005
const int iInLen = vecIn.Size();
28923006

2893-
// check if at least two bytes are available
2894-
if ( ( iInLen - iPos ) < 2 )
3007+
// check if at least iNumberOfBytsLen bytes are available
3008+
if ( ( iInLen - iPos ) < iNumberOfBytsLen )
28953009
{
28963010
return true; // return error code
28973011
}
28983012

2899-
// number of bytes for utf-8 string (2 bytes)
2900-
const int iStrUTF8Len = static_cast<int> ( GetValFromStream ( vecIn, iPos, 2 ) );
3013+
// number of bytes for utf-8 string (1 or 2 bytes)
3014+
const int iStrUTF8Len = static_cast<int> ( GetValFromStream ( vecIn, iPos, iNumberOfBytsLen ) );
29013015

29023016
// (note that iPos was incremented by 2 in the above code!)
29033017
if ( ( iInLen - iPos ) < iStrUTF8Len )
@@ -3034,13 +3148,14 @@ void CProtocol::PutValOnStream ( CVector<uint8_t>& vecIn,
30343148

30353149
void CProtocol::PutStringUTF8OnStream ( CVector<uint8_t>& vecIn,
30363150
int& iPos,
3037-
const QByteArray& sStringUTF8 )
3151+
const QByteArray& sStringUTF8,
3152+
const int iNumberOfBytsLen )
30383153
{
30393154
// get the utf-8 string size
30403155
const int iStrUTF8Len = sStringUTF8.size();
30413156

3042-
// number of bytes for utf-8 string (2 bytes)
3043-
PutValOnStream ( vecIn, iPos, static_cast<uint32_t> ( iStrUTF8Len ), 2 );
3157+
// number of bytes for utf-8 string (iNumberOfBytsLen bytes)
3158+
PutValOnStream ( vecIn, iPos, static_cast<uint32_t> ( iStrUTF8Len ), iNumberOfBytsLen );
30443159

30453160
// actual utf-8 string (n bytes)
30463161
for ( int j = 0; j < iStrUTF8Len; j++ )

src/protocol.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#define PROTMESSID_CLM_CHANNEL_LEVEL_LIST 1015 // channel level list
8484
#define PROTMESSID_CLM_REGISTER_SERVER_RESP 1016 // status of server registration request
8585
#define PROTMESSID_CLM_REGISTER_SERVER_EX 1017 // register server with extended information
86+
#define PROTMESSID_CLM_RED_SERVER_LIST 1018 // reduced server list
8687

8788
// special IDs
8889
#define PROTMESSID_SPECIAL_SPLIT_MESSAGE 2001 // a container for split messages
@@ -145,6 +146,8 @@ class CProtocol : public QObject
145146
void CreateCLUnregisterServerMes ( const CHostAddress& InetAddr );
146147
void CreateCLServerListMes ( const CHostAddress& InetAddr,
147148
const CVector<CServerInfo> vecServerInfo );
149+
void CreateCLRedServerListMes ( const CHostAddress& InetAddr,
150+
const CVector<CServerInfo> vecServerInfo );
148151
void CreateCLReqServerListMes ( const CHostAddress& InetAddr );
149152
void CreateCLSendEmptyMesMes ( const CHostAddress& InetAddr,
150153
const CHostAddress& TargetInetAddr );
@@ -238,7 +241,8 @@ class CProtocol : public QObject
238241

239242
void PutStringUTF8OnStream ( CVector<uint8_t>& vecIn,
240243
int& iPos,
241-
const QByteArray& sStringUTF8 );
244+
const QByteArray& sStringUTF8,
245+
const int iNumberOfBytsLen = 2 ); // default is 2 bytes lenght indicator
242246

243247
static uint32_t GetValFromStream ( const CVector<uint8_t>& vecIn,
244248
int& iPos,
@@ -247,7 +251,8 @@ class CProtocol : public QObject
247251
bool GetStringFromStream ( const CVector<uint8_t>& vecIn,
248252
int& iPos,
249253
const int iMaxStringLen,
250-
QString& strOut );
254+
QString& strOut,
255+
const int iNumberOfBytsLen = 2 ); // default is 2 bytes lenght indicator
251256

252257
void SendMessage();
253258

@@ -290,6 +295,8 @@ class CProtocol : public QObject
290295
bool EvaluateCLUnregisterServerMes ( const CHostAddress& InetAddr );
291296
bool EvaluateCLServerListMes ( const CHostAddress& InetAddr,
292297
const CVector<uint8_t>& vecData );
298+
bool EvaluateCLRedServerListMes ( const CHostAddress& InetAddr,
299+
const CVector<uint8_t>& vecData );
293300
bool EvaluateCLReqServerListMes ( const CHostAddress& InetAddr );
294301
bool EvaluateCLSendEmptyMesMes ( const CVector<uint8_t>& vecData );
295302
bool EvaluateCLDisconnectionMes ( const CHostAddress& InetAddr );
@@ -367,6 +374,8 @@ public slots:
367374
void CLUnregisterServerReceived ( CHostAddress InetAddr );
368375
void CLServerListReceived ( CHostAddress InetAddr,
369376
CVector<CServerInfo> vecServerInfo );
377+
void CLRedServerListReceived ( CHostAddress InetAddr,
378+
CVector<CServerInfo> vecServerInfo );
370379
void CLReqServerList ( CHostAddress InetAddr );
371380
void CLSendEmptyMes ( CHostAddress TargetInetAddr );
372381
void CLDisconnection ( CHostAddress InetAddr );

0 commit comments

Comments
 (0)