彻底改版2.0
This commit is contained in:
13
tool/netserver/api/api.pri
Normal file
13
tool/netserver/api/api.pri
Normal file
@@ -0,0 +1,13 @@
|
||||
HEADERS += \
|
||||
$$PWD/appconfig.h \
|
||||
$$PWD/quihelper.h \
|
||||
$$PWD/quihelperdata.h \
|
||||
$$PWD/tcpserver1.h \
|
||||
$$PWD/tcpserver2.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/appconfig.cpp \
|
||||
$$PWD/quihelper.cpp \
|
||||
$$PWD/quihelperdata.cpp \
|
||||
$$PWD/tcpserver1.cpp \
|
||||
$$PWD/tcpserver2.cpp
|
||||
58
tool/netserver/api/appconfig.cpp
Normal file
58
tool/netserver/api/appconfig.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "appconfig.h"
|
||||
#include "quihelper.h"
|
||||
|
||||
QString AppConfig::ConfigFile = "config.ini";
|
||||
|
||||
int AppConfig::ListenPort1 = 6907;
|
||||
int AppConfig::CmdStart1 = 76;
|
||||
int AppConfig::CmdLen1 = 12;
|
||||
bool AppConfig::HexData1 = false;
|
||||
|
||||
int AppConfig::ListenPort2 = 6908;
|
||||
int AppConfig::CmdStart2 = 76;
|
||||
int AppConfig::CmdLen2 = 12;
|
||||
bool AppConfig::HexData2 = false;
|
||||
|
||||
void AppConfig::readConfig()
|
||||
{
|
||||
QSettings set(AppConfig::ConfigFile, QSettings::IniFormat);
|
||||
|
||||
set.beginGroup("AppConfig1");
|
||||
AppConfig::ListenPort1 = set.value("ListenPort1", AppConfig::ListenPort1).toInt();
|
||||
AppConfig::CmdStart1 = set.value("CmdStart1", AppConfig::CmdStart1).toInt();
|
||||
AppConfig::CmdLen1 = set.value("CmdLen1", AppConfig::CmdLen1).toInt();
|
||||
AppConfig::HexData1 = set.value("HexData1", AppConfig::HexData1).toBool();
|
||||
set.endGroup();
|
||||
|
||||
set.beginGroup("AppConfig2");
|
||||
AppConfig::ListenPort2 = set.value("ListenPort2", AppConfig::ListenPort2).toInt();
|
||||
AppConfig::CmdStart2 = set.value("CmdStart2", AppConfig::CmdStart2).toInt();
|
||||
AppConfig::CmdLen2 = set.value("CmdLen2", AppConfig::CmdLen2).toInt();
|
||||
AppConfig::HexData2 = set.value("HexData2", AppConfig::HexData2).toBool();
|
||||
set.endGroup();
|
||||
|
||||
//配置文件不存在或者不全则重新生成
|
||||
if (!QUIHelper::checkIniFile(AppConfig::ConfigFile)) {
|
||||
writeConfig();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void AppConfig::writeConfig()
|
||||
{
|
||||
QSettings set(AppConfig::ConfigFile, QSettings::IniFormat);
|
||||
|
||||
set.beginGroup("AppConfig1");
|
||||
set.setValue("ListenPort1", AppConfig::ListenPort1);
|
||||
set.setValue("CmdStart1", AppConfig::CmdStart1);
|
||||
set.setValue("CmdLen1", AppConfig::CmdLen1);
|
||||
set.setValue("HexData1", AppConfig::HexData1);
|
||||
set.endGroup();
|
||||
|
||||
set.beginGroup("AppConfig2");
|
||||
set.setValue("ListenPort2", AppConfig::ListenPort2);
|
||||
set.setValue("CmdStart2", AppConfig::CmdStart2);
|
||||
set.setValue("CmdLen2", AppConfig::CmdLen2);
|
||||
set.setValue("HexData2", AppConfig::HexData2);
|
||||
set.endGroup();
|
||||
}
|
||||
26
tool/netserver/api/appconfig.h
Normal file
26
tool/netserver/api/appconfig.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef APPCONFIG_H
|
||||
#define APPCONFIG_H
|
||||
|
||||
#include "head.h"
|
||||
|
||||
class AppConfig
|
||||
{
|
||||
public:
|
||||
static QString ConfigFile; //配置文件路径
|
||||
|
||||
static int ListenPort1; //监听端口1
|
||||
static int CmdStart1; //标识符1开始
|
||||
static int CmdLen1; //标识符1长度
|
||||
static bool HexData1; //16进制显示
|
||||
|
||||
static int ListenPort2; //监听端口2
|
||||
static int CmdStart2; //标识符2开始
|
||||
static int CmdLen2; //标识符2长度
|
||||
static bool HexData2; //16进制显示
|
||||
|
||||
//读写配置参数及其他操作
|
||||
static void readConfig(); //读取配置参数
|
||||
static void writeConfig(); //写入配置参数
|
||||
};
|
||||
|
||||
#endif // APPCONFIG_H
|
||||
420
tool/netserver/api/quihelper.cpp
Normal file
420
tool/netserver/api/quihelper.cpp
Normal file
@@ -0,0 +1,420 @@
|
||||
#include "quihelper.h"
|
||||
|
||||
int QUIHelper::getScreenIndex()
|
||||
{
|
||||
//需要对多个屏幕进行处理
|
||||
int screenIndex = 0;
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
int screenCount = qApp->screens().count();
|
||||
#else
|
||||
int screenCount = qApp->desktop()->screenCount();
|
||||
#endif
|
||||
|
||||
if (screenCount > 1) {
|
||||
//找到当前鼠标所在屏幕
|
||||
QPoint pos = QCursor::pos();
|
||||
for (int i = 0; i < screenCount; ++i) {
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
if (qApp->screens().at(i)->geometry().contains(pos)) {
|
||||
#else
|
||||
if (qApp->desktop()->screenGeometry(i).contains(pos)) {
|
||||
#endif
|
||||
screenIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return screenIndex;
|
||||
}
|
||||
|
||||
QRect QUIHelper::getScreenRect(bool available)
|
||||
{
|
||||
QRect rect;
|
||||
int screenIndex = QUIHelper::getScreenIndex();
|
||||
if (available) {
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
rect = qApp->screens().at(screenIndex)->availableGeometry();
|
||||
#else
|
||||
rect = qApp->desktop()->availableGeometry(screenIndex);
|
||||
#endif
|
||||
} else {
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
rect = qApp->screens().at(screenIndex)->geometry();
|
||||
#else
|
||||
rect = qApp->desktop()->screenGeometry(screenIndex);
|
||||
#endif
|
||||
}
|
||||
return rect;
|
||||
}
|
||||
|
||||
int QUIHelper::deskWidth()
|
||||
{
|
||||
return getScreenRect().width();
|
||||
}
|
||||
|
||||
int QUIHelper::deskHeight()
|
||||
{
|
||||
return getScreenRect().height();
|
||||
}
|
||||
|
||||
QWidget *QUIHelper::centerBaseForm = 0;
|
||||
void QUIHelper::setFormInCenter(QWidget *form)
|
||||
{
|
||||
int formWidth = form->width();
|
||||
int formHeight = form->height();
|
||||
|
||||
//如果=0表示采用系统桌面屏幕为参照
|
||||
QRect rect;
|
||||
if (centerBaseForm == 0) {
|
||||
rect = getScreenRect();
|
||||
} else {
|
||||
rect = centerBaseForm->geometry();
|
||||
}
|
||||
|
||||
int deskWidth = rect.width();
|
||||
int deskHeight = rect.height();
|
||||
QPoint movePoint(deskWidth / 2 - formWidth / 2 + rect.x(), deskHeight / 2 - formHeight / 2 + rect.y());
|
||||
form->move(movePoint);
|
||||
}
|
||||
|
||||
QString QUIHelper::appName()
|
||||
{
|
||||
//没有必要每次都获取,只有当变量为空时才去获取一次
|
||||
static QString name;
|
||||
if (name.isEmpty()) {
|
||||
name = qApp->applicationFilePath();
|
||||
//下面的方法主要为了过滤安卓的路径 lib程序名_armeabi-v7a
|
||||
QStringList list = name.split("/");
|
||||
name = list.at(list.count() - 1).split(".").at(0);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
QString QUIHelper::appPath()
|
||||
{
|
||||
#ifdef Q_OS_ANDROID
|
||||
//return QString("/sdcard/Android/%1").arg(appName());
|
||||
return QString("/storage/emulated/0/%1").arg(appName());
|
||||
#else
|
||||
return qApp->applicationDirPath();
|
||||
#endif
|
||||
}
|
||||
|
||||
QString QUIHelper::getUuid()
|
||||
{
|
||||
QString uuid = QUuid::createUuid().toString();
|
||||
uuid.replace("{", "");
|
||||
uuid.replace("}", "");
|
||||
return uuid;
|
||||
}
|
||||
|
||||
void QUIHelper::initRand()
|
||||
{
|
||||
//初始化随机数种子
|
||||
QTime t = QTime::currentTime();
|
||||
srand(t.msec() + t.second() * 1000);
|
||||
}
|
||||
|
||||
void QUIHelper::newDir(const QString &dirName)
|
||||
{
|
||||
QString strDir = dirName;
|
||||
|
||||
//如果路径中包含斜杠字符则说明是绝对路径
|
||||
//linux系统路径字符带有 / windows系统 路径字符带有 :/
|
||||
if (!strDir.startsWith("/") && !strDir.contains(":/")) {
|
||||
strDir = QString("%1/%2").arg(QUIHelper::appPath()).arg(strDir);
|
||||
}
|
||||
|
||||
QDir dir(strDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkpath(strDir);
|
||||
}
|
||||
}
|
||||
|
||||
void QUIHelper::sleep(int msec)
|
||||
{
|
||||
if (msec <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
QThread::msleep(msec);
|
||||
#else
|
||||
QTime endTime = QTime::currentTime().addMSecs(msec);
|
||||
while (QTime::currentTime() < endTime) {
|
||||
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void QUIHelper::setStyle()
|
||||
{
|
||||
//打印下所有内置风格的名字
|
||||
qDebug() << "Qt内置的样式" << QStyleFactory::keys();
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
qApp->setStyle(QStyleFactory::create("Fusion"));
|
||||
#else
|
||||
qApp->setStyle(QStyleFactory::create("Cleanlooks"));
|
||||
#endif
|
||||
//qApp->setPalette(QPalette("#FFFFFF"));
|
||||
}
|
||||
|
||||
void QUIHelper::setFont(int fontSize)
|
||||
{
|
||||
QFont font;
|
||||
font.setFamily("MicroSoft Yahei");
|
||||
#ifdef Q_OS_ANDROID
|
||||
font.setPixelSize(15);
|
||||
#elif __arm__
|
||||
font.setPixelSize(25);
|
||||
#else
|
||||
font.setPixelSize(fontSize);
|
||||
#endif
|
||||
|
||||
#ifndef rk3399
|
||||
qApp->setFont(font);
|
||||
#endif
|
||||
}
|
||||
|
||||
void QUIHelper::setCode(bool utf8)
|
||||
{
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
//如果想要控制台打印信息中文正常就注释掉这个设置
|
||||
if (utf8) {
|
||||
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
}
|
||||
#else
|
||||
#if _MSC_VER
|
||||
QTextCodec *codec = QTextCodec::codecForName("gbk");
|
||||
#else
|
||||
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
||||
#endif
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
QTextCodec::setCodecForCStrings(codec);
|
||||
QTextCodec::setCodecForTr(codec);
|
||||
#endif
|
||||
}
|
||||
|
||||
void QUIHelper::setTranslator(const QString &qmFile)
|
||||
{
|
||||
//过滤下不存在的就不用设置了
|
||||
if (!QFile(qmFile).exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QTranslator *translator = new QTranslator(qApp);
|
||||
translator->load(qmFile);
|
||||
qApp->installTranslator(translator);
|
||||
}
|
||||
|
||||
void QUIHelper::initAll()
|
||||
{
|
||||
//初始化随机数种子
|
||||
QUIHelper::initRand();
|
||||
//设置样式风格
|
||||
QUIHelper::setStyle();
|
||||
//设置字体
|
||||
QUIHelper::setFont(13);
|
||||
//设置编码
|
||||
QUIHelper::setCode();
|
||||
//设置翻译文件支持多个
|
||||
QUIHelper::setTranslator(":/qm/widgets.qm");
|
||||
QUIHelper::setTranslator(":/qm/qt_zh_CN.qm");
|
||||
QUIHelper::setTranslator(":/qm/designer_zh_CN.qm");
|
||||
}
|
||||
|
||||
void QUIHelper::setFramelessForm(QWidget *widgetMain, bool tool, bool top, bool menu)
|
||||
{
|
||||
widgetMain->setProperty("form", true);
|
||||
widgetMain->setProperty("canMove", true);
|
||||
|
||||
//根据设定逐个追加属性
|
||||
#ifdef __arm__
|
||||
widgetMain->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
|
||||
#else
|
||||
widgetMain->setWindowFlags(Qt::FramelessWindowHint);
|
||||
#endif
|
||||
if (tool) {
|
||||
widgetMain->setWindowFlags(widgetMain->windowFlags() | Qt::Tool);
|
||||
}
|
||||
if (top) {
|
||||
widgetMain->setWindowFlags(widgetMain->windowFlags() | Qt::WindowStaysOnTopHint);
|
||||
}
|
||||
if (menu) {
|
||||
//如果是其他系统比如neokylin会产生系统边框
|
||||
#ifdef Q_OS_WIN
|
||||
widgetMain->setWindowFlags(widgetMain->windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
int QUIHelper::showMessageBox(const QString &info, int type, int closeSec, bool exec)
|
||||
{
|
||||
int result = 0;
|
||||
if (type == 0) {
|
||||
showMessageBoxInfo(info, closeSec, exec);
|
||||
} else if (type == 1) {
|
||||
showMessageBoxError(info, closeSec, exec);
|
||||
} else if (type == 2) {
|
||||
result = showMessageBoxQuestion(info);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void QUIHelper::showMessageBoxInfo(const QString &info, int closeSec, bool exec)
|
||||
{
|
||||
QMessageBox box(QMessageBox::Information, "提示", info);
|
||||
box.setStandardButtons(QMessageBox::Yes);
|
||||
box.setButtonText(QMessageBox::Yes, QString("确 定"));
|
||||
box.exec();
|
||||
//QMessageBox::information(0, "提示", info, QMessageBox::Yes);
|
||||
}
|
||||
|
||||
void QUIHelper::showMessageBoxError(const QString &info, int closeSec, bool exec)
|
||||
{
|
||||
QMessageBox box(QMessageBox::Critical, "错误", info);
|
||||
box.setStandardButtons(QMessageBox::Yes);
|
||||
box.setButtonText(QMessageBox::Yes, QString("确 定"));
|
||||
box.exec();
|
||||
//QMessageBox::critical(0, "错误", info, QMessageBox::Yes);
|
||||
}
|
||||
|
||||
int QUIHelper::showMessageBoxQuestion(const QString &info)
|
||||
{
|
||||
QMessageBox box(QMessageBox::Question, "询问", info);
|
||||
box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
||||
box.setButtonText(QMessageBox::Yes, QString("确 定"));
|
||||
box.setButtonText(QMessageBox::No, QString("取 消"));
|
||||
return box.exec();
|
||||
//return QMessageBox::question(0, "询问", info, QMessageBox::Yes | QMessageBox::No);
|
||||
}
|
||||
|
||||
QString QUIHelper::getXorEncryptDecrypt(const QString &value, char key)
|
||||
{
|
||||
//矫正范围外的数据
|
||||
if (key < 0 || key >= 127) {
|
||||
key = 127;
|
||||
}
|
||||
|
||||
QString result = value;
|
||||
int count = result.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
result[i] = QChar(result.at(i).toLatin1() ^ key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
uchar QUIHelper::getOrCode(const QByteArray &data)
|
||||
{
|
||||
int len = data.length();
|
||||
uchar result = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
result ^= data.at(i);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uchar QUIHelper::getCheckCode(const QByteArray &data)
|
||||
{
|
||||
int len = data.length();
|
||||
uchar temp = 0;
|
||||
for (uchar i = 0; i < len; i++) {
|
||||
temp += data.at(i);
|
||||
}
|
||||
|
||||
return temp % 256;
|
||||
}
|
||||
|
||||
void QUIHelper::initTableView(QTableView *tableView, int rowHeight, bool headVisible, bool edit, bool stretchLast)
|
||||
{
|
||||
//取消自动换行
|
||||
tableView->setWordWrap(false);
|
||||
//超出文本不显示省略号
|
||||
tableView->setTextElideMode(Qt::ElideNone);
|
||||
//奇数偶数行颜色交替
|
||||
tableView->setAlternatingRowColors(false);
|
||||
//垂直表头是否可见
|
||||
tableView->verticalHeader()->setVisible(headVisible);
|
||||
//选中一行表头是否加粗
|
||||
tableView->horizontalHeader()->setHighlightSections(false);
|
||||
//最后一行拉伸填充
|
||||
tableView->horizontalHeader()->setStretchLastSection(stretchLast);
|
||||
//行标题最小宽度尺寸
|
||||
tableView->horizontalHeader()->setMinimumSectionSize(0);
|
||||
//行标题最小高度,等同于和默认行高一致
|
||||
tableView->horizontalHeader()->setFixedHeight(rowHeight);
|
||||
//默认行高
|
||||
tableView->verticalHeader()->setDefaultSectionSize(rowHeight);
|
||||
//选中时一行整体选中
|
||||
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
//只允许选择单个
|
||||
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
|
||||
//表头不可单击
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
tableView->horizontalHeader()->setSectionsClickable(false);
|
||||
#else
|
||||
tableView->horizontalHeader()->setClickable(false);
|
||||
#endif
|
||||
|
||||
//鼠标按下即进入编辑模式
|
||||
if (edit) {
|
||||
tableView->setEditTriggers(QAbstractItemView::CurrentChanged | QAbstractItemView::DoubleClicked);
|
||||
} else {
|
||||
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
}
|
||||
}
|
||||
|
||||
void QUIHelper::openFile(const QString &fileName, const QString &msg)
|
||||
{
|
||||
#ifdef __arm__
|
||||
return;
|
||||
#endif
|
||||
if (fileName.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (QUIHelper::showMessageBoxQuestion(msg + "成功!确定现在就打开吗?") == QMessageBox::Yes) {
|
||||
QString url = QString("file:///%1").arg(fileName);
|
||||
QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode));
|
||||
}
|
||||
}
|
||||
|
||||
bool QUIHelper::checkIniFile(const QString &iniFile)
|
||||
{
|
||||
//如果配置文件大小为0,则以初始值继续运行,并生成配置文件
|
||||
QFile file(iniFile);
|
||||
if (file.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//如果配置文件不完整,则以初始值继续运行,并生成配置文件
|
||||
if (file.open(QFile::ReadOnly)) {
|
||||
bool ok = true;
|
||||
while (!file.atEnd()) {
|
||||
QString line = file.readLine();
|
||||
line.replace("\r", "");
|
||||
line.replace("\n", "");
|
||||
QStringList list = line.split("=");
|
||||
|
||||
if (list.count() == 2) {
|
||||
if (list.at(1) == "") {
|
||||
qDebug() << "ini node no value" << list.at(0);
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
70
tool/netserver/api/quihelper.h
Normal file
70
tool/netserver/api/quihelper.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef QUIHELPER2_H
|
||||
#define QUIHELPER2_H
|
||||
|
||||
#include "head.h"
|
||||
|
||||
class QUIHelper
|
||||
{
|
||||
public:
|
||||
//获取当前鼠标所在屏幕索引+尺寸
|
||||
static int getScreenIndex();
|
||||
static QRect getScreenRect(bool available = true);
|
||||
|
||||
//获取桌面宽度高度+居中显示
|
||||
static int deskWidth();
|
||||
static int deskHeight();
|
||||
|
||||
//居中显示窗体
|
||||
//定义标志位指定是以桌面为参照还是主程序界面为参照
|
||||
static QWidget *centerBaseForm;
|
||||
static void setFormInCenter(QWidget *form);
|
||||
|
||||
//程序文件名称+当前所在路径
|
||||
static QString appName();
|
||||
static QString appPath();
|
||||
|
||||
//获取uuid+初始化随机数种子+新建目录+延时
|
||||
static QString getUuid();
|
||||
static void initRand();
|
||||
static void newDir(const QString &dirName);
|
||||
static void sleep(int msec);
|
||||
|
||||
//设置样式+字体+编码+居中+翻译
|
||||
static void setStyle();
|
||||
static void setFont(int fontSize = 12);
|
||||
static void setCode(bool utf8 = true);
|
||||
static void setTranslator(const QString &qmFile);
|
||||
//一次性设置所有
|
||||
static void initAll();
|
||||
|
||||
//设置无边框
|
||||
static void setFramelessForm(QWidget *widgetMain, bool tool = false, bool top = false, bool menu = true);
|
||||
|
||||
//弹出框
|
||||
static int showMessageBox(const QString &info, int type = 0, int closeSec = 0, bool exec = false);
|
||||
//弹出消息框
|
||||
static void showMessageBoxInfo(const QString &info, int closeSec = 0, bool exec = false);
|
||||
//弹出错误框
|
||||
static void showMessageBoxError(const QString &info, int closeSec = 0, bool exec = false);
|
||||
//弹出询问框
|
||||
static int showMessageBoxQuestion(const QString &info);
|
||||
|
||||
//异或加密-只支持字符,如果是中文需要将其转换base64编码
|
||||
static QString getXorEncryptDecrypt(const QString &value, char key);
|
||||
//异或校验
|
||||
static uchar getOrCode(const QByteArray &data);
|
||||
//计算校验码
|
||||
static uchar getCheckCode(const QByteArray &data);
|
||||
|
||||
//初始化表格
|
||||
static void initTableView(QTableView *tableView, int rowHeight = 25,
|
||||
bool headVisible = false, bool edit = false,
|
||||
bool stretchLast = true);
|
||||
//打开文件带提示框
|
||||
static void openFile(const QString &fileName, const QString &msg);
|
||||
|
||||
//检查ini配置文件
|
||||
static bool checkIniFile(const QString &iniFile);
|
||||
};
|
||||
|
||||
#endif // QUIHELPER2_H
|
||||
450
tool/netserver/api/quihelperdata.cpp
Normal file
450
tool/netserver/api/quihelperdata.cpp
Normal file
@@ -0,0 +1,450 @@
|
||||
#include "quihelperdata.h"
|
||||
#include "quihelper.h"
|
||||
|
||||
int QUIHelperData::strHexToDecimal(const QString &strHex)
|
||||
{
|
||||
bool ok;
|
||||
return strHex.toInt(&ok, 16);
|
||||
}
|
||||
|
||||
int QUIHelperData::strDecimalToDecimal(const QString &strDecimal)
|
||||
{
|
||||
bool ok;
|
||||
return strDecimal.toInt(&ok, 10);
|
||||
}
|
||||
|
||||
int QUIHelperData::strBinToDecimal(const QString &strBin)
|
||||
{
|
||||
bool ok;
|
||||
return strBin.toInt(&ok, 2);
|
||||
}
|
||||
|
||||
QString QUIHelperData::strHexToStrBin(const QString &strHex)
|
||||
{
|
||||
uchar decimal = strHexToDecimal(strHex);
|
||||
QString bin = QString::number(decimal, 2);
|
||||
uchar len = bin.length();
|
||||
|
||||
if (len < 8) {
|
||||
for (int i = 0; i < 8 - len; i++) {
|
||||
bin = "0" + bin;
|
||||
}
|
||||
}
|
||||
|
||||
return bin;
|
||||
}
|
||||
|
||||
QString QUIHelperData::decimalToStrBin1(int decimal)
|
||||
{
|
||||
QString bin = QString::number(decimal, 2);
|
||||
uchar len = bin.length();
|
||||
if (len <= 8) {
|
||||
for (int i = 0; i < 8 - len; i++) {
|
||||
bin = "0" + bin;
|
||||
}
|
||||
}
|
||||
|
||||
return bin;
|
||||
}
|
||||
|
||||
QString QUIHelperData::decimalToStrBin2(int decimal)
|
||||
{
|
||||
QString bin = QString::number(decimal, 2);
|
||||
uchar len = bin.length();
|
||||
if (len <= 16) {
|
||||
for (int i = 0; i < 16 - len; i++) {
|
||||
bin = "0" + bin;
|
||||
}
|
||||
}
|
||||
|
||||
return bin;
|
||||
}
|
||||
|
||||
QString QUIHelperData::decimalToStrHex(int decimal)
|
||||
{
|
||||
QString temp = QString::number(decimal, 16);
|
||||
if (temp.length() == 1) {
|
||||
temp = "0" + temp;
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::intToByte(int data)
|
||||
{
|
||||
QByteArray result;
|
||||
result.resize(4);
|
||||
result[3] = (uchar)(0x000000ff & data);
|
||||
result[2] = (uchar)((0x0000ff00 & data) >> 8);
|
||||
result[1] = (uchar)((0x00ff0000 & data) >> 16);
|
||||
result[0] = (uchar)((0xff000000 & data) >> 24);
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::intToByteRec(int data)
|
||||
{
|
||||
QByteArray result;
|
||||
result.resize(4);
|
||||
result[0] = (uchar)(0x000000ff & data);
|
||||
result[1] = (uchar)((0x0000ff00 & data) >> 8);
|
||||
result[2] = (uchar)((0x00ff0000 & data) >> 16);
|
||||
result[3] = (uchar)((0xff000000 & data) >> 24);
|
||||
return result;
|
||||
}
|
||||
|
||||
int QUIHelperData::byteToInt(const QByteArray &data)
|
||||
{
|
||||
int i = data.at(3) & 0x000000ff;
|
||||
i |= ((data.at(2) << 8) & 0x0000ff00);
|
||||
i |= ((data.at(1) << 16) & 0x00ff0000);
|
||||
i |= ((data.at(0) << 24) & 0xff000000);
|
||||
return i;
|
||||
}
|
||||
|
||||
int QUIHelperData::byteToIntRec(const QByteArray &data)
|
||||
{
|
||||
int i = data.at(0) & 0x000000ff;
|
||||
i |= ((data.at(1) << 8) & 0x0000ff00);
|
||||
i |= ((data.at(2) << 16) & 0x00ff0000);
|
||||
i |= ((data.at(3) << 24) & 0xff000000);
|
||||
return i;
|
||||
}
|
||||
|
||||
quint32 QUIHelperData::byteToUInt(const QByteArray &data)
|
||||
{
|
||||
quint32 i = data.at(3) & 0x000000ff;
|
||||
i |= ((data.at(2) << 8) & 0x0000ff00);
|
||||
i |= ((data.at(1) << 16) & 0x00ff0000);
|
||||
i |= ((data.at(0) << 24) & 0xff000000);
|
||||
return i;
|
||||
}
|
||||
|
||||
quint32 QUIHelperData::byteToUIntRec(const QByteArray &data)
|
||||
{
|
||||
quint32 i = data.at(0) & 0x000000ff;
|
||||
i |= ((data.at(1) << 8) & 0x0000ff00);
|
||||
i |= ((data.at(2) << 16) & 0x00ff0000);
|
||||
i |= ((data.at(3) << 24) & 0xff000000);
|
||||
return i;
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::ushortToByte(ushort data)
|
||||
{
|
||||
QByteArray result;
|
||||
result.resize(2);
|
||||
result[1] = (uchar)(0x000000ff & data);
|
||||
result[0] = (uchar)((0x0000ff00 & data) >> 8);
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::ushortToByteRec(ushort data)
|
||||
{
|
||||
QByteArray result;
|
||||
result.resize(2);
|
||||
result[0] = (uchar)(0x000000ff & data);
|
||||
result[1] = (uchar)((0x0000ff00 & data) >> 8);
|
||||
return result;
|
||||
}
|
||||
|
||||
int QUIHelperData::byteToUShort(const QByteArray &data)
|
||||
{
|
||||
int i = data.at(1) & 0x000000FF;
|
||||
i |= ((data.at(0) << 8) & 0x0000FF00);
|
||||
if (i >= 32768) {
|
||||
i = i - 65536;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
int QUIHelperData::byteToUShortRec(const QByteArray &data)
|
||||
{
|
||||
int i = data.at(0) & 0x000000FF;
|
||||
i |= ((data.at(1) << 8) & 0x0000FF00);
|
||||
if (i >= 32768) {
|
||||
i = i - 65536;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
QString QUIHelperData::getValue(quint8 value)
|
||||
{
|
||||
QString result = QString::number(value);
|
||||
if (result.length() <= 1) {
|
||||
result = QString("0%1").arg(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QString QUIHelperData::getXorEncryptDecrypt(const QString &value, char key)
|
||||
{
|
||||
//矫正范围外的数据
|
||||
if (key < 0 || key >= 127) {
|
||||
key = 127;
|
||||
}
|
||||
|
||||
QString result = value;
|
||||
int count = result.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
result[i] = QChar(result.at(i).toLatin1() ^ key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
uchar QUIHelperData::getOrCode(const QByteArray &data)
|
||||
{
|
||||
int len = data.length();
|
||||
uchar result = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
result ^= data.at(i);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uchar QUIHelperData::getCheckCode(const QByteArray &data)
|
||||
{
|
||||
int len = data.length();
|
||||
uchar temp = 0;
|
||||
for (uchar i = 0; i < len; i++) {
|
||||
temp += data.at(i);
|
||||
}
|
||||
|
||||
return temp % 256;
|
||||
}
|
||||
|
||||
//函数功能:计算CRC16
|
||||
//参数1:*data 16位CRC校验数据,
|
||||
//参数2:len 数据流长度
|
||||
//参数3:init 初始化值
|
||||
//参数4:table 16位CRC查找表
|
||||
|
||||
//正序CRC计算
|
||||
quint16 QUIHelperData::getCrc16(quint8 *data, int len, quint16 init, const quint16 *table)
|
||||
{
|
||||
quint16 crc_16 = init;
|
||||
quint8 temp;
|
||||
while (len-- > 0) {
|
||||
temp = crc_16 & 0xff;
|
||||
crc_16 = (crc_16 >> 8) ^ table[(temp ^ *data++) & 0xff];
|
||||
}
|
||||
|
||||
return crc_16;
|
||||
}
|
||||
|
||||
//逆序CRC计算
|
||||
quint16 QUIHelperData::getCrc16Rec(quint8 *data, int len, quint16 init, const quint16 *table)
|
||||
{
|
||||
quint16 crc_16 = init;
|
||||
quint8 temp;
|
||||
while (len-- > 0) {
|
||||
temp = crc_16 >> 8;
|
||||
crc_16 = (crc_16 << 8) ^ table[(temp ^ *data++) & 0xff];
|
||||
}
|
||||
|
||||
return crc_16;
|
||||
}
|
||||
|
||||
//Modbus CRC16校验
|
||||
quint16 QUIHelperData::getModbus16(quint8 *data, int len)
|
||||
{
|
||||
//MODBUS CRC-16表 8005 逆序
|
||||
const quint16 table_16[256] = {
|
||||
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
|
||||
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
|
||||
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
|
||||
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
|
||||
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
|
||||
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
|
||||
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
|
||||
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
|
||||
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
|
||||
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
|
||||
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
|
||||
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
|
||||
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
|
||||
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
|
||||
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
|
||||
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
|
||||
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
|
||||
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
|
||||
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
|
||||
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
|
||||
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
|
||||
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
|
||||
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
|
||||
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
|
||||
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
|
||||
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
|
||||
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
|
||||
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
|
||||
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
|
||||
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
|
||||
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
|
||||
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
|
||||
};
|
||||
|
||||
return getCrc16(data, len, 0xFFFF, table_16);
|
||||
}
|
||||
|
||||
//CRC16校验
|
||||
QByteArray QUIHelperData::getCrcCode(const QByteArray &data)
|
||||
{
|
||||
quint16 result = getModbus16((quint8 *)data.data(), data.length());
|
||||
return QUIHelperData::ushortToByteRec(result);
|
||||
}
|
||||
|
||||
static QMap<char, QString> listChar;
|
||||
void QUIHelperData::initAscii()
|
||||
{
|
||||
//0x20为空格,空格以下都是不可见字符
|
||||
if (listChar.count() == 0) {
|
||||
listChar.insert(0, "\\NUL");
|
||||
listChar.insert(1, "\\SOH");
|
||||
listChar.insert(2, "\\STX");
|
||||
listChar.insert(3, "\\ETX");
|
||||
listChar.insert(4, "\\EOT");
|
||||
listChar.insert(5, "\\ENQ");
|
||||
listChar.insert(6, "\\ACK");
|
||||
listChar.insert(7, "\\BEL");
|
||||
listChar.insert(8, "\\BS");
|
||||
listChar.insert(9, "\\HT");
|
||||
listChar.insert(10, "\\LF");
|
||||
listChar.insert(11, "\\VT");
|
||||
listChar.insert(12, "\\FF");
|
||||
listChar.insert(13, "\\CR");
|
||||
listChar.insert(14, "\\SO");
|
||||
listChar.insert(15, "\\SI");
|
||||
listChar.insert(16, "\\DLE");
|
||||
listChar.insert(17, "\\DC1");
|
||||
listChar.insert(18, "\\DC2");
|
||||
listChar.insert(19, "\\DC3");
|
||||
listChar.insert(20, "\\DC4");
|
||||
listChar.insert(21, "\\NAK");
|
||||
listChar.insert(22, "\\SYN");
|
||||
listChar.insert(23, "\\ETB");
|
||||
listChar.insert(24, "\\CAN");
|
||||
listChar.insert(25, "\\EM");
|
||||
listChar.insert(26, "\\SUB");
|
||||
listChar.insert(27, "\\ESC");
|
||||
listChar.insert(28, "\\FS");
|
||||
listChar.insert(29, "\\GS");
|
||||
listChar.insert(30, "\\RS");
|
||||
listChar.insert(31, "\\US");
|
||||
listChar.insert(0x5C, "\\");
|
||||
listChar.insert(0x7F, "\\DEL");
|
||||
}
|
||||
}
|
||||
|
||||
QString QUIHelperData::byteArrayToAsciiStr(const QByteArray &data)
|
||||
{
|
||||
//先初始化字符表
|
||||
initAscii();
|
||||
|
||||
QString temp;
|
||||
int len = data.size();
|
||||
for (int i = 0; i < len; i++) {
|
||||
char byte = data.at(i);
|
||||
QString value = listChar.value(byte);
|
||||
if (!value.isEmpty()) {
|
||||
} else if (byte >= 0 && byte <= 0x7F) {
|
||||
value = QString("%1").arg(byte);
|
||||
} else {
|
||||
value = decimalToStrHex((quint8)byte);
|
||||
value = QString("\\x%1").arg(value.toUpper());
|
||||
}
|
||||
|
||||
temp += value;
|
||||
}
|
||||
|
||||
return temp.trimmed();
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::asciiStrToByteArray(const QString &data)
|
||||
{
|
||||
//先初始化字符表
|
||||
initAscii();
|
||||
|
||||
QByteArray buffer;
|
||||
QStringList list = data.split("\\");
|
||||
|
||||
int count = list.count();
|
||||
for (int i = 1; i < count; i++) {
|
||||
QString str = list.at(i);
|
||||
int key = 0;
|
||||
if (str.contains("x")) {
|
||||
key = strHexToDecimal(str.mid(1, 2));
|
||||
} else {
|
||||
key = listChar.key("\\" + str);
|
||||
}
|
||||
|
||||
buffer.append(key);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
char QUIHelperData::hexStrToChar(char data)
|
||||
{
|
||||
if ((data >= '0') && (data <= '9')) {
|
||||
return data - 0x30;
|
||||
} else if ((data >= 'A') && (data <= 'F')) {
|
||||
return data - 'A' + 10;
|
||||
} else if ((data >= 'a') && (data <= 'f')) {
|
||||
return data - 'a' + 10;
|
||||
} else {
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray QUIHelperData::hexStrToByteArray(const QString &data)
|
||||
{
|
||||
QByteArray senddata;
|
||||
int hexdata, lowhexdata;
|
||||
int hexdatalen = 0;
|
||||
int len = data.length();
|
||||
senddata.resize(len / 2);
|
||||
char lstr, hstr;
|
||||
|
||||
for (int i = 0; i < len;) {
|
||||
hstr = data.at(i).toLatin1();
|
||||
if (hstr == ' ') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
i++;
|
||||
if (i >= len) {
|
||||
break;
|
||||
}
|
||||
|
||||
lstr = data.at(i).toLatin1();
|
||||
hexdata = hexStrToChar(hstr);
|
||||
lowhexdata = hexStrToChar(lstr);
|
||||
|
||||
if ((hexdata == 16) || (lowhexdata == 16)) {
|
||||
break;
|
||||
} else {
|
||||
hexdata = hexdata * 16 + lowhexdata;
|
||||
}
|
||||
|
||||
i++;
|
||||
senddata[hexdatalen] = (char)hexdata;
|
||||
hexdatalen++;
|
||||
}
|
||||
|
||||
senddata.resize(hexdatalen);
|
||||
return senddata;
|
||||
}
|
||||
|
||||
QString QUIHelperData::byteArrayToHexStr(const QByteArray &data)
|
||||
{
|
||||
QString temp = "";
|
||||
QString hex = data.toHex();
|
||||
for (int i = 0; i < hex.length(); i = i + 2) {
|
||||
temp += hex.mid(i, 2) + " ";
|
||||
}
|
||||
|
||||
return temp.trimmed().toUpper();
|
||||
}
|
||||
70
tool/netserver/api/quihelperdata.h
Normal file
70
tool/netserver/api/quihelperdata.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef QUIHELPERDATA_H
|
||||
#define QUIHELPERDATA_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class QUIHelperData
|
||||
{
|
||||
public:
|
||||
//16进制字符串转10进制
|
||||
static int strHexToDecimal(const QString &strHex);
|
||||
//10进制字符串转10进制
|
||||
static int strDecimalToDecimal(const QString &strDecimal);
|
||||
//2进制字符串转10进制
|
||||
static int strBinToDecimal(const QString &strBin);
|
||||
|
||||
//16进制字符串转2进制字符串
|
||||
static QString strHexToStrBin(const QString &strHex);
|
||||
//10进制转2进制字符串一个字节
|
||||
static QString decimalToStrBin1(int decimal);
|
||||
//10进制转2进制字符串两个字节
|
||||
static QString decimalToStrBin2(int decimal);
|
||||
//10进制转16进制字符串,补零.
|
||||
static QString decimalToStrHex(int decimal);
|
||||
|
||||
//int转字节数组
|
||||
static QByteArray intToByte(int data);
|
||||
static QByteArray intToByteRec(int data);
|
||||
|
||||
//字节数组转int
|
||||
static int byteToInt(const QByteArray &data);
|
||||
static int byteToIntRec(const QByteArray &data);
|
||||
static quint32 byteToUInt(const QByteArray &data);
|
||||
static quint32 byteToUIntRec(const QByteArray &data);
|
||||
|
||||
//ushort转字节数组
|
||||
static QByteArray ushortToByte(ushort data);
|
||||
static QByteArray ushortToByteRec(ushort data);
|
||||
|
||||
//字节数组转ushort
|
||||
static int byteToUShort(const QByteArray &data);
|
||||
static int byteToUShortRec(const QByteArray &data);
|
||||
|
||||
//字符串补全
|
||||
static QString getValue(quint8 value);
|
||||
|
||||
//异或加密-只支持字符,如果是中文需要将其转换base64编码
|
||||
static QString getXorEncryptDecrypt(const QString &value, char key);
|
||||
//异或校验
|
||||
static uchar getOrCode(const QByteArray &data);
|
||||
//计算校验码
|
||||
static uchar getCheckCode(const QByteArray &data);
|
||||
|
||||
//CRC校验
|
||||
static quint16 getCrc16Rec(quint8 *data, int len, quint16 init, const quint16 *table);
|
||||
static quint16 getCrc16(quint8 *data, int len, quint16 init, const quint16 *table);
|
||||
static quint16 getModbus16(quint8 *data, int len);
|
||||
static QByteArray getCrcCode(const QByteArray &data);
|
||||
|
||||
//字节数组与Ascii字符串互转
|
||||
static void initAscii();
|
||||
static QString byteArrayToAsciiStr(const QByteArray &data);
|
||||
static QByteArray asciiStrToByteArray(const QString &data);
|
||||
|
||||
//16进制字符串与字节数组互转
|
||||
static char hexStrToChar(char data);
|
||||
static QByteArray hexStrToByteArray(const QString &data);
|
||||
static QString byteArrayToHexStr(const QByteArray &data);
|
||||
};
|
||||
|
||||
#endif // QUIHELPERDATA_H
|
||||
156
tool/netserver/api/tcpserver1.cpp
Normal file
156
tool/netserver/api/tcpserver1.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
#include "tcpserver1.h"
|
||||
#include "quihelper.h"
|
||||
#include "quihelperdata.h"
|
||||
|
||||
TcpClient1::TcpClient1(QObject *parent) : QTcpSocket(parent)
|
||||
{
|
||||
ip = "127.0.0.1";
|
||||
port = 6907;
|
||||
deviceID = "SSJC00000001";
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
|
||||
connect(this, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(deleteLater()));
|
||||
#else
|
||||
connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(deleteLater()));
|
||||
#endif
|
||||
connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
|
||||
connect(this, SIGNAL(readyRead()), this, SLOT(readData()));
|
||||
}
|
||||
|
||||
void TcpClient1::setIP(const QString &ip)
|
||||
{
|
||||
this->ip = ip;
|
||||
}
|
||||
|
||||
QString TcpClient1::getIP() const
|
||||
{
|
||||
return this->ip;
|
||||
}
|
||||
|
||||
void TcpClient1::setPort(int port)
|
||||
{
|
||||
this->port = port;
|
||||
}
|
||||
|
||||
int TcpClient1::getPort() const
|
||||
{
|
||||
return this->port;
|
||||
}
|
||||
|
||||
QString TcpClient1::getDeviceID()
|
||||
{
|
||||
return this->deviceID;
|
||||
}
|
||||
|
||||
void TcpClient1::readData()
|
||||
{
|
||||
QByteArray data = this->readAll();
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
//取出唯一标识符,并过滤,可自行更改过滤条件
|
||||
QByteArray cmd = data.mid(AppConfig::CmdStart1, AppConfig::CmdLen1);
|
||||
QString id = QString(cmd);
|
||||
if (id.startsWith("S") && deviceID != id) {
|
||||
deviceID = id;
|
||||
//发送信号更新标识符
|
||||
emit receiveDeviceID(ip, port, deviceID);
|
||||
}
|
||||
|
||||
QString buffer;
|
||||
if (AppConfig::HexData1) {
|
||||
buffer = QUIHelperData::byteArrayToHexStr(data);
|
||||
} else {
|
||||
buffer = QString(data);
|
||||
}
|
||||
|
||||
emit receiveData(ip, port, deviceID, buffer);
|
||||
}
|
||||
|
||||
void TcpClient1::sendData(const QString &data)
|
||||
{
|
||||
QByteArray buffer;
|
||||
if (AppConfig::HexData1) {
|
||||
buffer = QUIHelperData::hexStrToByteArray(data);
|
||||
} else {
|
||||
buffer = data.toLatin1();
|
||||
}
|
||||
|
||||
this->write(buffer);
|
||||
emit sendData(ip, port, deviceID, data);
|
||||
}
|
||||
|
||||
TcpServer1::TcpServer1(QObject *parent) : QTcpServer(parent)
|
||||
{
|
||||
}
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
void TcpServer1::incomingConnection(qintptr handle)
|
||||
#else
|
||||
void TcpServer1::incomingConnection(int handle)
|
||||
#endif
|
||||
{
|
||||
TcpClient1 *client = new TcpClient1(this);
|
||||
client->setSocketDescriptor(handle);
|
||||
connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
|
||||
connect(client, SIGNAL(sendData(QString, int, QString, QString)), this, SIGNAL(sendData(QString, int, QString, QString)));
|
||||
connect(client, SIGNAL(receiveData(QString, int, QString, QString)), this, SIGNAL(receiveData(QString, int, QString, QString)));
|
||||
connect(client, SIGNAL(receiveDeviceID(QString, int, QString)), this, SIGNAL(receiveDeviceID(QString, int, QString)));
|
||||
|
||||
QString ip = client->peerAddress().toString();
|
||||
int port = client->peerPort();
|
||||
QString deviceID = client->getDeviceID();
|
||||
client->setIP(ip);
|
||||
client->setPort(port);
|
||||
emit clientConnected(ip, port, deviceID);
|
||||
emit sendData(ip, port, deviceID, "客户端上线");
|
||||
|
||||
//追加到链表中
|
||||
clients.append(client);
|
||||
}
|
||||
|
||||
void TcpServer1::disconnected()
|
||||
{
|
||||
TcpClient1 *client = (TcpClient1 *)sender();
|
||||
QString ip = client->getIP();
|
||||
int port = client->getPort();
|
||||
QString deviceID = client->getDeviceID();
|
||||
emit clientDisconnected(ip, port, deviceID);
|
||||
emit sendData(ip, port, deviceID, "客户端下线");
|
||||
|
||||
//断开连接后从链表中移除
|
||||
clients.removeOne(client);
|
||||
}
|
||||
|
||||
bool TcpServer1::start()
|
||||
{
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
bool ok = listen(QHostAddress::AnyIPv4, AppConfig::ListenPort1);
|
||||
#else
|
||||
bool ok = listen(QHostAddress::Any, AppConfig::ListenPort1);
|
||||
#endif
|
||||
return ok;
|
||||
}
|
||||
|
||||
void TcpServer1::stop()
|
||||
{
|
||||
foreach (TcpClient1 *client, clients) {
|
||||
client->disconnectFromHost();
|
||||
}
|
||||
|
||||
this->close();
|
||||
}
|
||||
|
||||
bool TcpServer1::writeData(const QString &deviceID, const QString &data)
|
||||
{
|
||||
bool ok = false;
|
||||
foreach (TcpClient1 *client, clients) {
|
||||
if (client->getDeviceID() == deviceID) {
|
||||
client->sendData(data);
|
||||
ok = true;
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
74
tool/netserver/api/tcpserver1.h
Normal file
74
tool/netserver/api/tcpserver1.h
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef TCPSERVER1_H
|
||||
#define TCPSERVER1_H
|
||||
|
||||
#include <QtNetwork>
|
||||
|
||||
class TcpClient1 : public QTcpSocket
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit TcpClient1(QObject *parent = 0);
|
||||
|
||||
private:
|
||||
QString ip;
|
||||
int port;
|
||||
QString deviceID;
|
||||
|
||||
public:
|
||||
void setIP(const QString &ip);
|
||||
QString getIP()const;
|
||||
void setPort(int port);
|
||||
int getPort()const;
|
||||
|
||||
QString getDeviceID();
|
||||
|
||||
private slots:
|
||||
void readData();
|
||||
|
||||
signals:
|
||||
void sendData(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveData(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveDeviceID(const QString &ip, int port, const QString &deviceID);
|
||||
|
||||
public slots:
|
||||
void sendData(const QString &data);
|
||||
|
||||
};
|
||||
|
||||
class TcpServer1 : public QTcpServer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit TcpServer1(QObject *parent = 0);
|
||||
|
||||
private:
|
||||
QList<TcpClient1 *> clients;
|
||||
|
||||
protected:
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
void incomingConnection(qintptr handle);
|
||||
#else
|
||||
void incomingConnection(int handle);
|
||||
#endif
|
||||
|
||||
private slots:
|
||||
void disconnected();
|
||||
|
||||
signals:
|
||||
void clientConnected(const QString &ip, int port, const QString &deviceID);
|
||||
void clientDisconnected(const QString &ip, int port, const QString &deviceID);
|
||||
void sendData(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveData(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveDeviceID(const QString &ip, int port, const QString &deviceID);
|
||||
|
||||
public slots:
|
||||
//启动服务
|
||||
bool start();
|
||||
//停止服务
|
||||
void stop();
|
||||
|
||||
//对指定连接发送数据
|
||||
bool writeData(const QString &deviceID, const QString &data);
|
||||
};
|
||||
|
||||
#endif // TCPSERVER1_H
|
||||
156
tool/netserver/api/tcpserver2.cpp
Normal file
156
tool/netserver/api/tcpserver2.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
#include "tcpserver2.h"
|
||||
#include "quihelper.h"
|
||||
#include "quihelperdata.h"
|
||||
|
||||
TcpClient2::TcpClient2(QObject *parent) : QTcpSocket(parent)
|
||||
{
|
||||
ip = "127.0.0.1";
|
||||
port = 6908;
|
||||
deviceID = "SSJC00000001";
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
|
||||
connect(this, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(deleteLater()));
|
||||
#else
|
||||
connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(deleteLater()));
|
||||
#endif
|
||||
connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
|
||||
connect(this, SIGNAL(readyRead()), this, SLOT(readData()));
|
||||
}
|
||||
|
||||
void TcpClient2::setIP(const QString &ip)
|
||||
{
|
||||
this->ip = ip;
|
||||
}
|
||||
|
||||
QString TcpClient2::getIP() const
|
||||
{
|
||||
return this->ip;
|
||||
}
|
||||
|
||||
void TcpClient2::setPort(int port)
|
||||
{
|
||||
this->port = port;
|
||||
}
|
||||
|
||||
int TcpClient2::getPort() const
|
||||
{
|
||||
return this->port;
|
||||
}
|
||||
|
||||
QString TcpClient2::getDeviceID()
|
||||
{
|
||||
return this->deviceID;
|
||||
}
|
||||
|
||||
void TcpClient2::readData()
|
||||
{
|
||||
QByteArray data = this->readAll();
|
||||
if (data.length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
//取出唯一标识符,并过滤,可自行更改过滤条件
|
||||
QByteArray cmd = data.mid(AppConfig::CmdStart2, AppConfig::CmdLen2);
|
||||
QString id = QString(cmd);
|
||||
if (id.startsWith("S") && deviceID != id) {
|
||||
deviceID = id;
|
||||
//发送信号更新标识符
|
||||
emit receiveDeviceID(ip, port, deviceID);
|
||||
}
|
||||
|
||||
QString buffer;
|
||||
if (AppConfig::HexData2) {
|
||||
buffer = QUIHelperData::byteArrayToHexStr(data);
|
||||
} else {
|
||||
buffer = QString(data);
|
||||
}
|
||||
|
||||
emit receiveData(ip, port, deviceID, buffer);
|
||||
}
|
||||
|
||||
void TcpClient2::sendData(const QString &data)
|
||||
{
|
||||
QByteArray buffer;
|
||||
if (AppConfig::HexData2) {
|
||||
buffer = QUIHelperData::hexStrToByteArray(data);
|
||||
} else {
|
||||
buffer = data.toLatin1();
|
||||
}
|
||||
|
||||
this->write(buffer);
|
||||
emit sendData(ip, port, deviceID, data);
|
||||
}
|
||||
|
||||
TcpServer2::TcpServer2(QObject *parent) : QTcpServer(parent)
|
||||
{
|
||||
}
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
void TcpServer2::incomingConnection(qintptr handle)
|
||||
#else
|
||||
void TcpServer2::incomingConnection(int handle)
|
||||
#endif
|
||||
{
|
||||
TcpClient2 *client = new TcpClient2(this);
|
||||
client->setSocketDescriptor(handle);
|
||||
connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
|
||||
connect(client, SIGNAL(sendData(QString, int, QString, QString)), this, SIGNAL(sendData(QString, int, QString, QString)));
|
||||
connect(client, SIGNAL(receiveData(QString, int, QString, QString)), this, SIGNAL(receiveData(QString, int, QString, QString)));
|
||||
connect(client, SIGNAL(receiveDeviceID(QString, int, QString)), this, SIGNAL(receiveDeviceID(QString, int, QString)));
|
||||
|
||||
QString ip = client->peerAddress().toString();
|
||||
int port = client->peerPort();
|
||||
QString deviceID = client->getDeviceID();
|
||||
client->setIP(ip);
|
||||
client->setPort(port);
|
||||
emit clientConnected(ip, port, deviceID);
|
||||
emit sendData(ip, port, deviceID, "客户端上线");
|
||||
|
||||
//追加到链表中
|
||||
clients.append(client);
|
||||
}
|
||||
|
||||
void TcpServer2::disconnected()
|
||||
{
|
||||
TcpClient2 *client = (TcpClient2 *)sender();
|
||||
QString ip = client->getIP();
|
||||
int port = client->getPort();
|
||||
QString deviceID = client->getDeviceID();
|
||||
emit clientDisconnected(ip, port, deviceID);
|
||||
emit sendData(ip, port, deviceID, "客户端下线");
|
||||
|
||||
//断开连接后从链表中移除
|
||||
clients.removeOne(client);
|
||||
}
|
||||
|
||||
bool TcpServer2::start()
|
||||
{
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
bool ok = listen(QHostAddress::AnyIPv4, AppConfig::ListenPort2);
|
||||
#else
|
||||
bool ok = listen(QHostAddress::Any, AppConfig::ListenPort2);
|
||||
#endif
|
||||
return ok;
|
||||
}
|
||||
|
||||
void TcpServer2::stop()
|
||||
{
|
||||
foreach (TcpClient2 *client, clients) {
|
||||
client->disconnectFromHost();
|
||||
}
|
||||
|
||||
this->close();
|
||||
}
|
||||
|
||||
bool TcpServer2::writeData(const QString &deviceID, const QString &data)
|
||||
{
|
||||
bool ok = false;
|
||||
foreach (TcpClient2 *client, clients) {
|
||||
if (client->getDeviceID() == deviceID) {
|
||||
client->sendData(data);
|
||||
ok = true;
|
||||
}
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
74
tool/netserver/api/tcpserver2.h
Normal file
74
tool/netserver/api/tcpserver2.h
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef TCPSERVER2_H
|
||||
#define TCPSERVER2_H
|
||||
|
||||
#include <QtNetwork>
|
||||
|
||||
class TcpClient2 : public QTcpSocket
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit TcpClient2(QObject *parent = 0);
|
||||
|
||||
private:
|
||||
QString ip;
|
||||
int port;
|
||||
QString deviceID;
|
||||
|
||||
public:
|
||||
void setIP(const QString &ip);
|
||||
QString getIP()const;
|
||||
void setPort(int port);
|
||||
int getPort()const;
|
||||
|
||||
QString getDeviceID();
|
||||
|
||||
private slots:
|
||||
void readData();
|
||||
|
||||
signals:
|
||||
void sendData(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveData(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveDeviceID(const QString &ip, int port, const QString &deviceID);
|
||||
|
||||
public slots:
|
||||
void sendData(const QString &data);
|
||||
|
||||
};
|
||||
|
||||
class TcpServer2 : public QTcpServer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit TcpServer2(QObject *parent = 0);
|
||||
|
||||
private:
|
||||
QList<TcpClient2 *> clients;
|
||||
|
||||
protected:
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
void incomingConnection(qintptr handle);
|
||||
#else
|
||||
void incomingConnection(int handle);
|
||||
#endif
|
||||
|
||||
private slots:
|
||||
void disconnected();
|
||||
|
||||
signals:
|
||||
void clientConnected(const QString &ip, int port, const QString &deviceID);
|
||||
void clientDisconnected(const QString &ip, int port, const QString &deviceID);
|
||||
void sendData(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveData(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveDeviceID(const QString &ip, int port, const QString &deviceID);
|
||||
|
||||
public slots:
|
||||
//启动服务
|
||||
bool start();
|
||||
//停止服务
|
||||
void stop();
|
||||
|
||||
//对指定连接发送数据
|
||||
bool writeData(const QString &deviceID, const QString &data);
|
||||
};
|
||||
|
||||
#endif // TCPSERVER2_H
|
||||
8
tool/netserver/form/form.pri
Normal file
8
tool/netserver/form/form.pri
Normal file
@@ -0,0 +1,8 @@
|
||||
FORMS += \
|
||||
$$PWD/frmmain.ui
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/frmmain.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/frmmain.cpp
|
||||
264
tool/netserver/form/frmmain.cpp
Normal file
264
tool/netserver/form/frmmain.cpp
Normal file
@@ -0,0 +1,264 @@
|
||||
#include "frmmain.h"
|
||||
#include "ui_frmmain.h"
|
||||
#include "quihelper.h"
|
||||
|
||||
frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
on_btnListen1_clicked();
|
||||
on_btnListen2_clicked();
|
||||
}
|
||||
|
||||
frmMain::~frmMain()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void frmMain::initForm()
|
||||
{
|
||||
tcpServer1 = new TcpServer1(this);
|
||||
connect(tcpServer1, SIGNAL(clientConnected(QString, int, QString)), this, SLOT(clientConnected1(QString, int, QString)));
|
||||
connect(tcpServer1, SIGNAL(clientDisconnected(QString, int, QString)), this, SLOT(clientDisconnected1(QString, int, QString)));
|
||||
connect(tcpServer1, SIGNAL(sendData(QString, int, QString, QString)), this, SLOT(sendData1(QString, int, QString, QString)));
|
||||
connect(tcpServer1, SIGNAL(receiveData(QString, int, QString, QString)), this, SLOT(receiveData1(QString, int, QString, QString)));
|
||||
connect(tcpServer1, SIGNAL(receiveDeviceID(QString, int, QString)), this, SLOT(receiveDeviceID1(QString, int, QString)));
|
||||
|
||||
tcpServer2 = new TcpServer2(this);
|
||||
connect(tcpServer2, SIGNAL(clientConnected(QString, int, QString)), this, SLOT(clientConnected2(QString, int, QString)));
|
||||
connect(tcpServer2, SIGNAL(clientDisconnected(QString, int, QString)), this, SLOT(clientDisconnected2(QString, int, QString)));
|
||||
connect(tcpServer2, SIGNAL(sendData(QString, int, QString, QString)), this, SLOT(sendData2(QString, int, QString, QString)));
|
||||
connect(tcpServer2, SIGNAL(receiveData(QString, int, QString, QString)), this, SLOT(receiveData2(QString, int, QString, QString)));
|
||||
connect(tcpServer2, SIGNAL(receiveDeviceID(QString, int, QString)), this, SLOT(receiveDeviceID2(QString, int, QString)));
|
||||
}
|
||||
|
||||
void frmMain::initConfig()
|
||||
{
|
||||
ui->txtListenPort1->setText(QString::number(AppConfig::ListenPort1));
|
||||
connect(ui->txtListenPort1, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtListenPort2->setText(QString::number(AppConfig::ListenPort2));
|
||||
connect(ui->txtListenPort2, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
}
|
||||
|
||||
void frmMain::saveConfig()
|
||||
{
|
||||
AppConfig::ListenPort1 = ui->txtListenPort1->text().trimmed().toInt();
|
||||
AppConfig::ListenPort2 = ui->txtListenPort2->text().trimmed().toInt();
|
||||
AppConfig::writeConfig();
|
||||
}
|
||||
|
||||
void frmMain::append1(int type, const QString &data, bool clear)
|
||||
{
|
||||
static int currentCount = 0;
|
||||
static int maxCount = 100;
|
||||
|
||||
if (clear) {
|
||||
ui->txtMain1->clear();
|
||||
currentCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentCount >= maxCount) {
|
||||
ui->txtMain1->clear();
|
||||
currentCount = 0;
|
||||
}
|
||||
|
||||
//过滤回车换行符
|
||||
QString strData = data.left(500);
|
||||
strData = strData.replace("\r", "");
|
||||
strData = strData.replace("\n", "");
|
||||
|
||||
//不同类型不同颜色显示
|
||||
QString strType;
|
||||
if (type == 0) {
|
||||
strType = "发送";
|
||||
ui->txtMain1->setTextColor(QColor("darkgreen"));
|
||||
} else {
|
||||
strType = "接收";
|
||||
ui->txtMain1->setTextColor(QColor("red"));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
ui->txtMain1->append(strData);
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmMain::append2(int type, const QString &data, bool clear)
|
||||
{
|
||||
static int currentCount = 0;
|
||||
static int maxCount = 100;
|
||||
|
||||
if (clear) {
|
||||
ui->txtMain2->clear();
|
||||
currentCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentCount >= maxCount) {
|
||||
ui->txtMain2->clear();
|
||||
currentCount = 0;
|
||||
}
|
||||
|
||||
//过滤回车换行符
|
||||
QString strData = data.left(500);
|
||||
strData = strData.replace("\r", "");
|
||||
strData = strData.replace("\n", "");
|
||||
|
||||
//不同类型不同颜色显示
|
||||
QString strType;
|
||||
if (type == 0) {
|
||||
strType = "发送";
|
||||
ui->txtMain2->setTextColor(QColor("darkgreen"));
|
||||
} else {
|
||||
strType = "接收";
|
||||
ui->txtMain2->setTextColor(QColor("red"));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
ui->txtMain2->append(strData);
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmMain::clientConnected1(const QString &ip, int port, const QString &deviceID)
|
||||
{
|
||||
QString str = QString("%1 %2:%3").arg(deviceID).arg(ip).arg(port);
|
||||
ui->listWidget1->addItem(str);
|
||||
ui->labCount1->setText(QString("共 %1 个连接").arg(ui->listWidget1->count()));
|
||||
}
|
||||
|
||||
void frmMain::clientDisconnected1(const QString &ip, int port, const QString &deviceID)
|
||||
{
|
||||
int row = -1;
|
||||
QString str = QString("%1 %2:%3").arg(deviceID).arg(ip).arg(port);
|
||||
for (int i = 0; i < ui->listWidget1->count(); i++) {
|
||||
if (ui->listWidget1->item(i)->text() == str) {
|
||||
row = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ui->listWidget1->takeItem(row);
|
||||
ui->labCount1->setText(QString("共 %1 个连接").arg(ui->listWidget1->count()));
|
||||
}
|
||||
|
||||
void frmMain::sendData1(const QString &ip, int port, const QString &deviceID, const QString &data)
|
||||
{
|
||||
QString str = QString("%1 [%2:%3] %4").arg(deviceID).arg(ip).arg(port).arg(data);
|
||||
bool error = (data.contains("下线") || data.contains("离线"));
|
||||
append1(error ? 1 : 0, str);
|
||||
}
|
||||
|
||||
void frmMain::receiveData1(const QString &ip, int port, const QString &deviceID, const QString &data)
|
||||
{
|
||||
QString str = QString("%1 [%2:%3] %4").arg(deviceID).arg(ip).arg(port).arg(data);
|
||||
append1(1, str);
|
||||
|
||||
//将收到的数据转发到另一路网络
|
||||
bool ok = tcpServer2->writeData(deviceID, data);
|
||||
sendData2(ip, port, deviceID, ok ? "转发成功" : "对方离线");
|
||||
if (!ok) {
|
||||
tcpServer1->writeData(deviceID, "deviceError");
|
||||
}
|
||||
}
|
||||
|
||||
void frmMain::receiveDeviceID1(const QString &ip, int port, const QString &deviceID)
|
||||
{
|
||||
QString temp = QString("%1:%2").arg(ip).arg(port);
|
||||
QString str = QString("%1 %2:%3").arg(deviceID).arg(ip).arg(port);
|
||||
for (int i = 0; i < ui->listWidget1->count(); i++) {
|
||||
if (ui->listWidget1->item(i)->text().endsWith(temp)) {
|
||||
ui->listWidget1->item(i)->setText(str);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmMain::clientConnected2(const QString &ip, int port, const QString &deviceID)
|
||||
{
|
||||
QString str = QString("%1 %2:%3").arg(deviceID).arg(ip).arg(port);
|
||||
ui->listWidget2->addItem(str);
|
||||
ui->labCount2->setText(QString("共 %1 个连接").arg(ui->listWidget2->count()));
|
||||
}
|
||||
|
||||
void frmMain::clientDisconnected2(const QString &ip, int port, const QString &deviceID)
|
||||
{
|
||||
int row = -1;
|
||||
QString str = QString("%1 %2:%3").arg(deviceID).arg(ip).arg(port);
|
||||
for (int i = 0; i < ui->listWidget2->count(); i++) {
|
||||
if (ui->listWidget2->item(i)->text() == str) {
|
||||
row = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ui->listWidget2->takeItem(row);
|
||||
ui->labCount2->setText(QString("共 %1 个连接").arg(ui->listWidget2->count()));
|
||||
}
|
||||
|
||||
void frmMain::sendData2(const QString &ip, int port, const QString &deviceID, const QString &data)
|
||||
{
|
||||
QString str = QString("%1 [%2:%3] %4").arg(deviceID).arg(ip).arg(port).arg(data);
|
||||
bool error = (data.contains("下线") || data.contains("离线"));
|
||||
append2(error ? 1 : 0, str);
|
||||
}
|
||||
|
||||
void frmMain::receiveData2(const QString &ip, int port, const QString &deviceID, const QString &data)
|
||||
{
|
||||
QString str = QString("%1 [%2:%3] %4").arg(deviceID).arg(ip).arg(port).arg(data);
|
||||
append2(1, str);
|
||||
|
||||
//将收到的数据转发到另一路网络
|
||||
bool ok = tcpServer1->writeData(deviceID, data);
|
||||
sendData1(ip, port, deviceID, ok ? "转发成功" : "对方离线");
|
||||
if (!ok) {
|
||||
tcpServer2->writeData(deviceID, "deviceError");
|
||||
}
|
||||
}
|
||||
|
||||
void frmMain::receiveDeviceID2(const QString &ip, int port, const QString &deviceID)
|
||||
{
|
||||
QString temp = QString("%1:%2").arg(ip).arg(port);
|
||||
QString str = QString("%1 %2:%3").arg(deviceID).arg(ip).arg(port);
|
||||
for (int i = 0; i < ui->listWidget2->count(); i++) {
|
||||
if (ui->listWidget2->item(i)->text().endsWith(temp)) {
|
||||
ui->listWidget2->item(i)->setText(str);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmMain::on_btnListen1_clicked()
|
||||
{
|
||||
if (ui->btnListen1->text() == "监听") {
|
||||
if (tcpServer1->start()) {
|
||||
ui->btnListen1->setText("关闭");
|
||||
}
|
||||
} else {
|
||||
tcpServer1->stop();
|
||||
ui->btnListen1->setText("监听");
|
||||
}
|
||||
}
|
||||
|
||||
void frmMain::on_btnClear1_clicked()
|
||||
{
|
||||
append1(0, "", true);
|
||||
}
|
||||
|
||||
void frmMain::on_btnListen2_clicked()
|
||||
{
|
||||
if (ui->btnListen2->text() == "监听") {
|
||||
if (tcpServer2->start()) {
|
||||
ui->btnListen2->setText("关闭");
|
||||
}
|
||||
} else {
|
||||
tcpServer2->stop();
|
||||
ui->btnListen2->setText("监听");
|
||||
}
|
||||
}
|
||||
|
||||
void frmMain::on_btnClear2_clicked()
|
||||
{
|
||||
append2(0, "", true);
|
||||
}
|
||||
53
tool/netserver/form/frmmain.h
Normal file
53
tool/netserver/form/frmmain.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef FRMMAIN_H
|
||||
#define FRMMAIN_H
|
||||
|
||||
#include <QWidget>
|
||||
#include "tcpserver1.h"
|
||||
#include "tcpserver2.h"
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class frmMain;
|
||||
}
|
||||
|
||||
class frmMain : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmMain(QWidget *parent = 0);
|
||||
~frmMain();
|
||||
|
||||
private:
|
||||
Ui::frmMain *ui;
|
||||
TcpServer1 *tcpServer1;
|
||||
TcpServer2 *tcpServer2;
|
||||
|
||||
private slots:
|
||||
void initForm();
|
||||
void initConfig();
|
||||
void saveConfig();
|
||||
void append1(int type, const QString &data, bool clear = false);
|
||||
void append2(int type, const QString &data, bool clear = false);
|
||||
|
||||
private slots:
|
||||
void clientConnected1(const QString &ip, int port, const QString &deviceID);
|
||||
void clientDisconnected1(const QString &ip, int port, const QString &deviceID);
|
||||
void sendData1(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveData1(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveDeviceID1(const QString &ip, int port, const QString &deviceID);
|
||||
|
||||
void clientConnected2(const QString &ip, int port, const QString &deviceID);
|
||||
void clientDisconnected2(const QString &ip, int port, const QString &deviceID);
|
||||
void sendData2(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveData2(const QString &ip, int port, const QString &deviceID, const QString &data);
|
||||
void receiveDeviceID2(const QString &ip, int port, const QString &deviceID);
|
||||
|
||||
private slots:
|
||||
void on_btnListen1_clicked();
|
||||
void on_btnClear1_clicked();
|
||||
void on_btnListen2_clicked();
|
||||
void on_btnClear2_clicked();
|
||||
};
|
||||
|
||||
#endif // FRMMAIN_H
|
||||
246
tool/netserver/form/frmmain.ui
Normal file
246
tool/netserver/form/frmmain.ui
Normal file
@@ -0,0 +1,246 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmMain</class>
|
||||
<widget class="QWidget" name="frmMain">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="tab1">
|
||||
<attribute name="title">
|
||||
<string>服务器1</string>
|
||||
</attribute>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QTextEdit" name="txtMain1">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QWidget" name="widget1" native="true">
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>230</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item row="0" column="0" colspan="2">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||
<item>
|
||||
<widget class="QLabel" name="labListenPort1">
|
||||
<property name="text">
|
||||
<string>监听端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtListenPort1"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QPushButton" name="btnListen1">
|
||||
<property name="text">
|
||||
<string>监听</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QPushButton" name="btnClear1">
|
||||
<property name="text">
|
||||
<string>清空</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="2">
|
||||
<widget class="QLabel" name="labCount1">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>25</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::Box</enum>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Sunken</enum>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>共 0 个连接</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0" colspan="2">
|
||||
<widget class="QListWidget" name="listWidget1"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab2">
|
||||
<attribute name="title">
|
||||
<string>服务器2</string>
|
||||
</attribute>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QTextEdit" name="txtMain2">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QWidget" name="widget2" native="true">
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>230</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item row="0" column="0" colspan="2">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||
<item>
|
||||
<widget class="QLabel" name="labListenPort2">
|
||||
<property name="text">
|
||||
<string>监听端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="txtListenPort2"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QPushButton" name="btnListen2">
|
||||
<property name="text">
|
||||
<string>监听</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QPushButton" name="btnClear2">
|
||||
<property name="text">
|
||||
<string>清空</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="2">
|
||||
<widget class="QLabel" name="labCount2">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>25</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::Box</enum>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Sunken</enum>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>共 0 个连接</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0" colspan="2">
|
||||
<widget class="QListWidget" name="listWidget2"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
17
tool/netserver/head.h
Normal file
17
tool/netserver/head.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <QtCore>
|
||||
#include <QtGui>
|
||||
#include <QtNetwork>
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
#include <QtWidgets>
|
||||
#endif
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
|
||||
#include <QtCore5Compat>
|
||||
#endif
|
||||
|
||||
#pragma execution_character_set("utf-8")
|
||||
#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))
|
||||
#define STRDATETIME qPrintable(QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss"))
|
||||
|
||||
#include "appconfig.h"
|
||||
23
tool/netserver/main.cpp
Normal file
23
tool/netserver/main.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "frmmain.h"
|
||||
#include "quihelper.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
a.setWindowIcon(QIcon(":/main.ico"));
|
||||
|
||||
//设置编码以及加载中文翻译文件
|
||||
QUIHelper::initAll();
|
||||
|
||||
//读取配置文件
|
||||
AppConfig::ConfigFile = QString("%1/%2.ini").arg(QUIHelper::appPath()).arg(QUIHelper::appName());
|
||||
AppConfig::readConfig();
|
||||
|
||||
frmMain w;
|
||||
w.setWindowTitle("网络中转服务器 V2022 (QQ: 517216493 WX: feiyangqingyun)");
|
||||
w.resize(900, 650);
|
||||
QUIHelper::setFormInCenter(&w);
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
20
tool/netserver/netserver.pro
Normal file
20
tool/netserver/netserver.pro
Normal file
@@ -0,0 +1,20 @@
|
||||
QT += core gui network
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
|
||||
|
||||
TARGET = netserver
|
||||
TEMPLATE = app
|
||||
DESTDIR = $$PWD/../bin
|
||||
RC_FILE = qrc/main.rc
|
||||
|
||||
HEADERS += head.h
|
||||
SOURCES += main.cpp
|
||||
RESOURCES += qrc/main.qrc
|
||||
CONFIG += warn_off
|
||||
|
||||
INCLUDEPATH += $$PWD
|
||||
INCLUDEPATH += $$PWD/api
|
||||
INCLUDEPATH += $$PWD/form
|
||||
|
||||
include ($$PWD/api/api.pri)
|
||||
include ($$PWD/form/form.pri)
|
||||
BIN
tool/netserver/qrc/main.ico
Normal file
BIN
tool/netserver/qrc/main.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 66 KiB |
7
tool/netserver/qrc/main.qrc
Normal file
7
tool/netserver/qrc/main.qrc
Normal file
@@ -0,0 +1,7 @@
|
||||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>main.ico</file>
|
||||
<file>qm/qt_zh_CN.qm</file>
|
||||
<file>qm/widgets.qm</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
1
tool/netserver/qrc/main.rc
Normal file
1
tool/netserver/qrc/main.rc
Normal file
@@ -0,0 +1 @@
|
||||
IDI_ICON1 ICON DISCARDABLE "main.ico"
|
||||
BIN
tool/netserver/qrc/qm/qt_zh_CN.qm
Normal file
BIN
tool/netserver/qrc/qm/qt_zh_CN.qm
Normal file
Binary file not shown.
BIN
tool/netserver/qrc/qm/widgets.qm
Normal file
BIN
tool/netserver/qrc/qm/widgets.qm
Normal file
Binary file not shown.
9
tool/netserver/readme.md
Normal file
9
tool/netserver/readme.md
Normal file
@@ -0,0 +1,9 @@
|
||||
**场景需求**
|
||||
1. 手机端或者其他端可以对设备进行回控,并查看设备各种运行状态,接收报警推送等。
|
||||
2. 同时支持在局域网、广域网、互联网访问,尤其是互联网访问。
|
||||
3. 权限控制,给定账号控制授权的设备,并自动拉取设备信息。
|
||||
4. 设备不在线要给出反馈信息提示以便分析。
|
||||
5. 每个连接都有自己的唯一编号作为标识符。
|
||||
|
||||
|
||||
查看端口号连接 netstat -aon|findstr "6907"
|
||||
Reference in New Issue
Block a user