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.0 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
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("Destruct: START");
  31. # endif
  32. m_mBufferProtectorMutex.unlock();
  33. #ifdef DEBUG
  34. LOG("Destruct: DONE");
  35. # endif
  36. };
  37. /*
  38. bool transportSocket::create( void ){
  39. m_bInitComplete = init();
  40. if( m_bInitComplete ) setConnectionStatus( CS_CONNECTED );
  41. return m_bInitComplete;
  42. }
  43. ;*/
  44. void transportSocket::m_fThreadAProc(void){
  45. int index;
  46. int RetVal;
  47. m_bThreadAStatus = TH_WORKING;
  48. #ifdef DEBUG
  49. LOG("Sender Thread Started!");
  50. #endif
  51. while (m_bThreadAStatus < TH_ENDED) {
  52. m_mBufferProtectorMutex.lock();
  53. if( m_nOutputBufferSize > 0 ){
  54. index = 0;
  55. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
  56. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  57. if( RetVal > 0 ) index += RetVal;
  58. if( RetVal == -1 ){
  59. setConnectionStatus( CS_DISCONNECTED );
  60. m_nOutputBufferSize = 0;
  61. m_bThreadBStatus = TH_ENDED;
  62. m_bThreadAStatus = TH_ENDED;
  63. LOG_ERROR("Connection Droped on Sending Data");
  64. };
  65. };
  66. m_nOutputBufferSize = 0;
  67. m_mBufferProtectorMutex.unlock();
  68. } else {
  69. m_mBufferProtectorMutex.unlock();
  70. SLEEP(5);
  71. };
  72. };
  73. };
  74. void transportSocket::m_fThreadBProc(void){
  75. m_bThreadBStatus = TH_WORKING;
  76. int RetVal;
  77. #ifdef DEBUG
  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. //if(!getStatus()) {
  86. m_bThreadBStatus = TH_ENDED;
  87. m_bThreadAStatus = TH_ENDED;
  88. //} else SLEEP(100);
  89. LOG_ERROR("Connection Droped on Reception Data");
  90. } else if( m_nInputBufferSize > 0 ){
  91. do{
  92. m_mQueueProtectorMutex.lock();
  93. if( m_vParsers.size() ){
  94. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  95. m_nInputBufferSize = 0;
  96. m_mQueueProtectorMutex.unlock();
  97. } else {
  98. m_nInputBufferSize = 0;
  99. m_mQueueProtectorMutex.unlock();
  100. #ifdef DEBUG_EXTRA
  101. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  102. #endif
  103. SLEEP(100);
  104. };
  105. } while( ( m_vParsers.size() == 0 ) && ( m_bThreadBStatus < TH_ENDED ) && ( m_bThreadBStatus < TH_ENDED ) );
  106. } else {
  107. SLEEP(5);
  108. };
  109. };
  110. };
  111. bool transportSocket::sendData(unsigned char *Data, int DataSize){
  112. if( m_bThreadAStatus != TH_WORKING ) {
  113. if( m_bThreadAStatus < TH_WORKING ) {
  114. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  115. } else {
  116. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  117. }
  118. return false;
  119. };
  120. m_mBufferProtectorMutex.lock();
  121. if (m_nOutputBufferSize + DataSize > MAXIMAL_SOCKET_PACKET_LENGTH ){
  122. m_mBufferProtectorMutex.unlock();
  123. LOG_ERROR("Data Too BIG");
  124. return false;
  125. };
  126. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  127. m_nOutputBufferSize += DataSize;
  128. m_mBufferProtectorMutex.unlock();
  129. return true;
  130. };
  131. #undef LOG
  132. #undef LOG_ERROR