qt_demoe/tool/nettool/form/frmtcpserver.cpp

237 lines
7.2 KiB
C++
Raw Permalink Normal View History

2019-09-29 05:13:01 +00:00
#include "frmtcpserver.h"
#include "ui_frmtcpserver.h"
2023-10-17 03:31:58 +00:00
#include "qthelper.h"
2019-09-29 05:13:01 +00:00
frmTcpServer::frmTcpServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmTcpServer)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
2021-08-21 08:10:22 +00:00
on_btnListen_clicked();
2019-09-29 05:13:01 +00:00
}
frmTcpServer::~frmTcpServer()
{
2021-08-22 02:57:02 +00:00
//结束的时候停止服务
server->stop();
2019-09-29 05:13:01 +00:00
delete ui;
}
2021-08-22 02:57:02 +00:00
bool frmTcpServer::eventFilter(QObject *watched, QEvent *event)
{
//双击清空
if (watched == ui->txtMain->viewport()) {
if (event->type() == QEvent::MouseButtonDblClick) {
on_btnClear_clicked();
}
}
return QWidget::eventFilter(watched, event);
}
2019-09-29 05:13:01 +00:00
void frmTcpServer::initForm()
{
2021-08-22 02:57:02 +00:00
QFont font;
font.setPixelSize(16);
ui->txtMain->setFont(font);
ui->txtMain->viewport()->installEventFilter(this);
2019-09-29 05:13:01 +00:00
isOk = false;
//实例化对象并绑定信号槽
server = new TcpServer(this);
2021-08-22 02:57:02 +00:00
connect(server, SIGNAL(connected(QString, int)), this, SLOT(connected(QString, int)));
connect(server, SIGNAL(disconnected(QString, int)), this, SLOT(disconnected(QString, int)));
connect(server, SIGNAL(error(QString, int, QString)), this, SLOT(error(QString, int, QString)));
connect(server, SIGNAL(sendData(QString, int, QString)), this, SLOT(sendData(QString, int, QString)));
connect(server, SIGNAL(receiveData(QString, int, QString)), this, SLOT(receiveData(QString, int, QString)));
2019-09-29 05:13:01 +00:00
//定时器发送数据
2019-09-29 05:13:01 +00:00
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSend_clicked()));
//填充数据到下拉框
ui->cboxInterval->addItems(AppData::Intervals);
ui->cboxData->addItems(AppData::Datas);
AppData::loadIP(ui->cboxListenIP);
2019-12-05 08:01:43 +00:00
}
2019-09-29 05:13:01 +00:00
void frmTcpServer::initConfig()
{
2021-04-13 01:36:22 +00:00
ui->ckHexSend->setChecked(AppConfig::HexSendTcpServer);
2019-09-29 05:13:01 +00:00
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
2021-04-13 01:36:22 +00:00
ui->ckHexReceive->setChecked(AppConfig::HexReceiveTcpServer);
2019-09-29 05:13:01 +00:00
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
2021-04-13 01:36:22 +00:00
ui->ckAscii->setChecked(AppConfig::AsciiTcpServer);
2019-09-29 05:13:01 +00:00
connect(ui->ckAscii, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
2021-04-13 01:36:22 +00:00
ui->ckDebug->setChecked(AppConfig::DebugTcpServer);
2019-09-29 05:13:01 +00:00
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
2021-04-13 01:36:22 +00:00
ui->ckAutoSend->setChecked(AppConfig::AutoSendTcpServer);
2021-01-22 09:20:01 +00:00
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
2019-09-29 05:13:01 +00:00
2021-04-13 01:36:22 +00:00
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(AppConfig::IntervalTcpServer)));
2019-09-29 05:13:01 +00:00
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
2021-04-13 01:36:22 +00:00
ui->cboxListenIP->setCurrentIndex(ui->cboxListenIP->findText(AppConfig::TcpListenIP));
2019-12-05 08:01:43 +00:00
connect(ui->cboxListenIP, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
2021-04-13 01:36:22 +00:00
ui->txtListenPort->setText(QString::number(AppConfig::TcpListenPort));
2019-09-29 05:13:01 +00:00
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
2021-04-13 01:36:22 +00:00
ui->ckSelectAll->setChecked(AppConfig::SelectAllTcpServer);
2019-12-05 08:01:43 +00:00
connect(ui->ckSelectAll, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
this->initTimer();
2019-09-29 05:13:01 +00:00
}
void frmTcpServer::saveConfig()
{
2021-04-13 01:36:22 +00:00
AppConfig::HexSendTcpServer = ui->ckHexSend->isChecked();
AppConfig::HexReceiveTcpServer = ui->ckHexReceive->isChecked();
AppConfig::AsciiTcpServer = ui->ckAscii->isChecked();
AppConfig::DebugTcpServer = ui->ckDebug->isChecked();
AppConfig::AutoSendTcpServer = ui->ckAutoSend->isChecked();
AppConfig::IntervalTcpServer = ui->cboxInterval->currentText().toInt();
AppConfig::TcpListenIP = ui->cboxListenIP->currentText();
AppConfig::TcpListenPort = ui->txtListenPort->text().trimmed().toInt();
AppConfig::SelectAllTcpServer = ui->ckSelectAll->isChecked();
AppConfig::writeConfig();
2019-09-29 05:13:01 +00:00
this->initTimer();
2019-12-05 08:01:43 +00:00
}
void frmTcpServer::initTimer()
2019-12-05 08:01:43 +00:00
{
if (timer->interval() != AppConfig::IntervalTcpServer) {
timer->setInterval(AppConfig::IntervalTcpServer);
}
2021-04-13 01:36:22 +00:00
if (AppConfig::AutoSendTcpServer) {
2019-12-05 08:01:43 +00:00
if (!timer->isActive()) {
timer->start();
}
} else {
if (timer->isActive()) {
timer->stop();
}
}
2019-09-29 05:13:01 +00:00
}
void frmTcpServer::append(int type, const QString &data, bool clear)
{
static int currentCount = 0;
static int maxCount = 100;
2023-10-27 06:31:09 +00:00
QtHelper::appendMsg(ui->txtMain, type, data, maxCount, currentCount, clear, ui->ckShow->isChecked());
2019-09-29 05:13:01 +00:00
}
2021-08-22 02:57:02 +00:00
void frmTcpServer::connected(const QString &ip, int port)
2019-09-29 05:13:01 +00:00
{
2023-10-27 06:31:09 +00:00
append(2, QString("[%1:%2] %3").arg(ip).arg(port).arg("客户端上线"));
2021-08-22 02:57:02 +00:00
2019-09-29 05:13:01 +00:00
QString str = QString("%1:%2").arg(ip).arg(port);
ui->listWidget->addItem(str);
2019-12-05 08:01:43 +00:00
ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count()));
2019-09-29 05:13:01 +00:00
}
2021-08-22 02:57:02 +00:00
void frmTcpServer::disconnected(const QString &ip, int port)
2019-09-29 05:13:01 +00:00
{
2021-08-22 02:57:02 +00:00
append(2, QString("[%1:%2] %3").arg(ip).arg(port).arg("客户端下线"));
2019-09-29 05:13:01 +00:00
int row = -1;
QString str = QString("%1:%2").arg(ip).arg(port);
for (int i = 0; i < ui->listWidget->count(); i++) {
if (ui->listWidget->item(i)->text() == str) {
row = i;
break;
}
}
delete ui->listWidget->takeItem(row);
2019-12-05 08:01:43 +00:00
ui->labCount->setText(QString("共 %1 个客户端").arg(ui->listWidget->count()));
2019-09-29 05:13:01 +00:00
}
2021-08-22 02:57:02 +00:00
void frmTcpServer::error(const QString &ip, int port, const QString &error)
{
2023-10-27 06:31:09 +00:00
append(3, QString("[%1:%2] %3").arg(ip).arg(port).arg(error));
2021-08-22 02:57:02 +00:00
}
2019-09-29 05:13:01 +00:00
void frmTcpServer::sendData(const QString &ip, int port, const QString &data)
{
2021-08-22 02:57:02 +00:00
append(0, QString("[%1:%2] %3").arg(ip).arg(port).arg(data));
2019-09-29 05:13:01 +00:00
}
void frmTcpServer::receiveData(const QString &ip, int port, const QString &data)
{
2021-08-22 02:57:02 +00:00
append(1, QString("[%1:%2] %3").arg(ip).arg(port).arg(data));
2019-09-29 05:13:01 +00:00
}
void frmTcpServer::on_btnListen_clicked()
{
if (ui->btnListen->text() == "监听") {
isOk = server->start();
2019-09-29 05:13:01 +00:00
if (isOk) {
2023-10-27 06:31:09 +00:00
append(2, "监听成功");
2019-09-29 05:13:01 +00:00
ui->btnListen->setText("关闭");
2022-01-13 03:27:55 +00:00
} else {
2023-10-27 06:31:09 +00:00
append(3, QString("监听失败: %1").arg(server->errorString()));
2019-09-29 05:13:01 +00:00
}
} else {
isOk = false;
server->stop();
2019-09-29 05:13:01 +00:00
ui->btnListen->setText("监听");
}
}
void frmTcpServer::on_btnSave_clicked()
{
QString data = ui->txtMain->toPlainText();
AppData::saveData(data);
2019-09-29 05:13:01 +00:00
on_btnClear_clicked();
}
void frmTcpServer::on_btnClear_clicked()
{
append(0, "", true);
}
void frmTcpServer::on_btnSend_clicked()
{
if (!isOk) {
return;
}
QString data = ui->cboxData->currentText();
if (data.length() <= 0) {
return;
}
2019-12-05 08:01:43 +00:00
if (ui->ckSelectAll->isChecked()) {
server->writeData(data);
2019-12-05 08:01:43 +00:00
} else {
int row = ui->listWidget->currentRow();
if (row >= 0) {
QString str = ui->listWidget->item(row)->text();
QStringList list = str.split(":");
server->writeData(list.at(0), list.at(1).toInt(), data);
2019-12-05 08:01:43 +00:00
}
}
2019-09-29 05:13:01 +00:00
}
2021-08-22 02:57:02 +00:00
void frmTcpServer::on_btnRemove_clicked()
2019-09-29 05:13:01 +00:00
{
if (ui->ckSelectAll->isChecked()) {
server->remove();
2019-09-29 05:13:01 +00:00
} else {
int row = ui->listWidget->currentRow();
if (row >= 0) {
QString str = ui->listWidget->item(row)->text();
QStringList list = str.split(":");
server->remove(list.at(0), list.at(1).toInt());
2019-09-29 05:13:01 +00:00
}
}
}