Commit 207e6666 authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Don't try connect if not unconnected

parent 8a8ab6e0
......@@ -153,54 +153,59 @@ CanDriver_cobsTcp::CanDriver_cobsTcp (QObject * parent)
bool CanDriver_cobsTcp::init (const QVariantMap & options) {
bool ret = false;
const bool serv = options.value (SERV, false).toBool ();
const bool loop = options.value (LOOPBACK, false).toBool ();
const bool flush = options.value (FORCE_FLUSH, false).toBool ();
const bool autoClose = options.value (AUTO_CLOSE, false).toBool ();
const int watchdog = options.value (IDLE_WATCHDOG, 60000).toInt ();
const int delay = options.value (WAIT_DELAY, 3000).toInt ();
const quint16 port = options.value (PORT, 0).toUInt ();
const QString host = options.value (HOST, "localhost").toString ();
const QVariantList cacheAll = options.value (CACHE_ALL).toList ();
const QVariantList cacheLast = options.value (CACHE_LAST).toList ();
m_loopback = loop;
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;
if (serv) {
if (m_server->listen (QHostAddress::AnyIPv4, actualPort)) {
actualPort = m_server->serverPort ();
diag (Information, QS ("COBS server listening on localhost port %1").arg (actualPort));
if (m_socket->state () != QTcpSocket::UnconnectedState) {
const bool serv = options.value (SERV, false).toBool ();
const bool loop = options.value (LOOPBACK, false).toBool ();
const bool flush = options.value (FORCE_FLUSH, false).toBool ();
const bool autoClose = options.value (AUTO_CLOSE, false).toBool ();
const int watchdog = options.value (IDLE_WATCHDOG, 60000).toInt ();
const int delay = options.value (WAIT_DELAY, 3000).toInt ();
const quint16 port = options.value (PORT, 0).toUInt ();
const QString host = options.value (HOST, "localhost").toString ();
const QVariantList cacheAll = options.value (CACHE_ALL).toList ();
const QVariantList cacheLast = options.value (CACHE_LAST).toList ();
m_loopback = loop;
m_forceFlush = flush;
foreach (const QVariant & id, cacheAll) {
m_idsCacheAll.insert (id.value<quint32> ());
m_cacheById.insert (id.value<quint32> (), new QByteArray);
}
else {
diag (Error, QS ("Couldn't create COBS server on localhost port %1 ! %2").arg (actualPort).arg (m_server->errorString ()));
foreach (const QVariant & id, cacheLast) {
m_idsCacheLast.insert (id.value<quint32> ());
m_cacheById.insert (id.value<quint32> (), new QByteArray);
}
}
if (actualPort > 0 && !host.isEmpty ()) {
diag (Information, QS ("Initializing COBS socket driver with host %1 and port %2...").arg (host).arg (actualPort));
m_socket->connectToHost (host, actualPort, QTcpSocket::ReadWrite, QTcpSocket::IPv4Protocol);
if (m_socket->waitForConnected (delay)) {
ret = true;
diag (Information, QS ("COBS socket driver connected."));
if (!serv && autoClose) {
m_watchdog->setInterval (watchdog);
m_watchdog->start ();
m_caching = (!m_idsCacheAll.isEmpty () || !m_idsCacheLast.isEmpty ());
quint16 actualPort = port;
if (serv) {
if (m_server->listen (QHostAddress::AnyIPv4, actualPort)) {
actualPort = m_server->serverPort ();
diag (Information, QS ("COBS server listening on localhost port %1").arg (actualPort));
}
else {
diag (Error, QS ("Couldn't create COBS server on localhost port %1 ! %2").arg (actualPort).arg (m_server->errorString ()));
}
}
if (actualPort > 0 && !host.isEmpty ()) {
diag (Information, QS ("Initializing COBS socket driver with host %1 and port %2...").arg (host).arg (actualPort));
m_socket->connectToHost (host, actualPort, QTcpSocket::ReadWrite, QTcpSocket::IPv4Protocol);
if (m_socket->waitForConnected (delay)) {
ret = true;
diag (Information, QS ("COBS socket driver connected."));
if (!serv && autoClose) {
m_watchdog->setInterval (watchdog);
m_watchdog->start ();
}
}
else {
diag (Error, QS ("COBS socket couldn't connect ! %1").arg (m_socket->errorString ()));
}
}
else {
diag (Error, QS ("COBS socket couldn't connect ! %1").arg (m_socket->errorString ()));
diag (Error, QS ("Can't init COBS socket driver with host %1 and port %2 !").arg (host).arg (actualPort));
}
}
else {
diag (Error, QS ("Can't init COBS socket driver with host %1 and port %2 !").arg (host).arg (actualPort));
diag (Error, QS ("Can't init COBS socket driver while in busy state (%2) !").arg (m_socket->state ()));
}
return ret;
}
......
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