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.
*
* 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 <QFileInfo>
#include <QtGlobal>
#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<void(QAbstractSocket::*)\
(QAbstractSocket::SocketError)>(&QAbstractSocket::error)
#define SAK_SOCKET_ERROR \
static_cast<void (QAbstractSocket::*)(QAbstractSocket::SocketError)>(&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<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

View File

@ -17,6 +17,7 @@
#include <QTimer>
#include <QUrl>
#include "sakcompatibility.h"
#include "saksettings.h"
QVector<SAKLog::LogContext> 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();

View File

@ -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(); });

View File

@ -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();

View File

@ -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());
});

View File

@ -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());
});

View File

@ -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<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;
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;
}

View File

@ -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 <QWebSocket>
#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