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.

112 lines
3.0 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. init();
  14. };
  15. };
  16. };
  17. // Destructor implementation
  18. transportSocket::~transportSocket(){
  19. m_mBufferProtectorMutex.unlock();
  20. SLEEP(1);
  21. if( m_bThreadAStatus > TH_ARMED ) {
  22. waitThreadAEnd( true );
  23. };
  24. m_mQueueProtectorMutex.unlock();
  25. SLEEP(1);
  26. if( m_bThreadBStatus > TH_ARMED ) {
  27. waitThreadBEnd( true );
  28. };
  29. if (m_pInputBuffer != NULL) {
  30. free(m_pInputBuffer);
  31. };
  32. if (m_pOutputBuffer != NULL) {
  33. free(m_pOutputBuffer);
  34. };
  35. };
  36. void transportSocket::m_fThreadAProc(void){
  37. m_bThreadAStatus = TH_WORKING;
  38. while (m_bThreadAStatus < TH_ENDED) {
  39. m_mBufferProtectorMutex.lock();
  40. if( m_nOutputBufferSize > 0 ){
  41. int index;
  42. int RetVal;
  43. index = 0;
  44. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
  45. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  46. if (RetVal > 0) index += RetVal;
  47. };
  48. m_nOutputBufferSize = 0;
  49. m_mBufferProtectorMutex.unlock();
  50. } else {
  51. m_mBufferProtectorMutex.unlock();
  52. SLEEP(1);
  53. };
  54. };
  55. };
  56. void transportSocket::m_fThreadBProc(void){
  57. m_bThreadBStatus = TH_WORKING;
  58. int RetVal;
  59. while( m_bThreadBStatus < TH_ENDED ){
  60. try{
  61. RetVal = recptBlock(m_pInputBuffer, &m_nInputBufferSize);
  62. if (m_nInputBufferSize > 0){
  63. m_mQueueProtectorMutex.lock();
  64. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++){
  65. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  66. };
  67. m_mQueueProtectorMutex.unlock();
  68. } else if (RetVal == -1){
  69. m_bThreadBStatus = TH_ENDED;//
  70. m_bThreadAStatus = TH_ENDED;//
  71. LOG_ERROR("Connection Error");
  72. } else {
  73. SLEEP(1);
  74. };
  75. }
  76. catch (exception e){
  77. m_bThreadBStatus = TH_ENDED;//
  78. m_bThreadAStatus = TH_ENDED;//
  79. LOG_ERROR("rcptBlock Error");
  80. };
  81. };
  82. LOG("Ended");
  83. };
  84. void transportSocket::setConnectionStatus( CONNECTION_STATUS CS ){
  85. m_mQueueProtectorMutex.lock();
  86. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++){
  87. (*index)->setConnectionStatus( CS );
  88. };
  89. m_mQueueProtectorMutex.unlock();
  90. }
  91. bool transportSocket::sendData(unsigned char *Data, int DataSize){
  92. m_mBufferProtectorMutex.lock();
  93. if (m_nOutputBufferSize + DataSize > MAXIMAL_SOCKET_PACKET_LENGTH) {
  94. m_mBufferProtectorMutex.unlock();
  95. LOG_ERROR("Data Too BIG");
  96. return false;
  97. };
  98. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  99. m_nOutputBufferSize += DataSize;
  100. m_mBufferProtectorMutex.unlock();
  101. return true;
  102. };
  103. #undef LOG
  104. #undef LOG_ERROR