Commit 76a9c42a authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Fix caching feature

parent ebf474bb
......@@ -155,9 +155,11 @@ bool CanDriver_cobsTcp::init (const QVariantMap & options) {
m_forceFlush = flush;
foreach (const QVariant & id, cacheAll) {
m_idsCacheAll.insert (id.value<quint32> ());
m_cacheById.insert (id.value<quint32> (), new QByteArray);
}
foreach (const QVariant & id, cacheLast) {
m_idsCacheLast.insert (id.value<quint32> ());
m_cacheById.insert (id.value<quint32> (), new QByteArray);
}
m_caching = (!m_idsCacheAll.isEmpty () || !m_idsCacheLast.isEmpty ());
quint16 actualPort = port;
......@@ -205,6 +207,7 @@ bool CanDriver_cobsTcp::stop (void) {
m_cacheById.clear ();
m_idsCacheAll.clear ();
m_idsCacheLast.clear ();
qDeleteAll (m_cacheById);
return true;
}
......@@ -230,13 +233,13 @@ bool CanDriver_cobsTcp::send (CanMessage * message) {
const quint32 id (message->getCanId ().isEFF () ? message->getCanId ().canIdEFF () : message->getCanId ().canIdSFF ());
const QByteArray data (reinterpret_cast<const char *> (m_tx.buffer), CobsTcpHelper::FRAME_PAYLOAD);
if (m_idsCacheLast.contains (id)) {
m_cacheById [id].clear ();
m_cacheById [id].append (data);
m_cacheById [id].append (CobsTcpHelper::SEP);
m_cacheById [id]->clear ();
m_cacheById [id]->append (data);
m_cacheById [id]->append (CobsTcpHelper::SEP);
}
if (m_idsCacheAll.contains (id)) {
m_cacheById [id].append (data);
m_cacheById [id].append (CobsTcpHelper::SEP);
m_cacheById [id]->append (data);
m_cacheById [id]->append (CobsTcpHelper::SEP);
}
}
if (m_socket->state () == QTcpSocket::ConnectedState) {
......@@ -282,8 +285,10 @@ void CanDriver_cobsTcp::onClientConnected (void) {
connect (client, &QTcpSocket::readyRead, this, &CanDriver_cobsTcp::onClientSentData);
connect (client, &QTcpSocket::disconnected, this, &CanDriver_cobsTcp::onClientDisconnected);
m_clients.append (client);
for (QHash<quint32, QByteArray>::const_iterator it = m_cacheById.constBegin (); it != m_cacheById.constEnd (); ++it) {
client->write (it.value ());
for (QHash<quint32, QByteArray *>::const_iterator it = m_cacheById.constBegin (); it != m_cacheById.constEnd (); ++it) {
if (!it.value ()->isEmpty ()) {
client->write (* it.value ());
}
}
if (m_forceFlush) {
client->flush ();
......
......@@ -99,17 +99,17 @@ private slots:
void onClientDisconnected (void);
private:
bool m_caching;
bool m_loopback;
bool m_forceFlush;
CobsTcpHelper m_rx;
CobsTcpHelper m_tx;
QTcpSocket * m_socket;
QTcpServer * m_server;
QVector<QTcpSocket *> m_clients;
QSet<quint32> m_idsCacheLast;
QSet<quint32> m_idsCacheAll;
QHash<quint32, QByteArray> m_cacheById;
bool m_caching;
bool m_loopback;
bool m_forceFlush;
CobsTcpHelper m_rx;
CobsTcpHelper m_tx;
QTcpSocket * m_socket;
QTcpServer * m_server;
QVector<QTcpSocket *> m_clients;
QSet<quint32> m_idsCacheLast;
QSet<quint32> m_idsCacheAll;
QHash<quint32, QByteArray *> m_cacheById;
};
#ifndef QTCAN_STATIC_DRIVERS
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment