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.

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