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.

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