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.

142 lines
3.7 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 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. #ifdef DEBUG
  11. LOG("Init IO Threads!");
  12. #endif
  13. if( init() ){
  14. int cnt = 5;
  15. while( cnt-- && m_bThreadAStatus < TH_WORKING ) SLEEP(1); // To ensure start Thread procedures
  16. #ifdef DEBUG
  17. LOG("Init IO Threads Done");
  18. #endif
  19. } else {
  20. LOG_ERROR("Init IO Threads ERROR!");
  21. };
  22. };
  23. // Destructor implementation
  24. transportSocket::~transportSocket(){
  25. # ifdef DEBUG
  26. LOG("Destruct: START");
  27. # endif
  28. m_mBufferProtectorMutex.unlock();
  29. if( m_bThreadAStatus > TH_ARMED ) {
  30. waitThreadAEnd();
  31. # ifdef DEBUG
  32. LOG("DONE");
  33. # endif
  34. };
  35. m_mQueueProtectorMutex.unlock();
  36. if( m_bThreadBStatus > TH_ARMED ) {
  37. waitThreadBEnd();
  38. # ifdef DEBUG
  39. LOG("DONE");
  40. # endif
  41. };
  42. # ifdef DEBUG
  43. LOG("Destruct: DONE");
  44. # endif
  45. };
  46. void transportSocket::m_fThreadAProc(void){
  47. int index;
  48. int RetVal;
  49. m_bThreadAStatus = TH_WORKING;
  50. LOG("Sender Thread Started!");
  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. m_bThreadBStatus = TH_ENDED;
  60. m_bThreadAStatus = TH_ENDED;
  61. LOG_ERROR("Connection Error[send]");
  62. setConnectionStatus( CS_DISCONNECTED );
  63. m_nOutputBufferSize = 0;
  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. LOG("Recipient Thread Started!");
  78. while( m_bThreadBStatus < TH_ENDED ){
  79. RetVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize);
  80. if( RetVal == -1 ){
  81. LOG_ERROR("Connection Error[rcpt]");
  82. m_nInputBufferSize = 0;
  83. if(!getStatus()) {
  84. setConnectionStatus(CS_DISCONNECTED);
  85. m_bThreadBStatus = TH_ENDED;
  86. m_bThreadAStatus = TH_ENDED;
  87. } else SLEEP(100);
  88. } else if( m_nInputBufferSize > 0 ){
  89. LOG("Try Parse Data!");
  90. if (m_vParsers.size()) {
  91. m_mQueueProtectorMutex.lock();
  92. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) {
  93. #ifdef DEBUG
  94. LOG("Parse Enter!");
  95. if(
  96. #endif
  97. (*index)->parse(m_pInputBuffer, m_nInputBufferSize)
  98. #ifdef DEBUG
  99. ){
  100. LOG("Data Consumed by Parser!");
  101. }
  102. LOG("Parse Done!")
  103. #endif
  104. ;
  105. };
  106. m_nInputBufferSize = 0;
  107. m_mQueueProtectorMutex.unlock();
  108. } else {
  109. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  110. };
  111. } else {
  112. SLEEP(5);
  113. };
  114. };
  115. };
  116. bool transportSocket::sendData(unsigned char *Data, int DataSize){
  117. if( m_bThreadAStatus != TH_WORKING ) {
  118. LOG_ERROR("Trying to Send Data Before Transport Started!");
  119. return false;
  120. };
  121. m_mBufferProtectorMutex.lock();
  122. if (m_nOutputBufferSize + DataSize > MAXIMAL_SOCKET_PACKET_LENGTH ){
  123. m_mBufferProtectorMutex.unlock();
  124. LOG_ERROR("Data Too BIG");
  125. return false;
  126. };
  127. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  128. m_nOutputBufferSize += DataSize;
  129. m_mBufferProtectorMutex.unlock();
  130. return true;
  131. };
  132. #undef LOG
  133. #undef LOG_ERROR