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

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocol.h"
  2. #define LOG( X ) m_pLog->Log( "Proto", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "Proto", this, _LOG_ERROR, X )
  4. protocolParser::protocolParser(config_setting_t *Settings) :basicThread(),m_mBufferProtectorMutex(){
  5. m_pLog = basicLogCollection::getInstance();
  6. m_pParserBuffer = (U8*) malloc(PROTOCOL_PARSER_BUFFER_SIZE);
  7. m_nParserDataCount = 0;
  8. m_bStatus = 0;
  9. m_pCustomHandler = NULL;
  10. m_bBufferAltered = false;
  11. m_bStatus = 1;
  12. m_eConnectionStatus = CS_UNKNOWN;
  13. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, PROTO_TAG_GROUP);
  14. };
  15. protocolParser::~protocolParser(){
  16. # ifdef DEBUG
  17. LOG("Destructor Begin");
  18. # endif
  19. m_mBufferProtectorMutex.unlock();
  20. if (m_bThreadStatus < TH_ENDED ) {
  21. waitThreadEnd( true );
  22. };
  23. if( m_pParserBuffer != NULL ) {
  24. free( m_pParserBuffer );
  25. };
  26. # ifdef DEBUG
  27. LOG("Destructor End");
  28. # endif
  29. };
  30. bool protocolParser::create(void){
  31. if (m_pSettings == NULL)return false;
  32. if (m_pParserBuffer != NULL) {
  33. return basicThread::init();
  34. } else return false;
  35. };
  36. void protocolParser::setConnectionStatus( CONNECTION_STATUS cStatus ){
  37. m_eConnectionStatus = cStatus;
  38. };
  39. CONNECTION_STATUS protocolParser::getConnectionStatus( void ){
  40. return m_eConnectionStatus;
  41. };
  42. bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){
  43. if( m_pParserBuffer != NULL ) {
  44. m_mBufferProtectorMutex.lock();
  45. memcpy( m_pParserBuffer+m_nParserDataCount, Buffer, BufferSize );
  46. m_nParserDataCount+=BufferSize;
  47. m_bBufferAltered = true;
  48. m_mBufferProtectorMutex.unlock();
  49. };
  50. return m_bBufferAltered;
  51. };
  52. void protocolParser::m_fThreadProc(void){
  53. int RetVal;
  54. m_bThreadStatus = TH_WORKING;
  55. while(m_bThreadStatus < TH_ENDED){
  56. if( m_eConnectionStatus < CS_DISCONNECTED ){
  57. if( ( m_bBufferAltered == false ) && ( m_bThreadStatus < TH_ENDED ) ) {
  58. SLEEP(1);
  59. continue;
  60. };
  61. m_mBufferProtectorMutex.lock();
  62. do{
  63. #ifdef DUBUG_COMMUNICATION
  64. LOG("Check Pack");
  65. #endif
  66. RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
  67. #ifdef DUBUG_COMMUNICATION
  68. LOG("Check Pack Done");
  69. #endif
  70. if( RetVal < 0 ){
  71. // Note: RetVal is NEGATIVE!!
  72. memcpy( m_pParserBuffer, m_pParserBuffer-RetVal, m_nParserDataCount+RetVal );
  73. m_nParserDataCount+=RetVal;
  74. };
  75. }while( ( RetVal < 0 ) && ( m_nParserDataCount > 0 ) && ( m_bThreadStatus < TH_ENDED ) );
  76. if( RetVal == 0 ){ // Package Complete at beginning of buffer
  77. protocolPackage *tVal = NULL;
  78. do{
  79. tVal = packetExtractor( m_pParserBuffer, m_nParserDataCount );
  80. if( tVal != NULL ) {
  81. #ifdef DUBUG_COMMUNICATION
  82. LOG("Package Handling");
  83. char tBuf[256];
  84. sprintf(tBuf,"Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount );
  85. LOG( tBuf );
  86. #endif
  87. packetHandler( tVal );
  88. //m_vPackagePool.push_back( tVal );
  89. //ToDo: Something seems to be wrong!!
  90. m_nParserDataCount-=tVal->m_nSize;
  91. memcpy( m_pParserBuffer, m_pParserBuffer+tVal->m_nSize, m_nParserDataCount );
  92. };
  93. m_nParserDataCount = 0;
  94. } while ( ( tVal != NULL ) && ( m_nParserDataCount > 0 ) && ( m_bThreadStatus < TH_ENDED ) );
  95. };
  96. m_bBufferAltered = false;
  97. m_mBufferProtectorMutex.unlock();
  98. }else{
  99. // Handle Disconnect
  100. m_bThreadStatus = TH_ENDED;
  101. };
  102. };
  103. };
  104. bool protocolParser::getPackage( U8 **Package, size_t *Size ){
  105. if( this ) {
  106. m_mBufferProtectorMutex.lock();
  107. if( m_vPackagePool.size() ){
  108. protocolPackage *tVal;
  109. tVal = m_vPackagePool.front();
  110. *Package =( U8 * ) malloc( tVal->m_nSize );
  111. if( *Package != NULL && tVal != NULL ) {
  112. memcpy( *Package, tVal->m_pData, tVal->m_nSize );
  113. *Size = tVal->m_nSize;
  114. m_vPackagePool.erase( m_vPackagePool.begin() );
  115. free( tVal );
  116. m_mBufferProtectorMutex.unlock();
  117. return true;
  118. };
  119. };
  120. m_mBufferProtectorMutex.unlock();
  121. };
  122. return false;
  123. };
  124. #undef LOG
  125. #undef LOG_ERROR