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.

137 lines
3.6 KiB

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. m_bThreadBStatus = TH_ENDED;
  96. m_bThreadAStatus = TH_ENDED;
  97. setConnectionStatus( CS_DISCONNECTED );
  98. } else SLEEP(100);
  99. } else if( m_nInputBufferSize > 0 ){
  100. m_mQueueProtectorMutex.lock();
  101. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++){
  102. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  103. };
  104. m_nInputBufferSize = 0;
  105. m_mQueueProtectorMutex.unlock();
  106. } else {
  107. SLEEP(5);
  108. };
  109. };
  110. };
  111. bool transportSocket::sendData(unsigned char *Data, int DataSize){
  112. if( m_bThreadAStatus != TH_WORKING ) {
  113. LOG_ERROR("Trying to Send Data Before Transport Started!");
  114. return false;
  115. };
  116. m_mBufferProtectorMutex.lock();
  117. if (m_nOutputBufferSize + DataSize > MAXIMAL_SOCKET_PACKET_LENGTH ){
  118. m_mBufferProtectorMutex.unlock();
  119. LOG_ERROR("Data Too BIG");
  120. return false;
  121. };
  122. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  123. m_nOutputBufferSize += DataSize;
  124. m_mBufferProtectorMutex.unlock();
  125. return true;
  126. };
  127. #undef LOG
  128. #undef LOG_ERROR