|
|
|
@ -2,8 +2,11 @@ |
|
|
|
|
|
|
|
#define LOG( X ) m_pLog->Log( "Proto", this, X )
|
|
|
|
#define LOG_ERROR( X ) m_pLog->Log( "Proto", this, _LOG_ERROR, X )
|
|
|
|
//Thread Logging
|
|
|
|
#define SLOG( X ) mThis->m_pLog->Log( "ProtoS", mThis, X )
|
|
|
|
#define SLOG_ERROR( X ) mThis->m_pLog->Log( "ProtoS", mThis, _LOG_ERROR, X )
|
|
|
|
|
|
|
|
protocolParser::protocolParser() :basicThread(),m_mBufferProtectorMutex(){ |
|
|
|
protocolParser::protocolParser() : m_mBufferProtectorMutex(){ |
|
|
|
m_pLog = basicLogCollection::getInstance(); |
|
|
|
m_pParserBuffer = (U8*) malloc(PROTOCOL_PARSER_BUFFER_SIZE); |
|
|
|
m_nParserDataCount = 0; |
|
|
|
@ -11,6 +14,7 @@ protocolParser::protocolParser() :basicThread(),m_mBufferProtectorMutex(){ |
|
|
|
m_pCustomHandler = NULL; |
|
|
|
m_bBufferAltered = false; |
|
|
|
m_bStatus = 1; |
|
|
|
m_bThreadStatus = TH_PRE_INIT; |
|
|
|
m_eConnectionStatus = CS_UNKNOWN; |
|
|
|
}; |
|
|
|
|
|
|
|
@ -18,6 +22,9 @@ protocolParser::~protocolParser(){ |
|
|
|
# ifdef DEBUG
|
|
|
|
LOG("Destructor Begin"); |
|
|
|
# endif
|
|
|
|
if (m_bThreadStatus > TH_ARMED ) { |
|
|
|
waitThreadEnd( true ); |
|
|
|
}; |
|
|
|
m_mBufferProtectorMutex.unlock(); |
|
|
|
if (m_bThreadStatus < TH_ENDED ) { |
|
|
|
waitThreadEnd( true ); |
|
|
|
@ -32,7 +39,29 @@ protocolParser::~protocolParser(){ |
|
|
|
|
|
|
|
bool protocolParser::create(void){ |
|
|
|
if (m_pParserBuffer != NULL) { |
|
|
|
return basicThread::init(); |
|
|
|
if (m_bThreadStatus == TH_PRE_INIT) { |
|
|
|
m_bThreadStatus = TH_ARMED; |
|
|
|
#if defined( __linux__ ) || defined(WIN_PTHREAD)
|
|
|
|
int RetVal = pthread_create( &m_sThreadHandler, NULL, m_fThreadEntryPoint, ( void * ) this ); |
|
|
|
if( RetVal == -1 ) { |
|
|
|
LOG_ERROR("Thread Creation Failed."); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
#else
|
|
|
|
m_hThreadHandle = CreateThread( |
|
|
|
NULL, |
|
|
|
0, |
|
|
|
(LPTHREAD_START_ROUTINE)m_fThreadEntryPoint, |
|
|
|
(LPVOID)this, |
|
|
|
0, |
|
|
|
&m_dwThreadID ); |
|
|
|
//TODO: Add thread creation check
|
|
|
|
#endif
|
|
|
|
} else { |
|
|
|
LOG_ERROR("Thread object not ready to be created."); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
return true; |
|
|
|
} else return false; |
|
|
|
}; |
|
|
|
|
|
|
|
@ -77,7 +106,6 @@ void protocolParser::dataEraser( U8 *&tPtr ){ |
|
|
|
tPtr = NULL; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
void protocolParser::m_fThreadProc(void){ |
|
|
|
int RetVal; |
|
|
|
m_bThreadStatus = TH_WORKING; |
|
|
|
@ -143,6 +171,44 @@ void protocolParser::m_fThreadProc(void){ |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
#if defined( __linux__ ) || defined(WIN_PTHREAD)
|
|
|
|
void *protocolParser::m_fThreadEntryPoint( void *ptr ){ |
|
|
|
#else
|
|
|
|
DWORD WINAPI protocolParser::m_fThreadEntryPoint( LPVOID ptr){ |
|
|
|
#endif
|
|
|
|
protocolParser *mThis; |
|
|
|
mThis = ( protocolParser * ) ptr; |
|
|
|
if( mThis->m_bThreadStatus == TH_ARMED ) { |
|
|
|
mThis->m_fThreadProc(); |
|
|
|
mThis->m_bThreadStatus = TH_EXIT; |
|
|
|
} else { |
|
|
|
SLOG_ERROR("Thread is not ARMED!"); |
|
|
|
}; |
|
|
|
#ifdef __linux__
|
|
|
|
pthread_exit( NULL ); |
|
|
|
return NULL; |
|
|
|
#else
|
|
|
|
return 0; |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
|
|
|
|
void protocolParser::waitThreadEnd(bool forced){ |
|
|
|
|
|
|
|
if( m_bThreadStatus > TH_WORKING || m_bThreadStatus < TH_ARMED ) return; |
|
|
|
|
|
|
|
if( forced ) m_bThreadStatus = TH_ENDED; |
|
|
|
while (m_bThreadStatus != TH_EXIT){ |
|
|
|
SLEEP(DEFAULT_THREAD_SLEEP_TIME); |
|
|
|
}; |
|
|
|
#if defined( __linux__ ) || defined(WIN_PTHREAD)
|
|
|
|
pthread_join(m_sThreadHandler, NULL); |
|
|
|
#else
|
|
|
|
WaitForSingleObject(m_hThreadHandle, 2000); |
|
|
|
CloseHandle(m_hThreadHandle); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
bool protocolParser::getPackage( U8 **Package, size_t *Size ){ |
|
|
|
if( this ) { |
|
|
|
m_mBufferProtectorMutex.lock(); |
|
|
|
@ -164,5 +230,9 @@ bool protocolParser::getPackage( U8 **Package, size_t *Size ){ |
|
|
|
return false; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
#undef SLOG
|
|
|
|
#undef SLOG_ERROR
|
|
|
|
#undef LOG
|
|
|
|
#undef LOG_ERROR
|
|
|
|
|