From 24ecf0c3f01492d2b24be756de854ca3af09b77b Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Sat, 2 Apr 2011 12:07:42 +0000 Subject: [PATCH] finished register server protocol message --- src/global.h | 3 ++ src/protocol.cpp | 136 ++++++++++++++++++++++++++++++++++------------- src/protocol.h | 4 +- 3 files changed, 105 insertions(+), 38 deletions(-) diff --git a/src/global.h b/src/global.h index dc3e313f..23e622db 100755 --- a/src/global.h +++ b/src/global.h @@ -154,6 +154,9 @@ #define MAX_LEN_FADER_TAG 16 #define MAX_LEN_CHAT_TEXT 1600 #define MAX_LEN_CHAT_TEXT_PLUS_HTML 1800 +#define MAX_LEN_SERVER_NAME 20 +#define MAX_LEN_SERVER_TOPIC 32 +#define MAX_LEN_SERVER_CITY 20 // common tool tip bottom line text #define TOOLTIP_COM_END_TEXT tr ( \ diff --git a/src/protocol.cpp b/src/protocol.cpp index fb20b536..19702970 100755 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -163,21 +163,21 @@ CONNECTION LESS MESSAGES - PROTMESSID_CLM_REGISTER_SERVER: Register a server, providing server information - +------------------+----------------------------------+ ... - | 2 bytes number n | n bytes UTF-8 string server name | ... - +------------------+----------------------------------+ ... + +-----------------+---------------------------------+ ... + | 2 bytes country | 1 byte number connected clients | ... + +-----------------+---------------------------------+ ... + ... ---------------------------------+---------------------+ ... + ... 1 byte maximum connected clients | 1 byte is permanent | ... + ... ---------------------------------+---------------------+ ... + ... -----------------+----------------------------------+ ... + ... 2 bytes number n | n bytes UTF-8 string server name | ... + ... -----------------+----------------------------------+ ... ... -----------------+----------------------------+ ... ... 2 bytes number n | n bytes UTF-8 string topic | ... ... -----------------+----------------------------+ ... - ... ----------------+------------------+---------------------------+ ... - ... 2 bytes country | 2 bytes number n | n bytes UTF-8 string city | ... - ... ----------------+------------------+---------------------------+ ... - ... --------------------------------+ ... - ... 1 byte number connected clients | ... - ... --------------------------------+ ... - ... ---------------------------------+---------------------+ - ... 1 byte maximum connected clients | 1 byte is permanent | - ... ---------------------------------+---------------------+ + ... -----------------+---------------------------+ + ... 2 bytes number n | n bytes UTF-8 string city | + ... -----------------+---------------------------+ - "country" is according to "Common Locale Data Repository" which is used in the QLocale class @@ -717,14 +717,14 @@ void CProtocol::CreateConClientListMes ( const CVector& vecCh bool CProtocol::EvaluateConClientListMes ( const CVector& vecData ) { - unsigned int iPos = 0; // init position pointer + unsigned int iPos = 0; // init position pointer const unsigned int iDataLen = vecData.Size(); CVector vecChanInfo ( 0 ); while ( iPos < iDataLen ) { - // check size (the first 5 bytes) - if ( iDataLen - iPos < 5 ) + // check size (the next 5 bytes) + if ( ( iDataLen - iPos ) < 5 ) { return true; // return error code } @@ -751,6 +751,12 @@ bool CProtocol::EvaluateConClientListMes ( const CVector& vecData ) vecChanInfo.Add ( CChannelShortInfo ( iChanID, iIpAddr, strCurStr ) ); } + // check size: all data is read, the position must now be at the end + if ( iPos != iDataLen ) + { + return true; // return error code + } + // invoke message action emit ConClientListMesReceived ( vecChanInfo ); @@ -801,8 +807,8 @@ bool CProtocol::EvaluateChanNameMes ( const CVector& vecData ) return true; // return error code } - // check size - if ( ( vecData.Size() - 2 ) != strName.size() ) + // check size: all data is read, the position must now be at the end + if ( iPos != vecData.Size() ) { return true; // return error code } @@ -857,8 +863,8 @@ bool CProtocol::EvaluateChatTextMes ( const CVector& vecData ) return true; // return error code } - // check size - if ( ( vecData.Size() - 2 ) != strChatText.size() ) + // check size: all data is read, the position must now be at the end + if ( iPos != vecData.Size() ) { return true; // return error code } @@ -1118,7 +1124,7 @@ void CProtocol::CreateCLServerFullMes ( const CHostAddress& InetAddr ) bool CProtocol::EvaluateCLServerFullMes() { // invoke message action - emit ServerFull(); + emit ServerFullMesReceived(); return false; // no error } @@ -1135,30 +1141,21 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr, // size of current message body const int iEntrLen = - 2 /* name string size */ + iNameLen + - 2 /* topic string size */ + iTopicLen + 2 /* country */ + - 2 /* city string size */ + iCityLen + 1 /* number of connected clients */ + 1 /* maximum number of connected clients */ + - 1 /* is permanent flag */; + 1 /* is permanent flag */ + + 2 /* name string size */ + iNameLen + + 2 /* topic string size */ + iTopicLen + + 2 /* city string size */ + iCityLen; // build data vector CVector vecData ( iEntrLen ); - // name - PutStringOnStream ( vecData, iPos, ServerInfo.strName ); - - // topic - PutStringOnStream ( vecData, iPos, ServerInfo.strTopic ); - // country (2 bytes) PutValOnStream ( vecData, iPos, static_cast ( ServerInfo.eCountry ), 2 ); - // city - PutStringOnStream ( vecData, iPos, ServerInfo.strCity ); - // number of connected clients (1 byte) PutValOnStream ( vecData, iPos, static_cast ( ServerInfo.iNumClients ), 1 ); @@ -1167,20 +1164,87 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr, PutValOnStream ( vecData, iPos, static_cast ( ServerInfo.iMaxNumClients ), 1 ); - // is permanent flag (1 byte) + // "is permanent" flag (1 byte) PutValOnStream ( vecData, iPos, static_cast ( ServerInfo.bPermanentOnline ), 1 ); + // name + PutStringOnStream ( vecData, iPos, ServerInfo.strName ); + + // topic + PutStringOnStream ( vecData, iPos, ServerInfo.strTopic ); + + // city + PutStringOnStream ( vecData, iPos, ServerInfo.strCity ); + CreateAndImmSendConLessMessage ( PROTMESSID_CLM_REGISTER_SERVER, vecData, InetAddr ); } -bool CProtocol::EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, +bool CProtocol::EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, const CVector& vecData ) { + unsigned int iPos = 0; // init position pointer + const unsigned int iDataLen = vecData.Size(); + CServerInfo RecServerInfo; - // TODO + // check size (the first 5 bytes) + if ( iDataLen < 5 ) + { + return true; // return error code + } + + // country (2 bytes) + RecServerInfo.eCountry = + static_cast ( GetValFromStream ( vecData, iPos, 2 ) ); + + // number of connected clients (1 byte) + RecServerInfo.iNumClients = + static_cast ( GetValFromStream ( vecData, iPos, 1 ) ); + + // maximum number of connected clients (1 byte) + RecServerInfo.iMaxNumClients = + static_cast ( GetValFromStream ( vecData, iPos, 1 ) ); + + // "is permanent" flag (1 byte) + RecServerInfo.bPermanentOnline = + static_cast ( GetValFromStream ( vecData, iPos, 1 ) ); + + // server name + if ( GetStringFromStream ( vecData, + iPos, + MAX_LEN_SERVER_NAME, + RecServerInfo.strName ) ) + { + return true; // return error code + } + + // server topic + if ( GetStringFromStream ( vecData, + iPos, + MAX_LEN_SERVER_TOPIC, + RecServerInfo.strTopic ) ) + { + return true; // return error code + } + + // server city + if ( GetStringFromStream ( vecData, + iPos, + MAX_LEN_SERVER_CITY, + RecServerInfo.strCity ) ) + { + return true; // return error code + } + + // check size: all data is read, the position must now be at the end + if ( iPos != iDataLen ) + { + return true; // return error code + } + + emit CLRegisterServerReceived ( InetAddr, RecServerInfo ); return false; // no error } diff --git a/src/protocol.h b/src/protocol.h index 83c2332f..a2a22a09 100755 --- a/src/protocol.h +++ b/src/protocol.h @@ -187,7 +187,6 @@ protected: bool EvaluateReqJitBufMes(); bool EvaluateChanGainMes ( const CVector& vecData ); bool EvaluateConClientListMes ( const CVector& vecData ); - bool EvaluateServerFullMes(); bool EvaluateReqConnClientsList(); bool EvaluateChanNameMes ( const CVector& vecData ); bool EvaluateReqChanNameMes(); @@ -228,7 +227,7 @@ signals: void ChangeNetwBlSiFact ( int iNewNetwBlSiFact ); void ChangeChanGain ( int iChanID, double dNewGain ); void ConClientListMesReceived ( CVector vecChanInfo ); - void ServerFull(); + void ServerFullMesReceived(); void ReqConnClientsList(); void ChangeChanName ( QString strName ); void ReqChanName(); @@ -239,6 +238,7 @@ signals: void Disconnection(); void CLPingReceived ( CHostAddress InetAddr, int iMs ); + void CLRegisterServerReceived ( CHostAddress InetAddr, CServerInfo ServerInfo ); }; #endif /* !defined ( PROTOCOL_H__3B123453_4344_BB2392354455IUHF1912__INCLUDED_ ) */