livoxProto1: Convert heartbeat sender into daemon coro

This commit is contained in:
2026-06-10 07:02:07 -04:00
parent facb665217
commit f9c64cf363
2 changed files with 194 additions and 130 deletions
+22 -7
View File
@@ -1,6 +1,8 @@
#ifndef LIVOX_PROTO1_DEVICE_H
#define LIVOX_PROTO1_DEVICE_H
#include <boostAsioLinkageFix.h>
#include <string>
#include <cstdint>
#include <cstddef>
@@ -17,8 +19,10 @@
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/posix/stream_descriptor.hpp>
#include "protocol.h"
#include <spinscale/co/dynamicPostingInvoker.h>
#include <spinscale/co/invokers.h>
#include <spinscale/spinLock.h>
#include <spinscale/co/nonViralTaskNursery.h>
#include <spinscale/syncCancelerForAsyncWork.h>
// Custom hash function for std::pair<uint8_t, uint8_t>
namespace std {
@@ -86,8 +90,20 @@ private:
// Heartbeat mechanism
void startHeartbeat();
void stopHeartbeat();
void sendHeartbeat();
void onHeartbeatTimer(const boost::system::error_code& error);
void sendHeartbeatOnce();
/** EXPLANATION:
* deviceCDaemon is a dynamic posting non-viral coroutine: startHeartbeat()
* passes ExplicitPostTarget{componentThread->getIoContext()} so the daemon
* body always runs on componentThread. Extensible for future per-device
* background work beyond heartbeats.
*/
sscl::co::DynamicNonViralPostingInvoker deviceCDaemon(
sscl::co::ExplicitPostTarget postTarget,
std::exception_ptr &exceptionPtr,
std::function<void()> callback,
sscl::SyncCancelerForAsyncWork &canceler);
std::string generateClientDeviceIpFromSerialNumber(
const std::string& broadcastCode);
@@ -172,10 +188,9 @@ public:
uint8_t smoSubnetNbits;
uint16_t dataPort, cmdPort, imuPort;
// Heartbeat state
std::unique_ptr<boost::asio::deadline_timer> heartbeatTimer;
std::atomic<bool> heartbeatActive;
sscl::SpinLock heartbeatActiveLock;
// Heartbeat state (timer lifetime tied to Device ctor/dtor)
boost::asio::deadline_timer heartbeatTimer;
sscl::co::NonViralTaskNursery daemonNursery;
// Point cloud data state
std::atomic<bool> pcloudDataActive;