diff --git a/commonLibs/livoxProto1/device.cpp b/commonLibs/livoxProto1/device.cpp index 2375522..2450c46 100644 --- a/commonLibs/livoxProto1/device.cpp +++ b/commonLibs/livoxProto1/device.cpp @@ -625,16 +625,12 @@ private: } } - // This is called from the UDP command handler - void executeHandshakeReq1_2( + void executeHandshakeReq1_2( std::shared_ptr context, const uint8_t* data, ssize_t bytesReceived, const struct sockaddr_in& senderAddr - ) + ) { - // Unregister the handler to decrement refcount - device.unregisterUdpCommandHandler(0x00, 0x01, context->deviceIP); - // Store the received data context->bytesReceived = bytesReceived; context->senderAddr = senderAddr; @@ -661,13 +657,14 @@ private: // Ensure we only execute once using atomic exchange if (handlerExecuted.exchange(true) == true) { return; } + // Cancel timer if still running + timeoutTimer.cancel(); + device.unregisterUdpCommandHandler(0x00, 0x01, deviceIP); + // Examine the flags and decide what happened SocketState finalSocketState = socketState.load(); bool finalTimerFired = timerFired.load(); - // Cancel timer if still running - timeoutTimer.cancel(); - // Check for timeout only if there was no socket activity if (finalTimerFired && finalSocketState == SocketState::SOCKET_STILL_WAITING) @@ -1371,12 +1368,8 @@ protected: std::shared_ptr> context, const uint8_t* data, ssize_t bytesReceived, const struct sockaddr_in& senderAddr - ) + ) { - // Unregister the handler to decrement refcount - device.unregisterUdpCommandHandler( - 0x00, 0x04, device.discoveredDevice.ipAddr); - // Store the received data context->bytesReceived = bytesReceived; context->senderAddr = senderAddr; @@ -1405,11 +1398,13 @@ protected: // Only execute once if (context->handlerExecuted.exchange(true)) { return; } + context->timeoutTimer.cancel(); + device.unregisterUdpCommandHandler( + 0x00, 0x04, device.discoveredDevice.ipAddr); + SocketState finalSocketState = context->socketState.load(); bool finalTimerFired = context->timerFired.load(); - context->timeoutTimer.cancel(); - if (finalTimerFired && finalSocketState == SocketState::SOCKET_STILL_WAITING) {