You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
3.8 KiB
134 lines
3.8 KiB
#include "protocol.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "Proto", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "Proto", this, _LOG_ERROR, X )
|
|
|
|
protocolParser::protocolParser(config_setting_t *Settings) :basicThread(),m_mBufferProtectorMutex(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pParserBuffer = (U8*) malloc(PROTOCOL_PARSER_BUFFER_SIZE);
|
|
m_nParserDataCount = 0;
|
|
m_bStatus = 0;
|
|
m_pCustomHandler = NULL;
|
|
m_bBufferAltered = false;
|
|
m_bStatus = 1;
|
|
m_eConnectionStatus = CS_UNKNOWN;
|
|
m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, PROTO_TAG_GROUP);
|
|
};
|
|
|
|
protocolParser::~protocolParser(){
|
|
# ifdef DEBUG
|
|
LOG("Destructor Begin");
|
|
# endif
|
|
m_mBufferProtectorMutex.unlock();
|
|
if (m_bThreadStatus < TH_ENDED ) {
|
|
waitThreadEnd( true );
|
|
};
|
|
if( m_pParserBuffer != NULL ) {
|
|
free( m_pParserBuffer );
|
|
};
|
|
# ifdef DEBUG
|
|
LOG("Destructor End");
|
|
# endif
|
|
};
|
|
|
|
bool protocolParser::create(void){
|
|
if (m_pSettings == NULL)return false;
|
|
if (m_pParserBuffer != NULL) {
|
|
return basicThread::init();
|
|
} else return false;
|
|
};
|
|
|
|
void protocolParser::setConnectionStatus( CONNECTION_STATUS cStatus ){
|
|
m_eConnectionStatus = cStatus;
|
|
};
|
|
CONNECTION_STATUS protocolParser::getConnectionStatus( void ){
|
|
return m_eConnectionStatus;
|
|
};
|
|
bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){
|
|
if( m_pParserBuffer != NULL ) {
|
|
m_mBufferProtectorMutex.lock();
|
|
memcpy( m_pParserBuffer+m_nParserDataCount, Buffer, BufferSize );
|
|
m_nParserDataCount+=BufferSize;
|
|
m_bBufferAltered = true;
|
|
m_mBufferProtectorMutex.unlock();
|
|
};
|
|
return m_bBufferAltered;
|
|
};
|
|
|
|
void protocolParser::m_fThreadProc(void){
|
|
int RetVal;
|
|
m_bThreadStatus = TH_WORKING;
|
|
while(m_bThreadStatus < TH_ENDED){
|
|
if( m_eConnectionStatus < CS_DISCONNECTED ){
|
|
if( ( m_bBufferAltered == false ) && ( m_bThreadStatus < TH_ENDED ) ) {
|
|
SLEEP(1);
|
|
continue;
|
|
};
|
|
m_mBufferProtectorMutex.lock();
|
|
do{
|
|
#ifdef DUBUG_COMMUNICATION
|
|
LOG("Check Pack");
|
|
#endif
|
|
RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
|
|
#ifdef DUBUG_COMMUNICATION
|
|
LOG("Check Pack Done");
|
|
#endif
|
|
if( RetVal < 0 ){
|
|
// Note: RetVal is NEGATIVE!!
|
|
memcpy( m_pParserBuffer, m_pParserBuffer-RetVal, m_nParserDataCount+RetVal );
|
|
m_nParserDataCount+=RetVal;
|
|
};
|
|
}while( ( RetVal < 0 ) && ( m_nParserDataCount > 0 ) && ( m_bThreadStatus < TH_ENDED ) );
|
|
|
|
if( RetVal == 0 ){ // Package Complete at beginning of buffer
|
|
protocolPackage *tVal = NULL;
|
|
do{
|
|
tVal = packetExtractor( m_pParserBuffer, m_nParserDataCount );
|
|
if( tVal != NULL ) {
|
|
#ifdef DUBUG_COMMUNICATION
|
|
LOG("Package Handling");
|
|
char tBuf[256];
|
|
sprintf(tBuf,"Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount );
|
|
LOG( tBuf );
|
|
#endif
|
|
packetHandler( tVal );
|
|
//m_vPackagePool.push_back( tVal );
|
|
//ToDo: Something seems to be wrong!!
|
|
m_nParserDataCount-=tVal->m_nSize;
|
|
memcpy( m_pParserBuffer, m_pParserBuffer+tVal->m_nSize, m_nParserDataCount );
|
|
};
|
|
m_nParserDataCount = 0;
|
|
} while ( ( tVal != NULL ) && ( m_nParserDataCount > 0 ) && ( m_bThreadStatus < TH_ENDED ) );
|
|
};
|
|
m_bBufferAltered = false;
|
|
m_mBufferProtectorMutex.unlock();
|
|
}else{
|
|
// Handle Disconnect
|
|
m_bThreadStatus = TH_ENDED;
|
|
};
|
|
};
|
|
};
|
|
|
|
bool protocolParser::getPackage( U8 **Package, size_t *Size ){
|
|
if( this ) {
|
|
m_mBufferProtectorMutex.lock();
|
|
if( m_vPackagePool.size() ){
|
|
protocolPackage *tVal;
|
|
tVal = m_vPackagePool.front();
|
|
*Package =( U8 * ) malloc( tVal->m_nSize );
|
|
if( *Package != NULL && tVal != NULL ) {
|
|
memcpy( *Package, tVal->m_pData, tVal->m_nSize );
|
|
*Size = tVal->m_nSize;
|
|
m_vPackagePool.erase( m_vPackagePool.begin() );
|
|
free( tVal );
|
|
m_mBufferProtectorMutex.unlock();
|
|
return true;
|
|
};
|
|
};
|
|
m_mBufferProtectorMutex.unlock();
|
|
};
|
|
return false;
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|