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.

141 lines
3.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "transportSocket.h"
  2. #define LOG( X ) m_pLog->Log( "TransportSocket", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "TransportSocket", this, _LOG_ERROR, X )
  4. // Constructor implementation
  5. transportSocket::transportSocket() : basicTransport(), basicDualThread(){
  6. m_pLog = basicLogCollection::getInstance();
  7. //m_pInputBuffer = (U8 *)malloc(MAXIMAL_SOCKET_PACKET_LENGTH);
  8. //if (m_pInputBuffer != NULL) {
  9. m_nInputBufferSize = 0;
  10. // m_pOutputBuffer = (U8 *)malloc(MAXIMAL_SOCKET_PACKET_LENGTH);
  11. // if (m_pOutputBuffer != NULL) {
  12. m_nOutputBufferSize = 0;
  13. #ifdef DEBUG
  14. LOG("Init IO Threads!");
  15. #endif
  16. if( init() ){ //basicDualThread::
  17. int cnt = 5;
  18. while( cnt-- && m_bThreadAStatus < TH_WORKING ) SLEEP(1); // To ensure start Thread procedures
  19. #ifdef DEBUG
  20. LOG("Init IO Threads Done");
  21. #endif
  22. } else {
  23. LOG_ERROR("Init IO Threads ERROR!");
  24. };
  25. // };
  26. //};
  27. };
  28. // Destructor implementation
  29. transportSocket::~transportSocket(){
  30. # ifdef DEBUG
  31. LOG("Destruct: START");
  32. # endif
  33. m_mBufferProtectorMutex.unlock();
  34. if( m_bThreadAStatus > TH_ARMED ) {
  35. waitThreadAEnd();
  36. # ifdef DEBUG
  37. LOG("DONE");
  38. # endif
  39. };
  40. m_mQueueProtectorMutex.unlock();
  41. if( m_bThreadBStatus > TH_ARMED ) {
  42. waitThreadBEnd();
  43. # ifdef DEBUG
  44. LOG("DONE");
  45. # endif
  46. };
  47. //if (m_pInputBuffer != NULL) {
  48. // free(m_pInputBuffer);
  49. //};
  50. //if (m_pOutputBuffer != NULL) {
  51. // free(m_pOutputBuffer);
  52. //};
  53. # ifdef DEBUG
  54. LOG("Destruct: DONE");
  55. # endif
  56. };
  57. void transportSocket::m_fThreadAProc(void){
  58. int index;
  59. int RetVal;
  60. m_bThreadAStatus = TH_WORKING;
  61. LOG("Sender Thread Started!");
  62. while (m_bThreadAStatus < TH_ENDED) {
  63. m_mBufferProtectorMutex.lock();
  64. if( m_nOutputBufferSize > 0 ){
  65. index = 0;
  66. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
  67. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  68. if( RetVal > 0 ) index += RetVal;
  69. if( RetVal == -1 ){
  70. m_bThreadBStatus = TH_ENDED;
  71. m_bThreadAStatus = TH_ENDED;
  72. LOG_ERROR("Connection Error[send]");
  73. setConnectionStatus( CS_DISCONNECTED );
  74. m_nOutputBufferSize = 0;
  75. };
  76. };
  77. m_nOutputBufferSize = 0;
  78. m_mBufferProtectorMutex.unlock();
  79. } else {
  80. m_mBufferProtectorMutex.unlock();
  81. SLEEP(5);
  82. };
  83. };
  84. };
  85. void transportSocket::m_fThreadBProc(void){
  86. m_bThreadBStatus = TH_WORKING;
  87. int RetVal;
  88. LOG("Recipient Thread Started!");
  89. while( m_bThreadBStatus < TH_ENDED ){
  90. RetVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize);
  91. if( RetVal == -1 ){
  92. LOG_ERROR("Connection Error[rcpt]");
  93. m_nInputBufferSize = 0;
  94. if(!getStatus()) {
  95. setConnectionStatus(CS_DISCONNECTED);
  96. m_bThreadBStatus = TH_ENDED;
  97. m_bThreadAStatus = TH_ENDED;
  98. } else SLEEP(100);
  99. } else if( m_nInputBufferSize > 0 ){
  100. if (m_vParsers.size()) {
  101. m_mQueueProtectorMutex.lock();
  102. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) {
  103. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  104. };
  105. m_nInputBufferSize = 0;
  106. m_mQueueProtectorMutex.unlock();
  107. } else {
  108. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  109. };
  110. } else {
  111. SLEEP(5);
  112. };
  113. };
  114. };
  115. bool transportSocket::sendData(unsigned char *Data, int DataSize){
  116. if( m_bThreadAStatus != TH_WORKING ) {
  117. LOG_ERROR("Trying to Send Data Before Transport Started!");
  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