livoxProto1:Device: Spinlock guard heartbeat stop() for races
This commit is contained in:
@@ -1042,6 +1042,8 @@ void Device::startHeartbeat()
|
||||
}
|
||||
|
||||
// Register heartbeat ACK handler (cmd_set=0x00, cmd_id=0x03)
|
||||
smo::SpinLock::Guard lock(heartbeatActiveLock);
|
||||
|
||||
registerUdpCommandHandler(
|
||||
0x00, 0x03, discardHeartbeatAck, discoveredDevice.ipAddr);
|
||||
|
||||
@@ -1057,15 +1059,17 @@ void Device::startHeartbeat()
|
||||
|
||||
void Device::stopHeartbeat()
|
||||
{
|
||||
if (heartbeatActive.load())
|
||||
{
|
||||
heartbeatActive.store(false);
|
||||
if (heartbeatTimer) {
|
||||
heartbeatTimer->cancel();
|
||||
}
|
||||
smo::SpinLock::Guard lock(heartbeatActiveLock);
|
||||
|
||||
heartbeatActive.store(false);
|
||||
unregisterUdpCommandHandler(0x00, 0x03, discoveredDevice.ipAddr);
|
||||
}
|
||||
|
||||
if (heartbeatTimer) {
|
||||
heartbeatTimer->cancel();
|
||||
heartbeatTimer.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void Device::sendHeartbeat()
|
||||
@@ -1140,7 +1144,6 @@ void Device::sendHeartbeat()
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
heartbeatActive.store(false);
|
||||
std::cerr << __func__ << ": Heartbeat send failed for device "
|
||||
<< discoveredDevice.deviceIdentifier
|
||||
<< ": " << e.what() << std::endl;
|
||||
@@ -1156,15 +1159,21 @@ void Device::onHeartbeatTimer(const boost::system::error_code& error)
|
||||
|
||||
if (error)
|
||||
{
|
||||
heartbeatActive.store(false);
|
||||
std::cerr << "[" << __func__ << "] Heartbeat timer error for device "
|
||||
<< discoveredDevice.deviceIdentifier
|
||||
<< ": " << error.message() << std::endl;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Send next heartbeat
|
||||
sendHeartbeat();
|
||||
{
|
||||
smo::SpinLock::Guard lock(heartbeatActiveLock);
|
||||
if (!heartbeatActive.load())
|
||||
{ return; }
|
||||
|
||||
sendHeartbeat();
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t Device::getSubnetMaskFor(uint8_t nbits)
|
||||
|
||||
Reference in New Issue
Block a user