-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow multiple messages to be scheduled with the same ID if the interval is set to 0 #30
Changes from 2 commits
83b9cea
31795ab
e6aa6a0
e03703c
cf937c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,20 +84,41 @@ class DriverDeviceThread { | |
return nullptr; // If no matching element found | ||
} | ||
|
||
detail::CANThreadSendQueueElement* findFirstMatchingIdWithNonZeroInterval(int targetId) { | ||
for (auto& element : m_sendQueue) { | ||
if (element.m_msg.GetMessageId() == targetId && element.m_intervalMs > 0) { | ||
return &element; | ||
} | ||
} | ||
return nullptr; // If no matching element found | ||
} | ||
|
||
void removeElementsWithId(int targetId) { | ||
m_sendQueue.erase(std::remove_if(m_sendQueue.begin(), m_sendQueue.end(), [targetId](detail::CANThreadSendQueueElement element) { return element.m_msg.GetMessageId() == targetId; }), m_sendQueue.end()); | ||
} | ||
|
||
bool EnqueueMessage(const CANMessage& msg, int32_t timeIntervalMs) { | ||
std::lock_guard<std::mutex> lock(m_writeMutex); | ||
|
||
detail::CANThreadSendQueueElement* existing = findFirstMatchingId(msg.GetMessageId()); | ||
if(timeIntervalMs == 0) { | ||
// If the time interval is 0, we want to allow duplicates. | ||
m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs)); | ||
|
||
if(existing) { | ||
existing->m_intervalMs = timeIntervalMs; | ||
existing->m_msg = msg; | ||
// Cancel existing repeating frame with same id | ||
detail::CANThreadSendQueueElement* existing = findFirstMatchingIdWithNonZeroInterval(msg.GetMessageId()); | ||
if(existing) { | ||
existing->m_intervalMs = -1; | ||
} | ||
} else { | ||
m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs)); | ||
// We don't want to replace elements with zero as the interval. Those should be guaranteed to be sent | ||
detail::CANThreadSendQueueElement* existing = findFirstMatchingIdWithNonZeroInterval(msg.GetMessageId()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After this change, calling with (data1, 0), then immediately calling with (data2, 5) will ensure that data1 is always sent once, then data2 is sent every 5 milliseconds. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, more practically, calling with (data_i, 0) repeatedly will ensure all the data actually gets sent. |
||
|
||
if(existing) { | ||
existing->m_intervalMs = timeIntervalMs; | ||
existing->m_msg = msg; | ||
} else { | ||
m_sendQueue.push_back(detail::CANThreadSendQueueElement(msg, timeIntervalMs)); | ||
} | ||
} | ||
|
||
// TODO: Limit the max queue size | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like
findFirstMatchingId()
can be deletedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted