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.

139 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. bool transportSocket::create( void ){
  38. m_bInitComplete = init();
  39. if( m_bInitComplete ) setConnectionStatus( CS_CONNECTED );
  40. return m_bInitComplete;
  41. };
  42. void transportSocket::m_fThreadAProc(void){
  43. int index;
  44. int RetVal;
  45. m_bThreadAStatus = TH_WORKING;
  46. #ifdef DEBUG
  47. LOG("Sender Thread Started!");
  48. #endif
  49. while (m_bThreadAStatus < TH_ENDED) {
  50. m_mBufferProtectorMutex.lock();
  51. if( m_nOutputBufferSize > 0 ){
  52. index = 0;
  53. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
  54. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  55. if( RetVal > 0 ) index += RetVal;
  56. if( RetVal == -1 ){
  57. setConnectionStatus( CS_DISCONNECTED );
  58. m_nOutputBufferSize = 0;
  59. m_bThreadBStatus = TH_ENDED;
  60. m_bThreadAStatus = TH_ENDED;
  61. LOG_ERROR("Connection Droped on Sending Data");
  62. };
  63. };
  64. m_nOutputBufferSize = 0;
  65. m_mBufferProtectorMutex.unlock();
  66. } else {
  67. m_mBufferProtectorMutex.unlock();
  68. SLEEP(5);
  69. };
  70. };
  71. };
  72. void transportSocket::m_fThreadBProc(void){
  73. m_bThreadBStatus = TH_WORKING;
  74. int RetVal;
  75. #ifdef DEBUG
  76. LOG("Recipient Thread Started!");
  77. #endif
  78. while( m_bThreadBStatus < TH_ENDED ){
  79. RetVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
  80. if( RetVal == -1 ){
  81. setConnectionStatus(CS_DISCONNECTED);
  82. m_nInputBufferSize = 0;
  83. //if(!getStatus()) {
  84. m_bThreadBStatus = TH_ENDED;
  85. m_bThreadAStatus = TH_ENDED;
  86. //} else SLEEP(100);
  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. SLEEP(100);
  102. };
  103. } while( ( m_vParsers.size() == 0 ) && ( m_bThreadBStatus < TH_ENDED ) && ( m_bThreadBStatus < TH_ENDED ) );
  104. } else {
  105. SLEEP(5);
  106. };
  107. };
  108. };
  109. bool transportSocket::sendData(unsigned char *Data, int DataSize){
  110. if( m_bThreadAStatus != TH_WORKING ) {
  111. if( m_bThreadAStatus < TH_WORKING ) {
  112. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  113. } else {
  114. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  115. }
  116. return false;
  117. };
  118. m_mBufferProtectorMutex.lock();
  119. if (m_nOutputBufferSize + DataSize > MAXIMAL_SOCKET_PACKET_LENGTH ){
  120. m_mBufferProtectorMutex.unlock();
  121. LOG_ERROR("Data Too BIG");
  122. return false;
  123. };
  124. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  125. m_nOutputBufferSize += DataSize;
  126. m_mBufferProtectorMutex.unlock();
  127. return true;
  128. };
  129. #undef LOG
  130. #undef LOG_ERROR