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.

315 lines
7.6 KiB

7 years ago
6 years ago
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
11 hours ago
​
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
11 hours ago
​
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
11 hours ago
​
​
​
​
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
11 hours ago
​
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
11 hours ago
​
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
11 hours ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
1 year ago
1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
1 year ago
1 year 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("TSOC", this, X)
  4. #define LOG_ERROR(X) m_pLog->Log("TSOC", this, _LOG_ERROR, X)
  5. // Constructor implementation
  6. transportSocket::transportSocket() :
  7. basicTransport(),
  8. basicDualThread()
  9. {
  10. m_pLog = basicLogCollection::getInstance();
  11. m_nInputBufferSize = 0;
  12. m_nOutputBufferSize = 0;
  13. m_bInitComplete = false;
  14. // For Select IO Mode
  15. pipe(m_iControlPipeFD);
  16. FD_ZERO(&m_sSenderSet);
  17. FD_ZERO(&m_sReaderSet);
  18. FD_SET(m_iControlPipeFD[0], &m_sSenderSet);
  19. m_nSenderSetMax = m_iControlPipeFD[0];
  20. FD_SET(m_iControlPipeFD[1], &m_sReaderSet);
  21. m_nReaderSetMax = m_iControlPipeFD[1];
  22. /*
  23. #ifdef DEBUG
  24. LOG("Init IO Threads!");
  25. #endif
  26. if( init() ){
  27. int cnt = 5;
  28. while( cnt-- && m_bThreadAStatus < TH_WORKING ) SLEEP(1); // To ensure start Thread procedures
  29. #ifdef DEBUG
  30. LOG("Init IO Threads Done");
  31. #endif
  32. setConnectionStatus( CS_CONNECTED );
  33. } else {
  34. LOG_ERROR("Init IO Threads ERROR!");
  35. };
  36. */
  37. }
  38. // Destructor implementation
  39. transportSocket::~transportSocket()
  40. {
  41. #ifdef DEBUG_EXTRA
  42. LOG("Destructor begin!");
  43. #endif
  44. m_mBufferProtectorMutex.unlock();
  45. _close(); //Uninit Threads in the function
  46. #ifdef DEBUG_EXTRA
  47. LOG("Destructor end!");
  48. #endif
  49. }
  50. bool transportSocket::create(void)
  51. {
  52. m_bInitComplete = init();
  53. if (m_bInitComplete)
  54. {
  55. setConnectionStatus(CS_CONNECTED);
  56. }
  57. return m_bInitComplete;
  58. }
  59. bool transportSocket::_close(void)
  60. {
  61. setConnectionStatus(CS_DISCONNECTED);
  62. // The sender thread (A) reads the control pipe's read end [0]: the stop byte goes into the
  63. // write end [1] (it was written into [0], which fails, so A only stopped at its 5 s select timeout).
  64. // The receiver thread (B) is stopped by the force flag (and wakes up when the socket is shut down).
  65. if (m_iControlPipeFD[1] > -1)
  66. {
  67. if (write(m_iControlPipeFD[1], "E", 1) < 0)
  68. {
  69. // pipe full or broken: the force flag below still stops the thread
  70. }
  71. };
  72. waitThreadAEnd(true);
  73. waitThreadBEnd(true);
  74. //if ( m_bInitComplete )
  75. //{
  76. // LOG("Uninit");
  77. // m_bInitComplete = ! uninit();
  78. // LOG("Uninit Done!");
  79. //}
  80. // LOG("Closeing FIDs!");
  81. // Mark closed: _close() runs again from ~transportSocket after a derived class closed the transport,
  82. // and the old numbers may already belong to another connection's socket or pipe.
  83. if (m_iControlPipeFD[0] > -1)
  84. {
  85. close(m_iControlPipeFD[0]);
  86. m_iControlPipeFD[0] = -1;
  87. };
  88. if (m_iControlPipeFD[1] > -1)
  89. {
  90. close(m_iControlPipeFD[1]);
  91. m_iControlPipeFD[1] = -1;
  92. };
  93. return true;
  94. }
  95. void transportSocket::m_fThreadAProc(void)
  96. {
  97. int index;
  98. int RetVal;
  99. int iActiveFDS;
  100. fd_set sWorkingSet;
  101. struct timeval sTimeOut;
  102. m_bThreadAStatus = TH_WORKING;
  103. #ifdef DEBUG_EXTRA
  104. LOG("Sender Thread Started!");
  105. #endif
  106. while (m_bThreadAStatus < TH_ENDED)
  107. {
  108. sTimeOut.tv_sec = 5;
  109. sTimeOut.tv_usec = 0;
  110. memcpy(&sWorkingSet, &m_sSenderSet, sizeof(m_sSenderSet));
  111. iActiveFDS = select(m_nSenderSetMax + 1, &sWorkingSet, NULL, NULL, &sTimeOut);
  112. if (iActiveFDS == 0)
  113. {
  114. }
  115. else if (iActiveFDS == -1)
  116. {
  117. // m_bThreadBStatus = TH_ENDED;
  118. m_bThreadAStatus = TH_ENDED;
  119. //LOG_ERROR("Failure during Select[s]!");
  120. }
  121. else
  122. {
  123. for (int i = 0; i <= m_nSenderSetMax && iActiveFDS > 0; i++)
  124. {
  125. if (FD_ISSET(i, &sWorkingSet))
  126. {
  127. iActiveFDS--;
  128. // Successful
  129. if (i == m_iControlPipeFD[0])
  130. {
  131. char buffer;
  132. RetVal = read(i, &buffer, 1);
  133. if (buffer == 'A')
  134. {
  135. m_mBufferProtectorMutex.lock();
  136. if (m_nOutputBufferSize > 0 && m_bThreadAStatus < TH_ENDED)
  137. {
  138. index = 0;
  139. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED))
  140. {
  141. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  142. if (RetVal > 0)
  143. {
  144. index += RetVal;
  145. }
  146. if (RetVal == -1)
  147. {
  148. setConnectionStatus(CS_DISCONNECTED);
  149. m_nOutputBufferSize = 0;
  150. m_bThreadBStatus = TH_ENDED;
  151. m_bThreadAStatus = TH_ENDED;
  152. LOG_ERROR("Connection Droped on Sending Data!");
  153. };
  154. };
  155. m_nOutputBufferSize = 0;
  156. m_mBufferProtectorMutex.unlock();
  157. }
  158. else
  159. {
  160. m_mBufferProtectorMutex.unlock();
  161. //if( m_bThreadAStatus < TH_ENDED ) usleep(100);
  162. };
  163. };
  164. if (buffer == 'E')
  165. {
  166. // m_bThreadBStatus = TH_ENDED;
  167. m_bThreadAStatus = TH_ENDED;
  168. //LOG("Requesting Destruction of Transport[s]!");
  169. };
  170. };
  171. };
  172. };
  173. };
  174. };
  175. #ifdef DEBUG_EXTRA
  176. LOG("Sender Thread End!");
  177. #endif
  178. }
  179. void transportSocket::m_fThreadBProc(void)
  180. {
  181. int RetVal;
  182. int iActiveFDS;
  183. fd_set sWorkingSet;
  184. struct timeval sTimeOut;
  185. #ifdef DEBUG_EXTRA
  186. LOG("Recipient Thread Started!");
  187. #endif
  188. m_bThreadBStatus = TH_WORKING;
  189. while (m_bThreadBStatus < TH_ENDED)
  190. {
  191. sTimeOut.tv_sec = 5;
  192. sTimeOut.tv_usec = 0;
  193. memcpy(&sWorkingSet, &m_sReaderSet, sizeof(m_sReaderSet));
  194. iActiveFDS = select(m_nReaderSetMax + 1, &sWorkingSet, NULL, NULL, &sTimeOut);
  195. if (iActiveFDS == 0)
  196. {
  197. }
  198. else if (iActiveFDS == -1)
  199. {
  200. m_bThreadBStatus = TH_ENDED;
  201. m_bThreadAStatus = TH_ENDED;
  202. // No Message - Error Generated During Forced Disconnect!!
  203. //LOG_ERROR("Failure during Select[r]!");
  204. }
  205. else
  206. {
  207. for (int i = 0; i <= m_nReaderSetMax && iActiveFDS > 0; i++)
  208. {
  209. if (FD_ISSET(i, &sWorkingSet))
  210. {
  211. if (i == m_iControlPipeFD[1])
  212. {
  213. char buffer;
  214. RetVal = read(i, &buffer, 1);
  215. if (buffer == 'E')
  216. {
  217. m_bThreadBStatus = TH_ENDED;
  218. LOG("Requesting Destruction of Transport[r]!");
  219. };
  220. }
  221. else
  222. {
  223. RetVal = recptBlock(m_pInputBuffer, &m_nInputBufferSize);
  224. if (RetVal == -1)
  225. {
  226. setConnectionStatus(CS_DISCONNECTED);
  227. m_nInputBufferSize = 0;
  228. m_bThreadBStatus = TH_ENDED;
  229. // m_bThreadAStatus = TH_ENDED;
  230. #ifdef DEBUG_EXTRA
  231. LOG_ERROR("Connection Droped on Reception Data");
  232. #endif
  233. }
  234. else if (m_nInputBufferSize > 0)
  235. {
  236. do
  237. {
  238. m_mQueueProtectorMutex.lock();
  239. if (m_vParsers.size())
  240. {
  241. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++)
  242. {
  243. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  244. }
  245. m_nInputBufferSize = 0;
  246. m_mQueueProtectorMutex.unlock();
  247. }
  248. else
  249. {
  250. m_nInputBufferSize = 0;
  251. m_mQueueProtectorMutex.unlock();
  252. #ifdef DEBUG_EXTRA
  253. LOG_ERROR("Try to parse data but No Parsers Assigned!");
  254. #endif
  255. // if( m_bThreadAStatus < TH_ENDED ) usleep(100);
  256. };
  257. }
  258. while ((m_vParsers.size() == 0) && (m_bThreadBStatus < TH_ENDED) && (m_bThreadBStatus < TH_ENDED));
  259. }
  260. else
  261. {
  262. // if( m_bThreadAStatus < TH_ENDED ) usleep(100);
  263. };
  264. };
  265. };
  266. };
  267. };
  268. };
  269. #ifdef DEBUG_EXTRA
  270. LOG("Recipient Thread End!");
  271. #endif
  272. }
  273. bool transportSocket::sendData(unsigned char * Data, int DataSize)
  274. {
  275. if (m_bThreadAStatus != TH_WORKING)
  276. {
  277. if (m_bThreadAStatus < TH_WORKING)
  278. {
  279. LOG_ERROR("Trying to Send Data Before Thread is Started!");
  280. }
  281. else
  282. {
  283. LOG_ERROR("Trying to Send Data After Thread is Dead!");
  284. }
  285. return false;
  286. };
  287. m_mBufferProtectorMutex.lock();
  288. if (m_nOutputBufferSize + DataSize > MAXIMAL_SOCKET_IO_BUFFER_LENGTH)
  289. {
  290. m_mBufferProtectorMutex.unlock();
  291. LOG_ERROR("Data Too BIG");
  292. return false;
  293. };
  294. memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
  295. m_nOutputBufferSize += DataSize;
  296. m_mBufferProtectorMutex.unlock();
  297. if (m_iControlPipeFD[1] > -1)
  298. {
  299. write(m_iControlPipeFD[1], "A", 1);
  300. }
  301. return true;
  302. }
  303. #undef LOG
  304. #undef LOG_ERROR