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