mirror of
https://github.com/x-tools-author/x-tools.git
synced 2025-09-15 15:28:40 +08:00
chore: update files of project
This commit is contained in:
parent
46ad37008b
commit
e69f157c37
@ -14,65 +14,65 @@ SAKSocketServerTool::SAKSocketServerTool(QObject *parent)
|
||||
|
||||
QString SAKSocketServerTool::serverIp()
|
||||
{
|
||||
return mServerIp;
|
||||
return m_serverIp;
|
||||
}
|
||||
|
||||
void SAKSocketServerTool::setServerIp(const QString &ip)
|
||||
{
|
||||
mServerIp = ip;
|
||||
m_serverIp = ip;
|
||||
emit serverIpChanged();
|
||||
}
|
||||
|
||||
int SAKSocketServerTool::serverPort()
|
||||
{
|
||||
return mServerPort;
|
||||
return m_serverPort;
|
||||
}
|
||||
|
||||
void SAKSocketServerTool::setServerPort(int port)
|
||||
{
|
||||
mServerPort = port;
|
||||
m_serverPort = port;
|
||||
emit serverPortChanged();
|
||||
}
|
||||
|
||||
bool SAKSocketServerTool::specifyIpAndPort()
|
||||
{
|
||||
return mSpecifyIpAndPort;
|
||||
return m_specifyIpAndPort;
|
||||
}
|
||||
|
||||
void SAKSocketServerTool::setSpecifyIpAndPort(bool specified)
|
||||
{
|
||||
mSpecifyIpAndPort = specified;
|
||||
m_specifyIpAndPort = specified;
|
||||
emit specifyIpAndPortChanged();
|
||||
}
|
||||
|
||||
QStringList SAKSocketServerTool::clients()
|
||||
{
|
||||
return mClients;
|
||||
return m_clients;
|
||||
}
|
||||
|
||||
int SAKSocketServerTool::clientIndex()
|
||||
{
|
||||
return mClientIndex;
|
||||
return m_clientIndex;
|
||||
}
|
||||
|
||||
void SAKSocketServerTool::setClientIndex(int index)
|
||||
{
|
||||
mClientIndex = index;
|
||||
m_clientIndex = index;
|
||||
emit clientIndexChanged();
|
||||
}
|
||||
|
||||
QString SAKSocketServerTool::bindingIpPort()
|
||||
{
|
||||
return mBindingIpPort;
|
||||
return m_bindingIpPort;
|
||||
}
|
||||
|
||||
int SAKSocketServerTool::messageType()
|
||||
{
|
||||
return mMessageType;
|
||||
return m_messageType;
|
||||
}
|
||||
|
||||
void SAKSocketServerTool::setMessageType(int type)
|
||||
{
|
||||
mMessageType = type;
|
||||
m_messageType = type;
|
||||
emit messageTypeChanged();
|
||||
}
|
||||
|
||||
@ -41,13 +41,13 @@ public:
|
||||
void setMessageType(int type);
|
||||
|
||||
protected:
|
||||
QString mServerIp;
|
||||
int mServerPort;
|
||||
bool mSpecifyIpAndPort;
|
||||
QStringList mClients;
|
||||
int mClientIndex;
|
||||
QString mBindingIpPort;
|
||||
std::atomic_int8_t mMessageType;
|
||||
QString m_serverIp;
|
||||
int m_serverPort;
|
||||
bool m_specifyIpAndPort;
|
||||
QStringList m_clients;
|
||||
int m_clientIndex;
|
||||
QString m_bindingIpPort;
|
||||
std::atomic_int8_t m_messageType;
|
||||
|
||||
signals:
|
||||
void serverIpChanged();
|
||||
|
||||
@ -17,51 +17,51 @@ SAKTcpServerTool::SAKTcpServerTool(QObject *parent)
|
||||
|
||||
bool SAKTcpServerTool::initialize(QString &errStr)
|
||||
{
|
||||
mTcpServer = new QTcpServer();
|
||||
if (!mTcpServer->listen(QHostAddress(mServerIp), mServerPort)) {
|
||||
errStr = "Error occurred: " + mTcpServer->errorString();
|
||||
m_tcpServer = new QTcpServer();
|
||||
if (!m_tcpServer->listen(QHostAddress(m_serverIp), m_serverPort)) {
|
||||
errStr = "Error occurred: " + m_tcpServer->errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
mBindingIpPort = QString("%1:%2").arg(mServerIp).arg(mServerPort);
|
||||
qInfo() << "Server ip and port: " + mBindingIpPort;
|
||||
m_bindingIpPort = QString("%1:%2").arg(m_serverIp).arg(m_serverPort);
|
||||
qInfo() << "Server ip and port: " + m_bindingIpPort;
|
||||
|
||||
connect(mTcpServer, &QTcpServer::newConnection, mTcpServer, [=]() {
|
||||
QTcpSocket *client = mTcpServer->nextPendingConnection();
|
||||
this->mTcpSocketListMutex.lock();
|
||||
this->mTcpSocketList.append(client);
|
||||
this->mTcpSocketListMutex.unlock();
|
||||
connect(m_tcpServer, &QTcpServer::newConnection, m_tcpServer, [=]() {
|
||||
QTcpSocket *client = m_tcpServer->nextPendingConnection();
|
||||
this->m_tcpSocketListMutex.lock();
|
||||
this->m_tcpSocketList.append(client);
|
||||
this->m_tcpSocketListMutex.unlock();
|
||||
|
||||
QString ip = client->peerAddress().toString();
|
||||
quint16 port = client->peerPort();
|
||||
QString ipPort = QString("%1:%2").arg(ip).arg(port);
|
||||
mClients.append(ipPort);
|
||||
m_clients.append(ipPort);
|
||||
qInfo() << "New connection:" + ipPort;
|
||||
emit clientsChanged();
|
||||
|
||||
connect(client, &QTcpSocket::readyRead, client, [=]() {
|
||||
QByteArray bytes = client->readAll();
|
||||
QString hex = bytes.toHex();
|
||||
qInfo() << QString("%1<-%2:%3").arg(mBindingIpPort, ipPort, hex);
|
||||
qInfo() << QString("%1<-%2:%3").arg(m_bindingIpPort, ipPort, hex);
|
||||
emit outputBytes(bytes);
|
||||
});
|
||||
|
||||
connect(client, &QTcpSocket::disconnected, client, [=]() {
|
||||
client->deleteLater();
|
||||
this->mTcpSocketListMutex.lock();
|
||||
this->mTcpSocketList.removeOne(client);
|
||||
this->mTcpSocketListMutex.unlock();
|
||||
this->mClients.removeOne(ipPort);
|
||||
this->m_tcpSocketListMutex.lock();
|
||||
this->m_tcpSocketList.removeOne(client);
|
||||
this->m_tcpSocketListMutex.unlock();
|
||||
this->m_clients.removeOne(ipPort);
|
||||
qInfo() << QString("Connection(%1) has been disconnected: %2")
|
||||
.arg(ipPort, client->errorString());
|
||||
emit clientsChanged();
|
||||
});
|
||||
|
||||
connect(client, SAK_SIG_SOCKETERROROCCURRED, client, [=]() {
|
||||
this->mTcpSocketListMutex.lock();
|
||||
this->mTcpSocketList.removeOne(client);
|
||||
this->mTcpSocketListMutex.unlock();
|
||||
this->mClients.removeOne(ipPort);
|
||||
this->m_tcpSocketListMutex.lock();
|
||||
this->m_tcpSocketList.removeOne(client);
|
||||
this->m_tcpSocketListMutex.unlock();
|
||||
this->m_clients.removeOne(ipPort);
|
||||
qInfo() << QString("Error occurred: %1").arg(client->errorString());
|
||||
emit clientsChanged();
|
||||
});
|
||||
@ -72,11 +72,11 @@ bool SAKTcpServerTool::initialize(QString &errStr)
|
||||
|
||||
void SAKTcpServerTool::writeBytes(const QByteArray &bytes)
|
||||
{
|
||||
if (mClientIndex >= 0 && mClientIndex < mTcpSocketList.length()) {
|
||||
QTcpSocket *client = mTcpSocketList.at(mClientIndex);
|
||||
if (m_clientIndex >= 0 && m_clientIndex < m_tcpSocketList.length()) {
|
||||
QTcpSocket *client = m_tcpSocketList.at(m_clientIndex);
|
||||
writeBytesInner(client, bytes);
|
||||
} else {
|
||||
for (auto &client : mTcpSocketList) {
|
||||
for (auto &client : m_tcpSocketList) {
|
||||
writeBytesInner(client, bytes);
|
||||
}
|
||||
}
|
||||
@ -84,22 +84,22 @@ void SAKTcpServerTool::writeBytes(const QByteArray &bytes)
|
||||
|
||||
void SAKTcpServerTool::uninitialize()
|
||||
{
|
||||
mTcpServer->close();
|
||||
mTcpServer->deleteLater();
|
||||
mTcpServer = nullptr;
|
||||
m_tcpServer->close();
|
||||
m_tcpServer->deleteLater();
|
||||
m_tcpServer = nullptr;
|
||||
}
|
||||
|
||||
void SAKTcpServerTool::writeBytesInner(QTcpSocket *client, const QByteArray &bytes)
|
||||
{
|
||||
qint64 ret = client->write(bytes);
|
||||
if (ret == -1) {
|
||||
qWarning() << mTcpServer->errorString();
|
||||
qWarning() << m_tcpServer->errorString();
|
||||
} else {
|
||||
QString ip = client->peerAddress().toString();
|
||||
quint16 port = client->peerPort();
|
||||
QString ipPort = QString("%1:%2").arg(ip).arg(port);
|
||||
QString hex = bytes.toHex();
|
||||
qInfo() << QString("%1->%2:%3").arg(mBindingIpPort, ipPort, hex);
|
||||
qInfo() << QString("%1->%2:%3").arg(m_bindingIpPort, ipPort, hex);
|
||||
emit bytesWritten(bytes, ipPort);
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,9 +28,9 @@ private:
|
||||
void writeBytesInner(QTcpSocket *client, const QByteArray &bytes);
|
||||
|
||||
private:
|
||||
QTcpServer *mTcpServer{nullptr};
|
||||
QList<QTcpSocket *> mTcpSocketList;
|
||||
QMutex mTcpSocketListMutex;
|
||||
QTcpServer *m_tcpServer{nullptr};
|
||||
QList<QTcpSocket *> m_tcpSocketList;
|
||||
QMutex m_tcpSocketListMutex;
|
||||
};
|
||||
|
||||
#endif // SAKTCPSERVERTOOL_H
|
||||
|
||||
@ -19,8 +19,8 @@ bool SAKUdpServerTool::initialize(QString &errStr)
|
||||
{
|
||||
m_udpSocket = new QUdpSocket();
|
||||
bool ret = false;
|
||||
if (mSpecifyIpAndPort) {
|
||||
ret = m_udpSocket->bind(QHostAddress(mServerIp), mServerPort);
|
||||
if (m_specifyIpAndPort) {
|
||||
ret = m_udpSocket->bind(QHostAddress(m_serverIp), m_serverPort);
|
||||
} else {
|
||||
ret = m_udpSocket->bind();
|
||||
}
|
||||
@ -33,8 +33,8 @@ bool SAKUdpServerTool::initialize(QString &errStr)
|
||||
|
||||
QString ip = m_udpSocket->localAddress().toString();
|
||||
int port = m_udpSocket->localPort();
|
||||
mBindingIpPort = QString("%1:%2").arg(ip).arg(port);
|
||||
qInfo() << mBindingIpPort;
|
||||
m_bindingIpPort = QString("%1:%2").arg(ip).arg(port);
|
||||
qInfo() << m_bindingIpPort;
|
||||
emit bindingIpPortChanged();
|
||||
|
||||
connect(m_udpSocket, &QUdpSocket::readyRead, m_udpSocket, [=]() { readBytes(); });
|
||||
@ -47,14 +47,14 @@ bool SAKUdpServerTool::initialize(QString &errStr)
|
||||
|
||||
void SAKUdpServerTool::writeBytes(const QByteArray &bytes)
|
||||
{
|
||||
if (mClientIndex >= 0 && mClientIndex < mClients.length()) {
|
||||
QString ipPort = mClients.at(mClientIndex);
|
||||
if (m_clientIndex >= 0 && m_clientIndex < m_clients.length()) {
|
||||
QString ipPort = m_clients.at(m_clientIndex);
|
||||
QStringList list = ipPort.split(":");
|
||||
QString ip = list.first();
|
||||
quint16 port = list.last().toInt();
|
||||
writeDatagram(bytes, ip, port);
|
||||
} else {
|
||||
for (auto &client : mClients) {
|
||||
for (auto &client : m_clients) {
|
||||
QStringList list = client.split(":");
|
||||
QString ip = list.first();
|
||||
quint16 port = list.last().toInt();
|
||||
@ -89,11 +89,11 @@ void SAKUdpServerTool::readBytes()
|
||||
QByteArray ba = SAKInterface::arrayToHex(bytes, ' ');
|
||||
QString hex = QString::fromLatin1(ba);
|
||||
QString info = QString("%1:%2").arg(address.toString()).arg(port);
|
||||
QString msg = QString("%1<-%2:%3").arg(mBindingIpPort, info, hex);
|
||||
QString msg = QString("%1<-%2:%3").arg(m_bindingIpPort, info, hex);
|
||||
qInfo() << msg;
|
||||
|
||||
if (!mClients.contains(info)) {
|
||||
mClients.append(info);
|
||||
if (!m_clients.contains(info)) {
|
||||
m_clients.append(info);
|
||||
emit clientsChanged();
|
||||
}
|
||||
|
||||
@ -111,7 +111,7 @@ void SAKUdpServerTool::writeDatagram(const QByteArray &bytes,
|
||||
qWarning() << m_udpSocket->errorString();
|
||||
} else {
|
||||
QString hex = QString::fromLatin1(SAKInterface::arrayToHex(bytes, ' '));
|
||||
qInfo() << QString("%1<-%2").arg(mBindingIpPort, hex);
|
||||
emit bytesWritten(bytes, mBindingIpPort);
|
||||
qInfo() << QString("%1<-%2").arg(m_bindingIpPort, hex);
|
||||
emit bytesWritten(bytes, m_bindingIpPort);
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,16 +20,16 @@ SAKWebSocketServerTool::SAKWebSocketServerTool(QObject *parent)
|
||||
|
||||
bool SAKWebSocketServerTool::initialize(QString &errStr)
|
||||
{
|
||||
QString serverName = QString("%1:%2").arg(mServerIp).arg(mServerPort);
|
||||
QString serverName = QString("%1:%2").arg(m_serverIp).arg(m_serverPort);
|
||||
mWebSocketServer = new QWebSocketServer(serverName, QWebSocketServer::NonSecureMode);
|
||||
if (!mWebSocketServer->listen(QHostAddress(mServerIp), mServerPort)) {
|
||||
if (!mWebSocketServer->listen(QHostAddress(m_serverIp), m_serverPort)) {
|
||||
errStr = mWebSocketServer->errorString();
|
||||
//outputMessage(QtWarningMsg, errStr);
|
||||
qWarning() << errStr;
|
||||
return false;
|
||||
}
|
||||
|
||||
QString mBindingIpPort = QString("%1:%2").arg(mServerIp).arg(mServerPort);
|
||||
//outputMessage(QtInfoMsg, "Server url: " + mBindingIpPort);
|
||||
QString mBindingIpPort = QString("%1:%2").arg(m_serverIp).arg(m_serverPort);
|
||||
qInfo() << "Server url: " + mBindingIpPort;
|
||||
|
||||
connect(mWebSocketServer,
|
||||
&QWebSocketServer::serverError,
|
||||
@ -50,7 +50,7 @@ bool SAKWebSocketServerTool::initialize(QString &errStr)
|
||||
QString ip = client->peerAddress().toString();
|
||||
quint16 port = client->peerPort();
|
||||
QString ipPort = QString("%1:%2").arg(ip).arg(port);
|
||||
mClients.append(ipPort);
|
||||
m_clients.append(ipPort);
|
||||
emit clientsChanged();
|
||||
|
||||
connect(client, &QWebSocket::textMessageReceived, client, [=](const QString &message) {
|
||||
@ -74,7 +74,7 @@ bool SAKWebSocketServerTool::initialize(QString &errStr)
|
||||
|
||||
connect(client, &QWebSocket::disconnected, client, [=]() {
|
||||
this->mWebSocketList.removeOne(client);
|
||||
this->mClients.removeOne(ipPort);
|
||||
this->m_clients.removeOne(ipPort);
|
||||
qInfo() << QString("Connection(%1) has been disconnected: %2")
|
||||
.arg(mBindingIpPort, client->errorString());
|
||||
emit clientsChanged();
|
||||
@ -89,7 +89,7 @@ bool SAKWebSocketServerTool::initialize(QString &errStr)
|
||||
[=](QAbstractSocket::SocketError err) {
|
||||
Q_UNUSED(err);
|
||||
this->mWebSocketList.removeOne(client);
|
||||
this->mClients.removeOne(ipPort);
|
||||
this->m_clients.removeOne(ipPort);
|
||||
qInfo() << QString("Error occurred: %1").arg(client->errorString());
|
||||
emit clientsChanged();
|
||||
});
|
||||
@ -100,8 +100,8 @@ bool SAKWebSocketServerTool::initialize(QString &errStr)
|
||||
|
||||
void SAKWebSocketServerTool::writeBytes(const QByteArray &bytes)
|
||||
{
|
||||
if (mClientIndex >= 0 && mClientIndex < mWebSocketList.length()) {
|
||||
QWebSocket *client = mWebSocketList.at(mClientIndex);
|
||||
if (m_clientIndex >= 0 && m_clientIndex < mWebSocketList.length()) {
|
||||
QWebSocket *client = mWebSocketList.at(m_clientIndex);
|
||||
writeBytesInner(client, bytes);
|
||||
} else {
|
||||
for (auto &client : mWebSocketList) {
|
||||
@ -123,7 +123,7 @@ void SAKWebSocketServerTool::writeBytesInner(QWebSocket *client,
|
||||
{
|
||||
qint64 ret = -1;
|
||||
QString hex;
|
||||
if (mMessageType == 0) {
|
||||
if (m_messageType == 0) {
|
||||
hex = QString::fromLatin1(SAKInterface::arrayToHex(bytes, ' '));
|
||||
ret = client->sendBinaryMessage(bytes);
|
||||
} else {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user