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.

140 lines
4.2 KiB

7 years ago
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
7 years ago
7 years ago
​
​
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
2 years ago
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
7 years ago
​
  1. #include "transportSocket.h"
  2. #include "../../data/protocol/protocol.h"
  3. #define LOG( X ) m_pLog->Log( "TransportSocket", this, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "TransportSocket", this, _LOG_ERROR, X )
  5. // Constructor implementation
  6. transportSocket::transportSocket() : basicTransport(), basicDualThread(){
  7. m_pLog = basicLogCollection::getInstance();
  8. m_nInputBufferSize = 0;
  9. m_nOutputBufferSize = 0;
  10. m_bInitComplete = false;
  11. /*
  12. #ifdef DEBUG
  13. LOG("Init IO Threads!");
  14. #endif
  15. if( init() ){
  16. int cnt = 5;
  17. while( cnt-- && m_bThreadAStatus < TH_WORKING ) SLEEP(1); // To ensure start Thread procedures
  18. #ifdef DEBUG
  19. LOG("Init IO Threads Done");
  20. #endif
  21. setConnectionStatus( CS_CONNECTED );
  22. } else {
  23. LOG_ERROR("Init IO Threads ERROR!");
  24. };
  25. */
  26. };
  27. // Destructor implementation
  28. transportSocket::~transportSocket(){
  29. # ifdef DEBUG
  30. LOG("Destructor begin!");
  31. # endif
  32. m_mBufferProtectorMutex.unlock();
  33. uninit();
  34. # ifdef DEBUG
  35. LOG("Destructor end!");
  36. # endif
  37. };
  38. bool transportSocket::create( void ){
  39. m_bInitComplete = init();
  40. if( m_bInitComplete ) setConnectionStatus( CS_CONNECTED );
  41. return m_bInitComplete;
  42. };
  43. void transportSocket::m_fThreadAProc(void){
  44. int index;
  45. int RetVal;
  46. m_bThreadAStatus = TH_WORKING;
  47. # ifdef DEBUG_EXTRA
  48. LOG("Sender Thread Started!");
  49. #endif
  50. while (m_bThreadAStatus < TH_ENDED) {
  51. m_mBufferProtectorMutex.lock();
  52. if( m_nOutputBufferSize > 0 && m_bThreadAStatus < TH_ENDED ){
  53. index = 0;
  54. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
  55. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  56. if( RetVal > 0 ) index += RetVal;
  57. if( RetVal == -1 ){
  58. setConnectionStatus( CS_DISCONNECTED );
  59. m_nOutputBufferSize = 0;
  60. m_bThreadBStatus = TH_ENDED;
  61. m_bThreadAStatus = TH_ENDED;
  62. LOG_ERROR("Connection Droped on Sending Data");
  63. };
  64. };
  65. m_nOutputBufferSize = 0;
  66. m_mBufferProtectorMutex.unlock();
  67. } else {
  68. m_mBufferProtectorMutex.unlock();
  69. if( m_bThreadAStatus < TH_ENDED ) usleep(100);
  70. };
  71. };
  72. LOG("Sender Thread End!");
  73. };
  74. void transportSocket::m_fThreadBProc(void){
  75. m_bThreadBStatus = TH_WORKING;
  76. int RetVal;
  77. #ifdef DEBUG_EXTRA
  78. LOG("Recipient Thread Started!");
  79. #endif
  80. while( m_bThreadBStatus < TH_ENDED ){
  81. RetVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
  82. if( RetVal == -1 ){
  83. setConnectionStatus(CS_DISCONNECTED);
  84. m_nInputBufferSize = 0;
  85. m_bThreadBStatus = TH_ENDED;
  86. m_bThreadAStatus = TH_ENDED;
  87. LOG_ERROR("Connection Droped on Reception Data");
  88. } else if( m_nInputBufferSize > 0 ){
  89. do{
  90. m_mQueueProtectorMutex.lock();
  91. if( m_vParsers.size() ){
  92. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  93. m_nInputBufferSize = 0;
  94. m_mQueueProtectorMutex.unlock();
  95. } else {
  96. m_nInputBufferSize = 0;
  97. m_mQueueProtectorMutex.unlock();
  98. #ifdef DEBUG_EXTRA
  99. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  100. #endif
  101. if( m_bThreadAStatus < TH_ENDED ) usleep(100);
  102. };
  103. } while( ( m_vParsers.size() == 0 ) && ( m_bThreadBStatus < TH_ENDED ) && ( m_bThreadBStatus < TH_ENDED ) );
  104. } else {
  105. if( m_bThreadAStatus < TH_ENDED ) usleep(100);
  106. };
  107. };
  108. LOG("Recipient Thread End!");
  109. };
  110. bool transportSocket::sendData(unsigned char *Data, int DataSize){
  111. if( m_bThreadAStatus != TH_WORKING ) {
  112. if( m_bThreadAStatus < TH_WORKING ) {
  113. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  114. } else {
  115. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  116. }
  117. return false;
  118. };
  119. m_mBufferProtectorMutex.lock();
  120. if (m_nOutputBufferSize + DataSize > MAXIMAL_SOCKET_PACKET_LENGTH ){
  121. m_mBufferProtectorMutex.unlock();
  122. LOG_ERROR("Data Too BIG");
  123. return false;
  124. };
  125. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  126. m_nOutputBufferSize += DataSize;
  127. m_mBufferProtectorMutex.unlock();
  128. return true;
  129. };
  130. #undef LOG
  131. #undef LOG_ERROR