From 266107b969c7b06517b5754c12ef25674e5abf95 Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Fri, 1 Apr 2011 18:09:58 +0000 Subject: [PATCH] some cleanup, moved some code in a separate function --- src/protocol.cpp | 124 ++++++++++++++++++----------------------------- src/protocol.h | 55 ++++++++++++--------- 2 files changed, 79 insertions(+), 100 deletions(-) diff --git a/src/protocol.cpp b/src/protocol.cpp index 8acceba5..2460e114 100755 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -582,7 +582,9 @@ bool CProtocol::ParseConnectionLessMessage ( const CVector& vecbyData, } -// Access-functions for creating and parsing messages -------------------------- +/******************************************************************************\ +* Access-functions for creating and parsing messages * +\******************************************************************************/ void CProtocol::CreateJitBufMes ( const int iJitBufSize ) { CVector vecData ( 2 ); // 2 bytes of data @@ -705,17 +707,8 @@ void CProtocol::CreateConClientListMes ( const CVector& vecCh PutValOnStream ( vecData, iPos, static_cast ( vecChanInfo[i].iIpAddr ), 4 ); - // number of bytes for name string (2 bytes) - PutValOnStream ( vecData, iPos, - static_cast ( iCurStrLen ), 2 ); - - // name string (n bytes) - for ( int j = 0; j < iCurStrLen; j++ ) - { - // byte-by-byte copying of the string data - PutValOnStream ( vecData, iPos, - static_cast ( vecChanInfo[i].strName[j].toAscii() ), 1 ); - } + // name string + PutStringOnStream ( vecData, iPos, vecChanInfo[i].strName ); } CreateAndSendMessage ( PROTMESSID_CONN_CLIENTS_LIST, vecData ); @@ -795,16 +788,8 @@ void CProtocol::CreateChanNameMes ( const QString strName ) // build data vector CVector vecData ( iEntrLen ); - // number of bytes for name string (2 bytes) - PutValOnStream ( vecData, iPos, static_cast ( iStrLen ), 2 ); - - // name string (n bytes) - for ( int i = 0; i < iStrLen; i++ ) - { - // byte-by-byte copying of the string data - PutValOnStream ( vecData, iPos, - static_cast ( strName[i].toAscii() ), 1 ); - } + // name string + PutStringOnStream ( vecData, iPos, strName ); CreateAndSendMessage ( PROTMESSID_CHANNEL_NAME, vecData ); } @@ -868,16 +853,8 @@ void CProtocol::CreateChatTextMes ( const QString strChatText ) // build data vector CVector vecData ( iEntrLen ); - // number of bytes for chat text string (2 bytes) - PutValOnStream ( vecData, iPos, static_cast ( iStrLen ), 2 ); - - // chat text string (n bytes) - for ( int i = 0; i < iStrLen; i++ ) - { - // byte-by-byte copying of the string data - PutValOnStream ( vecData, iPos, - static_cast ( strChatText[i].toAscii() ), 1 ); - } + // chat text + PutStringOnStream ( vecData, iPos, strChatText ); CreateAndSendMessage ( PROTMESSID_CHAT_TEXT, vecData ); } @@ -1194,45 +1171,18 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr, // build data vector CVector vecData ( iEntrLen ); - // number of bytes for name string (2 bytes) - PutValOnStream ( vecData, iPos, - static_cast ( iNameLen ), 2 ); + // name + PutStringOnStream ( vecData, iPos, ServerInfo.strName ); - // name string (n bytes) - for ( int j = 0; j < iNameLen; j++ ) - { - // byte-by-byte copying of the string data - PutValOnStream ( vecData, iPos, - static_cast ( ServerInfo.strName[j].toAscii() ), 1 ); - } - - // number of bytes for topic string (2 bytes) - PutValOnStream ( vecData, iPos, - static_cast ( iTopicLen ), 2 ); - - // topic string (n bytes) - for ( int j = 0; j < iTopicLen; j++ ) - { - // byte-by-byte copying of the string data - PutValOnStream ( vecData, iPos, - static_cast ( ServerInfo.strTopic[j].toAscii() ), 1 ); - } + // topic + PutStringOnStream ( vecData, iPos, ServerInfo.strTopic ); // country (2 bytes) PutValOnStream ( vecData, iPos, static_cast ( ServerInfo.eCountry ), 2 ); - // number of bytes for city string (2 bytes) - PutValOnStream ( vecData, iPos, - static_cast ( iCityLen ), 2 ); - - // city string (n bytes) - for ( int j = 0; j < iCityLen; j++ ) - { - // byte-by-byte copying of the string data - PutValOnStream ( vecData, iPos, - static_cast ( ServerInfo.strCity[j].toAscii() ), 1 ); - } + // city + PutStringOnStream ( vecData, iPos, ServerInfo.strCity ); // number of connected clients (1 byte) PutValOnStream ( vecData, iPos, @@ -1262,7 +1212,7 @@ bool CProtocol::EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, /******************************************************************************\ -* Message generation (parsing) * +* Message generation and parsing * \******************************************************************************/ bool CProtocol::ParseMessageFrame ( const CVector& vecIn, const int iNumBytesIn, @@ -1270,7 +1220,7 @@ bool CProtocol::ParseMessageFrame ( const CVector& vecIn, int& iID, CVector& vecData ) { - int iLenBy, i; + int iLenBy, i; unsigned int iCurPos; // vector must be at least "MESS_LEN_WITHOUT_DATA_BYTE" bytes long @@ -1280,7 +1230,7 @@ bool CProtocol::ParseMessageFrame ( const CVector& vecIn, } - // decode header ----- + // Decode header ----------------------------------------------------------- iCurPos = 0; // start from beginning // 2 bytes TAG @@ -1292,13 +1242,13 @@ bool CProtocol::ParseMessageFrame ( const CVector& vecIn, return true; // return error code } - /* 2 bytes ID */ + // 2 bytes ID iID = static_cast ( GetValFromStream ( vecIn, iCurPos, 2 ) ); - /* 1 byte cnt */ + // 1 byte cnt iCnt = static_cast ( GetValFromStream ( vecIn, iCurPos, 1 ) ); - /* 2 bytes length */ + // 2 bytes length iLenBy = static_cast ( GetValFromStream ( vecIn, iCurPos, 2 ) ); // make sure the length is correct @@ -1308,11 +1258,11 @@ bool CProtocol::ParseMessageFrame ( const CVector& vecIn, } - // now check CRC ----- + // Now check CRC ----------------------------------------------------------- CCRC CRCObj; const int iLenCRCCalc = MESS_HEADER_LENGTH_BYTE + iLenBy; - iCurPos = 0; // start from beginning + iCurPos = 0; // start from the beginning for ( i = 0; i < iLenCRCCalc; i++ ) { CRCObj.AddByte ( static_cast ( @@ -1325,7 +1275,7 @@ bool CProtocol::ParseMessageFrame ( const CVector& vecIn, } - // extract actual data ----- + // Extract actual data ----------------------------------------------------- vecData.Init ( iLenBy ); iCurPos = MESS_HEADER_LENGTH_BYTE; // start from beginning of data for ( i = 0; i < iLenBy; i++ ) @@ -1374,7 +1324,8 @@ void CProtocol::GenMessageFrame ( CVector& vecOut, // init message vector vecOut.Init ( iTotLenByte ); - // encode header ----- + + // Encode header ----------------------------------------------------------- unsigned int iCurPos = 0; // init position pointer // 2 bytes TAG (all zero bits) @@ -1400,7 +1351,8 @@ void CProtocol::GenMessageFrame ( CVector& vecOut, static_cast ( vecData[i] ), 1 ); } - // encode CRC ----- + + // Encode CRC -------------------------------------------------------------- CCRC CRCObj; iCurPos = 0; // start from beginning @@ -1435,3 +1387,23 @@ void CProtocol::PutValOnStream ( CVector& vecIn, iPos++; } } + +void CProtocol::PutStringOnStream ( CVector& vecData, + unsigned int& iPos, + const QString& sString ) +{ + // get the string size + const int iCurStrLen = sString.size(); + + // number of bytes for string (2 bytes) + PutValOnStream ( vecData, iPos, + static_cast ( iCurStrLen ), 2 ); + + // actual string (n bytes) + for ( int j = 0; j < iCurStrLen; j++ ) + { + // byte-by-byte copying of the string data + PutValOnStream ( vecData, iPos, + static_cast ( sString[j].toAscii() ), 1 ); + } +} diff --git a/src/protocol.h b/src/protocol.h index 1e040b8e..f3f4f737 100755 --- a/src/protocol.h +++ b/src/protocol.h @@ -101,10 +101,11 @@ public: const CServerInfo& ServerInfo ); void CreateAndImmSendDisconnectionMes(); - void CreateAndImmSendAcknMess ( const int& iID, const int& iCnt ); + void CreateAndImmSendAcknMess ( const int& iID, + const int& iCnt ); bool ParseMessage ( const CVector& vecbyData, - const int iNumBytes ); + const int iNumBytes ); bool ParseConnectionLessMessage ( const CVector& vecbyData, const int iNumBytes, @@ -133,44 +134,49 @@ protected: return *this; } - CVector vecMessage; - int iID, iCnt; + CVector vecMessage; + int iID, iCnt; }; void EnqueueMessage ( CVector& vecMessage, - const int iCnt, - const int iID ); + const int iCnt, + const int iID ); bool ParseMessageFrame ( const CVector& vecIn, - const int iNumBytesIn, - int& iCnt, - int& iID, - CVector& vecData ); + const int iNumBytesIn, + int& iCnt, + int& iID, + CVector& vecData ); - void GenMessageFrame ( CVector& vecOut, - const int iCnt, - const int iID, + void GenMessageFrame ( CVector& vecOut, + const int iCnt, + const int iID, const CVector& vecData ); - void PutValOnStream ( CVector& vecIn, - unsigned int& iPos, - const uint32_t iVal, + void PutValOnStream ( CVector& vecIn, + unsigned int& iPos, + const uint32_t iVal, const unsigned int iNumOfBytes ); + void PutStringOnStream ( CVector& vecData, + unsigned int& iPos, + const QString& sString ); + uint32_t GetValFromStream ( const CVector& vecIn, - unsigned int& iPos, - const unsigned int iNumOfBytes ); + unsigned int& iPos, + const unsigned int iNumOfBytes ); bool IsConnectionLessMessageID ( const int iID ) const { return (iID >= 1000) & (iID < 2000); } void SendMessage(); - void CreateAndSendMessage ( const int iID, const CVector& vecData ); + void CreateAndSendMessage ( const int iID, + const CVector& vecData ); - void CreateAndImmSendConLessMessage ( const int iID, + void CreateAndImmSendConLessMessage ( const int iID, const CVector& vecData, - const CHostAddress& InetAddr ); + const CHostAddress& InetAddr ); bool EvaluateJitBufMes ( const CVector& vecData ); bool EvaluateReqJitBufMes(); @@ -186,13 +192,14 @@ protected: bool EvaluateReqNetwTranspPropsMes(); bool EvaluateDisconnectionMes(); - bool EvaluateCLPingMes ( const CHostAddress& InetAddr, + bool EvaluateCLPingMes ( const CHostAddress& InetAddr, const CVector& vecData ); bool EvaluateCLServerFullMes(); - bool EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, + bool EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, const CVector& vecData ); - int iOldRecID, iOldRecCnt; + int iOldRecID; + int iOldRecCnt; // these two objects must be sequred by a mutex uint8_t iCounter;