来源于微信公众号:yafeilinux和他的朋友们
作者
feiyangqingyun(liudianwu),擅长Qt自定义控件编写、UI定制等。
博客:https://blog.csdn.net/feiyangqingyun
导语
今天带来feiyangqingyun同学的两个开源项目,分别是:Qt编写守护程序 和 Qt编写密钥生成器 。
一、Qt编写守护程序
没有任何人敢保证自己写的程序没有任何BUG,尤其是在商业项目中,程序量越大,复杂度越高,出错的概率越大,尤其是现场环境千差万别,和当初本地电脑测试环境很可能不一样,有很多特殊情况没有考虑到,如果需要保证程序7*24小时运行,则需要想一些办法能够让程序死了能够活过来,在嵌入式linux上,大部分会采用看门狗的形式来处理,程序打开看门狗驱动后,定时喂狗,一旦超过规定的时间,则硬件软复位等。这种方式相对来说比较可靠,如果需要在普通PC机上运行怎办呢?本篇文章提供一个软件实现守护进程的办法,原理就是udp通信,单独写个守护进程程序,专门负责检测主程序是否存在,不存在则启动。主程序只需要启动live类监听端口,收到hello就回复ok就行。
为了使得兼容任意程序,特意提炼出来共性,增加了多种设置。
可设置检测的程序名称。
可设置udp通信端口。
可设置超时次数。
自动记录已重启次数。
自动记录最后一次重启时间。
是否需要重新刷新桌面。
可重置当前重启次数和最后重启时间。
自动隐藏的托盘运行或者后台运行。
提供界面设置程序名称已经开启和暂停服务。
守护进程核心代码: 1#pragma execution_character_set("utf-8") 2#include "frmmain.h" 3#include "ui_frmmain.h" 4#include "qtimer.h" 5#include "qudpsocket.h" 6#include "qsharedmemory.h" 7#include "qprocess.h" 8#include "qdatetime.h" 9#include "qapplication.h" 10#include "qdesktopservices.h" 11#include "qmessagebox.h" 12#if (QT_VERSION > QT_VERSION_CHECK(5,0,0)) 13#include "qstandardpaths.h" 14#endif 15 16#include "app.h" 17 18frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain) 19{ 20 ui->setupUi(this); 21 this->initForm(); 22} 23 24frmMain::~frmMain() 25{ 26 delete ui; 27} 28 29void frmMain::changeEvent(QEvent *event) 30{ 31 //隐藏当前界面,最小化到托盘 32 if(event->type() == QEvent::WindowStateChange) { 33 if(windowState() & Qt::WindowMinimized) { 34 hide(); 35 } 36 } 37 38 QWidget::changeEvent(event); 39} 40 41void frmMain::initForm() 42{ 43 count = 0; 44 ok = false; 45 46 //每秒钟定时询问心跳 47 timerHeart = new QTimer(this); 48 timerHeart->setInterval(2000); 49 connect(timerHeart, SIGNAL(timeout()), this, SLOT(sendHearData())); 50 51 //从6050端口开始,如果绑定失败则将端口加1,直到绑定成功 52 udp = new QUdpSocket(this); 53 int port = 6050; 54 while(!udp->bind(port)) { 55 port++; 56 } 57 58 connect(udp, SIGNAL(readyRead()), this, SLOT(readData())); 59 60 if (App::TargetAppName.isEmpty()) { 61 ui->btnStart->setText("启动"); 62 ui->btnStart->setEnabled(false); 63 timerHeart->stop(); 64 } else { 65 ui->btnStart->setText("暂停"); 66 ui->btnStart->setEnabled(true); 67 timerHeart->start(); 68 } 69 70 ui->txtAppName->setText(App::TargetAppName); 71 ui->txtAppName->setFocus(); 72} 73 74void frmMain::sendHearData() 75{ 76 udp->writeDatagram("hello", QHostAddress::LocalHost, App::TargetAppPort); 77 78 //判断当前是否没有回复 79 if (!ok) { 80 count++; 81 } else { 82 count = 0; 83 ok = false; 84 } 85 86 //如果超过规定次数没有收到心跳回复,则超时重启 87 if (count >= App::TimeoutCount) { 88 timerHeart->stop(); 89 90 QSharedMemory mem(App::TargetAppName); 91 if (!mem.create(1)) { 92 killApp(); 93 } 94 95 QTimer::singleShot(1000 , this, SLOT(killOther())); 96 QTimer::singleShot(3000 , this, SLOT(startApp())); 97 QTimer::singleShot(4000 , this, SLOT(startExplorer())); 98 } 99} 100 101void frmMain::killApp() 102{ 103 QProcess *p = new QProcess; 104 p->start(QString("taskkill /im %1.exe /f").arg(App::TargetAppName)); 105} 106 107void frmMain::killOther() 108{ 109 QProcess *p = new QProcess; 110 p->start(QString("taskkill /im %1.exe /f").arg("WerFault")); 111 112 //重建缓存,彻底清除托盘图标 113 if (App::ReStartExplorer) { 114 QProcess *p1 = new QProcess; 115 p1->start("taskkill /f /im explorer.exe"); 116 } 117} 118 119void frmMain::startApp() 120{ 121 if (ui->btnStart->text() == "开始" || ui->btnStart->text() == "启动") { 122 count = 0; 123 return; 124 } 125 126 QProcess *p = new QProcess; 127 p->start(QString(""%1/%2.exe"").arg(qApp->applicationDirPath()).arg(App::TargetAppName)); 128 129 count = 0; 130 ok = true; 131 timerHeart->start(); 132 133 App::ReStartCount++; 134 App::ReStartLastTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"); 135 App::writeConfig(); 136 137 ui->labCount->setText(QString("已重启 %1 次").arg(App::ReStartCount)); 138 ui->labInfo->setText(QString("最后一次重启在 %1").arg(App::ReStartLastTime)); 139} 140 141void frmMain::startExplorer() 142{ 143 //取得操作系统目录路径,指定操作系统目录下的explorer程序,采用绝对路径,否则在64位操作系统下无效 144#if (QT_VERSION > QT_VERSION_CHECK(5,0,0)) 145 QString str = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation); 146#else 147 QString str = QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation); 148#endif 149 150 if (App::ReStartExplorer) { 151 str = QString("%1\\Windows\\explorer.exe").arg(str.mid(0, 2)); 152 QProcess *p = new QProcess(this); 153 p->start(str); 154 } 155} 156 157void frmMain::readData() 158{ 159 QByteArray tempData; 160 do { 161 tempData.resize(udp->pendingDatagramSize()); 162 udp->readDatagram(tempData.data(), tempData.size()); 163 QString data = QLatin1String(tempData); 164 if (data.right(2) == "OK") { 165 count = 0; 166 ok = true; 167 } 168 } while (udp->hasPendingDatagrams()); 169} 170 171void frmMain::on_btnOk_clicked() 172{ 173 App::TargetAppName = ui->txtAppName->text(); 174 if (App::TargetAppName == "") { 175 QMessageBox::critical(this, "提示", "应用程序名称不能为空!"); 176 ui->txtAppName->setFocus(); 177 return; 178 } 179 180 App::writeConfig(); 181 ui->btnStart->setEnabled(true); 182} 183 184void frmMain::on_btnStart_clicked() 185{ 186 count = 0; 187 if (ui->btnStart->text() == "暂停") { 188 timerHeart->stop(); 189 ui->btnStart->setText("开始"); 190 } else { 191 timerHeart->start(); 192 ui->btnStart->setText("暂停"); 193 } 194} 195 196void frmMain::on_btnReset_clicked() 197{ 198 App::ReStartCount = 0; 199 App::ReStartLastTime = "2019-01-01 12:00:00"; 200 App::writeConfig(); 201 202 ui->txtAppName->setText(App::TargetAppName); 203 ui->labCount->setText(QString("已重启 %1 次").arg(App::ReStartCount)); 204 ui->labInfo->setText(QString("最后一次重启在 %1").arg(App::ReStartLastTime)); 205 QMessageBox::information(this, "提示", "重置配置文件成功!"); 206}
主程序使用核心代码: 1#include "applive.h" 2#include "qmutex.h" 3#include "qudpsocket.h" 4#include "qstringlist.h" 5#include "qapplication.h" 6#include "qdatetime.h" 7#include "qdebug.h" 8 9#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz")) 10 11QScopedPointer<AppLive> AppLive::self; 12AppLive *AppLive::Instance() 13{ 14 if (self.isNull()) { 15 QMutex mutex; 16 QMutexLocker locker(&mutex); 17 if (self.isNull()) { 18 self.reset(new AppLive); 19 } 20 } 21 22 return self.data(); 23} 24 25AppLive::AppLive(QObject *parent) : QObject(parent) 26{ 27 udpServer = new QUdpSocket(this); 28 29 QString name = qApp->applicationFilePath(); 30 QStringList list = name.split("/"); 31 appName = list.at(list.count() - 1).split(".").at(0); 32} 33 34void AppLive::readData() 35{ 36 QByteArray tempData; 37 38 do { 39 tempData.resize(udpServer->pendingDatagramSize()); 40 QHostAddress sender; 41 quint16 senderPort; 42 udpServer->readDatagram(tempData.data(), tempData.size(), &sender, &senderPort); 43 QString data = QLatin1String(tempData); 44 45 if (data == "hello") { 46 udpServer->writeDatagram(QString("%1OK").arg(appName).toLatin1(), sender, senderPort); 47 } 48 } while (udpServer->hasPendingDatagrams()); 49} 50 51bool AppLive::start(int port) 52{ 53 bool ok = udpServer->bind(port); 54 if (ok) { 55 connect(udpServer, SIGNAL(readyRead()), this, SLOT(readData())); 56 qDebug() << TIMEMS << "Start AppLive Ok"; 57 } 58 59 return ok; 60} 61 62void AppLive::stop() 63{ 64 udpServer->abort(); 65 disconnect(udpServer, SIGNAL(readyRead()), this, SLOT(readData())); 66}
二、Qt编写密钥生成器
在很多商业软件中,需要提供一些可以试运行的版本,这样就需要配套密钥机制来控制,纵观大部分的试用版软件,基本上采用以下几种机制来控制。
远程联网激活,每次启动都联网查看使用时间等,这种方法最完美,缺点是没法联网的设备就歇菜了。
通过获取本地的硬盘+CPU等硬件的编号,做一个运算,生成一个激活码,超过半数的软件会采用此方法,缺点是不能自由控制软件的其他参数,比如软件中添加的设备数量的控制。
设定一个运行到期时间+数量限制+已运行时间的密钥文件,发给用户配套软件使用,缺点是如果仅仅设置的是运行到期时间,用户可以更改电脑时间来获取更长的使用时间,在电脑不联网的情况下。
本demo采用抛砖引玉的形式,用第三种方法来实现,密钥文件采用最简单的异或加密,可以自行改成其他加密方法。
核心代码: 1#include "frmmain.h" 2#include "ui_frmmain.h" 3#include "qmessagebox.h" 4#include "qfile.h" 5 6frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain) 7{ 8 ui->setupUi(this); 9 this->initForm(); 10} 11 12frmMain::~frmMain() 13{ 14 delete ui; 15} 16 17void frmMain::initForm() 18{ 19 QStringList min; 20 min << "1" << "5" << "10" << "20" << "30"; 21 for (int i = 1; i <= 24; i++) { 22 min << QString::number(i * 60); 23 } 24 25 ui->cboxMin->addItems(min); 26 ui->cboxMin->setCurrentIndex(1); 27 ui->dateEdit->setDate(QDate::currentDate()); 28 29 for (int i = 5; i <= 150; i = i + 5) { 30 ui->cboxCount->addItem(QString("%1").arg(i)); 31 } 32} 33 34QString frmMain::getXorEncryptDecrypt(const QString &data, char key) 35{ 36 //采用异或加密,也可以自行更改算法 37 QByteArray buffer = data.toLatin1(); 38 int size = buffer.size(); 39 for (int i = 0; i < size; i++) { 40 buffer[i] = buffer.at(i) ^ key; 41 } 42 43 return QLatin1String(buffer); 44} 45 46void frmMain::on_btnOk_clicked() 47{ 48 bool useDate = ui->ckDate->isChecked(); 49 bool useRun = ui->ckRun->isChecked(); 50 bool useCount = ui->ckCount->isChecked(); 51 52 if (!useDate && !useRun && !useCount) { 53 if (QMessageBox::question(this, "询问", "确定要生成没有任何限制的密钥吗?") != QMessageBox::Yes) { 54 return; 55 } 56 } 57 58 QString strDate = ui->dateEdit->date().toString("yyyy-MM-dd"); 59 QString strRun = ui->cboxMin->currentText(); 60 QString strCount = ui->cboxCount->currentText(); 61 QString key = QString("%1|%2|%3|%4|%5|%6").arg(useDate).arg(strDate).arg(useRun).arg(strRun).arg(useCount).arg(strCount); 62 63 QFile file(QApplication::applicationDirPath() + "/key.db"); 64 file.open(QFile::WriteOnly | QIODevice::Text); 65 file.write(getXorEncryptDecrypt(key, 110).toLatin1()); 66 file.close(); 67 QMessageBox::information(this, "提示", "生成密钥成功,将 key.db 文件拷贝到对应目录即可!"); 68} 69 70void frmMain::on_btnClose_clicked() 71{ 72 this->close(); 73}
结语 可以直接点击下方阅读原文,或到Qt开源社区下载相关源码。
---------------------------------------------------------------------------------------------------------------------- 我们尊重原创,也注重分享,文章来源于微信公众号:yafeilinux和他的朋友们,建议关注公众号查看原文。如若侵权请联系qter@qter.org。 ----------------------------------------------------------------------------------------------------------------------
|