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
​
​
​
2 years ago
2 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_EXTRA
  30. LOG("Destructor begin!");
  31. # endif
  32. uninit();
  33. # ifdef DEBUG_EXTRA
  34. LOG("Destructor end!");
  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_EXTRA
  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. usleep(100);
  69. };
  70. };
  71. LOG("Sender Thread End!");
  72. };
  73. void transportSocket::m_fThreadBProc(void){
  74. m_bThreadBStatus = TH_WORKING;
  75. int RetVal;
  76. #ifdef DEBUG_EXTRA
  77. LOG("Recipient Thread Started!");
  78. #endif
  79. while( m_bThreadBStatus < TH_ENDED ){
  80. RetVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
  81. if( RetVal == -1 ){
  82. setConnectionStatus(CS_DISCONNECTED);
  83. m_nInputBufferSize = 0;
  84. m_bThreadBStatus = TH_ENDED;
  85. m_bThreadAStatus = TH_ENDED;
  86. LOG_ERROR("Connection Droped on Reception Data");
  87. } else if( m_nInputBufferSize > 0 ){
  88. do{
  89. m_mQueueProtectorMutex.lock();
  90. if( m_vParsers.size() ){
  91. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  92. m_nInputBufferSize = 0;
  93. m_mQueueProtectorMutex.unlock();
  94. } else {
  95. m_nInputBufferSize = 0;
  96. m_mQueueProtectorMutex.unlock();
  97. #ifdef DEBUG_EXTRA
  98. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  99. #endif
  100. SLEEP(100);
  101. };
  102. } while( ( m_vParsers.size() == 0 ) && ( m_bThreadBStatus < TH_ENDED ) && ( m_bThreadBStatus < TH_ENDED ) );
  103. } else {
  104. usleep(100);
  105. };
  106. };
  107. LOG("Recipient Thread End!");
  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