chore: update files of project

This commit is contained in:
wuhai 2023-11-21 11:59:54 +08:00
parent 4f52fe9716
commit b0d37265d0
8 changed files with 124 additions and 121 deletions

View File

@ -1,22 +1,38 @@
/******************************************************************************* /***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved. * Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
* *
* The file is encoded using "utf8 with bom", it is a part * The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
* of QtSwissArmyKnife project.
* *
* QtSwissArmyKnife is licensed according to the terms in * QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* the file LICENCE in the root of the source code directory. * code directory.
******************************************************************************/ **************************************************************************************************/
#ifndef SAKCOMPATIBILITY_H #ifndef SAKCOMPATIBILITY_H
#define SAKCOMPATIBILITY_H #define SAKCOMPATIBILITY_H
#include <QFileInfo>
#include <QtGlobal> #include <QtGlobal>
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
#define SAK_SOCKET_ERROR &QAbstractSocket::errorOccurred #define SAK_SIG_SOCKETERROROCCURRED &QAbstractSocket::errorOccurred
#else #else
#define SAK_SOCKET_ERROR static_cast<void(QAbstractSocket::*)\ #define SAK_SOCKET_ERROR \
(QAbstractSocket::SocketError)>(&QAbstractSocket::error) static_cast<void (QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error)
#endif #endif
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
#define SAK_SIG_WEBSOCKETERROROCCURRED &QWebSocket::errorOccurred
#else
#define SAK_SIG_WEBSOCKETERROROCCURRED \
static_cast<void (QWebSocket::*)(QWebSocket::error)>(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 #endif // SAKCOMPATIBILITY_H

View File

@ -17,6 +17,7 @@
#include <QTimer> #include <QTimer>
#include <QUrl> #include <QUrl>
#include "sakcompatibility.h"
#include "saksettings.h" #include "saksettings.h"
QVector<SAKLog::LogContext> SAKLog::mLogContextVector; QVector<SAKLog::LogContext> SAKLog::mLogContextVector;
@ -429,11 +430,7 @@ void SAKLog::clearLog()
QDir dir(logPath()); QDir dir(logPath());
QFileInfoList infoList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot); QFileInfoList infoList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
for (auto& info : infoList) { for (auto& info : infoList) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) qint64 dateTime = sakBirthTimeOfFile(info);
qint64 dateTime = info.birthTime().toMSecsSinceEpoch();
#else
qint64 dateTime = info.created().toMSecsSinceEpoch();
#endif
mParametersMutex.lock(); mParametersMutex.lock();
int logLifeCycle = mParameters.logLifeCycle; int logLifeCycle = mParameters.logLifeCycle;
mParametersMutex.unlock(); mParametersMutex.unlock();

View File

@ -38,10 +38,10 @@ bool SAKTcpClientTool::initialize(QString& errStr) {
outputMessage(QtInfoMsg, "Client address and port: " + mBindingIpPort); outputMessage(QtInfoMsg, "Client address and port: " + mBindingIpPort);
emit bindingIpPortChanged(); emit bindingIpPortChanged();
connect(mTcpSocket, SAK_SOCKET_ERROR, mTcpSocket, [=]() { connect(mTcpSocket, SAK_SIG_SOCKETERROROCCURRED, mTcpSocket, [=]() {
QString info = "Error occurred: " + mTcpSocket->errorString(); QString info = "Error occurred: " + mTcpSocket->errorString();
outputMessage(QtWarningMsg, info); outputMessage(QtWarningMsg, info);
emit errorOccured(mTcpSocket->errorString()); emit errorOccured(mTcpSocket->errorString());
}); });
connect(mTcpSocket, &QTcpSocket::disconnected, mTcpSocket, [=]() { exit(); }); connect(mTcpSocket, &QTcpSocket::disconnected, mTcpSocket, [=]() { exit(); });

View File

@ -63,7 +63,7 @@ bool SAKTcpServerTool::initialize(QString &errStr)
emit clientsChanged(); emit clientsChanged();
}); });
connect(client, SAK_SOCKET_ERROR, client, [=](){ connect(client, SAK_SIG_SOCKETERROROCCURRED, client, [=]() {
this->mTcpSocketListMutex.lock(); this->mTcpSocketListMutex.lock();
this->mTcpSocketList.removeOne(client); this->mTcpSocketList.removeOne(client);
this->mTcpSocketListMutex.unlock(); this->mTcpSocketListMutex.unlock();

View File

@ -45,7 +45,7 @@ bool SAKUdpClientTool::initialize(QString &errStr)
connect(mUdpSocket, &QUdpSocket::readyRead, connect(mUdpSocket, &QUdpSocket::readyRead,
mUdpSocket, [=](){readBytes();}); mUdpSocket, [=](){readBytes();});
connect(mUdpSocket, SAK_SOCKET_ERROR, this, [=](){ connect(mUdpSocket, SAK_SIG_SOCKETERROROCCURRED, this, [=]() {
emit errorOccured(mUdpSocket->errorString()); emit errorOccured(mUdpSocket->errorString());
}); });

View File

@ -41,7 +41,7 @@ bool SAKUdpServerTool::initialize(QString &errStr)
connect(mUdpSocket, &QUdpSocket::readyRead, connect(mUdpSocket, &QUdpSocket::readyRead,
mUdpSocket, [=](){readBytes();}); mUdpSocket, [=](){readBytes();});
connect(mUdpSocket, SAK_SOCKET_ERROR, this, [=](){ connect(mUdpSocket, SAK_SIG_SOCKETERROROCCURRED, this, [=]() {
emit errorOccured(mUdpSocket->errorString()); emit errorOccured(mUdpSocket->errorString());
}); });

View File

@ -1,104 +1,96 @@
/******************************************************************************* /***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved. * Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
* *
* The file is encoded using "utf8 with bom", it is a part * The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
* of QtSwissArmyKnife project.
* *
* QtSwissArmyKnife is licensed according to the terms in * QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* the file LICENCE in the root of the source code directory. * code directory.
******************************************************************************/ **************************************************************************************************/
#include "sakwebsocketclienttool.h" #include "sakwebsocketclienttool.h"
#include "sakcompatibility.h"
#include "sakinterface.h" #include "sakinterface.h"
#define WS_ERR_SIGNAL void (QWebSocket::*)(QAbstractSocket::SocketError)
SAKWebSocketClientTool::SAKWebSocketClientTool(QObject *parent) SAKWebSocketClientTool::SAKWebSocketClientTool(QObject *parent)
: SAKSocketClientTool{"sak.websocketclienttool", parent} {} : SAKSocketClientTool{"sak.websocketclienttool", parent}
{}
bool SAKWebSocketClientTool::initialize(QString &errStr) { bool SAKWebSocketClientTool::initialize(QString &errStr)
Q_UNUSED(errStr) {
mWebSocket = new QWebSocket(); Q_UNUSED(errStr)
connect(mWebSocket, &QWebSocket::connected, mWebSocket, [=]() { m_webSocket = new QWebSocket();
QString address = mWebSocket->localAddress().toString(); connect(m_webSocket, &QWebSocket::connected, m_webSocket, [=]() {
QString port = QString::number(mWebSocket->localPort()); QString address = m_webSocket->localAddress().toString();
this->mBindingIpPort = address + ":" + port; QString port = QString::number(m_webSocket->localPort());
emit bindingIpPortChanged(); this->mBindingIpPort = address + ":" + port;
emit bindingIpPortChanged();
QString ip = mWebSocket->peerAddress().toString(); QString ip = m_webSocket->peerAddress().toString();
this->mPeerInfo = ip + ":" + QString::number(mWebSocket->peerPort()); this->m_peerInfo = ip + ":" + QString::number(m_webSocket->peerPort());
}); });
connect(mWebSocket, &QWebSocket::disconnected, mWebSocket, [=]() { connect(m_webSocket, &QWebSocket::disconnected, m_webSocket, [=]() {
QString errStr = mWebSocket->errorString(); QString errStr = m_webSocket->errorString();
if (!errStr.isEmpty()) { if (!errStr.isEmpty()) {
errStr = "Disconected from server:" + errStr; errStr = "Disconected from server:" + errStr;
outputMessage(QtInfoMsg, 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(); if (ret == -1) {
}); outputMessage(QtWarningMsg, m_webSocket->errorString());
} else {
connect(mWebSocket, &QWebSocket::binaryFrameReceived, mWebSocket, QString info = mBindingIpPort + "<-" + this->m_peerInfo + ":" + hex;
[=](const QByteArray &message) { outputMessage(QtInfoMsg, info);
QByteArray ba = SAKInterface::arrayToHex(message, ' '); emit bytesInputted(bytes, txJsonObject());
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<WS_ERR_SIGNAL>(&QWebSocket::errorOccurred),
#else
connect(mWebSocket, static_cast<WS_ERR_SIGNAL>(&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;
} }
void SAKWebSocketClientTool::writeBytes(const QByteArray &bytes, void SAKWebSocketClientTool::uninitialize()
const QVariant &context) { {
Q_UNUSED(context); m_webSocket->close();
qint64 ret = -1; m_webSocket->deleteLater();
QString hex; m_webSocket = nullptr;
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;
} }

View File

@ -1,34 +1,32 @@
/******************************************************************************* /***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved. * Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
* *
* The file is encoded using "utf8 with bom", it is a part * The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
* of QtSwissArmyKnife project.
* *
* QtSwissArmyKnife is licensed according to the terms in * QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
* the file LICENCE in the root of the source code directory. * code directory.
******************************************************************************/ **************************************************************************************************/
#ifndef SAKWEBSOCKETCLIENTTOOL_H #ifndef SAKWEBSOCKETCLIENTTOOL_H
#define SAKWEBSOCKETCLIENTTOOL_H #define SAKWEBSOCKETCLIENTTOOL_H
#include <QWebSocket> #include <QWebSocket>
#include "saksocketclienttool.h"
#include "saksocketclienttool.h"
class SAKWebSocketClientTool : public SAKSocketClientTool class SAKWebSocketClientTool : public SAKSocketClientTool
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit SAKWebSocketClientTool(QObject *parent = nullptr); explicit SAKWebSocketClientTool(QObject *parent = Q_NULLPTR);
protected: protected:
virtual bool initialize(QString &errStr) final; virtual bool initialize(QString &errStr) final;
virtual void writeBytes(const QByteArray &bytes, virtual void writeBytes(const QByteArray &bytes, const QVariant &context = QJsonObject()) final;
const QVariant &context = QJsonObject()) final;
virtual void uninitialize() final; virtual void uninitialize() final;
private: private:
QWebSocket *mWebSocket{nullptr}; QWebSocket *m_webSocket{Q_NULLPTR};
QString mPeerInfo; QString m_peerInfo;
}; };
#endif // SAKWEBSOCKETCLIENTTOOL_H #endif // SAKWEBSOCKETCLIENTTOOL_H