Ensure proper closure of the heartbeat thread on reopen
parent
c48995520f
commit
046e2bae9d
|
|
@ -69,6 +69,12 @@ bool Device::SerialStringIsNumeric(const std::string& serial) {
|
||||||
return isdigit(serial[0]) && isdigit(serial[1]);
|
return isdigit(serial[0]) && isdigit(serial[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Device::~Device() {
|
||||||
|
if(isMessagePollingEnabled())
|
||||||
|
disableMessagePolling();
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t Device::getTimestampResolution() const {
|
uint16_t Device::getTimestampResolution() const {
|
||||||
return com->decoder->timestampResolution;
|
return com->decoder->timestampResolution;
|
||||||
}
|
}
|
||||||
|
|
@ -205,14 +211,24 @@ bool Device::open() {
|
||||||
handleInternalMessage(message);
|
handleInternalMessage(message);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
std::atomic<bool> receivedMessage{false};
|
heartbeatThread = std::thread([this]() {
|
||||||
messageReceivedCallbackID = com->addMessageCallback(MessageCallback(filter, [&](std::shared_ptr<Message> message) {
|
|
||||||
receivedMessage = true;
|
|
||||||
}));
|
|
||||||
|
|
||||||
heartbeatThread = std::thread([&]() {
|
|
||||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
||||||
while(true) {
|
|
||||||
|
MessageFilter filter;
|
||||||
|
filter.includeInternalInAny = true;
|
||||||
|
std::atomic<bool> receivedMessage{false};
|
||||||
|
auto messageReceivedCallbackID = com->addMessageCallback(MessageCallback(filter, [&receivedMessage](std::shared_ptr<Message> message) {
|
||||||
|
receivedMessage = true;
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Give the device time to get situated
|
||||||
|
auto i = 150;
|
||||||
|
while(!stopHeartbeatThread && i != 0) {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(!stopHeartbeatThread) {
|
||||||
// Wait for 110ms for a possible heartbeat
|
// Wait for 110ms for a possible heartbeat
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(110));
|
std::this_thread::sleep_for(std::chrono::milliseconds(110));
|
||||||
if(!receivedMessage) {
|
if(!receivedMessage) {
|
||||||
|
|
@ -229,6 +245,8 @@ bool Device::open() {
|
||||||
}
|
}
|
||||||
receivedMessage = false;
|
receivedMessage = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
com->removeMessageCallback(messageReceivedCallbackID);
|
||||||
});
|
});
|
||||||
|
|
||||||
forEachExtension([](const std::shared_ptr<DeviceExtension>& ext) { ext->onDeviceOpen(); return true; });
|
forEachExtension([](const std::shared_ptr<DeviceExtension>& ext) { ext->onDeviceOpen(); return true; });
|
||||||
|
|
@ -249,11 +267,12 @@ bool Device::close() {
|
||||||
if(internalHandlerCallbackID)
|
if(internalHandlerCallbackID)
|
||||||
com->removeMessageCallback(internalHandlerCallbackID);
|
com->removeMessageCallback(internalHandlerCallbackID);
|
||||||
|
|
||||||
if(messageReceivedCallbackID)
|
|
||||||
com->removeMessageCallback(messageReceivedCallbackID);
|
|
||||||
|
|
||||||
internalHandlerCallbackID = 0;
|
internalHandlerCallbackID = 0;
|
||||||
|
|
||||||
|
if(heartbeatThread.joinable())
|
||||||
|
heartbeatThread.join();
|
||||||
|
stopHeartbeatThread = false;
|
||||||
|
|
||||||
forEachExtension([](const std::shared_ptr<DeviceExtension>& ext) { ext->onDeviceClose(); return true; });
|
forEachExtension([](const std::shared_ptr<DeviceExtension>& ext) { ext->onDeviceClose(); return true; });
|
||||||
return com->close();
|
return com->close();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,7 @@ namespace icsneo {
|
||||||
|
|
||||||
class Device {
|
class Device {
|
||||||
public:
|
public:
|
||||||
virtual ~Device() {
|
virtual ~Device();
|
||||||
if(isMessagePollingEnabled())
|
|
||||||
disableMessagePolling();
|
|
||||||
close();
|
|
||||||
if(heartbeatThread.joinable())
|
|
||||||
heartbeatThread.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string SerialNumToString(uint32_t serial);
|
static std::string SerialNumToString(uint32_t serial);
|
||||||
static uint32_t SerialStringToNum(const std::string& serial);
|
static uint32_t SerialStringToNum(const std::string& serial);
|
||||||
|
|
@ -103,7 +97,6 @@ protected:
|
||||||
bool online = false;
|
bool online = false;
|
||||||
int messagePollingCallbackID = 0;
|
int messagePollingCallbackID = 0;
|
||||||
int internalHandlerCallbackID = 0;
|
int internalHandlerCallbackID = 0;
|
||||||
int messageReceivedCallbackID = 0;
|
|
||||||
device_eventhandler_t report;
|
device_eventhandler_t report;
|
||||||
|
|
||||||
// START Initialization Functions
|
// START Initialization Functions
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue