From fad0bc1e621e63f9c23f3a8ae3f8817760ee4ef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 20 Nov 2023 16:05:34 +0100 Subject: [PATCH] Add Qt6 support --- common/slipdataprocessor.cpp | 8 +++ libnymea-remoteproxy/jsonrpc/jsonhandler.cpp | 4 +- libnymea-remoteproxy/jsonrpc/jsonreply.h | 1 + libnymea-remoteproxy/jsonrpc/jsontypes.cpp | 5 +- libnymea-remoteproxy/jsonrpc/jsontypes.h | 1 - .../jsonrpc/tunnelproxyhandler.h | 11 ++- libnymea-remoteproxy/logengine.cpp | 3 +- libnymea-remoteproxy/proxyconfiguration.cpp | 2 +- libnymea-remoteproxy/server/jsonrpcserver.cpp | 69 ++++++++++++++----- libnymea-remoteproxy/server/monitorserver.cpp | 2 +- .../server/tcpsocketserver.cpp | 26 ++++++- .../server/transportclient.cpp | 2 +- .../server/unixsocketserver.cpp | 4 +- .../proxyjsonrpcclient.cpp | 2 +- .../tcpsocketconnection.cpp | 6 ++ monitor/monitorclient.cpp | 2 +- monitor/noninteractivemonitor.cpp | 4 +- monitor/terminalwindow.cpp | 8 +-- monitor/utils.h | 2 +- nymea-remoteproxy.pri | 19 ++++- .../remoteproxyteststunnelproxy.cpp | 6 +- tests/testbase/basetest.cpp | 8 +-- tunnelclient/clientconnection.cpp | 6 +- tunnelclient/serverconnection.cpp | 8 +-- 24 files changed, 151 insertions(+), 58 deletions(-) diff --git a/common/slipdataprocessor.cpp b/common/slipdataprocessor.cpp index 91e9d10..4cb32f5 100644 --- a/common/slipdataprocessor.cpp +++ b/common/slipdataprocessor.cpp @@ -68,7 +68,11 @@ QByteArray SlipDataProcessor::deserializeData(const QByteArray &data) QByteArray SlipDataProcessor::serializeData(const QByteArray &data) { QByteArray serializedData; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + QDataStream stream(&serializedData, QDataStream::WriteOnly); +#else QDataStream stream(&serializedData, QIODevice::WriteOnly); +#endif // stream << static_cast(ProtocolByteEnd); for (int i = 0; i < data.length(); i++) { @@ -110,7 +114,11 @@ SlipDataProcessor::Frame SlipDataProcessor::parseFrame(const QByteArray &data) QByteArray SlipDataProcessor::buildFrame(const Frame &frame) { QByteArray data; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + QDataStream stream(&data, QDataStream::WriteOnly); +#else QDataStream stream(&data, QIODevice::WriteOnly); +#endif stream << frame.socketAddress; for (int i = 0; i < frame.data.size(); i++) { stream << static_cast(frame.data.at(i)); diff --git a/libnymea-remoteproxy/jsonrpc/jsonhandler.cpp b/libnymea-remoteproxy/jsonrpc/jsonhandler.cpp index bdecbcc..2eb571e 100644 --- a/libnymea-remoteproxy/jsonrpc/jsonhandler.cpp +++ b/libnymea-remoteproxy/jsonrpc/jsonhandler.cpp @@ -28,7 +28,7 @@ #include "jsonhandler.h" #include #include -#include +#include #include "jsonreply.h" #include "jsontypes.h" @@ -67,7 +67,7 @@ QVariantMap JsonHandler::introspect(const QMetaMethod::MethodType &type) if (!m_descriptions.contains(method.name()) || !m_params.contains(method.name())) { continue; } - if (QString(method.name()).contains(QRegExp("^[A-Z]"))) { + if (QString(method.name()).contains(QRegularExpression("^[A-Z]"))) { QVariantMap methodData; methodData.insert("description", m_descriptions.value(method.name())); methodData.insert("params", m_params.value(method.name())); diff --git a/libnymea-remoteproxy/jsonrpc/jsonreply.h b/libnymea-remoteproxy/jsonrpc/jsonreply.h index 01977ca..bf582a1 100644 --- a/libnymea-remoteproxy/jsonrpc/jsonreply.h +++ b/libnymea-remoteproxy/jsonrpc/jsonreply.h @@ -39,6 +39,7 @@ namespace remoteproxy { class JsonReply: public QObject { Q_OBJECT + public: enum Type { TypeSync, diff --git a/libnymea-remoteproxy/jsonrpc/jsontypes.cpp b/libnymea-remoteproxy/jsonrpc/jsontypes.cpp index 94e6df1..57f6cf6 100644 --- a/libnymea-remoteproxy/jsonrpc/jsontypes.cpp +++ b/libnymea-remoteproxy/jsonrpc/jsontypes.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include "loggingcategories.h" @@ -72,7 +73,7 @@ QPair JsonTypes::validateMap(const QVariantMap &templateMap, cons // Make sure all values defined in the template are around foreach (const QString &key, templateMap.keys()) { QString strippedKey = key; - strippedKey.remove(QRegExp("^o:")); + strippedKey.remove(QRegularExpression("^o:")); if (!key.startsWith("o:") && !map.contains(strippedKey)) { qCWarning(dcJsonRpc()) << "*** missing key" << key; qCWarning(dcJsonRpc()) << "Expected: " << templateMap; @@ -274,7 +275,7 @@ QString JsonTypes::basicTypeToString(const QVariant::Type &type) QPair JsonTypes::report(bool status, const QString &message) { - return qMakePair(status, message); + return QPair(status, message); } QVariantList JsonTypes::enumToStrings(const QMetaObject &metaObject, const QString &enumName) diff --git a/libnymea-remoteproxy/jsonrpc/jsontypes.h b/libnymea-remoteproxy/jsonrpc/jsontypes.h index 812fefc..9be5c21 100644 --- a/libnymea-remoteproxy/jsonrpc/jsontypes.h +++ b/libnymea-remoteproxy/jsonrpc/jsontypes.h @@ -70,7 +70,6 @@ namespace remoteproxy { class JsonTypes { Q_GADGET - Q_ENUMS(BasicType) public: enum BasicType { diff --git a/libnymea-remoteproxy/jsonrpc/tunnelproxyhandler.h b/libnymea-remoteproxy/jsonrpc/tunnelproxyhandler.h index 707e030..59bf779 100644 --- a/libnymea-remoteproxy/jsonrpc/tunnelproxyhandler.h +++ b/libnymea-remoteproxy/jsonrpc/tunnelproxyhandler.h @@ -45,6 +45,15 @@ public: QString name() const override; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + // Server + Q_INVOKABLE remoteproxy::JsonReply *RegisterServer(const QVariantMap ¶ms, TransportClient *transportClient); + Q_INVOKABLE remoteproxy::JsonReply *DisconnectClient(const QVariantMap ¶ms, TransportClient *transportClient); + Q_INVOKABLE remoteproxy::JsonReply *Ping(const QVariantMap ¶ms, TransportClient *transportClient); + + // Client + Q_INVOKABLE remoteproxy::JsonReply *RegisterClient(const QVariantMap ¶ms, TransportClient *transportClient); +#else // Server Q_INVOKABLE JsonReply *RegisterServer(const QVariantMap ¶ms, TransportClient *transportClient); Q_INVOKABLE JsonReply *DisconnectClient(const QVariantMap ¶ms, TransportClient *transportClient); @@ -52,7 +61,7 @@ public: // Client Q_INVOKABLE JsonReply *RegisterClient(const QVariantMap ¶ms, TransportClient *transportClient); - +#endif signals: void ClientConnected(const QVariantMap ¶ms, TransportClient *transportClient); void ClientDisconnected(const QVariantMap ¶ms, TransportClient *transportClient); diff --git a/libnymea-remoteproxy/logengine.cpp b/libnymea-remoteproxy/logengine.cpp index 73f5a71..0bfbeea 100644 --- a/libnymea-remoteproxy/logengine.cpp +++ b/libnymea-remoteproxy/logengine.cpp @@ -29,6 +29,7 @@ #include "loggingcategories.h" #include +#include namespace remoteproxy { @@ -101,7 +102,7 @@ void LogEngine::rotateLogs() QString LogEngine::createTimestamp() { - return QString::number(QDateTime::currentDateTimeUtc().toTime_t()); + return QString::number(QDateTime::currentDateTimeUtc().toSecsSinceEpoch()); } void LogEngine::enable() diff --git a/libnymea-remoteproxy/proxyconfiguration.cpp b/libnymea-remoteproxy/proxyconfiguration.cpp index 6c4afc0..f85ca9a 100644 --- a/libnymea-remoteproxy/proxyconfiguration.cpp +++ b/libnymea-remoteproxy/proxyconfiguration.cpp @@ -331,7 +331,7 @@ QDebug operator<<(QDebug debug, ProxyConfiguration *configuration) debug.nospace() << " Locality name:" << configuration->sslConfiguration().localCertificate().issuerInfo(QSslCertificate::LocalityName) << "\n"; debug.nospace() << " State/Province:" << configuration->sslConfiguration().localCertificate().issuerInfo(QSslCertificate::StateOrProvinceName) << "\n"; debug.nospace() << " Email address:" << configuration->sslConfiguration().localCertificate().issuerInfo(QSslCertificate::EmailAddress) << "\n"; - debug.nospace() << "UnixSocketServer Proxy" << endl; + debug.nospace() << "UnixSocketServer Proxy" << "\n"; debug.nospace() << " - Filename:" << configuration->unixSocketFileName() << "\n"; debug.nospace() << "WebSocketServer TunnelProxy" << "\n"; debug.nospace() << " - Host:" << configuration->webSocketServerTunnelProxyHost().toString() << "\n"; diff --git a/libnymea-remoteproxy/server/jsonrpcserver.cpp b/libnymea-remoteproxy/server/jsonrpcserver.cpp index 6cc8c5e..3aa83a8 100644 --- a/libnymea-remoteproxy/server/jsonrpcserver.cpp +++ b/libnymea-remoteproxy/server/jsonrpcserver.cpp @@ -42,6 +42,9 @@ namespace remoteproxy { JsonRpcServer::JsonRpcServer(QObject *parent) : JsonHandler(parent) { + + //qRegisterMetaType(); + // Methods QVariantMap params; QVariantMap returns; @@ -66,9 +69,9 @@ JsonRpcServer::JsonRpcServer(QObject *parent) : // Notifications params.clear(); returns.clear(); setDescription("TunnelEstablished", "Emitted whenever the tunnel has been established successfully. " - "This is the last message from the remote proxy server! Any following data will be from " - "the other tunnel client until the connection will be closed. The parameter contain some information " - "about the other tunnel client."); + "This is the last message from the remote proxy server! Any following data will be from " + "the other tunnel client until the connection will be closed. The parameter contain some information " + "about the other tunnel client."); params.insert("uuid", JsonTypes::basicTypeToString(JsonTypes::String)); params.insert("name", JsonTypes::basicTypeToString(JsonTypes::String)); setParams("TunnelEstablished", params); @@ -114,14 +117,20 @@ JsonReply *JsonRpcServer::Introspect(const QVariantMap ¶ms, TransportClient QVariantMap methods; foreach (JsonHandler *handler, m_handlers) { - methods.unite(handler->introspect(QMetaMethod::Method)); + QVariantMap handlerMethods = handler->introspect(QMetaMethod::Method); + foreach (const QString &method, handlerMethods.keys()) { + methods.insert(method, handlerMethods.value(method)); + } } data.insert("methods", methods); QVariantMap signalsMap; foreach (JsonHandler *handler, m_handlers) { - signalsMap.unite(handler->introspect(QMetaMethod::Signal)); + QVariantMap handlerSignals = handler->introspect(QMetaMethod::Signal); + foreach (const QString &signalName, handlerSignals.keys()) { + signalsMap.insert(signalName, handlerSignals.value(signalName)); + } } data.insert("notifications", signalsMap); @@ -141,10 +150,10 @@ void JsonRpcServer::sendResponse(TransportClient *client, int commandId, const Q if (client->slipEnabled()) { SlipDataProcessor::Frame frame; frame.socketAddress = 0x0000; - frame.data = data + '\n'; + frame.data = data + "\n"; client->sendData(SlipDataProcessor::serializeData(SlipDataProcessor::buildFrame(frame))); } else { - client->sendData(data + '\n'); + client->sendData(data + "\n"); } } @@ -160,10 +169,10 @@ void JsonRpcServer::sendErrorResponse(TransportClient *client, int commandId, co if (client->slipEnabled()) { SlipDataProcessor::Frame frame; frame.socketAddress = 0x0000; - frame.data = data + '\n'; + frame.data = data + "\n"; client->sendData(SlipDataProcessor::serializeData(SlipDataProcessor::buildFrame(frame))); } else { - client->sendData(data + '\n'); + client->sendData(data + "\n"); } } @@ -172,9 +181,9 @@ QString JsonRpcServer::formatAssertion(const QString &targetNamespace, const QSt QJsonDocument doc = QJsonDocument::fromVariant(handler->introspect(QMetaMethod::Method).value(targetNamespace + "." + method)); QJsonDocument doc2 = QJsonDocument::fromVariant(data); return QString("\nMethod: %1\nTemplate: %2\nValue: %3") - .arg(targetNamespace + "." + method) - .arg(QString(doc.toJson(QJsonDocument::Indented))) - .arg(QString(doc2.toJson(QJsonDocument::Indented))); + .arg(targetNamespace + "." + method) + .arg(QString(doc.toJson(QJsonDocument::Indented))) + .arg(QString(doc2.toJson(QJsonDocument::Indented))); } void JsonRpcServer::registerHandler(JsonHandler *handler) @@ -248,14 +257,38 @@ void JsonRpcServer::processDataPacket(TransportClient *transportClient, const QB return; } - JsonReply *reply; - QMetaObject::invokeMethod(handler, method.toLatin1().data(), Q_RETURN_ARG(JsonReply*, reply), Q_ARG(QVariantMap, params), Q_ARG(TransportClient*, transportClient)); + + JsonReply *reply = nullptr; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + bool invokedSuccessfully = QMetaObject::invokeMethod(handler, method.toLatin1().constData(), + Qt::DirectConnection, + qReturnArg(reply), + params, transportClient); + + if (!invokedSuccessfully) { + qCWarning(dcJsonRpc()) << "Failed to invoke method" << handler << method; + } +#else + QMetaObject::invokeMethod(handler, method.toLatin1().constData(), + Qt::DirectConnection, + Q_RETURN_ARG(JsonReply *, reply), + Q_ARG(QVariantMap, params), + Q_ARG(TransportClient *, transportClient)); +#endif + + + if (!reply) { + qCWarning(dcJsonRpc()) << "Internal error. No reply, could not invoke method."; + return; + + } + if (reply->type() == JsonReply::TypeAsync) { m_asyncReplies.insert(reply, transportClient); reply->setClientId(transportClient->clientId()); reply->setCommandId(commandId); - connect(reply, &JsonReply::finished, this, &JsonRpcServer::asyncReplyFinished); + connect(reply, &remoteproxy::JsonReply::finished, this, &JsonRpcServer::asyncReplyFinished); reply->startWait(); } else { Q_ASSERT_X((targetNamespace == "RemoteProxy" && method == "Introspect") || handler->validateReturns(method, reply->data()).first @@ -295,7 +328,7 @@ void JsonRpcServer::asyncReplyFinished() if (!reply->timedOut()) { Q_ASSERT_X(reply->handler()->validateReturns(reply->method(), reply->data()).first ,"validating return value", formatAssertion(reply->handler()->name(), - reply->method(), reply->handler(), reply->data()).toLatin1().data()); + reply->method(), reply->handler(), reply->data()).toLatin1().data()); QPair returnValidation = reply->handler()->validateReturns(reply->method(), reply->data()); if (!returnValidation.first) { @@ -385,12 +418,12 @@ void JsonRpcServer::sendNotification(const QString &nameSpace, const QString &me if (transportClient->slipEnabled()) { SlipDataProcessor::Frame frame; frame.socketAddress = 0x0000; - frame.data = data + '\n'; + frame.data = data + "\n"; qCDebug(dcJsonRpcTraffic()) << "Sending notification frame:" <sendData(SlipDataProcessor::serializeData(SlipDataProcessor::buildFrame(frame))); } else { qCDebug(dcJsonRpcTraffic()) << "Sending notification:" << data; - transportClient->sendData(data + '\n'); + transportClient->sendData(data + "\n"); } } diff --git a/libnymea-remoteproxy/server/monitorserver.cpp b/libnymea-remoteproxy/server/monitorserver.cpp index e12d0d9..9ba8fc4 100644 --- a/libnymea-remoteproxy/server/monitorserver.cpp +++ b/libnymea-remoteproxy/server/monitorserver.cpp @@ -56,7 +56,7 @@ bool MonitorServer::running() const void MonitorServer::sendMonitorData(QLocalSocket *clientConnection, const QVariantMap &dataMap) { - QByteArray data = QJsonDocument::fromVariant(dataMap).toJson(QJsonDocument::Compact) + '\n'; + QByteArray data = QJsonDocument::fromVariant(dataMap).toJson(QJsonDocument::Compact) + "\n"; qCDebug(dcMonitorServer()) << "Sending monitor data" << qUtf8Printable(data); clientConnection->write(data); clientConnection->flush(); diff --git a/libnymea-remoteproxy/server/tcpsocketserver.cpp b/libnymea-remoteproxy/server/tcpsocketserver.cpp index a6d60cb..7dc7ad2 100644 --- a/libnymea-remoteproxy/server/tcpsocketserver.cpp +++ b/libnymea-remoteproxy/server/tcpsocketserver.cpp @@ -45,7 +45,7 @@ TcpSocketServer::~TcpSocketServer() void TcpSocketServer::sendData(const QUuid &clientId, const QByteArray &data) { - QTcpSocket *client = m_clientList.value(clientId); + QSslSocket *client = m_clientList.value(clientId); if (!client) { qCWarning(dcTcpSocketServer()) << "Client" << clientId << "unknown to this transport"; return; @@ -59,7 +59,7 @@ void TcpSocketServer::sendData(const QUuid &clientId, const QByteArray &data) void TcpSocketServer::killClientConnection(const QUuid &clientId, const QString &killReason) { - QTcpSocket *client = m_clientList.value(clientId); + QSslSocket *client = m_clientList.value(clientId); if (!client) { qCWarning(dcTcpSocketServer()) << "Could not kill connection with id" << clientId.toString() << "with reason" << killReason << "because there is no socket with this id."; return; @@ -193,10 +193,17 @@ void SslServer::incomingConnection(qintptr socketDescriptor) qCDebug(dcTcpSocketServer()) << "Client socket disconnected:" << sslSocket << sslSocket->peerAddress().toString();; if (m_sslEnabled) { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + // FIXME: the SSL socket behavior seems to have changed in Qt6, + // we need to emit this signal in any case, otherwise the upper layer assumes the + // socket object still exists, even if we only hand over to upper layers on encypted. + emit socketDisconnected(sslSocket); +#else // Only tell the upper layer the client has disconnecred if it was encrypted if (sslSocket->isEncrypted()) { emit socketDisconnected(sslSocket); } +#endif } else { emit socketDisconnected(sslSocket); } @@ -221,6 +228,19 @@ void SslServer::incomingConnection(qintptr socketDescriptor) emit socketConnected(sslSocket); }); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + connect(sslSocket, &QSslSocket::errorOccurred, this, [sslSocket](QAbstractSocket::SocketError error){ + qCWarning(dcTcpSocketServer()) << "Socket error occurred on" << sslSocket << error << sslSocket->errorString() << "Explicitly closing the client connection."; + sslSocket->close(); + }); + + connect(sslSocket, &QSslSocket::sslErrors, this, [sslSocket](const QList &errors) { + qCWarning(dcTcpSocketServer()) << "SSL error occurred in the client connection" << sslSocket; + foreach (const QSslError &error, errors) { + qCWarning(dcTcpSocketServer()) << "--> SSL error:" << error.error() << error.errorString(); + } + }); +#else typedef void (QAbstractSocket:: *errorSignal)(QAbstractSocket::SocketError); connect(sslSocket, static_cast(&QAbstractSocket::error), this, [sslSocket](QAbstractSocket::SocketError error){ qCWarning(dcTcpSocketServer()) << "Socket error occurred on" << sslSocket << error << sslSocket->errorString() << "Explicitly closing the client connection."; @@ -235,6 +255,8 @@ void SslServer::incomingConnection(qintptr socketDescriptor) } }); +#endif + if (m_sslEnabled) { qCDebug(dcTcpSocketServer()) << "Starting SSL encryption for" << sslSocket; sslSocket->setSslConfiguration(m_config); diff --git a/libnymea-remoteproxy/server/transportclient.cpp b/libnymea-remoteproxy/server/transportclient.cpp index 0831d83..35c68e5 100644 --- a/libnymea-remoteproxy/server/transportclient.cpp +++ b/libnymea-remoteproxy/server/transportclient.cpp @@ -38,7 +38,7 @@ TransportClient::TransportClient(TransportInterface *interface, const QUuid &cli m_clientId(clientId), m_peerAddress(address) { - m_creationTimeStamp = QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000; + m_creationTimeStamp = QDateTime::currentDateTime().toSecsSinceEpoch(); } QUuid TransportClient::clientId() const diff --git a/libnymea-remoteproxy/server/unixsocketserver.cpp b/libnymea-remoteproxy/server/unixsocketserver.cpp index 04aa3f8..d8c917b 100644 --- a/libnymea-remoteproxy/server/unixsocketserver.cpp +++ b/libnymea-remoteproxy/server/unixsocketserver.cpp @@ -134,14 +134,14 @@ void UnixSocketServer::onClientConnected() qCDebug(dcUnixSocketServer()) << "New client connected" << clientId.toString(); m_clientList.insert(clientId, client); - connect(client, &QLocalSocket::disconnected, this, [=](){ + connect(client, &QLocalSocket::disconnected, this, [this, client](){ QUuid clientId = m_clientList.key(client); qCDebug(dcUnixSocketServer()) << "Client disconnected:" << clientId.toString(); if (m_clientList.take(clientId)) { emit clientDisconnected(clientId); } }); - connect(client, &QLocalSocket::readyRead, this, [=](){ + connect(client, &QLocalSocket::readyRead, this, [this, client, clientId](){ QByteArray data = client->readAll(); qCDebug(dcUnixSocketServerTraffic()) << "Incomming data from" << clientId.toString() << data; emit dataAvailable(clientId, data); diff --git a/libnymea-remoteproxyclient/proxyjsonrpcclient.cpp b/libnymea-remoteproxyclient/proxyjsonrpcclient.cpp index 4170548..aa05329 100644 --- a/libnymea-remoteproxyclient/proxyjsonrpcclient.cpp +++ b/libnymea-remoteproxyclient/proxyjsonrpcclient.cpp @@ -105,7 +105,7 @@ JsonReply *JsonRpcClient::callPing(uint timestamp) void JsonRpcClient::sendRequest(const QVariantMap &request, bool slipEnabled) { - QByteArray data = QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact) + '\n'; + QByteArray data = QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact) + "\n"; if (slipEnabled) { SlipDataProcessor::Frame frame; diff --git a/libnymea-remoteproxyclient/tcpsocketconnection.cpp b/libnymea-remoteproxyclient/tcpsocketconnection.cpp index 75ef006..36d7cb0 100644 --- a/libnymea-remoteproxyclient/tcpsocketconnection.cpp +++ b/libnymea-remoteproxyclient/tcpsocketconnection.cpp @@ -41,11 +41,17 @@ TcpSocketConnection::TcpSocketConnection(QObject *parent) : QObject::connect(m_tcpSocket, &QSslSocket::readyRead, this, &TcpSocketConnection::onReadyRead); QObject::connect(m_tcpSocket, &QSslSocket::stateChanged, this, &TcpSocketConnection::onStateChanged); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + connect(m_tcpSocket, &QSslSocket::errorOccurred, this, &TcpSocketConnection::onError); + connect(m_tcpSocket, &QSslSocket::sslErrors, this, &TcpSocketConnection::sslErrors); +#else typedef void (QSslSocket:: *errorSignal)(QAbstractSocket::SocketError); QObject::connect(m_tcpSocket, static_cast(&QSslSocket::error), this, &TcpSocketConnection::onError); typedef void (QSslSocket:: *sslErrorsSignal)(const QList &); QObject::connect(m_tcpSocket, static_cast(&QSslSocket::sslErrors), this, &TcpSocketConnection::sslErrors); +#endif + } TcpSocketConnection::~TcpSocketConnection() diff --git a/monitor/monitorclient.cpp b/monitor/monitorclient.cpp index 1e735e9..fd04f06 100644 --- a/monitor/monitorclient.cpp +++ b/monitor/monitorclient.cpp @@ -87,7 +87,7 @@ void MonitorClient::onDisconnected() void MonitorClient::onReadyRead() { - // Note: the server sends the data compact with '\n' at the end + // Note: the server sends the data compact with "\n" at the end QByteArray data = m_socket->readAll(); int index = data.indexOf("}\n"); diff --git a/monitor/noninteractivemonitor.cpp b/monitor/noninteractivemonitor.cpp index 6fe03f8..e8f6625 100644 --- a/monitor/noninteractivemonitor.cpp +++ b/monitor/noninteractivemonitor.cpp @@ -69,7 +69,7 @@ void NonInteractiveMonitor::onConnected() QVariantList clientList = serverMap.value("clientConnections").toList(); // Server line - QString serverConnectionTime = QDateTime::fromTime_t(serverMap.value("timestamp").toUInt()).toString("dd.MM.yyyy hh:mm:ss"); + QString serverConnectionTime = QDateTime::fromMSecsSinceEpoch(serverMap.value("timestamp").toLongLong() * 1000).toString("dd.MM.yyyy hh:mm:ss"); QString serverLinePrint; if (clientList.isEmpty()) { serverLinePrint.prepend("├──"); @@ -97,7 +97,7 @@ void NonInteractiveMonitor::onConnected() } clientLinePrint += QString("%1 | %2 | %3 RX: %4 TX: %5 | %6") - .arg(QDateTime::fromTime_t(clientMap.value("timestamp").toUInt()).toString("dd.MM.yyyy hh:mm:ss")) + .arg(QDateTime::fromMSecsSinceEpoch(clientMap.value("timestamp").toLongLong() * 1000).toString("dd.MM.yyyy hh:mm:ss")) .arg(clientMap.value("clientUuid").toString()) .arg(clientMap.value("address").toString(), - 15) .arg(Utils::humanReadableTraffic(serverMap.value("rxDataCount").toInt()), - 9) diff --git a/monitor/terminalwindow.cpp b/monitor/terminalwindow.cpp index f6836a3..0a0b001 100644 --- a/monitor/terminalwindow.cpp +++ b/monitor/terminalwindow.cpp @@ -195,7 +195,7 @@ void TerminalWindow::paintContentClients() QVariantMap clientMap = clientVariant.toMap(); uint timeStamp = clientMap.value("timestamp").toUInt(); - QString clientConnectionTime = QDateTime::fromTime_t(timeStamp).toString("dd.MM.yyyy hh:mm:ss"); + QString clientConnectionTime = QDateTime::fromMSecsSinceEpoch(timeStamp * 1000).toString("dd.MM.yyyy hh:mm:ss"); int rxDataCountBytes = clientMap.value("rxDataCount").toInt(); int txDataCountBytes = clientMap.value("txDataCount").toInt(); @@ -226,7 +226,7 @@ void TerminalWindow::paintContentTunnels() // Tunnel time uint timeStamp = tunnelMap.value("timestamp").toUInt(); - QString tunnelConnectionTime = QDateTime::fromTime_t(timeStamp).toString("dd.MM.yyyy hh:mm:ss"); + QString tunnelConnectionTime = QDateTime::fromMSecsSinceEpoch(timeStamp * 1000).toString("dd.MM.yyyy hh:mm:ss"); QString tunnelPrint = QString("%1 | %2 | %3 | %4 | %5 (%6) <---> %7 (%8)") .arg(tunnelConnectionTime) @@ -252,7 +252,7 @@ void TerminalWindow::paintContentTunnelProxy() foreach (const QVariant &serverVariant, tunnelProxyMap.value("tunnelConnections").toList()) { QVariantMap serverMap = serverVariant.toMap(); uint timeStamp = serverMap.value("timestamp").toUInt(); - QString serverConnectionTime = QDateTime::fromTime_t(timeStamp).toString("dd.MM.yyyy hh:mm:ss"); + QString serverConnectionTime = QDateTime::fromMSecsSinceEpoch(timeStamp * 1000).toString("dd.MM.yyyy hh:mm:ss"); int rxDataCountBytes = serverMap.value("rxDataCount").toInt(); int txDataCountBytes = serverMap.value("txDataCount").toInt(); QString serverLinePrint = QString("%1 | %2 | RX: %3 | TX: %4 | %5") @@ -287,7 +287,7 @@ void TerminalWindow::paintContentTunnelProxy() mvwaddch(m_contentWindow, i, 5, ACS_HLINE); QString clientLinePrint = QString("%1 | %2 | RX: %3 | TX: %4 | %5") - .arg(QDateTime::fromTime_t(clientMap.value("timestamp").toUInt()).toString("dd.MM.yyyy hh:mm:ss")) + .arg(QDateTime::fromMSecsSinceEpoch(clientMap.value("timestamp").toULongLong() * 1000).toString("dd.MM.yyyy hh:mm:ss")) .arg(clientMap.value("address").toString(), - 16) .arg(Utils::humanReadableTraffic(clientMap.value("rxDataCount").toInt()), - 10) .arg(Utils::humanReadableTraffic(clientMap.value("txDataCount").toInt()), - 10) diff --git a/monitor/utils.h b/monitor/utils.h index 93cbcd5..2ab1d20 100644 --- a/monitor/utils.h +++ b/monitor/utils.h @@ -43,7 +43,7 @@ public: Utils() = default; inline static QString getDurationString(uint timestamp) { - uint duration = QDateTime::currentDateTimeUtc().toTime_t() - timestamp; + uint duration = QDateTime::currentDateTimeUtc().toSecsSinceEpoch() - timestamp; int seconds = static_cast(duration % 60); duration /= 60; int minutes = static_cast(duration % 60); diff --git a/nymea-remoteproxy.pri b/nymea-remoteproxy.pri index be3350e..f5be407 100644 --- a/nymea-remoteproxy.pri +++ b/nymea-remoteproxy.pri @@ -1,10 +1,22 @@ QT *= network websockets QT -= gui -CONFIG += c++11 console +CONFIG += console -QMAKE_CXXFLAGS *= -Werror -std=c++11 -g -Wno-deprecated-declarations -QMAKE_LFLAGS *= -std=c++11 +greaterThan(QT_MAJOR_VERSION, 5) { + message("Building using Qt6 support") + CONFIG *= c++17 + QMAKE_LFLAGS *= -std=c++17 + QMAKE_CXXFLAGS *= -std=c++17 +} else { + message("Building using Qt5 support") + CONFIG *= c++11 + QMAKE_LFLAGS *= -std=c++11 + QMAKE_CXXFLAGS *= -std=c++11 + DEFINES += QT_DISABLE_DEPRECATED_UP_TO=0x050F00 +} + +QMAKE_CXXFLAGS *= -Werror -g -Wno-deprecated-declarations top_srcdir=$$PWD top_builddir=$$shadowed($$PWD) @@ -19,6 +31,7 @@ coverage { MOC_DIR = LIBS += -lgcov + QMAKE_CXXFLAGS += --coverage QMAKE_LDFLAGS += --coverage diff --git a/tests/test-tunnelproxy/remoteproxyteststunnelproxy.cpp b/tests/test-tunnelproxy/remoteproxyteststunnelproxy.cpp index f28f287..8a44e54 100644 --- a/tests/test-tunnelproxy/remoteproxyteststunnelproxy.cpp +++ b/tests/test-tunnelproxy/remoteproxyteststunnelproxy.cpp @@ -171,11 +171,9 @@ void RemoteProxyTestsTunnelProxy::getHello() void RemoteProxyTestsTunnelProxy::monitorServer() { - // Start the server startServer(); - // ** Create the server ** QString serverName = "nymea server"; QUuid serverUuid = QUuid::createUuid(); @@ -188,7 +186,9 @@ void RemoteProxyTestsTunnelProxy::monitorServer() tunnelProxyServer->startServer(m_serverUrlTunnelProxyTcp); QSignalSpy serverRunningSpy(tunnelProxyServer, &TunnelProxySocketServer::runningChanged); - serverRunningSpy.wait(); + if (serverRunningSpy.isEmpty()) + serverRunningSpy.wait(); + QVERIFY(serverRunningSpy.count() == 1); QList arguments = serverRunningSpy.takeFirst(); QVERIFY(arguments.at(0).toBool() == true); diff --git a/tests/testbase/basetest.cpp b/tests/testbase/basetest.cpp index 6594992..381a329 100644 --- a/tests/testbase/basetest.cpp +++ b/tests/testbase/basetest.cpp @@ -241,7 +241,7 @@ QVariant BaseTest::invokeTcpSocketTunnelProxyApiCall(const QString &method, cons } QSignalSpy dataSpy(socket, &QSslSocket::readyRead); - socket->write(jsonDoc.toJson(QJsonDocument::Compact) + '\n'); + socket->write(jsonDoc.toJson(QJsonDocument::Compact) + "\n"); dataSpy.wait(); if (dataSpy.count() != 1) { qWarning() << "No data received"; @@ -292,7 +292,7 @@ QVariant BaseTest::injectTcpSocketTunnelProxyData(const QByteArray &data) } QSignalSpy dataSpy(socket, &QSslSocket::readyRead); - socket->write(data + '\n'); + socket->write(data + "\n"); dataSpy.wait(); if (dataSpy.count() != 1) { qWarning() << "No data received"; @@ -345,7 +345,7 @@ QPair BaseTest::invokeTcpSocketTunnelProxyApiCallPersist request.insert("method", method); request.insert("params", params); QJsonDocument jsonDoc = QJsonDocument::fromVariant(request); - QByteArray payload = jsonDoc.toJson(QJsonDocument::Compact) + '\n'; + QByteArray payload = jsonDoc.toJson(QJsonDocument::Compact) + "\n"; QSignalSpy dataSpy(socket, &QSslSocket::readyRead); @@ -439,7 +439,7 @@ QPair BaseTest::invokeWebSocketTunnelProxyApiCallPersist request.insert("method", method); request.insert("params", params); QJsonDocument jsonDoc = QJsonDocument::fromVariant(request); - QByteArray payload = jsonDoc.toJson(QJsonDocument::Compact) + '\n'; + QByteArray payload = jsonDoc.toJson(QJsonDocument::Compact) + "\n"; if (socket->state() != QAbstractSocket::ConnectedState) { diff --git a/tunnelclient/clientconnection.cpp b/tunnelclient/clientconnection.cpp index 22fb183..e4f10d5 100644 --- a/tunnelclient/clientconnection.cpp +++ b/tunnelclient/clientconnection.cpp @@ -11,7 +11,7 @@ ClientConnection::ClientConnection(const QUrl &serverUrl, const QString &name, c { m_remoteConnection = new TunnelProxyRemoteConnection(m_uuid, m_name); m_timer.setSingleShot(true); - connect(&m_timer, &QTimer::timeout, this, [=](){ + connect(&m_timer, &QTimer::timeout, this, [this](){ if (m_remoteConnection->remoteConnected()) { m_remoteConnection->sendData(generateRandomString(100).toUtf8()); m_timer.start(1000); @@ -45,7 +45,7 @@ ClientConnection::ClientConnection(const QUrl &serverUrl, const QString &name, c qWarning() << "Socket error occurred" << error; }); - connect(m_remoteConnection, &TunnelProxyRemoteConnection::sslErrors, this, [=](const QList &errors){ + connect(m_remoteConnection, &TunnelProxyRemoteConnection::sslErrors, this, [this](const QList &errors){ if (m_insecure) { m_remoteConnection->ignoreSslErrors(errors); } else { @@ -67,7 +67,7 @@ QString ClientConnection::generateRandomString(uint length) const const QString possibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); QString randomString; for(uint i = 0; i < length; i++) { - randomString.append(possibleCharacters.at(qrand() % possibleCharacters.length())); + randomString.append(possibleCharacters.at(std::rand() % possibleCharacters.length())); } return randomString; } diff --git a/tunnelclient/serverconnection.cpp b/tunnelclient/serverconnection.cpp index 338607c..2144fb7 100644 --- a/tunnelclient/serverconnection.cpp +++ b/tunnelclient/serverconnection.cpp @@ -11,7 +11,7 @@ ServerConnection::ServerConnection(const QUrl &serverUrl, const QString &name, c m_socketServer = new TunnelProxySocketServer(m_uuid, m_name, this); - connect(m_socketServer, &TunnelProxySocketServer::clientConnected, this, [=](TunnelProxySocket *tunnelProxySocket){ + connect(m_socketServer, &TunnelProxySocketServer::clientConnected, this, [this](TunnelProxySocket *tunnelProxySocket){ qDebug() << "[+] Client connected" << tunnelProxySocket; if (m_echo) { connect(tunnelProxySocket, &TunnelProxySocket::dataReceived, m_socketServer, [tunnelProxySocket](const QByteArray &data){ @@ -20,18 +20,18 @@ ServerConnection::ServerConnection(const QUrl &serverUrl, const QString &name, c } }); - connect(m_socketServer, &TunnelProxySocketServer::clientDisconnected, this, [=](TunnelProxySocket *tunnelProxySocket){ + connect(m_socketServer, &TunnelProxySocketServer::clientDisconnected, this, [](TunnelProxySocket *tunnelProxySocket){ qDebug() << "[-] Client disconnected" << tunnelProxySocket; }); - connect(m_socketServer, &TunnelProxySocketServer::runningChanged, this, [=](bool running){ + connect(m_socketServer, &TunnelProxySocketServer::runningChanged, this, [this](bool running){ if (running) { qDebug() << "Connected with" << m_socketServer->remoteProxyServer() << m_socketServer->remoteProxyServerName() << m_socketServer->remoteProxyServerVersion() << m_socketServer->remoteProxyApiVersion(); } qDebug() << "--> The tunnel proxy server is" << (running ? "running and listening for incoming connections" : "not running any more"); }); - connect(m_socketServer, &TunnelProxySocketServer::sslErrors, this, [=](const QList &errors){ + connect(m_socketServer, &TunnelProxySocketServer::sslErrors, this, [this](const QList &errors){ if (m_insecure) { m_socketServer->ignoreSslErrors(errors); } else {