From b0d37265d0eaafeb67badff9044a2aa2e97c008c Mon Sep 17 00:00:00 2001 From: wuhai Date: Tue, 21 Nov 2023 11:59:54 +0800 Subject: [PATCH] chore: update files of project --- src/common/common/sakcompatibility.h | 34 +++-- src/log/log/saklog.cc | 7 +- src/tools/tools/saktcpclienttool.cc | 8 +- src/tools/tools/saktcpservertool.cc | 2 +- src/tools/tools/sakudpclienttool.cc | 2 +- src/tools/tools/sakudpservertool.cc | 2 +- src/tools/tools/sakwebsocketclienttool.cc | 168 +++++++++++----------- src/tools/tools/sakwebsocketclienttool.h | 22 ++- 8 files changed, 124 insertions(+), 121 deletions(-) diff --git a/src/common/common/sakcompatibility.h b/src/common/common/sakcompatibility.h index 42b72019..dec1f133 100644 --- a/src/common/common/sakcompatibility.h +++ b/src/common/common/sakcompatibility.h @@ -1,22 +1,38 @@ -/******************************************************************************* +/*************************************************************************************************** * Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved. * - * The file is encoded using "utf8 with bom", it is a part - * of QtSwissArmyKnife project. + * The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project. * - * QtSwissArmyKnife is licensed according to the terms in - * the file LICENCE in the root of the source code directory. - ******************************************************************************/ + * QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source + * code directory. + **************************************************************************************************/ #ifndef SAKCOMPATIBILITY_H #define SAKCOMPATIBILITY_H +#include #include #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) -#define SAK_SOCKET_ERROR &QAbstractSocket::errorOccurred +#define SAK_SIG_SOCKETERROROCCURRED &QAbstractSocket::errorOccurred #else -#define SAK_SOCKET_ERROR static_cast(&QAbstractSocket::error) +#define SAK_SOCKET_ERROR \ + static_cast(&QAbstractSocket::error) #endif +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) +#define SAK_SIG_WEBSOCKETERROROCCURRED &QWebSocket::errorOccurred +#else +#define SAK_SIG_WEBSOCKETERROROCCURRED \ + static_cast(QWebSocket::SocketError) +#endif + +static qint64 sakBirthTimeOfFile(const QFileInfo &info) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) + return info.birthTime().toMSecsSinceEpoch(); +#else + return info.created().toMSecsSinceEpoch(); +#endif +} + #endif // SAKCOMPATIBILITY_H diff --git a/src/log/log/saklog.cc b/src/log/log/saklog.cc index 095f428d..7f0b58cd 100644 --- a/src/log/log/saklog.cc +++ b/src/log/log/saklog.cc @@ -17,6 +17,7 @@ #include #include +#include "sakcompatibility.h" #include "saksettings.h" QVector SAKLog::mLogContextVector; @@ -429,11 +430,7 @@ void SAKLog::clearLog() QDir dir(logPath()); QFileInfoList infoList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot); for (auto& info : infoList) { -#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) - qint64 dateTime = info.birthTime().toMSecsSinceEpoch(); -#else - qint64 dateTime = info.created().toMSecsSinceEpoch(); -#endif + qint64 dateTime = sakBirthTimeOfFile(info); mParametersMutex.lock(); int logLifeCycle = mParameters.logLifeCycle; mParametersMutex.unlock(); diff --git a/src/tools/tools/saktcpclienttool.cc b/src/tools/tools/saktcpclienttool.cc index 20fdff12..cae17e07 100644 --- a/src/tools/tools/saktcpclienttool.cc +++ b/src/tools/tools/saktcpclienttool.cc @@ -38,10 +38,10 @@ bool SAKTcpClientTool::initialize(QString& errStr) { outputMessage(QtInfoMsg, "Client address and port: " + mBindingIpPort); emit bindingIpPortChanged(); - connect(mTcpSocket, SAK_SOCKET_ERROR, mTcpSocket, [=]() { - QString info = "Error occurred: " + mTcpSocket->errorString(); - outputMessage(QtWarningMsg, info); - emit errorOccured(mTcpSocket->errorString()); + connect(mTcpSocket, SAK_SIG_SOCKETERROROCCURRED, mTcpSocket, [=]() { + QString info = "Error occurred: " + mTcpSocket->errorString(); + outputMessage(QtWarningMsg, info); + emit errorOccured(mTcpSocket->errorString()); }); connect(mTcpSocket, &QTcpSocket::disconnected, mTcpSocket, [=]() { exit(); }); diff --git a/src/tools/tools/saktcpservertool.cc b/src/tools/tools/saktcpservertool.cc index c03b002b..6f02c0e3 100644 --- a/src/tools/tools/saktcpservertool.cc +++ b/src/tools/tools/saktcpservertool.cc @@ -63,7 +63,7 @@ bool SAKTcpServerTool::initialize(QString &errStr) emit clientsChanged(); }); - connect(client, SAK_SOCKET_ERROR, client, [=](){ + connect(client, SAK_SIG_SOCKETERROROCCURRED, client, [=]() { this->mTcpSocketListMutex.lock(); this->mTcpSocketList.removeOne(client); this->mTcpSocketListMutex.unlock(); diff --git a/src/tools/tools/sakudpclienttool.cc b/src/tools/tools/sakudpclienttool.cc index 678860de..890e3f4f 100644 --- a/src/tools/tools/sakudpclienttool.cc +++ b/src/tools/tools/sakudpclienttool.cc @@ -45,7 +45,7 @@ bool SAKUdpClientTool::initialize(QString &errStr) connect(mUdpSocket, &QUdpSocket::readyRead, mUdpSocket, [=](){readBytes();}); - connect(mUdpSocket, SAK_SOCKET_ERROR, this, [=](){ + connect(mUdpSocket, SAK_SIG_SOCKETERROROCCURRED, this, [=]() { emit errorOccured(mUdpSocket->errorString()); }); diff --git a/src/tools/tools/sakudpservertool.cc b/src/tools/tools/sakudpservertool.cc index b1204ba0..83629a1e 100644 --- a/src/tools/tools/sakudpservertool.cc +++ b/src/tools/tools/sakudpservertool.cc @@ -41,7 +41,7 @@ bool SAKUdpServerTool::initialize(QString &errStr) connect(mUdpSocket, &QUdpSocket::readyRead, mUdpSocket, [=](){readBytes();}); - connect(mUdpSocket, SAK_SOCKET_ERROR, this, [=](){ + connect(mUdpSocket, SAK_SIG_SOCKETERROROCCURRED, this, [=]() { emit errorOccured(mUdpSocket->errorString()); }); diff --git a/src/tools/tools/sakwebsocketclienttool.cc b/src/tools/tools/sakwebsocketclienttool.cc index 9806ea07..b0e71c2d 100644 --- a/src/tools/tools/sakwebsocketclienttool.cc +++ b/src/tools/tools/sakwebsocketclienttool.cc @@ -1,104 +1,96 @@ -/******************************************************************************* +/*************************************************************************************************** * Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved. * - * The file is encoded using "utf8 with bom", it is a part - * of QtSwissArmyKnife project. + * The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project. * - * QtSwissArmyKnife is licensed according to the terms in - * the file LICENCE in the root of the source code directory. - ******************************************************************************/ + * QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source + * code directory. + **************************************************************************************************/ #include "sakwebsocketclienttool.h" +#include "sakcompatibility.h" #include "sakinterface.h" -#define WS_ERR_SIGNAL void (QWebSocket::*)(QAbstractSocket::SocketError) - SAKWebSocketClientTool::SAKWebSocketClientTool(QObject *parent) - : SAKSocketClientTool{"sak.websocketclienttool", parent} {} + : SAKSocketClientTool{"sak.websocketclienttool", parent} +{} -bool SAKWebSocketClientTool::initialize(QString &errStr) { - Q_UNUSED(errStr) - mWebSocket = new QWebSocket(); - connect(mWebSocket, &QWebSocket::connected, mWebSocket, [=]() { - QString address = mWebSocket->localAddress().toString(); - QString port = QString::number(mWebSocket->localPort()); - this->mBindingIpPort = address + ":" + port; - emit bindingIpPortChanged(); +bool SAKWebSocketClientTool::initialize(QString &errStr) +{ + Q_UNUSED(errStr) + m_webSocket = new QWebSocket(); + connect(m_webSocket, &QWebSocket::connected, m_webSocket, [=]() { + QString address = m_webSocket->localAddress().toString(); + QString port = QString::number(m_webSocket->localPort()); + this->mBindingIpPort = address + ":" + port; + emit bindingIpPortChanged(); - QString ip = mWebSocket->peerAddress().toString(); - this->mPeerInfo = ip + ":" + QString::number(mWebSocket->peerPort()); - }); + QString ip = m_webSocket->peerAddress().toString(); + this->m_peerInfo = ip + ":" + QString::number(m_webSocket->peerPort()); + }); - connect(mWebSocket, &QWebSocket::disconnected, mWebSocket, [=]() { - QString errStr = mWebSocket->errorString(); - if (!errStr.isEmpty()) { - errStr = "Disconected from server:" + errStr; - outputMessage(QtInfoMsg, errStr); + connect(m_webSocket, &QWebSocket::disconnected, m_webSocket, [=]() { + QString errStr = m_webSocket->errorString(); + if (!errStr.isEmpty()) { + errStr = "Disconected from server:" + errStr; + outputMessage(QtInfoMsg, errStr); + } + + exit(); + }); + + connect(m_webSocket, &QWebSocket::binaryFrameReceived, m_webSocket, [=](const QByteArray &msg) { + QByteArray ba = SAKInterface::arrayToHex(msg, ' '); + QString hex = QString::fromLatin1(ba); + QString info = mBindingIpPort + "<-" + this->m_peerInfo + ":" + hex; + outputMessage(QtInfoMsg, info); + emit bytesOutputted(msg, rxJsonObject()); + }); + + connect(m_webSocket, &QWebSocket::textMessageReceived, m_webSocket, [=](QString message) { + QString info = mBindingIpPort + "<-" + this->m_peerInfo + ":" + message; + outputMessage(QtInfoMsg, info); + emit bytesOutputted(message.toUtf8(), rxJsonObject()); + }); + + connect(m_webSocket, SAK_SIG_WEBSOCKETERROROCCURRED, m_webSocket, [=]() { + QString errStr = m_webSocket->errorString(); + outputMessage(QtInfoMsg, errStr); + emit errorOccured(errStr); + }); + + QString address = "ws://" + mServerIp + ":" + QString::number(mServerPort); + qCDebug(mLoggingCategory) << "Server url: " + address; + m_webSocket->open(address); + + return true; +} + +void SAKWebSocketClientTool::writeBytes(const QByteArray &bytes, const QVariant &context) +{ + Q_UNUSED(context); + qint64 ret = -1; + QString hex; + if (mMessageType == 0) { + hex = QString::fromLatin1(SAKInterface::arrayToHex(bytes, ' ')); + ret = m_webSocket->sendBinaryMessage(bytes); + } else { + hex = QString::fromUtf8(bytes); + ret = m_webSocket->sendTextMessage(QString::fromUtf8(bytes)); } - exit(); - }); - - connect(mWebSocket, &QWebSocket::binaryFrameReceived, mWebSocket, - [=](const QByteArray &message) { - QByteArray ba = SAKInterface::arrayToHex(message, ' '); - QString hex = QString::fromLatin1(ba); - QString info = mBindingIpPort + "<-" + this->mPeerInfo + ":" + hex; - outputMessage(QtInfoMsg, info); - emit bytesOutputted(message, rxJsonObject()); - }); - - connect(mWebSocket, &QWebSocket::textMessageReceived, mWebSocket, - [=](QString message) { - QString info = - mBindingIpPort + "<-" + this->mPeerInfo + ":" + message; - outputMessage(QtInfoMsg, info); - emit bytesOutputted(message.toUtf8(), rxJsonObject()); - }); - -#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) - connect(mWebSocket, static_cast(&QWebSocket::errorOccurred), -#else - connect(mWebSocket, static_cast(&QWebSocket::error), -#endif - mWebSocket, [=](QAbstractSocket::SocketError error) { - Q_UNUSED(error); - QString errStr = mWebSocket->errorString(); - outputMessage(QtInfoMsg, errStr); - emit errorOccured(errStr); - }); - - QString address = "ws://" + mServerIp + ":" + QString::number(mServerPort); - qCDebug(mLoggingCategory) << "Server url: " + address; - mWebSocket->open(address); - - return true; + if (ret == -1) { + outputMessage(QtWarningMsg, m_webSocket->errorString()); + } else { + QString info = mBindingIpPort + "<-" + this->m_peerInfo + ":" + hex; + outputMessage(QtInfoMsg, info); + emit bytesInputted(bytes, txJsonObject()); + } } -void SAKWebSocketClientTool::writeBytes(const QByteArray &bytes, - const QVariant &context) { - Q_UNUSED(context); - qint64 ret = -1; - QString hex; - if (mMessageType == 0) { - hex = QString::fromLatin1(SAKInterface::arrayToHex(bytes, ' ')); - ret = mWebSocket->sendBinaryMessage(bytes); - } else { - hex = QString::fromUtf8(bytes); - ret = mWebSocket->sendTextMessage(QString::fromUtf8(bytes)); - } - - if (ret == -1) { - outputMessage(QtWarningMsg, mWebSocket->errorString()); - } else { - QString info = mBindingIpPort + "<-" + this->mPeerInfo + ":" + hex; - outputMessage(QtInfoMsg, info); - emit bytesInputted(bytes, txJsonObject()); - } -} - -void SAKWebSocketClientTool::uninitialize() { - mWebSocket->close(); - mWebSocket->deleteLater(); - mWebSocket = nullptr; +void SAKWebSocketClientTool::uninitialize() +{ + m_webSocket->close(); + m_webSocket->deleteLater(); + m_webSocket = nullptr; } diff --git a/src/tools/tools/sakwebsocketclienttool.h b/src/tools/tools/sakwebsocketclienttool.h index f1b85cf9..b8b5b370 100644 --- a/src/tools/tools/sakwebsocketclienttool.h +++ b/src/tools/tools/sakwebsocketclienttool.h @@ -1,34 +1,32 @@ -/******************************************************************************* +/*************************************************************************************************** * Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved. * - * The file is encoded using "utf8 with bom", it is a part - * of QtSwissArmyKnife project. + * The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project. * - * QtSwissArmyKnife is licensed according to the terms in - * the file LICENCE in the root of the source code directory. - ******************************************************************************/ + * QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source + * code directory. + **************************************************************************************************/ #ifndef SAKWEBSOCKETCLIENTTOOL_H #define SAKWEBSOCKETCLIENTTOOL_H #include -#include "saksocketclienttool.h" +#include "saksocketclienttool.h" class SAKWebSocketClientTool : public SAKSocketClientTool { Q_OBJECT public: - explicit SAKWebSocketClientTool(QObject *parent = nullptr); + explicit SAKWebSocketClientTool(QObject *parent = Q_NULLPTR); protected: virtual bool initialize(QString &errStr) final; - virtual void writeBytes(const QByteArray &bytes, - const QVariant &context = QJsonObject()) final; + virtual void writeBytes(const QByteArray &bytes, const QVariant &context = QJsonObject()) final; virtual void uninitialize() final; private: - QWebSocket *mWebSocket{nullptr}; - QString mPeerInfo; + QWebSocket *m_webSocket{Q_NULLPTR}; + QString m_peerInfo; }; #endif // SAKWEBSOCKETCLIENTTOOL_H