From 92084bed99872689b09670aca49b012a54567b64 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Fri, 14 Jul 2023 13:27:14 +0200 Subject: [PATCH] Added call to waitForConnected() when doing a connectToHost(). Replaced some qCritical() with qDebug()-calls. --- apism/apism_tcp_client.cpp | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/apism/apism_tcp_client.cpp b/apism/apism_tcp_client.cpp index 2fde29d..e878b87 100644 --- a/apism/apism_tcp_client.cpp +++ b/apism/apism_tcp_client.cpp @@ -2,6 +2,7 @@ #include #include +#include ApismTcpClient::ApismTcpClient(const QString & hostname, const QString & port, @@ -26,10 +27,15 @@ ApismTcpClient::ApismTcpClient(const QString & hostname, -void ApismTcpClient::connectToHost() -{ +void ApismTcpClient::connectToHost() { + qCritical() << "ApismTcpClient::connectToHost(this->" << hostname << ", " << port << ")"; int portNumber = this->port.toInt(); this->socket->connectToHost(QHostAddress(this->hostname), portNumber); + if (!socket->waitForConnected(10000)) { + qCritical() << "ERROR IN WAIT FOR CONNECTED" << socket->errorString(); + } else { + qDebug() << "connected to" << hostname << ", " << port << ")"; + } } void ApismTcpClient::connectToHost(const QString & hostname, const QString & port) @@ -76,17 +82,16 @@ bool ApismTcpClient::isConnected() -void ApismTcpClient::sendData(const QByteArray & message) -{ - //qCritical() << "ApismTcpClient::send: " << message; +void ApismTcpClient::sendData(const QByteArray & message) { + qDebug() << "ApismTcpClient::send: " << message; this->sendQueue.enqueue(message); if (this->isConnected()) { + qCritical() << "ApismTcpClient::send: connected, send" << message; this->private_sendData(); - - } - else { + } else { + qCritical() << "ApismTcpClient::send: not connected, connect"; this->connectToHost(); } } @@ -101,7 +106,7 @@ void ApismTcpClient::private_sendData() // take message from queue QByteArray ba = this->sendQueue.dequeue(); - qCritical() << "ApismTcpClient::send: " << QString(ba); + qDebug() << "ApismTcpClient::send: " << QString(ba); socket->write(ba); socket->flush(); @@ -112,7 +117,7 @@ void ApismTcpClient::private_sendData() void ApismTcpClient::onSocketConnected() { - qCritical() << "ApismTcpClient: Connected!"; + qInfo() << "ApismTcpClient: Connected!"; if (this->sendQueue.size() > 0) { this->private_sendData(); @@ -121,8 +126,8 @@ void ApismTcpClient::onSocketConnected() void ApismTcpClient::onSocketDisconnected() { - qCritical() << "ApismTcpClient: Disconnected!"; - qCritical() << " -> SocketErrorString: " << socket->errorString(); + qDebug() << "ApismTcpClient: Disconnected!"; + qDebug() << " -> SocketErrorString: " << socket->errorString(); if (this->sendQueue.size() > 0) { this->connectToHost(); @@ -143,9 +148,10 @@ void ApismTcpClient::onSocketReadyRead() readData = socket->readAll(); - qCritical() << "ISMAS received: " << QString(readData); + qDebug() << "ISMAS received: " << QString(readData); emit this->receivedData(readData); + //QCoreApplication::processEvents(); this->socket->close(); }