diff options
Diffstat (limited to 'src/Xpl2Client.cpp')
| -rw-r--r-- | src/Xpl2Client.cpp | 351 |
1 files changed, 173 insertions, 178 deletions
diff --git a/src/Xpl2Client.cpp b/src/Xpl2Client.cpp index 4fa502e..2a972e0 100644 --- a/src/Xpl2Client.cpp +++ b/src/Xpl2Client.cpp @@ -5,7 +5,6 @@ #include "Xpl2Client.h" #include <QDebug> -#include <QTcpSocket> bool Xpl2Client::s_wireDebug = false; @@ -158,19 +157,47 @@ Xpl2Client::s_responseTable = { Xpl2Client::Xpl2Client (QObject *parent) : QObject (parent) { - setupServer (m_commandServer, m_commandPort); - setupServer (m_imagingServer, m_imagingPort); - setupServer (m_statusServer, m_statusPort); + setupSocket (m_commandSocket); + setupSocket (m_imagingSocket); + setupSocket (m_statusSocket); + + connect (&m_retryTimer, &QTimer::timeout, this, + &Xpl2Client::retryConnection); } /* ------------------------------------------------------------------ */ /* Properties */ /* ------------------------------------------------------------------ */ -bool -Xpl2Client::isListening () const +QString +Xpl2Client::host () const +{ + return m_host; +} + +void +Xpl2Client::setHost (const QString &host) +{ + if (m_host == host) + return; + m_host = host; + emit hostChanged (); +} + +int +Xpl2Client::commandPort () const { - return m_listening; + return m_commandPort; +} + +void +Xpl2Client::setCommandPort (int port) +{ + quint16 p = static_cast<quint16> (port); + if (m_commandPort == p) + return; + m_commandPort = p; + emit commandPortChanged (); } bool @@ -214,90 +241,73 @@ Xpl2Client::enableWireDebug () /* ------------------------------------------------------------------ */ void -Xpl2Client::setupServer (QTcpServer &server, quint16 port) +Xpl2Client::setupSocket (QTcpSocket &socket) { - Q_UNUSED (port) - connect (&server, &QTcpServer::newConnection, this, - &Xpl2Client::onNewConnection); + connect (&socket, &QTcpSocket::connected, this, + &Xpl2Client::onSocketConnected); + connect (&socket, &QTcpSocket::disconnected, this, + &Xpl2Client::onSocketDisconnected); + connect (&socket, &QTcpSocket::readyRead, this, + &Xpl2Client::onSocketMessageReady); + connect (&socket, &QAbstractSocket::errorOccurred, this, + &Xpl2Client::onSocketError); } void -Xpl2Client::startListening () +Xpl2Client::connectToProxy () { - if (m_listening) + if (m_connected) { - emit statusMessage (QStringLiteral ("Already listening")); + emit statusMessage (QStringLiteral ("Already connected")); return; } - if (!m_commandServer.listen (QHostAddress::Any, m_commandPort) - || !m_imagingServer.listen (QHostAddress::Any, m_imagingPort) - || !m_statusServer.listen (QHostAddress::Any, m_statusPort)) - { - emit errorOccurred (QStringLiteral ("Failed to listen: %1 / %2 / %3") - .arg (m_commandServer.errorString ()) - .arg (m_imagingServer.errorString ()) - .arg (m_statusServer.errorString ())); - m_commandServer.close (); - m_imagingServer.close (); - m_statusServer.close (); - return; - } - m_listening = true; - emit listeningChanged (); - emit statusMessage (QStringLiteral ("Listening on ports %1/%2/%3…") + + quint16 imagingPort = m_commandPort + 1; + quint16 statusPort = m_commandPort + 2; + + for (auto *s : { &m_commandSocket, &m_imagingSocket, &m_statusSocket }) + if (s->state () == QAbstractSocket::UnconnectedState) + { + if (s == &m_commandSocket) + s->connectToHost (m_host, m_commandPort); + else if (s == &m_imagingSocket) + s->connectToHost (m_host, imagingPort); + else + s->connectToHost (m_host, statusPort); + } + + if (!m_retryTimer.isActive ()) + m_retryTimer.start (2000); + + emit statusMessage (QStringLiteral ("Connecting to %1:%2/%3/%4…") + .arg (m_host) .arg (m_commandPort) - .arg (m_imagingPort) - .arg (m_statusPort)); + .arg (imagingPort) + .arg (statusPort)); } void -Xpl2Client::stopListening () +Xpl2Client::disconnectFromProxy () { - m_commandServer.close (); - m_imagingServer.close (); - m_statusServer.close (); + m_retryTimer.stop (); - auto cleanup = [] (QTcpSocket *&sock) - { - if (sock) - { - sock->disconnectFromHost (); - sock->deleteLater (); - sock = nullptr; - } - }; - cleanup (m_commandSocket); - cleanup (m_imagingSocket); - cleanup (m_statusSocket); + for (auto *s : { &m_commandSocket, &m_imagingSocket, &m_statusSocket }) + if (s->state () != QAbstractSocket::UnconnectedState) + s->disconnectFromHost (); - if (m_listening) - { - m_listening = false; - emit listeningChanged (); - } updateConnectedState (); } -QTcpSocket *& -Xpl2Client::socketForServer (QTcpServer *server) -{ - if (server == &m_commandServer) - return m_commandSocket; - if (server == &m_imagingServer) - return m_imagingSocket; - return m_statusSocket; -} - void Xpl2Client::getJcVersion () { - sendCommand (m_commandSocket, "GS_JC_VERSION"); + sendCommand (&m_commandSocket, "GS_JC_VERSION"); } void Xpl2Client::getPhVersion (int printheadId) { - sendCommand (m_commandSocket, "GS_PH_VERSION", { printheadId }); + sendCommand (&m_commandSocket, "GS_PH_VERSION", { printheadId }); } /* ------------------------------------------------------------------ */ @@ -307,131 +317,132 @@ Xpl2Client::getPhVersion (int printheadId) void Xpl2Client::jettingAllOn () { - sendCommand (m_commandSocket, "CN_JETTING_ALL_ON"); + sendCommand (&m_commandSocket, "CN_JETTING_ALL_ON"); } void Xpl2Client::jettingOn (const QString &jettingMask) { - sendCommand (m_commandSocket, "CN_JETTING_ON", { jettingMask }); + sendCommand (&m_commandSocket, "CN_JETTING_ON", { jettingMask }); } void Xpl2Client::jettingOff () { - sendCommand (m_commandSocket, "CN_JETTING_OFF"); + sendCommand (&m_commandSocket, "CN_JETTING_OFF"); } void Xpl2Client::phJettingOn (int printheadId, const QString &jettingMask) { - sendCommand (m_commandSocket, "CN_PH_JETTING_ON", + sendCommand (&m_commandSocket, "CN_PH_JETTING_ON", { printheadId, jettingMask }); } void Xpl2Client::phJettingOff (int printheadId) { - sendCommand (m_commandSocket, "CN_PH_JETTING_OFF", { printheadId }); + sendCommand (&m_commandSocket, "CN_PH_JETTING_OFF", { printheadId }); } void Xpl2Client::jcIdLedOn () { - sendCommand (m_commandSocket, "CN_JC_ID_LED_ON"); + sendCommand (&m_commandSocket, "CN_JC_ID_LED_ON"); } void Xpl2Client::jcIdLedOff () { - sendCommand (m_commandSocket, "CN_JC_ID_LED_OFF"); + sendCommand (&m_commandSocket, "CN_JC_ID_LED_OFF"); } void Xpl2Client::phIdLedOn (int printheadId) { - sendCommand (m_commandSocket, "CN_PH_ID_LED_ON", { printheadId }); + sendCommand (&m_commandSocket, "CN_PH_ID_LED_ON", { printheadId }); } void Xpl2Client::phIdLedOff (int printheadId) { - sendCommand (m_commandSocket, "CN_PH_ID_LED_OFF", { printheadId }); + sendCommand (&m_commandSocket, "CN_PH_ID_LED_OFF", { printheadId }); } void Xpl2Client::jcCalibration () { - sendCommand (m_commandSocket, "CN_JC_CALIBRATION"); + sendCommand (&m_commandSocket, "CN_JC_CALIBRATION"); } void Xpl2Client::phCalibration (int printheadId) { - sendCommand (m_commandSocket, "CN_PH_CALIBRATION", { printheadId }); + sendCommand (&m_commandSocket, "CN_PH_CALIBRATION", { printheadId }); } void Xpl2Client::phCalibrationData (int printheadId) { - sendCommand (m_commandSocket, "CN_PH_CALIBRATION_DATA", { printheadId }); + sendCommand (&m_commandSocket, "CN_PH_CALIBRATION_DATA", { printheadId }); } void Xpl2Client::phCalibrationRawData (int printheadId) { - sendCommand (m_commandSocket, "CN_PH_CALIBRATION_RAW_DATA", { printheadId }); + sendCommand (&m_commandSocket, "CN_PH_CALIBRATION_RAW_DATA", + { printheadId }); } void Xpl2Client::phCalibratedBaseFrequency (int printheadId) { - sendCommand (m_commandSocket, "CN_PH_CALIBRATED_BASE_FREQUENCY", + sendCommand (&m_commandSocket, "CN_PH_CALIBRATED_BASE_FREQUENCY", { printheadId }); } void Xpl2Client::jcStatusMessagingStart (int statusLevel, int sendIntervalMs) { - sendCommand (m_statusSocket, "CN_JC_STATUS_MESSAGING_START", + sendCommand (&m_statusSocket, "CN_JC_STATUS_MESSAGING_START", { statusLevel, sendIntervalMs }); } void Xpl2Client::jcStatusMessagingStop () { - sendCommand (m_statusSocket, "CN_JC_STATUS_MESSAGING_STOP"); + sendCommand (&m_statusSocket, "CN_JC_STATUS_MESSAGING_STOP"); } void Xpl2Client::phStatusMessagingStart (int statusLevel, int sendIntervalMs) { - sendCommand (m_statusSocket, "CN_PH_STATUS_MESSAGING_START", + sendCommand (&m_statusSocket, "CN_PH_STATUS_MESSAGING_START", { statusLevel, sendIntervalMs }); } void Xpl2Client::phStatusMessagingStop () { - sendCommand (m_statusSocket, "CN_PH_STATUS_MESSAGING_STOP"); + sendCommand (&m_statusSocket, "CN_PH_STATUS_MESSAGING_STOP"); } void Xpl2Client::jcResetFaultCodes () { - sendCommand (m_commandSocket, "CN_JC_RESET_FAULT_CODES"); + sendCommand (&m_commandSocket, "CN_JC_RESET_FAULT_CODES"); } void Xpl2Client::phResetFaultCodes (int printheadId) { - sendCommand (m_commandSocket, "CN_PH_RESET_FAULT_CODES", { printheadId }); + sendCommand (&m_commandSocket, "CN_PH_RESET_FAULT_CODES", { printheadId }); } void Xpl2Client::phNozzlesDisabled (int printheadId, const QString &mask) { - sendCommand (m_commandSocket, "CN_PH_NOZZLES_DISABLED", + sendCommand (&m_commandSocket, "CN_PH_NOZZLES_DISABLED", { printheadId, mask }); } @@ -442,13 +453,13 @@ Xpl2Client::phNozzlesDisabled (int printheadId, const QString &mask) void Xpl2Client::phSetId (int printheadId) { - sendCommand (m_commandSocket, "CF_PH_SET_ID", { printheadId }); + sendCommand (&m_commandSocket, "CF_PH_SET_ID", { printheadId }); } void Xpl2Client::phDeassignId () { - sendCommand (m_commandSocket, "CF_PH_DEASSIGN_ID"); + sendCommand (&m_commandSocket, "CF_PH_DEASSIGN_ID"); } void @@ -456,7 +467,7 @@ Xpl2Client::jcSetJettingParams (int dutyCycle, int pwmFreq, int drive, int nozzleDriveFreq, int nozzleDriveDutyCycle) { sendCommand ( - m_commandSocket, "CF_JC_SET_JETTING_PARAMS", + &m_commandSocket, "CF_JC_SET_JETTING_PARAMS", { dutyCycle, pwmFreq, drive, nozzleDriveFreq, nozzleDriveDutyCycle }); } @@ -465,7 +476,7 @@ Xpl2Client::phSetJettingParams (int printheadId, int dutyCycle, int pwmFreq, int drive, int nozzleDriveFreq, int nozzleDriveDutyCycle) { - sendCommand (m_commandSocket, "CF_PH_SET_JETTING_PARAMS", + sendCommand (&m_commandSocket, "CF_PH_SET_JETTING_PARAMS", { printheadId, dutyCycle, pwmFreq, drive, nozzleDriveFreq, nozzleDriveDutyCycle }); } @@ -473,50 +484,50 @@ Xpl2Client::phSetJettingParams (int printheadId, int dutyCycle, int pwmFreq, void Xpl2Client::phGetJettingParams (int printheadId) { - sendCommand (m_commandSocket, "CF_PH_GET_JETTING_PARAMS", { printheadId }); + sendCommand (&m_commandSocket, "CF_PH_GET_JETTING_PARAMS", { printheadId }); } void Xpl2Client::jcSaveCalibration () { - sendCommand (m_commandSocket, "CF_JC_SAVE_CALIBRATION"); + sendCommand (&m_commandSocket, "CF_JC_SAVE_CALIBRATION"); } void Xpl2Client::phSaveCalibration (int printheadId) { - sendCommand (m_commandSocket, "CF_PH_SAVE_CALIBRATION", { printheadId }); + sendCommand (&m_commandSocket, "CF_PH_SAVE_CALIBRATION", { printheadId }); } void Xpl2Client::jcResetCalibration () { - sendCommand (m_commandSocket, "CF_JC_RESET_CALIBRATION"); + sendCommand (&m_commandSocket, "CF_JC_RESET_CALIBRATION"); } void Xpl2Client::phResetCalibration (int printheadId) { - sendCommand (m_commandSocket, "CF_PH_RESET_CALIBRATION", { printheadId }); + sendCommand (&m_commandSocket, "CF_PH_RESET_CALIBRATION", { printheadId }); } void Xpl2Client::jcSetPurgeSettings (int purgeIntervalMs, int purgeTimeMs) { - sendCommand (m_commandSocket, "CF_JC_SET_PURGE_SETTINGS", + sendCommand (&m_commandSocket, "CF_JC_SET_PURGE_SETTINGS", { purgeIntervalMs, purgeTimeMs }); } void Xpl2Client::jcSwitchOffPurge () { - sendCommand (m_commandSocket, "CF_JC_SWITCH_OFF_PURGE"); + sendCommand (&m_commandSocket, "CF_JC_SWITCH_OFF_PURGE"); } void Xpl2Client::jcSetter (bool saveNewValue, int setterId, const QString &newValue) { - sendCommand (m_commandSocket, "CF_JC_SETTER", + sendCommand (&m_commandSocket, "CF_JC_SETTER", { saveNewValue, setterId, newValue }); } @@ -524,75 +535,75 @@ void Xpl2Client::phSetter (int printheadId, bool saveNewValue, int setterId, const QString &newValue) { - sendCommand (m_commandSocket, "CF_PH_SETTER", + sendCommand (&m_commandSocket, "CF_PH_SETTER", { printheadId, saveNewValue, setterId, newValue }); } void Xpl2Client::jcGetter (bool getSavedValue, int getterId) { - sendCommand (m_commandSocket, "CF_JC_GETTER", { getSavedValue, getterId }); + sendCommand (&m_commandSocket, "CF_JC_GETTER", { getSavedValue, getterId }); } void Xpl2Client::phGetter (int printheadId, bool getSavedValue, int getterId) { - sendCommand (m_commandSocket, "CF_PH_GETTER", + sendCommand (&m_commandSocket, "CF_PH_GETTER", { printheadId, getSavedValue, getterId }); } void Xpl2Client::jcResetSettingsAllPrintheads () { - sendCommand (m_commandSocket, "CF_JC_RESET_SETTINGS_ALL_PRINTHEADS"); + sendCommand (&m_commandSocket, "CF_JC_RESET_SETTINGS_ALL_PRINTHEADS"); } void Xpl2Client::phResetAllSettings (int printheadId) { - sendCommand (m_commandSocket, "CF_PH_RESET_ALL_SETTINGS", { printheadId }); + sendCommand (&m_commandSocket, "CF_PH_RESET_ALL_SETTINGS", { printheadId }); } void Xpl2Client::jcRebootAllPrintheads () { - sendCommand (m_commandSocket, "CF_JC_REBOOT_ALL_PRINTHEADS"); + sendCommand (&m_commandSocket, "CF_JC_REBOOT_ALL_PRINTHEADS"); } void Xpl2Client::phReboot (int printheadId) { - sendCommand (m_commandSocket, "CF_PH_REBOOT", { printheadId }); + sendCommand (&m_commandSocket, "CF_PH_REBOOT", { printheadId }); } void Xpl2Client::jcResetControllerSoftware () { - sendCommand (m_commandSocket, "CF_JC_RESET_CONTROLLER_SOFTWARE"); + sendCommand (&m_commandSocket, "CF_JC_RESET_CONTROLLER_SOFTWARE"); } void Xpl2Client::jcRestart () { - sendCommand (m_commandSocket, "CF_JC_RESTART"); + sendCommand (&m_commandSocket, "CF_JC_RESTART"); } void Xpl2Client::jcShutdown () { - sendCommand (m_commandSocket, "CF_JC_SHUTDOWN"); + sendCommand (&m_commandSocket, "CF_JC_SHUTDOWN"); } void Xpl2Client::jcSaveAllPrintheadSettings () { - sendCommand (m_commandSocket, "CF_JC_SAVE_ALL_PRINTHEAD_SETTINGS"); + sendCommand (&m_commandSocket, "CF_JC_SAVE_ALL_PRINTHEAD_SETTINGS"); } void Xpl2Client::phSaveSettings (int printheadId) { - sendCommand (m_commandSocket, "CF_PH_SAVE_SETTINGS", { printheadId }); + sendCommand (&m_commandSocket, "CF_PH_SAVE_SETTINGS", { printheadId }); } /* ------------------------------------------------------------------ */ @@ -602,43 +613,43 @@ Xpl2Client::phSaveSettings (int printheadId) void Xpl2Client::imagingStart (double speed) { - sendCommand (m_imagingSocket, "m2", { speed }); + sendCommand (&m_imagingSocket, "m2", { speed }); } void Xpl2Client::imagingStop () { - sendCommand (m_imagingSocket, "m4"); + sendCommand (&m_imagingSocket, "m4"); } void Xpl2Client::imageMaskStart (const QString &mask) { - sendCommand (m_imagingSocket, "m0", { mask }); + sendCommand (&m_imagingSocket, "m0", { mask }); } void Xpl2Client::imageMaskEnd (const QString &tail) { - sendCommand (m_imagingSocket, "m1", { tail }); + sendCommand (&m_imagingSocket, "m1", { tail }); } void Xpl2Client::dutyCycleMaskStart (const QString &mask) { - sendCommand (m_imagingSocket, "m5", { mask }); + sendCommand (&m_imagingSocket, "m5", { mask }); } void Xpl2Client::dutyCycleMaskEnd (const QString &tail) { - sendCommand (m_imagingSocket, "m6", { tail }); + sendCommand (&m_imagingSocket, "m6", { tail }); } void Xpl2Client::imageCount () { - sendCommand (m_imagingSocket, "m3"); + sendCommand (&m_imagingSocket, "m3"); } /* ------------------------------------------------------------------ */ @@ -669,15 +680,15 @@ void Xpl2Client::dispatchCommandMessage (const Xpl2Protocol::ParsedMessage &msg) { if (msg.command == "KA_PING") - handleKaPing (m_commandSocket); + handleKaPing (&m_commandSocket); else if (msg.command == "EV_SHUTTING_DOWN") emit shuttingDown (); else if (msg.command == "GS_JC_VERSION") handleGsJcVersion (msg.params); else if (msg.command == "GS_PH_VERSION") handleGsPhVersion (msg.params); - else if (!dispatchResponse (msg.command, msg.params, m_commandSocket)) - qWarning ("%s Unknown command: %s", qPrintable (logTag (m_commandSocket)), + else if (!dispatchResponse (msg.command, msg.params, &m_commandSocket)) + qWarning ("%s Unknown command: %s", qPrintable (logTag (&m_commandSocket)), msg.command.constData ()); } @@ -685,7 +696,7 @@ void Xpl2Client::dispatchImagingMessage (const Xpl2Protocol::ParsedMessage &msg) { if (msg.command == "KA_PING") - handleKaPing (m_imagingSocket); + handleKaPing (&m_imagingSocket); else if (msg.command == "EV_SHUTTING_DOWN") emit shuttingDown (); else if (msg.command == "n") @@ -693,7 +704,7 @@ Xpl2Client::dispatchImagingMessage (const Xpl2Protocol::ParsedMessage &msg) int lines = 0; if (!msg.params.isEmpty ()) lines = msg.params[0].toString ().toInt (nullptr, 16); - qDebug ("%s n imageLines=%d", qPrintable (logTag (m_imagingSocket)), + qDebug ("%s n imageLines=%d", qPrintable (logTag (&m_imagingSocket)), lines); emit statusMessage (QStringLiteral ("RX n: imageLines=%1").arg (lines)); emit imagingReply (lines); @@ -701,13 +712,13 @@ Xpl2Client::dispatchImagingMessage (const Xpl2Protocol::ParsedMessage &msg) else if (msg.command == "m4") { bool ok = !msg.params.isEmpty () && msg.params[0].toInt () == 1; - qDebug ("%s m4 success=%d", qPrintable (logTag (m_imagingSocket)), + qDebug ("%s m4 success=%d", qPrintable (logTag (&m_imagingSocket)), ok); emit statusMessage (QStringLiteral ("RX m4: success=%1").arg (ok)); emit imagingStopResult (ok); } - else if (!dispatchResponse (msg.command, msg.params, m_imagingSocket)) - qWarning ("%s Unknown command: %s", qPrintable (logTag (m_imagingSocket)), + else if (!dispatchResponse (msg.command, msg.params, &m_imagingSocket)) + qWarning ("%s Unknown command: %s", qPrintable (logTag (&m_imagingSocket)), msg.command.constData ()); } @@ -715,15 +726,15 @@ void Xpl2Client::dispatchStatusMessage (const Xpl2Protocol::ParsedMessage &msg) { if (msg.command == "KA_PING") - handleKaPing (m_statusSocket); + handleKaPing (&m_statusSocket); else if (msg.command == "EV_SHUTTING_DOWN") emit shuttingDown (); else if (msg.command == "EV_STATUS_MSG_JC") handleEvStatusMsgJc (msg.params); else if (msg.command == "EV_STATUS_MSG_PH") handleEvStatusMsgPh (msg.params); - else if (!dispatchResponse (msg.command, msg.params, m_statusSocket)) - qWarning ("%s Unknown command: %s", qPrintable (logTag (m_statusSocket)), + else if (!dispatchResponse (msg.command, msg.params, &m_statusSocket)) + qWarning ("%s Unknown command: %s", qPrintable (logTag (&m_statusSocket)), msg.command.constData ()); } @@ -746,7 +757,7 @@ Xpl2Client::handleGsJcVersion (const QVariantList ¶ms) m_hardwareVersion = params[2].toString (); m_printheadCount = params[3].toInt (); qDebug ("%s controller=%d fw=%s hw=%s phCount=%d", - qPrintable (logTag (m_commandSocket)), m_controllerId, + qPrintable (logTag (&m_commandSocket)), m_controllerId, qPrintable (m_firmwareVersion), qPrintable (m_hardwareVersion), m_printheadCount); emit statusMessage ( @@ -776,7 +787,7 @@ Xpl2Client::handleGsPhVersion (const QVariantList ¶ms) QString bootVer = params[7].toString (); qDebug ("%s PH[%d] mcuFw=%s mcuHw=%s mcuFwVar=%s fpgaFw=%s fpgaHw=%s " "boot=%s", - qPrintable (logTag (m_commandSocket)), phId, qPrintable (mcuFw), + qPrintable (logTag (&m_commandSocket)), phId, qPrintable (mcuFw), qPrintable (mcuHw), qPrintable (mcuFwVar), qPrintable (fpgaFw), qPrintable (fpgaHw), qPrintable (bootVer)); emit statusMessage ( @@ -942,7 +953,7 @@ Xpl2Client::handleEvStatusMsgJc (const QVariantList ¶ms) { Xpl2JcStatus status = Xpl2JcStatus::fromParams (params); qDebug ("%s EV_STATUS_MSG_JC controller=%d level=%d temp=%.1f cpu=%.1f%%", - qPrintable (logTag (m_statusSocket)), status.controllerId, + qPrintable (logTag (&m_statusSocket)), status.controllerId, status.statusLevel, status.temperature, status.cpuPercentageBusy); emit statusMessage ( QStringLiteral ("RX EV_STATUS_MSG_JC: controller=%1 level=%2") @@ -956,7 +967,7 @@ Xpl2Client::handleEvStatusMsgPh (const QVariantList ¶ms) { Xpl2PhStatus status = Xpl2PhStatus::fromParams (params); qDebug ("%s EV_STATUS_MSG_PH controller=%d level=%d ph=%d temp=%.1f", - qPrintable (logTag (m_statusSocket)), status.controllerId, + qPrintable (logTag (&m_statusSocket)), status.controllerId, status.statusLevel, status.printheadId, status.temperature); emit statusMessage ( QStringLiteral ("RX EV_STATUS_MSG_PH: controller=%1 level=%2 ph=%3") @@ -974,12 +985,9 @@ void Xpl2Client::updateConnectedState () { bool allConnected - = m_commandSocket - && m_commandSocket->state () == QAbstractSocket::ConnectedState - && m_imagingSocket - && m_imagingSocket->state () == QAbstractSocket::ConnectedState - && m_statusSocket - && m_statusSocket->state () == QAbstractSocket::ConnectedState; + = m_commandSocket.state () == QAbstractSocket::ConnectedState + && m_imagingSocket.state () == QAbstractSocket::ConnectedState + && m_statusSocket.state () == QAbstractSocket::ConnectedState; if (m_connected == allConnected) return; m_connected = allConnected; @@ -991,20 +999,20 @@ Xpl2Client::logTag (const QTcpSocket *socket) const { const char *name = "Unknown"; quint16 port = 0; - if (socket == m_commandSocket) + if (socket == &m_commandSocket) { name = "Command"; port = m_commandPort; } - else if (socket == m_imagingSocket) + else if (socket == &m_imagingSocket) { name = "Imaging"; - port = m_imagingPort; + port = m_commandPort + 1; } - else if (socket == m_statusSocket) + else if (socket == &m_statusSocket) { name = "Status"; - port = m_statusPort; + port = m_commandPort + 2; } return QStringLiteral ("[%1:%2]").arg (name).arg (port).leftJustified (15); @@ -1015,47 +1023,23 @@ Xpl2Client::logTag (const QTcpSocket *socket) const /* ------------------------------------------------------------------ */ void -Xpl2Client::onNewConnection () +Xpl2Client::onSocketConnected () { - auto *server = qobject_cast<QTcpServer *> (sender ()); - QTcpSocket *&slot = socketForServer (server); - - while (auto *pending = server->nextPendingConnection ()) - { - if (slot) - { - qWarning ("%s Rejected extra connection", - qPrintable (logTag (slot))); - pending->deleteLater (); - continue; - } - slot = pending; - connect (slot, &QTcpSocket::readyRead, this, - &Xpl2Client::onSocketMessageReady); - connect (slot, &QTcpSocket::disconnected, this, - &Xpl2Client::onSocketDisconnected); - connect (slot, &QAbstractSocket::errorOccurred, this, - &Xpl2Client::onSocketError); - qInfo ("%s Controller connected", qPrintable (logTag (slot))); - updateConnectedState (); - } + auto *socket = qobject_cast<QTcpSocket *> (sender ()); + qInfo ("%s Connected to proxy", qPrintable (logTag (socket))); + updateConnectedState (); + if (m_connected) + m_retryTimer.stop (); } void Xpl2Client::onSocketDisconnected () { auto *socket = qobject_cast<QTcpSocket *> (sender ()); - qInfo ("%s Controller disconnected", qPrintable (logTag (socket))); - - if (socket == m_commandSocket) - m_commandSocket = nullptr; - else if (socket == m_imagingSocket) - m_imagingSocket = nullptr; - else if (socket == m_statusSocket) - m_statusSocket = nullptr; - - socket->deleteLater (); + qInfo ("%s Disconnected from proxy", qPrintable (logTag (socket))); updateConnectedState (); + if (!m_retryTimer.isActive ()) + m_retryTimer.start (2000); } void @@ -1074,9 +1058,9 @@ Xpl2Client::onSocketMessageReady () qDebug ("%s RX %s%s", qPrintable (logTag (socket)), msg.command.constData (), wire.constData ()); - if (socket == m_commandSocket) + if (socket == &m_commandSocket) dispatchCommandMessage (msg); - else if (socket == m_imagingSocket) + else if (socket == &m_imagingSocket) dispatchImagingMessage (msg); else dispatchStatusMessage (msg); @@ -1093,3 +1077,14 @@ Xpl2Client::onSocketError (QAbstractSocket::SocketError error) .arg (logTag (socket)) .arg (socket->errorString ())); } + +void +Xpl2Client::retryConnection () +{ + if (m_connected) + { + m_retryTimer.stop (); + return; + } + connectToProxy (); +} |
