diff --git a/commonLibs/livoxProto1/device.cpp b/commonLibs/livoxProto1/device.cpp index cd7c9ad..2375522 100644 --- a/commonLibs/livoxProto1/device.cpp +++ b/commonLibs/livoxProto1/device.cpp @@ -134,16 +134,30 @@ class Device::ConnectReq { private: Device& device; + boost::asio::deadline_timer delayTimer; public: ConnectReq(Device& dev, smo::Callback cb) : smo::NonPostedAsynchronousContinuation( - std::move(cb)), device(dev) + std::move(cb)), device(dev), + delayTimer(dev.componentThread->getIoService()) {} /** FIXME: * WE need to assign the ipAddr to the Device being connected up. */ + + // Helper method to delay and then call enablePcloudDataReq + void delayedEnablePcloudData(std::shared_ptr context) + { + delayTimer.expires_from_now(boost::posix_time::milliseconds(5)); + delayTimer.async_wait( + std::bind( + &ConnectReq::connectReq3, + context.get(), context, + std::placeholders::_1)); + } + // Callback methods for the connection sequence void connectReq1( std::shared_ptr context, @@ -172,9 +186,8 @@ public: context->device.discoveredDevice.ipAddr = ipAddr; context->device.startHeartbeat(); - context->device.enablePcloudDataReq( - {context, std::bind(&ConnectReq::connectReq3, context.get(), context, - std::placeholders::_1)}); + // Use async delay before enabling point cloud data + delayedEnablePcloudData(context); } void connectReq2( @@ -193,12 +206,29 @@ public: context->device.discoveredDevice.ipAddr = ipAddr; context->device.startHeartbeat(); - context->device.enablePcloudDataReq( - {context, std::bind(&ConnectReq::connectReq3, context.get(), context, - std::placeholders::_1)}); + // Use async delay before enabling point cloud data + delayedEnablePcloudData(context); } void connectReq3( + std::shared_ptr context, + const boost::system::error_code& error + ) + { + if (error) + { + // Timer was cancelled or error occurred + context->callOriginalCb(false); + return; + } + + context->device.enablePcloudDataReq( + {context, std::bind(&ConnectReq::connectReq4, + context.get(), context, + std::placeholders::_1)}); + } + + void connectReq4( std::shared_ptr context, bool success ) @@ -1588,8 +1618,6 @@ void Device::enablePcloudDataReq( smo::Callback callback ) { - std::this_thread::sleep_for(std::chrono::milliseconds(105)); - auto request = std::make_shared( *this, std::move(callback));