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.

721 lines
32 KiB

12 months 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
44 minutes ago
​
​
​
12 months 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
44 minutes ago
​
12 months 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
44 minutes ago
12 months ago
​
​
​
​
12 months ago
12 months ago
​
​
​
​
​
​
​
​
​
12 months ago
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
​
12 months ago
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months 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
44 minutes 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
44 minutes ago
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
12 months ago
12 months ago
12 months ago
​
​
12 months ago
​
12 months ago
12 months ago
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
12 months ago
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months 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
44 minutes ago
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 weeks ago
12 months 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
44 minutes ago
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months 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
44 minutes ago
​
12 months 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
44 minutes ago
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servaRemoteClientHandler.h"
  2. #include "servaRemoteClient.h"
  3. #include "servaKernel.h"
  4. #define LOG( X ) m_pLog->Log( "RPCH", this, X )
  5. #ifdef DEBUG
  6. #define LOG_ERROR( X ) {\
  7. char _tBfrErrMSG234[256];\
  8. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  9. m_pLog->Log( "RPCH", this, _LOG_ERROR, _tBfrErrMSG234 );\
  10. }
  11. #else
  12. #define LOG_ERROR( X ) m_pLog->Log( "RPCH", this, _LOG_ERROR, X );
  13. #endif
  14. servaRPCHadnler::servaRPCHadnler( int SocketFD, networkServiceEngine *ptr ):basicThread(),networkClientHandler( SocketFD, ptr ),protocolParser(){
  15. m_pEngine = ptr;
  16. m_pMaster = (servaRemoteClient *)ptr;
  17. m_pLog = basicLogCollection::getInstance();
  18. m_dMojoDBID = 0;
  19. m_dTrackDBID = 0;
  20. m_nInbondSerial = 0;
  21. m_nOutbondSerial = 0;
  22. m_dSessionDBID = 0;
  23. m_bClientStructsArePacked = true; // Hope the Client as We are
  24. m_bTrainOnTrack = false;
  25. m_tLastRx = time( NULL );
  26. memset( m_cRemoteIP, 0, sizeof( m_cRemoteIP ) );
  27. memset( m_cRemoteIdentity, 0, sizeof( m_cRemoteIdentity ) );
  28. m_nPreTrainCloudStatus = m_nCloudStatus = ARGUS_CLOUD_STATUS_NA;
  29. m_pDBC = servaMySQLDBC::getInstance();
  30. m_pSM = servaSessionManager::getInstance();
  31. m_pEventHeap = basicEventHeap::getInstance();
  32. m_pPort = new serverPort( SocketFD );
  33. if ( m_pPort ) {
  34. if ( m_pPort->create() ) {
  35. setTransport(m_pPort);
  36. init();
  37. } else {
  38. delete( m_pPort );
  39. m_pPort = NULL;
  40. };
  41. };
  42. };
  43. bool servaRPCHadnler::setTrainPresense( bool st ){
  44. if( m_dSessionDBID ){
  45. //m_pSM->setTrainStatus( m_dSessionDBID, st );
  46. };
  47. if( !st && ( m_nPreTrainCloudStatus != ARGUS_CLOUD_STATUS_NA ) ){
  48. m_nCloudStatus = m_nPreTrainCloudStatus;
  49. } else {
  50. m_nPreTrainCloudStatus = m_nCloudStatus;
  51. m_nCloudStatus = ARGUS_CLOUD_STATUS_TRAIN;
  52. };
  53. return m_pDBC->setCloudStatus(ARGUS_OBJECT_MODULE, m_dMojoDBID, m_nCloudStatus );
  54. };
  55. servaRPCHadnler::~servaRPCHadnler(){
  56. waitThreadEnd( true );
  57. if( m_pPort ) delete( m_pPort );
  58. };
  59. bool servaRPCHadnler::attachEngine( networkServiceEngine *ptr ){
  60. m_pEngine = ptr;
  61. m_pMaster = (servaRemoteClient *) ptr;
  62. return true;
  63. };
  64. bool servaRPCHadnler::dispatchEvent( EVENT_CONTAINER *Evt ){
  65. //LOG("EVT");
  66. return true;
  67. };
  68. bool servaRPCHadnler::getStatus( void ){
  69. if ( m_pPort ) {
  70. if( getConnectionStatus() != CS_DISCONNECTED ) return true;
  71. };
  72. return false;
  73. };
  74. bool servaRPCHadnler::setRemotePeerIP( CSTR ptr ){
  75. if ( ptr == NULL ) return false;
  76. #ifdef DEBUG
  77. char tB[64];
  78. sprintf(tB,"Remote Peer: %s", ptr );
  79. LOG( tB );
  80. #endif
  81. strncpy( m_cRemoteIP, ptr, sizeof( m_cRemoteIP ) - 1 );
  82. m_cRemoteIP[sizeof( m_cRemoteIP ) - 1] = (char)0;
  83. return true;
  84. };
  85. /********************PROTOCOL**************************/
  86. int servaRPCHadnler::packetChecker( U8 *Buffer, size_t BufferSize ){
  87. char tB[64];
  88. if ( Buffer[0] == ARGUS_START_CLIENT_FLAG ) {
  89. if( BufferSize > ARGUS_DATA_MSG_MIN_SIZE ){
  90. uint32_t SIZE = Buffer[5];
  91. SIZE = ( SIZE << 8 ) | Buffer[6];
  92. SIZE = ( SIZE << 8 ) | Buffer[7];
  93. SIZE = ( SIZE << 8 ) | Buffer[8];
  94. if ( ( SIZE + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE ) <= BufferSize ) {
  95. uint16_t tCRC = checksum16( Buffer, SIZE + ARGUS_HEADER_SIZE );
  96. uint16_t cCRC = Buffer[SIZE + ARGUS_HEADER_SIZE];
  97. cCRC = ( cCRC << 8 ) | Buffer[SIZE + ARGUS_HEADER_SIZE+1];
  98. if ( tCRC == cCRC ) return 0;
  99. } else return 1; // to grou
  100. } else return 1; //to grow
  101. };
  102. return -1; // to skip
  103. };
  104. protocolPackage *servaRPCHadnler::packetExtractor(U8 *Buffer, size_t BufferSize){
  105. protocolPackage *tPack = (protocolPackage*) malloc( sizeof(protocolPackage));
  106. if ( tPack ) {
  107. tPack->m_nCode = Buffer[ARGUS_HEADER_SIZE];
  108. tPack->m_nSize = Buffer[5];
  109. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[6];
  110. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[7];
  111. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[8];
  112. tPack->m_nSize = tPack->m_nSize + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE;
  113. tPack->m_pData = (U8 *) malloc( tPack->m_nSize );
  114. if ( tPack->m_pData ) {
  115. memcpy( tPack->m_pData, Buffer, tPack->m_nSize );
  116. } else {
  117. free( tPack );
  118. tPack = NULL;
  119. };
  120. };
  121. return tPack;
  122. };
  123. bool servaRPCHadnler::packetHandler( protocolPackage *Package ){
  124. m_tLastRx = time( NULL ); // the peer is alive
  125. m_nInbondSerial = Package->m_pData[1];
  126. m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[2];
  127. m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[3];
  128. m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[4];
  129. #ifdef DEBUG_EXTRA
  130. char bB[129];
  131. sprintf(bB, "Code: %02X PackSize: %d", Package->m_nCode, Package->m_nSize );
  132. LOG( bB );
  133. #endif
  134. switch( Package->m_nCode ){
  135. case ARGUS_DATA_MSG_REGISTER:{
  136. if ( Package->m_nSize == ARGUS_RPC_REG_V1_SIZE ) {
  137. #ifdef DEBUG
  138. LOG("Old Type Registration!");
  139. #endif
  140. m_bClientStructsArePacked = false; //Old Clients - No packed Structs
  141. //Only Identity Transfered
  142. strncpy( m_cRemoteIdentity, (char*)(Package->m_pData + ARGUS_PAYLOAD_START), 32 );
  143. } else {
  144. ARGUS_DATA_MOJO_REGISTRATION *tM = (ARGUS_DATA_MOJO_REGISTRATION *)(Package->m_pData + ARGUS_PAYLOAD_START);
  145. //To Have ability connect via tonel IPv4 address transfered also
  146. strncpy( m_cRemoteIdentity, tM->identity, 32);
  147. strncpy( m_cRemoteIP, tM->peer, 22);
  148. };
  149. m_cRemoteIdentity[32] = (char)0;
  150. #ifdef DEBUG
  151. char tB[128];
  152. sprintf(tB,"Registration request from[%s][%s]", m_cRemoteIP, m_cRemoteIdentity );
  153. LOG(tB);
  154. #endif
  155. m_dMojoDBID = m_pMaster->requestMojoRegistration( m_cRemoteIP, m_cRemoteIdentity );
  156. if ( m_dMojoDBID ) {
  157. char WM = m_pMaster->getMojoWorkMode( m_dMojoDBID );
  158. if ( WM == 'E') {
  159. m_dTrackDBID = m_pDBC->getTrackIdByMojo( m_dMojoDBID );
  160. #ifdef DEBUG
  161. sprintf(tB,"Module:[%" PRIu64 "] Track:[%" PRIu64 "]", m_dMojoDBID, m_dTrackDBID );
  162. LOG(tB);
  163. #endif
  164. m_mCloudProtector.lock();
  165. m_nCloudStatus = ARGUS_CLOUD_STATUS_ONLINE;
  166. m_mCloudProtector.unlock();
  167. m_pDBC->setCloudStatus( ARGUS_OBJECT_MODULE, m_dMojoDBID, ARGUS_CLOUD_STATUS_REG );
  168. DBC_REG_MODULE_DATA tData = {0};
  169. tData.id = m_dMojoDBID;
  170. tData.track_id = m_dTrackDBID;
  171. peer_send_reg_ack( &tData );
  172. } else {
  173. #ifdef DEBUG
  174. LOG("Module is Disabled!");
  175. #endif
  176. peer_send_byte_status( ARGUS_DATA_MSG_DISABLED );
  177. setConnectionStatus( CS_DISCONNECTED );
  178. };
  179. } else {
  180. #ifdef DEBUG
  181. LOG("Module Not Registered with System");
  182. #endif
  183. peer_send_byte_status( ARGUS_DATA_MSG_ACCESS_DENIED );
  184. setConnectionStatus( CS_DISCONNECTED );
  185. };
  186. break;
  187. };
  188. case ARGUS_DATA_MSG_INFO:{
  189. size_t tSize = sizeof( ARGUS_DATA_MOJO_INFO ) + ARGUS_PAYLOAD_START + ARGUS_CHECKSUM_SIZE;
  190. if( Package->m_nSize != tSize ) {
  191. m_bClientStructsArePacked = false;
  192. ARGUS_DATA_MOJO_INFO_EASED *tINFO = (ARGUS_DATA_MOJO_INFO_EASED *) (Package->m_pData+ARGUS_PAYLOAD_START);
  193. #ifdef DEBUG
  194. LOG("Module Info Received!");
  195. char tB[128];
  196. sprintf( tB, "Upper Sensor Node: %d", tINFO->UPPER_ID );
  197. LOG( tB );
  198. sprintf( tB, "Bottom Sensor Node: %d", tINFO->BOTTOM_ID );
  199. LOG( tB );
  200. sprintf( tB, "Camera Node: %d", tINFO->CAM_ID);
  201. LOG( tB );
  202. sprintf( tB, "Camera Stream ID: %d", tINFO->CAM_STREAM_ID );
  203. LOG( tB );
  204. //for ( uint8_t i = 0; i < Package->m_nSize; i++ ) {
  205. // if ( Package->m_pData[i] > 31 && Package->m_pData[i] < 128 ) {
  206. // sprintf(tB, "Data[%d]: 0x%02X [%c]", i, Package->m_pData[i], (char)Package->m_pData[i] );
  207. // } else {
  208. // sprintf(tB, "Data[%d]: 0x%02X", i, Package->m_pData[i] );
  209. // }
  210. // LOG( tB );
  211. //};
  212. #endif
  213. //ToDo: Module SW must wait to register onto Server untill all Nodes are loadded
  214. if( tINFO->CAM_ID == 0 ) {
  215. tINFO->CAM_ID = 4;
  216. LOG_ERROR( "Wrong Camrea Info From Module[1]!");
  217. };
  218. m_pDBC->updateModuleData( m_dMojoDBID, tINFO->UPPER_ID, tINFO->BOTTOM_ID, tINFO->CAM_ID, tINFO->CAM_STREAM_ID, tINFO->TLS_VERSION, tINFO->LLS_VERSION );
  219. } else {
  220. ARGUS_DATA_MOJO_INFO *tINFO = (ARGUS_DATA_MOJO_INFO *) (Package->m_pData+ARGUS_PAYLOAD_START);
  221. #ifdef DEBUG
  222. LOG("Module Info Received!");
  223. char tB[128];
  224. sprintf( tB, "Upper Sensor Node: %d", tINFO->UPPER_ID );
  225. LOG( tB );
  226. sprintf( tB, "Bottom Sensor Node: %d", tINFO->BOTTOM_ID );
  227. LOG( tB );
  228. sprintf( tB, "Camera Node: %d", tINFO->CAM_ID);
  229. LOG( tB );
  230. sprintf( tB, "Camera Stream ID: %d", tINFO->CAM_STREAM_ID );
  231. LOG( tB );
  232. //for ( uint8_t i = 0; i < Package->m_nSize; i++ ) {
  233. // if ( Package->m_pData[i] > 31 && Package->m_pData[i] < 128 ) {
  234. // sprintf(tB, "Data[%d]: 0x%02X [%c]", i, Package->m_pData[i], (char)Package->m_pData[i] );
  235. // } else {
  236. // sprintf(tB, "Data[%d]: 0x%02X", i, Package->m_pData[i] );
  237. // }
  238. // LOG( tB );
  239. //};
  240. #endif
  241. //ToDo: Module SW must wait to register onto Server untill all Nodes are loadded
  242. if( tINFO->CAM_ID == 0 ) {
  243. tINFO->CAM_ID = 4;
  244. LOG_ERROR( "Wrong Camrea Info From Module[2]!");
  245. };
  246. m_pDBC->updateModuleData( m_dMojoDBID, tINFO->UPPER_ID, tINFO->BOTTOM_ID, tINFO->CAM_ID, tINFO->CAM_STREAM_ID, tINFO->TLS_VERSION, tINFO->LLS_VERSION );
  247. };
  248. peer_send_byte_status( ARGUS_DATA_MSG_INFO );
  249. break;
  250. };
  251. case ARGUS_DATA_MSG_KEEP_ALIVE:
  252. case ARGUS_DATA_MSG_STAT:{
  253. size_t tSize = sizeof( ARGUS_DATA_MOJO_STATUS ) + ARGUS_PAYLOAD_START + ARGUS_CHECKSUM_SIZE;
  254. if( Package->m_nSize != tSize ) {
  255. m_bClientStructsArePacked = false;
  256. ARGUS_DATA_MOJO_STATUS_EASED *tSTATUS = (ARGUS_DATA_MOJO_STATUS_EASED *)(Package->m_pData + ARGUS_PAYLOAD_START);
  257. m_mCloudProtector.lock();
  258. m_nCloudStatus = ( tSTATUS->STATUSES.system == SYSTEM_STATUS_NA )?ARGUS_CLOUD_STATUS_NA:0;
  259. m_nCloudStatus |= (( tSTATUS->STATUSES.system & SYSTEM_STATUS_TRAIN ) == 0 )?ARGUS_CLOUD_STATUS_TRAIN:0;
  260. m_nCloudStatus |= ( tSTATUS->CPU_LOAD > 95 )?ARGUS_CLOUD_STATUS_E_CPU:0;
  261. m_nCloudStatus |= ( tSTATUS->CPU_TEMP > 95 )?ARGUS_CLOUD_STATUS_E_TEMPERATURE:0;
  262. m_nCloudStatus |= ( tSTATUS->MEMO_UTI > 95 )?ARGUS_CLOUD_STATUS_E_MEM:0;
  263. m_nCloudStatus |= ( tSTATUS->DISK_UTI > 95 )?ARGUS_CLOUD_STATUS_E_DISK:0;
  264. m_nCloudStatus |= ( tSTATUS->STATUSES.camera == CAMERA_STATUS_NA )?ARGUS_CLOUD_STATUS_E_CAM_GENERIC:0;
  265. m_nCloudStatus |= (( tSTATUS->STATUSES.camera & CAMERA_STATUS_OK ) == 0 )?ARGUS_CLOUD_STATUS_E_CAM_NC:0;
  266. m_nCloudStatus |= (( tSTATUS->STATUSES.camera & CAMERA_STATUS_STREAM_OK ) == 0 )?ARGUS_CLOUD_STATUS_E_CAM_NIM:0;
  267. m_nCloudStatus |= (( tSTATUS->STATUSES.camera & CAMERA_STATUS_PTZ_OK ) == 0 )?ARGUS_CLOUD_STATUS_E_CAM_NPTZ:0;
  268. m_nCloudStatus |= ( tSTATUS->STATUSES.b_sensor == SENOR_STATUS_NA )?ARGUS_CLOUD_STATUS_E_SEN_GENERIC:0;
  269. m_nCloudStatus |= ( tSTATUS->STATUSES.b_sensor == SENOR_STATUS_CONNECTING )?ARGUS_CLOUD_STATUS_E_BS_NC:0;
  270. m_nCloudStatus |= ( tSTATUS->STATUSES.b_sensor == SENOR_STATUS_DETECTION_ERR )?ARGUS_CLOUD_STATUS_E_BS_LRF:0;
  271. m_nCloudStatus |= ( tSTATUS->STATUSES.u_sensor == SENOR_STATUS_NA )?ARGUS_CLOUD_STATUS_E_SEN_GENERIC:0;
  272. m_nCloudStatus |= ( tSTATUS->STATUSES.u_sensor == SENOR_STATUS_CONNECTING )?ARGUS_CLOUD_STATUS_E_US_NC:0;
  273. m_nCloudStatus |= ( tSTATUS->STATUSES.u_sensor == SENOR_STATUS_DETECTION_ERR )?ARGUS_CLOUD_STATUS_E_US_LRF:0;
  274. m_mCloudProtector.unlock();
  275. } else {
  276. ARGUS_DATA_MOJO_STATUS *tSTATUS = (ARGUS_DATA_MOJO_STATUS *)(Package->m_pData + ARGUS_PAYLOAD_START);
  277. m_mCloudProtector.lock();
  278. m_nCloudStatus = ( tSTATUS->STATUSES.system == SYSTEM_STATUS_NA )?ARGUS_CLOUD_STATUS_NA:0;
  279. m_nCloudStatus |= (( tSTATUS->STATUSES.system & SYSTEM_STATUS_TRAIN ) == 0 )?ARGUS_CLOUD_STATUS_TRAIN:0;
  280. m_nCloudStatus |= ( tSTATUS->CPU_LOAD > 95 )?ARGUS_CLOUD_STATUS_E_CPU:0;
  281. m_nCloudStatus |= ( tSTATUS->CPU_TEMP > 95 )?ARGUS_CLOUD_STATUS_E_TEMPERATURE:0;
  282. m_nCloudStatus |= ( tSTATUS->MEMO_UTI > 95 )?ARGUS_CLOUD_STATUS_E_MEM:0;
  283. m_nCloudStatus |= ( tSTATUS->DISK_UTI > 95 )?ARGUS_CLOUD_STATUS_E_DISK:0;
  284. m_nCloudStatus |= ( tSTATUS->STATUSES.camera == CAMERA_STATUS_NA )?ARGUS_CLOUD_STATUS_E_CAM_GENERIC:0;
  285. m_nCloudStatus |= (( tSTATUS->STATUSES.camera & CAMERA_STATUS_OK ) == 0 )?ARGUS_CLOUD_STATUS_E_CAM_NC:0;
  286. m_nCloudStatus |= (( tSTATUS->STATUSES.camera & CAMERA_STATUS_STREAM_OK ) == 0 )?ARGUS_CLOUD_STATUS_E_CAM_NIM:0;
  287. m_nCloudStatus |= (( tSTATUS->STATUSES.camera & CAMERA_STATUS_PTZ_OK ) == 0 )?ARGUS_CLOUD_STATUS_E_CAM_NPTZ:0;
  288. m_nCloudStatus |= ( tSTATUS->STATUSES.b_sensor == SENOR_STATUS_NA )?ARGUS_CLOUD_STATUS_E_SEN_GENERIC:0;
  289. m_nCloudStatus |= ( tSTATUS->STATUSES.b_sensor == SENOR_STATUS_CONNECTING )?ARGUS_CLOUD_STATUS_E_BS_NC:0;
  290. m_nCloudStatus |= ( tSTATUS->STATUSES.b_sensor == SENOR_STATUS_DETECTION_ERR )?ARGUS_CLOUD_STATUS_E_BS_LRF:0;
  291. m_nCloudStatus |= ( tSTATUS->STATUSES.u_sensor == SENOR_STATUS_NA )?ARGUS_CLOUD_STATUS_E_SEN_GENERIC:0;
  292. m_nCloudStatus |= ( tSTATUS->STATUSES.u_sensor == SENOR_STATUS_CONNECTING )?ARGUS_CLOUD_STATUS_E_US_NC:0;
  293. m_nCloudStatus |= ( tSTATUS->STATUSES.u_sensor == SENOR_STATUS_DETECTION_ERR )?ARGUS_CLOUD_STATUS_E_US_LRF:0;
  294. m_mCloudProtector.unlock();
  295. };
  296. peer_send_byte_status( ARGUS_DATA_MSG_STAT );
  297. break;
  298. };
  299. case ARGUS_DATA_MSG_EVT:{
  300. peer_send_byte_status( ARGUS_DATA_MSG_EVT ); // make instant ACK to module
  301. size_t tSize = sizeof( ARGUS_DATA_EVENT ) + ARGUS_PAYLOAD_START + ARGUS_CHECKSUM_SIZE;
  302. ARGUS_DATA_EVENT tData = {0};
  303. if( Package->m_nSize != tSize ){
  304. m_bClientStructsArePacked = false;
  305. ARGUS_DATA_EVENT_EASED *tPtr = (ARGUS_DATA_EVENT_EASED *) (Package->m_pData+ARGUS_PAYLOAD_START);
  306. // was tData.X = tData.X: every field except AREA stayed 0
  307. tData.AREA = tPtr->AREA;
  308. tData.AZIMUTH = tPtr->AZIMUTH;
  309. tData.ELEVATION = tPtr->ELEVATION;
  310. tData.MODULE_ID = tPtr->MODULE_ID;
  311. tData.MS = tPtr->MS;
  312. tData.RANGE = tPtr->RANGE;
  313. tData.REF_ID = tPtr->REF_ID;
  314. tData.SRC = tPtr->SRC;
  315. tData.TS = tPtr->TS;
  316. tData.TYPE = tPtr->TYPE;
  317. memcpy( tData.UUID, tPtr->UUID, sizeof( tData.UUID ) );
  318. } else {
  319. memcpy( &tData, Package->m_pData+ARGUS_PAYLOAD_START, sizeof(ARGUS_DATA_EVENT) );
  320. };
  321. switch ( tData.TYPE ) {
  322. case EVT_ROI_ACTIVE:
  323. case EVT_TARGET_3D_WHITE:
  324. case EVT_TARGET_3D_BLACK:
  325. case EVT_TARGET_2D_WHITE:
  326. case EVT_TARGET_2D_BLACK:{
  327. #ifdef DEBUG_EXTRA
  328. char tB[128];
  329. sprintf(tB, "EVT[%d] SRC[%d] %d.%d", tData.TYPE, tData.SRC, tData.TS, tData.MS );
  330. LOG( tB );
  331. sprintf(tB, "AZ[%d] RANGE[%d] ELV[%d] AREA[%d]", tData.AZIMUTH, tData.RANGE, tData.ELEVATION, tData.AREA );
  332. LOG( tB );
  333. strncpy( tB, tData.UUID, 64 );
  334. tB[64] = (char)0;
  335. LOG( tB );
  336. #endif
  337. tData.MODULE_ID = m_dMojoDBID;
  338. if ( m_dSessionDBID ) {
  339. if ( m_pSM->validateSession( m_dSessionDBID ) ) {
  340. m_pSM->addEvent( m_dSessionDBID, &tData );
  341. } else {
  342. m_dSessionDBID = m_pSM->addEvent( m_dTrackDBID, m_dMojoDBID, &tData );
  343. };
  344. } else {
  345. m_dSessionDBID = m_pSM->addEvent( m_dTrackDBID, m_dMojoDBID, &tData );
  346. };
  347. break;
  348. };
  349. case EVT_FLT_LATF_TRP_DET:
  350. case EVT_FLT_LATF_TRP_UPDT:
  351. case EVT_FLT_LATF_OP_TD:{
  352. // Train Presence
  353. setTrainPresense( TRAIN_ON_TRACK );
  354. break;
  355. };
  356. case EVT_FLT_LATF_TRP_EXP:{
  357. //Train is Gone
  358. setTrainPresense( TRAIN_IS_GONE );
  359. break;
  360. };
  361. };
  362. break;
  363. };
  364. case ARGUS_DATA_MSG_REQ_TIME_SYNC:{
  365. peer_send_byte_status( ARGUS_DATA_MSG_REQ_TIME_SYNC );
  366. break;
  367. };
  368. case ARGUS_DATA_MSG_TIME_SYNC:{
  369. #ifdef DEBUG_EXTRA
  370. LOG( "Time Synchronization Request" );
  371. #endif
  372. ARGUS_DATA_TIME_SYNC_REQ *tTSR = ( ARGUS_DATA_TIME_SYNC_REQ *) (Package->m_pData+ARGUS_PAYLOAD_START);
  373. ARGUS_DATA_TIME_SYNC_ACK tTSA;
  374. tTSA.STS = tTSR->STS;
  375. tTSA.ATS = getMilliseconds();
  376. peer_send_time_sync_ack( &tTSA );
  377. break;
  378. };
  379. case ARGUS_DATA_MSG_VIDEO_INFO:{
  380. #ifdef DEBUG
  381. LOG( "Received Video Record Info" );
  382. #endif
  383. m_pVideoHelper.clear();
  384. CSTR BASE = m_pSM->getSessionBase( m_dSessionDBID );
  385. if( BASE ){
  386. ARGUS_DATA_VIDEO_INFO tData = {0};
  387. size_t tSize = sizeof( ARGUS_DATA_VIDEO_INFO ) + ARGUS_PAYLOAD_START + ARGUS_CHECKSUM_SIZE;
  388. if( Package->m_nSize != tSize ){
  389. m_bClientStructsArePacked = false;
  390. ARGUS_DATA_VIDEO_INFO_EASED *tPtr = (ARGUS_DATA_VIDEO_INFO_EASED *)(Package->m_pData + ARGUS_PAYLOAD_START);
  391. tData.DATA_SIZE = tPtr->DATA_SIZE;
  392. tData.DMS = tPtr->DMS;
  393. tData.DTS = tPtr->DTS;
  394. strncpy( tData.FMT, tPtr->FMT, 6 );
  395. tData.FPS = tPtr->FPS;
  396. tData.HEIGHT = tPtr->HEIGHT;
  397. tData.REF_ID = tPtr->REF_ID;
  398. tData.WIDTH = tPtr->WIDTH;
  399. } else {
  400. memcpy( &tData, Package->m_pData + ARGUS_PAYLOAD_START, sizeof( ARGUS_DATA_VIDEO_INFO ));
  401. };
  402. if (m_pVideoHelper.init( BASE, m_dMojoDBID, m_dSessionDBID, &tData )) {
  403. LOG("Helper Init Ok");
  404. peer_send_vinfo_ack( m_pVideoHelper.getReferenceID() );
  405. } else {
  406. LOG("Helper Init Failure");
  407. peer_send_vinfo_ack( 0 );
  408. };
  409. free( BASE );
  410. } else {
  411. LOG("Helper Init Failure (NO SESSION)");
  412. peer_send_vinfo_ack( 0 );
  413. };
  414. break;
  415. };
  416. case ARGUS_DATA_MSG_VIDEO_DATA:{
  417. // payload after the message code, without the checksum
  418. size_t tAvail = ( Package->m_nSize > ARGUS_HEADER_SIZE + 1 + ARGUS_CHECKSUM_SIZE ) ? Package->m_nSize - ARGUS_HEADER_SIZE - 1 - ARGUS_CHECKSUM_SIZE : 0;
  419. uint32_t tt = m_pVideoHelper.saveNextChunk( Package->m_pData+ARGUS_HEADER_SIZE+1, tAvail );
  420. #ifdef DEBUG_EXTRA
  421. char tK[128];
  422. sprintf( tK, "Saved %d Bytes of Video!", tt );
  423. LOG( tK );
  424. #endif
  425. peer_send_vdata_ack( tt );
  426. break;
  427. };
  428. case ARGUS_DATA_MSG_RESTART_SW:{
  429. LOG("Module Restarts control Software");
  430. break;
  431. };
  432. case ARGUS_DATA_MSG_RESTART_PLATFORM:{
  433. LOG("Module Restart HOST OS");
  434. break;
  435. };
  436. };
  437. return true;
  438. };
  439. void servaRPCHadnler::m_fThreadProc( void ){
  440. time_t tTimeTSeconds = 0;
  441. time_t cTimeTSeconds = 0;
  442. uint32_t tSecondsCounter = 0;
  443. m_bThreadStatus = TH_WORKING;
  444. #ifdef DEBUG
  445. LOG("Connector Thread Started");
  446. #endif
  447. pthread_setname_np( pthread_self(), "RPC Handler" );
  448. while ( m_bThreadStatus < TH_ENDED ) {
  449. // seconds Counter (for internal purposes)
  450. cTimeTSeconds = time( NULL );
  451. //Process each second
  452. if ( cTimeTSeconds != tTimeTSeconds ) {
  453. tSecondsCounter++;
  454. tTimeTSeconds = cTimeTSeconds;
  455. // Dead connection: mark it disconnected, the network server then removes this handler
  456. // (and ~serverPort closes the socket)
  457. if ( ( cTimeTSeconds - m_tLastRx ) > RPC_IDLE_TIMEOUT_S && getConnectionStatus() != CS_DISCONNECTED ) {
  458. char tB[160];
  459. snprintf( tB, sizeof( tB ), "No data from [%s][%s] for %d s - dropping the connection", m_cRemoteIP, m_cRemoteIdentity, RPC_IDLE_TIMEOUT_S );
  460. LOG_ERROR( tB );
  461. setConnectionStatus( CS_DISCONNECTED );
  462. };
  463. // update clowd status
  464. m_mCloudProtector.lock();
  465. m_pDBC->setCloudStatus( ARGUS_OBJECT_MODULE, m_dMojoDBID, m_nCloudStatus );
  466. m_mCloudProtector.unlock();
  467. };
  468. DBC_RPC_WEB_CMD *tCMD = m_pDBC->fetchWebCmd( m_dMojoDBID );
  469. if ( tCMD ) {
  470. //LOG( "Have Web CMD!");
  471. //To Do -> Check with EASED Structs in Proto
  472. ARGUS_DATA_RPC RPC_PACK;
  473. RPC_PACK.CMD = tCMD->cmd;
  474. RPC_PACK.NODE_ID = tCMD->aux_id;
  475. strncpy( RPC_PACK.DATA, tCMD->param, 64 );
  476. //if( peer_send_rpc( &RPC_PACK ) ) m_pDBC->updateWebCmdStatus( tCMD->id, 200 ); // Set Finished
  477. if( peer_send_rpc( &RPC_PACK ) ) m_pDBC->removeWebCmd( tCMD->id ); // Instant Clear
  478. free( tCMD );
  479. };
  480. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  481. };
  482. #ifdef DEBUG_EXTRA
  483. LOG("Connector Thread Ended!");
  484. #endif
  485. };
  486. bool servaRPCHadnler::peer_send_vdata_ack( uint8_t status ){
  487. uint8_t Buffer[64];
  488. Buffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_VIDEO_DATA;
  489. Buffer[ARGUS_HEADER_SIZE+1] = status;
  490. m_nOutbondSerial++;
  491. return m_pPort->sendData( Buffer, argus_rpc_finishupPackage( Buffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+2 ) );
  492. };
  493. bool servaRPCHadnler::peer_send_vinfo_ack( uint64_t pid ){
  494. uint8_t Buffer[64];
  495. Buffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_VIDEO_INFO;
  496. Buffer[ARGUS_HEADER_SIZE+1] = ( pid >> 56 ) & 0xff;
  497. Buffer[ARGUS_HEADER_SIZE+2] = ( pid >> 48 ) & 0xff;
  498. Buffer[ARGUS_HEADER_SIZE+3] = ( pid >> 40 ) & 0xff;
  499. Buffer[ARGUS_HEADER_SIZE+4] = ( pid >> 32 ) & 0xff;
  500. Buffer[ARGUS_HEADER_SIZE+5] = ( pid >> 24 ) & 0xff;
  501. Buffer[ARGUS_HEADER_SIZE+6] = ( pid >> 16 ) & 0xff;
  502. Buffer[ARGUS_HEADER_SIZE+7] = ( pid >> 8 ) & 0xff;
  503. Buffer[ARGUS_HEADER_SIZE+8] = pid & 0xff;
  504. m_nOutbondSerial++;
  505. return m_pPort->sendData( Buffer, argus_rpc_finishupPackage( Buffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+9 ) );
  506. };
  507. bool servaRPCHadnler::peer_send_byte_status( uint8_t status ){
  508. uint8_t Buffer[64];
  509. Buffer[ARGUS_HEADER_SIZE] = status;
  510. m_nOutbondSerial++;
  511. return m_pPort->sendData( Buffer, argus_rpc_finishupPackage( Buffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1 ) );
  512. };
  513. bool servaRPCHadnler::peer_send_reg_ack( DBC_REG_MODULE_DATA *ptr ){
  514. if ( ptr == NULL ) {
  515. return peer_send_byte_status( ARGUS_DATA_MSG_ACCESS_DENIED );
  516. } else {
  517. uint8_t tBuffer[128];
  518. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_REGISTER;
  519. m_nOutbondSerial++;
  520. uint16_t i = 0;
  521. for(; i < sizeof(DBC_REG_MODULE_DATA); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  522. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  523. }
  524. };
  525. bool servaRPCHadnler::peer_send_req_draw( uint8_t sensor, uint8_t draw_type, bool getexisted ){
  526. if ( sensor == ARGUS_RPC_MOJO_ITSELF ) return false;
  527. uint8_t Buffer[64];
  528. Buffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_GET_DRAW;
  529. Buffer[ARGUS_HEADER_SIZE+1] = sensor;
  530. if( !getexisted ) {
  531. Buffer[ARGUS_HEADER_SIZE+2] = draw_type | 0x80;
  532. } else {
  533. Buffer[ARGUS_HEADER_SIZE+2] = draw_type;
  534. };
  535. return m_pPort->sendData( Buffer, argus_rpc_finishupPackage( Buffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+3 ) );
  536. };
  537. bool servaRPCHadnler::peer_send_ack_evt( uint64_t evt_dbid, CSTR uuid ){
  538. uint8_t Buffer[128];
  539. Buffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_EVT;
  540. Buffer[ARGUS_HEADER_SIZE+1] = ( evt_dbid >> 56 ) & 0xFF;
  541. Buffer[ARGUS_HEADER_SIZE+2] = ( evt_dbid >> 48 ) & 0xFF;
  542. Buffer[ARGUS_HEADER_SIZE+3] = ( evt_dbid >> 40 ) & 0xFF;
  543. Buffer[ARGUS_HEADER_SIZE+4] = ( evt_dbid >> 32 ) & 0xFF;
  544. Buffer[ARGUS_HEADER_SIZE+5] = ( evt_dbid >> 24 ) & 0xFF;
  545. Buffer[ARGUS_HEADER_SIZE+6] = ( evt_dbid >> 16 ) & 0xFF;
  546. Buffer[ARGUS_HEADER_SIZE+7] = ( evt_dbid >> 8 ) & 0xFF;
  547. Buffer[ARGUS_HEADER_SIZE+8] = evt_dbid & 0xFF;
  548. Buffer[ARGUS_HEADER_SIZE+9] = strlen( uuid );
  549. uint8_t i = 0;
  550. for( ; i< Buffer[ARGUS_HEADER_SIZE+9]; i++ ) Buffer[ARGUS_HEADER_SIZE+10+i] = (uint8_t)uuid[i];
  551. return m_pPort->sendData(Buffer, argus_rpc_finishupPackage( Buffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+11+i ));
  552. };
  553. bool servaRPCHadnler::peer_send_rpc( ARGUS_DATA_RPC *ptr ){
  554. uint8_t tBuffer[1024];
  555. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_RPC;
  556. uint16_t i = 0;
  557. for(; i < sizeof(ARGUS_DATA_RPC); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  558. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  559. };
  560. bool servaRPCHadnler::peer_send_time_sync_ack( ARGUS_DATA_TIME_SYNC_ACK *ptr ){
  561. uint8_t tBuffer[1024];
  562. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_TIME_SYNC;
  563. uint16_t i = 0;
  564. for(; i < sizeof(ARGUS_DATA_TIME_SYNC_ACK); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  565. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  566. };
  567. servaVideoTransferHelper::servaVideoTransferHelper(){
  568. m_pLog = basicLogCollection::getInstance();
  569. m_pEventHeap = basicEventHeap::getInstance();
  570. m_pFD = NULL;
  571. clear();
  572. };
  573. servaVideoTransferHelper::~servaVideoTransferHelper(){
  574. clear();
  575. };
  576. bool servaVideoTransferHelper::init( CSTR base, uint64_t MID, uint64_t SID, ARGUS_DATA_VIDEO_INFO *ptr ){
  577. m_nRemoteRefID = 0;
  578. if ( ptr ) {
  579. memcpy( &m_sIn, ptr, sizeof(ARGUS_DATA_VIDEO_INFO) );
  580. if ( ptr->REF_ID ) {
  581. servaMySQLDBC * m_pDBC = servaMySQLDBC::getInstance();
  582. CSTR tUUID = getUniqID_UUID(); //m_pDBC->getMediaUUID( ptr->REF_ID );
  583. if ( tUUID ) {
  584. #ifdef DEBUG
  585. char tB[256];
  586. sprintf(tB, "Update VideoData: %s, %d.%d, [%dx%d@%d] <%s>", tUUID, m_sIn.DTS, m_sIn.DMS, m_sIn.WIDTH, m_sIn.HEIGHT, m_sIn.FPS, m_sIn.FMT);
  587. LOG( tB );
  588. #endif
  589. sprintf( m_cCurrentFile, "%s/%s.%s", base, tUUID, m_sIn.FMT );
  590. m_pFD = fopen( m_cCurrentFile, "wb" );
  591. free( tUUID );
  592. m_nRemoteRefID = ptr->REF_ID;
  593. return true;
  594. };
  595. } else {
  596. //ToDo Set File Access to web user
  597. CSTR tUUID = getUniqID_UUID();
  598. if ( tUUID ) {
  599. #ifdef DEBUG
  600. char tB[256];
  601. sprintf(tB, "VideoData: %s, %d.%d, [%dx%d@%d] <%s>", tUUID, m_sIn.DTS, m_sIn.DMS, m_sIn.WIDTH, m_sIn.HEIGHT, m_sIn.FPS, m_sIn.FMT);
  602. LOG( tB );
  603. #endif
  604. servaMySQLDBC * m_pDBC = servaMySQLDBC::getInstance();
  605. //servaSessionManager * m_pSM = servaSessionManager::getInstance();
  606. if ( m_nRemoteRefID = m_pDBC->addMediaQueueRecord( tUUID, MID, SID, m_sIn.DTS, m_sIn.DMS, m_sIn.WIDTH, m_sIn.HEIGHT, m_sIn.FPS, m_sIn.FMT ) ) {
  607. sprintf( m_cCurrentFile, "%s/%s.%s", base, tUUID, m_sIn.FMT );
  608. m_pFD = fopen( m_cCurrentFile, "wb" );
  609. free( tUUID );
  610. return true;
  611. };
  612. free( tUUID );
  613. };
  614. }
  615. };
  616. memset( m_cCurrentFile, 0, MAX_PATH );
  617. memset( &m_sIn, 0, sizeof(ARGUS_DATA_VIDEO_INFO) );
  618. return false;
  619. };
  620. void servaVideoTransferHelper::clear(){
  621. if( m_pFD ) fclose( m_pFD );
  622. m_pFD = NULL;
  623. m_nRemoteRefID = 0;
  624. m_nSAVED = 0;
  625. m_bOPerc = 0;
  626. memset( m_cCurrentFile, 0, MAX_PATH );
  627. memset( &m_sIn, 0, sizeof(ARGUS_DATA_VIDEO_INFO) );
  628. };
  629. uint32_t servaVideoTransferHelper::saveNextChunk( uint8_t *ptr, size_t avail ){
  630. if ( ptr && avail >= 12 ){
  631. uint64_t tDBID = ptr[0];
  632. tDBID = ( tDBID << 8 ) | ptr[1];
  633. tDBID = ( tDBID << 8 ) | ptr[2];
  634. tDBID = ( tDBID << 8 ) | ptr[3];
  635. tDBID = ( tDBID << 8 ) | ptr[4];
  636. tDBID = ( tDBID << 8 ) | ptr[5];
  637. tDBID = ( tDBID << 8 ) | ptr[6];
  638. tDBID = ( tDBID << 8 ) | ptr[7];
  639. uint32_t chusz = ptr[8];
  640. chusz = ( chusz << 8 ) | ptr[9];
  641. chusz = ( chusz << 8 ) | ptr[10];
  642. chusz = ( chusz << 8 ) | ptr[11];
  643. if ( chusz > avail - 12 ) {
  644. // the length field claims more data than the packet holds
  645. LOG_ERROR("Video chunk size exceeds the packet");
  646. return 0;
  647. };
  648. if ( m_pFD ) {
  649. if ( chusz ) {
  650. int rv = fwrite( ptr+12, 1, chusz, m_pFD );
  651. if ( rv != chusz ) {
  652. fclose( m_pFD );
  653. m_pFD = NULL;
  654. unlink( m_cCurrentFile );
  655. return 0;
  656. };
  657. m_nSAVED += chusz;
  658. if ( chusz < ARGUS_RPC_DEFAULT_MEDIA_CHUNK ) {
  659. LOG("Video File Reception Done");
  660. fclose( m_pFD );
  661. m_pFD = NULL;
  662. servaKernel::getInstance()->updateEncodeStatus(tDBID, MEDIA_STORED, 100);
  663. } else {
  664. if ( m_sIn.DATA_SIZE ) {
  665. uint8_t perc = (uint8_t)((m_nSAVED * 100) / m_sIn.DATA_SIZE);
  666. if ( m_bOPerc != perc ) {
  667. m_bOPerc = perc;
  668. servaKernel::getInstance()->updateEncodeStatus(tDBID, MEDIA_TRANSFERING, perc );
  669. };
  670. } else servaKernel::getInstance()->updateEncodeStatus( tDBID, MEDIA_TRANSFERING, 50 );
  671. };
  672. return chusz;
  673. };
  674. };
  675. };
  676. return 0;
  677. };
  678. #undef LOG
  679. #undef LOG_ERROR