114 lines
2.7 KiB
C++
114 lines
2.7 KiB
C++
#include "MainServer.h"
|
|
|
|
#include <QHostAddress>
|
|
#include <QTcpSocket>
|
|
#include <QDataStream>
|
|
#include <QTimer>
|
|
|
|
QMainServer::QMainServer(QObject* param) : QTcpServer(param){
|
|
}
|
|
|
|
bool QMainServer::Start(quint16 port) {
|
|
qDebug() << "QMainServer::Start listen port=" << port;
|
|
if (!listen(QHostAddress::Any, port)) {
|
|
qDebug() << "QMainServer::Start listen failed";
|
|
return false;
|
|
}
|
|
|
|
connect(this, &QTcpServer::newConnection, this, &QMainServer::OnNewConnection);
|
|
|
|
/*connect(&m_boardcastTimer, &QTimer::timeout, this, [this, port]() {
|
|
QString message = QString::number(port);
|
|
m_udpSocket.writeDatagram(message.toLocal8Bit(), QHostAddress::Broadcast, 8527);
|
|
});
|
|
m_boardcastTimer.start(2000);*/
|
|
|
|
return true;
|
|
}
|
|
|
|
void QMainServer::Stop() {
|
|
for (auto tcpSocket : m_tcpSockets) {
|
|
tcpSocket->close();
|
|
}
|
|
if (m_boardcastTimer.isActive()) {
|
|
m_boardcastTimer.stop();
|
|
}
|
|
}
|
|
|
|
void QMainServer::writeIndex(qint32 index) {
|
|
//QString message = QString::number(index);
|
|
|
|
//QByteArray sendData;
|
|
//QDataStream sendStream(&sendData, QIODevice::WriteOnly);
|
|
|
|
//// 设置版本号,可根据需求进行修改
|
|
//sendStream.setVersion(QDataStream::Qt_6_2);
|
|
|
|
//int messageSize = message.toUtf8().size();
|
|
|
|
//// 写入包的大小
|
|
//sendStream << messageSize;
|
|
|
|
//// 写入实际数据
|
|
//sendStream.writeRawData(message.toUtf8().constData(), messageSize);
|
|
|
|
for (auto socket : m_tcpSockets) {
|
|
QDataStream out(socket);
|
|
out.setVersion(QDataStream::Qt_6_2);
|
|
out << index;
|
|
}
|
|
}
|
|
|
|
void QMainServer::BroadcastCmd(const QString& cmd) {
|
|
for (auto socket : m_tcpSockets) {
|
|
QDataStream out(socket);
|
|
out.setVersion(QDataStream::Qt_6_2);
|
|
out << cmd.toUtf8();
|
|
}
|
|
}
|
|
|
|
void QMainServer::OnNewConnection() {
|
|
qDebug() << "new Client Connected";
|
|
|
|
while (hasPendingConnections()) {
|
|
QTcpSocket* tcpSocket = nextPendingConnection();
|
|
connect(tcpSocket, &QTcpSocket::disconnected, this, &QMainServer::OnDisConnected);
|
|
connect(tcpSocket, &QTcpSocket::readyRead, this, &QMainServer::OnReadyRead);
|
|
|
|
m_tcpSockets.append(tcpSocket);
|
|
}
|
|
}
|
|
|
|
|
|
void QMainServer::OnDisConnected() {
|
|
QTcpSocket* tcpSocket = static_cast<QTcpSocket*>(sender());
|
|
bool success = m_tcpSockets.removeOne(tcpSocket);
|
|
qDebug() << "QMainServer::OnDisConnected " << success;
|
|
}
|
|
|
|
void QMainServer::OnStateChanged(QAbstractSocket::SocketState socketState)
|
|
{
|
|
}
|
|
|
|
void QMainServer::OnReadyRead() {
|
|
QTcpSocket* tcpSocket = static_cast<QTcpSocket*>(sender());
|
|
QByteArray buf = tcpSocket->readAll();
|
|
|
|
QDataStream receiveStream(buf);
|
|
receiveStream.setVersion(QDataStream::Qt_6_2);
|
|
|
|
while (!receiveStream.atEnd()) {
|
|
int packetSize = 0;
|
|
receiveStream >> packetSize;
|
|
|
|
if (buf.size() >= packetSize) {
|
|
QString message;
|
|
receiveStream >> message;
|
|
buf.remove(0, packetSize);
|
|
}
|
|
else {
|
|
break;
|
|
}
|
|
}
|
|
}
|