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.

143 lines
4.1 KiB

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