- Migrated old SIGNAL/SLOT connect syntax to function-pointer syntax - socket_handler.cpp: timeout, connected, disconnected, readyRead, bytesWritten - targettimer.cpp: timeout → MyTimerSlot - socketreader.cpp: readyRead, error (with QOverload for overloaded signal) - Updated MIGRATION_STATUS.md with migration progress - Part of Qt5→Qt6 migration with C++17 constraint (RTI 7.7.0 compatibility)
82 lines
2.2 KiB
C++
Executable File
82 lines
2.2 KiB
C++
Executable File
|
|
|
|
|
|
#include "socketreader.h"
|
|
|
|
#include <NGLLogger/NGLLoggerBootstrap.h>
|
|
|
|
#include <QtNetwork/QTcpSocket>
|
|
#include <QtCore/QTimer>
|
|
#include <QtCore/QCoreApplication>
|
|
#include <QtCore/QMetaEnum>
|
|
|
|
namespace HighLevelArchitecture {
|
|
|
|
SocketReader::SocketReader(QObject *parent) : QThread(parent)
|
|
, m_socket(new QTcpSocket(this))
|
|
, m_hlaRequestIdentifier(0)
|
|
.getNGLLogger("SocketReader")),
|
|
m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger()) {
|
|
}
|
|
|
|
bool SocketReader::init(const QString &host, quint16 port, int hlaRequestIdentifier)
|
|
{
|
|
DEBUG_LOG(m_logger) << "SR " << hlaRequestIdentifier << ": Trying to establish connection to " << host << ":" << port;
|
|
|
|
m_hlaRequestIdentifier = hlaRequestIdentifier;
|
|
connect(m_socket, &QTcpSocket::readyRead, this, &SocketReader::onReadyRead);
|
|
connect(m_socket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error),
|
|
this, &SocketReader::onSocketError);
|
|
|
|
m_socket->connectToHost(host, port, QIODevice::ReadOnly);
|
|
return m_socket->waitForConnected(1000);
|
|
}
|
|
|
|
SocketReader::~SocketReader()
|
|
{
|
|
m_socket->disconnectFromHost();
|
|
|
|
if (m_socket->state() != QAbstractSocket::UnconnectedState &&
|
|
!m_socket->waitForDisconnected(1000))
|
|
m_socket->abort();
|
|
|
|
delete m_socket;
|
|
m_socket = 0;
|
|
}
|
|
|
|
void SocketReader::onReadyRead()
|
|
{
|
|
|
|
|
|
|
|
DEBUG_LOG(m_logger) << "SR "<< m_hlaRequestIdentifier << ": Data received, transmitting";
|
|
QByteArray ba(m_socket->readAll());
|
|
TRACE_LOG(m_logger) << "SR "<< m_hlaRequestIdentifier << ": Data contents: " << ba.constData();
|
|
emit newData(ba, m_hlaRequestIdentifier);
|
|
}
|
|
|
|
void SocketReader::stopThread()
|
|
{
|
|
QTimer::singleShot(0, this, SLOT(quit()));
|
|
while (!isFinished())
|
|
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
|
|
deleteLater();
|
|
}
|
|
|
|
void SocketReader::onSocketError(const QAbstractSocket::SocketError &error)
|
|
{
|
|
QString errorValue;
|
|
QMetaObject meta = QAbstractSocket::staticMetaObject;
|
|
for (int i=0; i < meta.enumeratorCount(); ++i) {
|
|
QMetaEnum m = meta.enumerator(i);
|
|
if (QString::fromLatin1(m.name()) == QLatin1String("SocketError")) {
|
|
errorValue = QLatin1String(m.valueToKey(error));
|
|
break;
|
|
}
|
|
}
|
|
|
|
ERROR_LOG(m_logger) << "SR "<< m_hlaRequestIdentifier << ": Socket Error: " << errorValue;
|
|
}
|
|
|
|
}
|