mirror of
https://github.com/x-tools-author/x-tools.git
synced 2025-09-15 15:28:40 +08:00
chore: add log output
This commit is contained in:
parent
0cba7d86c5
commit
c08aa2af3b
@ -23,7 +23,7 @@ SAKBaseTool::SAKBaseTool(QObject *parent)
|
||||
emit this->isWorkingChanged();
|
||||
});
|
||||
connect(this, &SAKBaseTool::errorOccurred, this, [=](const QString &errorString) {
|
||||
qWarning() << "Error occured: " << qPrintable(errorString);
|
||||
qWarning() << "Error occured: " << errorString;
|
||||
exit();
|
||||
wait();
|
||||
});
|
||||
|
||||
@ -14,70 +14,70 @@ SAKSocketClientTool::SAKSocketClientTool(QObject *parent)
|
||||
|
||||
QString SAKSocketClientTool::clientIp()
|
||||
{
|
||||
return mClientIp;
|
||||
return m_clientIp;
|
||||
}
|
||||
|
||||
void SAKSocketClientTool::setClientIp(const QString &ip)
|
||||
{
|
||||
mClientIp = ip;
|
||||
m_clientIp = ip;
|
||||
emit clientIpChanged();
|
||||
}
|
||||
|
||||
int SAKSocketClientTool::clientPort()
|
||||
{
|
||||
return mClientPort;
|
||||
return m_clientPort;
|
||||
}
|
||||
|
||||
void SAKSocketClientTool::setClientPort(int port)
|
||||
{
|
||||
mClientPort = port;
|
||||
m_clientPort = port;
|
||||
emit clientPortChanged();
|
||||
}
|
||||
|
||||
bool SAKSocketClientTool::specifyClientIpPort()
|
||||
{
|
||||
return mSpecifyClientIpPort;
|
||||
return m_specifyClientIpPort;
|
||||
}
|
||||
|
||||
void SAKSocketClientTool::setSpecifyClientIpPort(bool specified)
|
||||
{
|
||||
mSpecifyClientIpPort = specified;
|
||||
m_specifyClientIpPort = specified;
|
||||
}
|
||||
|
||||
QString SAKSocketClientTool::serverIp()
|
||||
{
|
||||
return mServerIp;
|
||||
return m_serverIp;
|
||||
}
|
||||
|
||||
void SAKSocketClientTool::setServerIp(const QString &ip)
|
||||
{
|
||||
mServerIp = ip;
|
||||
m_serverIp = ip;
|
||||
emit serverIpChanged();
|
||||
}
|
||||
|
||||
int SAKSocketClientTool::serverPort()
|
||||
{
|
||||
return mServerPort;
|
||||
return m_serverPort;
|
||||
}
|
||||
|
||||
void SAKSocketClientTool::setServerPort(int port)
|
||||
{
|
||||
mServerPort = port;
|
||||
m_serverPort = port;
|
||||
emit serverPortChanged();
|
||||
}
|
||||
|
||||
QString SAKSocketClientTool::bindingIpPort()
|
||||
{
|
||||
return mBindingIpPort;
|
||||
return m_bindingIpPort;
|
||||
}
|
||||
|
||||
int SAKSocketClientTool::messageType()
|
||||
{
|
||||
return mMessageType;
|
||||
return m_messageType;
|
||||
}
|
||||
|
||||
void SAKSocketClientTool::setMessageType(int type)
|
||||
{
|
||||
mMessageType = type;
|
||||
m_messageType = type;
|
||||
emit messageTypeChanged();
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
|
||||
* Copyright 2023-2024 Qsaker(qsaker@foxmail.com). All rights reserved.
|
||||
*
|
||||
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
|
||||
*
|
||||
@ -14,15 +14,16 @@
|
||||
class SAKSocketClientTool : public SAKCommunicationTool
|
||||
{
|
||||
Q_OBJECT
|
||||
// clang-format off
|
||||
Q_PROPERTY(QString clientIp READ clientIp WRITE setClientIp NOTIFY clientIpChanged)
|
||||
Q_PROPERTY(int clientPort READ clientPort WRITE setClientPort NOTIFY clientPortChanged)
|
||||
Q_PROPERTY(bool specifyClientIpPort READ specifyClientIpPort WRITE setSpecifyClientIpPort NOTIFY
|
||||
specifyClientIpPortChanged)
|
||||
Q_PROPERTY(bool specifyClientIpPort READ specifyClientIpPort WRITE setSpecifyClientIpPort NOTIFY specifyClientIpPortChanged)
|
||||
Q_PROPERTY(QString serverIp READ serverIp WRITE setServerIp NOTIFY serverIpChanged)
|
||||
Q_PROPERTY(int serverPort READ serverPort WRITE setServerPort NOTIFY serverPortChanged)
|
||||
Q_PROPERTY(QString bindingIpPort READ bindingIpPort NOTIFY bindingIpPortChanged)
|
||||
// Just for web socket client.
|
||||
Q_PROPERTY(int messageType READ messageType WRITE setMessageType NOTIFY messageTypeChanged)
|
||||
// clang-format on
|
||||
public:
|
||||
explicit SAKSocketClientTool(QObject *parent = nullptr);
|
||||
|
||||
@ -42,13 +43,13 @@ public:
|
||||
void setMessageType(int type);
|
||||
|
||||
protected:
|
||||
QString mClientIp;
|
||||
int mClientPort;
|
||||
bool mSpecifyClientIpPort;
|
||||
QString mServerIp;
|
||||
int mServerPort;
|
||||
QString mBindingIpPort;
|
||||
std::atomic_int8_t mMessageType;
|
||||
QString m_clientIp;
|
||||
int m_clientPort;
|
||||
bool m_specifyClientIpPort;
|
||||
QString m_serverIp;
|
||||
int m_serverPort;
|
||||
QString m_bindingIpPort;
|
||||
std::atomic_int8_t m_messageType;
|
||||
|
||||
signals:
|
||||
void clientIpChanged();
|
||||
|
||||
@ -20,14 +20,14 @@ SAKTcpClientTool::SAKTcpClientTool(QObject* parent)
|
||||
bool SAKTcpClientTool::initialize(QString& errStr)
|
||||
{
|
||||
mTcpSocket = new QTcpSocket();
|
||||
if (mSpecifyClientIpPort) {
|
||||
if (!mTcpSocket->bind(QHostAddress(mClientIp), mClientPort)) {
|
||||
if (m_specifyClientIpPort) {
|
||||
if (!mTcpSocket->bind(QHostAddress(m_clientIp), m_clientPort)) {
|
||||
errStr = "Binding error: " + mTcpSocket->errorString();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
mTcpSocket->connectToHost(QHostAddress(mServerIp), mServerPort);
|
||||
|
||||
mTcpSocket->connectToHost(QHostAddress(m_serverIp), m_serverPort);
|
||||
if (!mTcpSocket->waitForConnected()) {
|
||||
errStr = "Connect to host error: " + mTcpSocket->errorString();
|
||||
return false;
|
||||
@ -35,8 +35,8 @@ bool SAKTcpClientTool::initialize(QString& errStr)
|
||||
|
||||
QString ip = mTcpSocket->localAddress().toString();
|
||||
QString port = QString::number(mTcpSocket->localPort());
|
||||
mBindingIpPort = ip + ":" + port;
|
||||
qInfo() << "Client address and port: " + mBindingIpPort;
|
||||
m_bindingIpPort = ip + ":" + port;
|
||||
qInfo() << "Client address and port: " + m_bindingIpPort;
|
||||
emit bindingIpPortChanged();
|
||||
|
||||
connect(mTcpSocket, SAK_SIG_SOCKETERROROCCURRED, mTcpSocket, [=]() {
|
||||
@ -58,7 +58,7 @@ void SAKTcpClientTool::writeBytes(const QByteArray& bytes)
|
||||
if (ret == -1) {
|
||||
qWarning() << mTcpSocket->errorString();
|
||||
} else {
|
||||
emit bytesWritten(bytes, mBindingIpPort);
|
||||
emit bytesWritten(bytes, m_bindingIpPort);
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,7 +78,7 @@ void SAKTcpClientTool::readBytes()
|
||||
if (!bytes.isEmpty()) {
|
||||
QByteArray ba = SAKInterface::arrayToHex(bytes, ' ');
|
||||
QString ipport = address.toString() + ":" + QString::number(port);
|
||||
QString info = mBindingIpPort + "<-" + ipport + ":";
|
||||
QString info = m_bindingIpPort + "<-" + ipport + ":";
|
||||
info += QString::fromLatin1(ba);
|
||||
qInfo() << info;
|
||||
emit outputBytes(bytes);
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
|
||||
* Copyright 2023-2024 Qsaker(qsaker@foxmail.com). All rights reserved.
|
||||
*
|
||||
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
|
||||
*
|
||||
@ -24,7 +24,7 @@ bool SAKTcpServerTool::initialize(QString &errStr)
|
||||
}
|
||||
|
||||
mBindingIpPort = QString("%1:%2").arg(mServerIp).arg(mServerPort);
|
||||
//outputMessage(QtInfoMsg, "Server ip and port: " + mBindingIpPort);
|
||||
qInfo() << "Server ip and port: " + mBindingIpPort;
|
||||
|
||||
connect(mTcpServer, &QTcpServer::newConnection, mTcpServer, [=]() {
|
||||
QTcpSocket *client = mTcpServer->nextPendingConnection();
|
||||
@ -36,13 +36,13 @@ bool SAKTcpServerTool::initialize(QString &errStr)
|
||||
quint16 port = client->peerPort();
|
||||
QString ipPort = QString("%1:%2").arg(ip).arg(port);
|
||||
mClients.append(ipPort);
|
||||
//outputMessage(QtInfoMsg, "New connection:" + ipPort);
|
||||
qInfo() << "New connection:" + ipPort;
|
||||
emit clientsChanged();
|
||||
|
||||
connect(client, &QTcpSocket::readyRead, client, [=]() {
|
||||
QByteArray bytes = client->readAll();
|
||||
QString hex = bytes.toHex();
|
||||
//outputMessage(QtInfoMsg, QString("%1<-%2:%3").arg(mBindingIpPort, ipPort, hex));
|
||||
qInfo() << QString("%1<-%2:%3").arg(mBindingIpPort, ipPort, hex);
|
||||
emit outputBytes(bytes);
|
||||
});
|
||||
|
||||
@ -62,7 +62,7 @@ bool SAKTcpServerTool::initialize(QString &errStr)
|
||||
this->mTcpSocketList.removeOne(client);
|
||||
this->mTcpSocketListMutex.unlock();
|
||||
this->mClients.removeOne(ipPort);
|
||||
//outputMessage(QtInfoMsg, QString("Error occurred: %1").arg(client->errorString()));
|
||||
qInfo() << QString("Error occurred: %1").arg(client->errorString());
|
||||
emit clientsChanged();
|
||||
});
|
||||
});
|
||||
@ -93,13 +93,13 @@ void SAKTcpServerTool::writeBytesInner(QTcpSocket *client, const QByteArray &byt
|
||||
{
|
||||
qint64 ret = client->write(bytes);
|
||||
if (ret == -1) {
|
||||
//outputMessage(QtWarningMsg, mTcpServer->errorString());
|
||||
qWarning() << mTcpServer->errorString();
|
||||
} else {
|
||||
QString ip = client->peerAddress().toString();
|
||||
quint16 port = client->peerPort();
|
||||
QString ipPort = QString("%1:%2").arg(ip).arg(port);
|
||||
QString hex = bytes.toHex();
|
||||
//outputMessage(QtInfoMsg, QString("%1->%2:%3").arg(mBindingIpPort, ipPort, hex));
|
||||
qInfo() << QString("%1->%2:%3").arg(mBindingIpPort, ipPort, hex);
|
||||
emit bytesWritten(bytes, ipPort);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
|
||||
* Copyright 2023-2024 Qsaker(qsaker@foxmail.com). All rights reserved.
|
||||
*
|
||||
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
|
||||
*
|
||||
@ -18,31 +18,29 @@ SAKUdpClientTool::SAKUdpClientTool(QObject *parent)
|
||||
|
||||
bool SAKUdpClientTool::initialize(QString &errStr)
|
||||
{
|
||||
mUdpSocket = new QUdpSocket();
|
||||
if (mSpecifyClientIpPort) {
|
||||
if (!mUdpSocket->bind(QHostAddress(mClientIp), mClientPort)) {
|
||||
errStr = mUdpSocket->errorString();
|
||||
//outputMessage(QtWarningMsg, errStr);
|
||||
m_udpSocket = new QUdpSocket();
|
||||
if (m_specifyClientIpPort) {
|
||||
if (!m_udpSocket->bind(QHostAddress(m_clientIp), m_clientPort)) {
|
||||
errStr = m_udpSocket->errorString();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!mUdpSocket->bind()) {
|
||||
errStr = mUdpSocket->errorString();
|
||||
//outputMessage(QtWarningMsg, errStr);
|
||||
if (!m_udpSocket->bind()) {
|
||||
errStr = m_udpSocket->errorString();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QString ip = mUdpSocket->localAddress().toString();
|
||||
int port = mUdpSocket->localPort();
|
||||
QString ip = m_udpSocket->localAddress().toString();
|
||||
int port = m_udpSocket->localPort();
|
||||
QString info = QString("%1:%2").arg(ip).arg(port);
|
||||
mBindingIpPort = info;
|
||||
//outputMessage(QtInfoMsg, info);
|
||||
m_bindingIpPort = info;
|
||||
qInfo() << qPrintable(QString("bind to %1").arg(info));
|
||||
emit bindingIpPortChanged();
|
||||
|
||||
connect(mUdpSocket, &QUdpSocket::readyRead, mUdpSocket, [=]() { readBytes(); });
|
||||
connect(mUdpSocket, SAK_SIG_SOCKETERROROCCURRED, this, [=]() {
|
||||
emit errorOccurred(mUdpSocket->errorString());
|
||||
connect(m_udpSocket, &QUdpSocket::readyRead, m_udpSocket, [=]() { readBytes(); });
|
||||
connect(m_udpSocket, SAK_SIG_SOCKETERROROCCURRED, this, [=]() {
|
||||
emit errorOccurred(m_udpSocket->errorString());
|
||||
});
|
||||
|
||||
return true;
|
||||
@ -50,37 +48,36 @@ bool SAKUdpClientTool::initialize(QString &errStr)
|
||||
|
||||
void SAKUdpClientTool::writeBytes(const QByteArray &bytes)
|
||||
{
|
||||
if (mServerIp.isEmpty()) {
|
||||
if (m_serverIp.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
qint64 ret = mUdpSocket->writeDatagram(bytes, QHostAddress(mServerIp), mServerPort);
|
||||
qint64 ret = m_udpSocket->writeDatagram(bytes, QHostAddress(m_serverIp), m_serverPort);
|
||||
if (ret == -1) {
|
||||
QString ipport = mServerIp + ":" + QString::number(mServerPort);
|
||||
QString str = mUdpSocket->errorString();
|
||||
QString info = QString("write bytes to %1 error: %2").arg(ipport, str);
|
||||
//outputMessage(QtWarningMsg, info);
|
||||
QString ipport = m_serverIp + ":" + QString::number(m_serverPort);
|
||||
QString str = m_udpSocket->errorString();
|
||||
qInfo() << qPrintable(QString("write bytes to %1 error: %2").arg(ipport, str));
|
||||
} else {
|
||||
QByteArray ba = SAKInterface::arrayToHex(bytes, ' ');
|
||||
QString hex = QString::fromLatin1(ba);
|
||||
QString portStr = QString::number(mServerPort);
|
||||
QString serverInfo = QString("%1:%2").arg(mServerIp, portStr);
|
||||
QString info = mBindingIpPort + "->" + serverInfo + ":" + hex;
|
||||
//outputMessage(QtInfoMsg, info);
|
||||
QString portStr = QString::number(m_serverPort);
|
||||
QString serverInfo = QString("%1:%2").arg(m_serverIp, portStr);
|
||||
QString info = m_bindingIpPort + "->" + serverInfo + ":" + hex;
|
||||
qInfo() << qPrintable(info);
|
||||
emit bytesWritten(bytes, info);
|
||||
}
|
||||
}
|
||||
|
||||
void SAKUdpClientTool::uninitialize()
|
||||
{
|
||||
mUdpSocket->deleteLater();
|
||||
mUdpSocket = nullptr;
|
||||
m_udpSocket->deleteLater();
|
||||
m_udpSocket = nullptr;
|
||||
}
|
||||
|
||||
void SAKUdpClientTool::readBytes()
|
||||
{
|
||||
while (mUdpSocket->hasPendingDatagrams()) {
|
||||
auto len = mUdpSocket->pendingDatagramSize();
|
||||
while (m_udpSocket->hasPendingDatagrams()) {
|
||||
auto len = m_udpSocket->pendingDatagramSize();
|
||||
if (len == -1) {
|
||||
break;
|
||||
}
|
||||
@ -88,18 +85,18 @@ void SAKUdpClientTool::readBytes()
|
||||
QByteArray bytes(len, 0);
|
||||
QHostAddress address;
|
||||
quint16 port;
|
||||
qint64 ret = mUdpSocket->readDatagram(bytes.data(), bytes.length(), &address, &port);
|
||||
qint64 ret = m_udpSocket->readDatagram(bytes.data(), bytes.length(), &address, &port);
|
||||
if (ret == -1) {
|
||||
//outputMessage(QtWarningMsg, mUdpSocket->errorString());
|
||||
qWarning() << m_udpSocket->errorString();
|
||||
} else {
|
||||
QByteArray ba = SAKInterface::arrayToHex(bytes, ' ');
|
||||
QString hex = QString::fromLatin1(ba);
|
||||
QString portStr = address.toString();
|
||||
QString serverInfo = address.toString() + ":" + portStr;
|
||||
QString info = mBindingIpPort + "<-" + serverInfo + ":" + hex;
|
||||
//outputMessage(QtInfoMsg, info);
|
||||
QString info = m_bindingIpPort + "<-" + serverInfo + ":" + hex;
|
||||
qInfo() << qPrintable(info);
|
||||
emit outputBytes(bytes);
|
||||
emit bytesRead(bytes, mBindingIpPort);
|
||||
emit bytesRead(bytes, m_bindingIpPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
|
||||
* Copyright 2023-2024 Qsaker(qsaker@foxmail.com). All rights reserved.
|
||||
*
|
||||
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
|
||||
*
|
||||
@ -25,7 +25,7 @@ protected:
|
||||
void readBytes();
|
||||
|
||||
private:
|
||||
QUdpSocket *mUdpSocket{nullptr};
|
||||
QUdpSocket *m_udpSocket{nullptr};
|
||||
};
|
||||
|
||||
#endif // SAKUDPCLIENTTOOL_H
|
||||
|
||||
@ -22,7 +22,7 @@ bool SAKWebSocketClientTool::initialize(QString &errStr)
|
||||
connect(m_webSocket, &QWebSocket::connected, m_webSocket, [=]() {
|
||||
QString address = m_webSocket->localAddress().toString();
|
||||
QString port = QString::number(m_webSocket->localPort());
|
||||
this->mBindingIpPort = address + ":" + port;
|
||||
this->m_bindingIpPort = address + ":" + port;
|
||||
emit bindingIpPortChanged();
|
||||
|
||||
QString ip = m_webSocket->peerAddress().toString();
|
||||
@ -42,14 +42,14 @@ bool SAKWebSocketClientTool::initialize(QString &errStr)
|
||||
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;
|
||||
QString info = m_bindingIpPort + "<-" + this->m_peerInfo + ":" + hex;
|
||||
//outputMessage(QtInfoMsg, info);
|
||||
emit outputBytes(msg);
|
||||
emit bytesRead(msg, this->m_peerInfo);
|
||||
});
|
||||
|
||||
connect(m_webSocket, &QWebSocket::textMessageReceived, m_webSocket, [=](QString message) {
|
||||
QString info = mBindingIpPort + "<-" + this->m_peerInfo + ":" + message;
|
||||
QString info = m_bindingIpPort + "<-" + this->m_peerInfo + ":" + message;
|
||||
//outputMessage(QtInfoMsg, info);
|
||||
emit outputBytes(message.toUtf8());
|
||||
emit bytesRead(message.toUtf8(), this->m_peerInfo);
|
||||
@ -60,8 +60,8 @@ bool SAKWebSocketClientTool::initialize(QString &errStr)
|
||||
//outputMessage(QtInfoMsg, errStr);
|
||||
emit errorOccurred(errStr);
|
||||
});
|
||||
|
||||
QString address = "ws://" + mServerIp + ":" + QString::number(mServerPort);
|
||||
|
||||
QString address = "ws://" + m_serverIp + ":" + QString::number(m_serverPort);
|
||||
qDebug() << "Server url: " + address;
|
||||
m_webSocket->open(address);
|
||||
|
||||
@ -72,7 +72,7 @@ void SAKWebSocketClientTool::writeBytes(const QByteArray &bytes)
|
||||
{
|
||||
qint64 ret = -1;
|
||||
QString hex;
|
||||
if (mMessageType == 0) {
|
||||
if (m_messageType == 0) {
|
||||
hex = QString::fromLatin1(SAKInterface::arrayToHex(bytes, ' '));
|
||||
ret = m_webSocket->sendBinaryMessage(bytes);
|
||||
} else {
|
||||
@ -83,7 +83,7 @@ void SAKWebSocketClientTool::writeBytes(const QByteArray &bytes)
|
||||
if (ret == -1) {
|
||||
//outputMessage(QtWarningMsg, m_webSocket->errorString());
|
||||
} else {
|
||||
QString info = mBindingIpPort + "<-" + this->m_peerInfo + ":" + hex;
|
||||
QString info = m_bindingIpPort + "<-" + this->m_peerInfo + ":" + hex;
|
||||
//outputMessage(QtInfoMsg, info);
|
||||
emit bytesWritten(bytes, this->m_peerInfo);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user