qt/LedOK/program/sendprogthread.cpp

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

2023-04-18 14:14:46 +08:00
#include "sendprogthread.h"
#include "gutil/qnetwork.h"
#include <QHostAddress>
#include <QDir>
#include <QDirIterator>
#include <QMessageBox>
#include <QMetaEnum>
SendProgThread::SendProgThread(const QString &progDir, const QString &ip, int port) : prog_dir(progDir), ip(ip), port(port) {
connect(this, &QThread::finished, this, &QThread::deleteLater);
}
void SendProgThread::run() {
emit emProgress(0); // 进度条归零
2024-06-06 21:55:36 +08:00
auto fileNames = QDir(prog_dir).entryList(QDir::Files);
if(fileNames.isEmpty()) {
2023-04-18 14:14:46 +08:00
emit emErr(tr("Program is empty"));
return;
}
if(stoped) return;
TcpSocket tcp;
tcp.connectToHost(ip, port);
if(! tcp.waitForConnected()) {
2024-06-06 21:55:36 +08:00
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at waitForConnected");
2023-04-18 14:14:46 +08:00
tcp.close();
return;
}
if(stoped) {
tcp.close();
return;
};
//发送节目列表协商
2024-06-06 21:55:36 +08:00
JArray files;
for(auto &name : fileNames) if(name!="program") {
QFileInfo info(prog_dir+"/"+name);
files.append(JObj{{"name", info.baseName()}, {"size", info.size()}});
2023-04-18 14:14:46 +08:00
}
2024-06-06 21:55:36 +08:00
if(! files.empty()) {
JObj req;
2023-04-18 14:14:46 +08:00
req.insert("_type", "consult");
2024-06-06 21:55:36 +08:00
req.insert("files", files);
req.insert("idList", JArray{"aaa"});
2023-04-18 14:14:46 +08:00
req.insert("zVer", "xixun1");
2024-06-06 21:55:36 +08:00
auto resNum = tcp.write(JToBytes(req));
2023-10-23 11:44:22 +08:00
tcp.flush();
2024-06-06 21:55:36 +08:00
if(resNum == -1 || ! tcp.waitForBytesWritten() || ! tcp.waitForReadyRead()) {
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at waitForRead 'checkExist'");
2023-04-18 14:14:46 +08:00
tcp.close();
return;
}
auto resp = tcp.readAll();
2024-06-06 21:55:36 +08:00
QString error;
auto res = JFrom(resp, &error);
2024-06-19 18:54:32 +08:00
while(error.contains("end-of-input")) {
2023-04-18 14:14:46 +08:00
if(! tcp.waitForReadyRead()) {
2024-06-06 21:55:36 +08:00
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at waitForRead2 'checkExist'");
2023-04-18 14:14:46 +08:00
tcp.close();
return;
}
2024-06-06 21:55:36 +08:00
resp += tcp.readAll();
2024-06-19 18:54:32 +08:00
error = "";
2024-06-06 21:55:36 +08:00
res = JFrom(resp, &error);
2023-04-18 14:14:46 +08:00
}
2024-06-06 21:55:36 +08:00
if(! error.isEmpty()) {
emit emErr(error+" at parse 'checkExist'");
2023-04-18 14:14:46 +08:00
tcp.close();
return;
}
2024-06-06 21:55:36 +08:00
auto existeds = res["existed"].toArray();
for(auto &existed : existeds) fileNames.removeAll(existed.toString());
2023-04-18 14:14:46 +08:00
}
if(stoped) {
tcp.close();
return;
}
qint64 progSize = 0;
2024-06-06 21:55:36 +08:00
for(auto &name : fileNames) progSize += QFileInfo(prog_dir+"/"+name).size();
2023-04-18 14:14:46 +08:00
if(progSize == 0) {
emit emErr(tr("Program is empty"));
tcp.close();
return;
}
2024-06-06 21:55:36 +08:00
auto req = JObj();
2023-04-18 14:14:46 +08:00
req.insert("_type", "proStart");
req.insert("proName", "program");
req.insert("proSize", progSize);
req.insert("zVer","xixun1");
2024-06-06 21:55:36 +08:00
auto resNum = tcp.write(JToBytes(req));
tcp.flush();
2023-04-18 14:14:46 +08:00
if(resNum == -1 || ! tcp.waitForBytesWritten()) {
2024-06-06 21:55:36 +08:00
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at write 'proStart'");
2023-04-18 14:14:46 +08:00
tcp.close();
return;
}
if(stoped) {
tcp.close();
return;
}
//4.发送协商列表应答里的文件
2024-02-21 18:08:50 +08:00
int64_t sentBytes = 0;
2023-10-23 11:44:22 +08:00
char buf[8192];
2024-06-06 21:55:36 +08:00
for(auto &name : fileNames) {
QFileInfo info(prog_dir+"/"+name);
2023-04-18 14:14:46 +08:00
auto baseName = info.baseName();
auto remain = info.size();
2024-06-06 21:55:36 +08:00
req = JObj();
2023-04-18 14:14:46 +08:00
req.insert("_type", "fileStart");
req.insert("id", baseName);
req.insert("size", remain);
req.insert("zVer","xixun1");
2024-06-06 21:55:36 +08:00
auto resNum = tcp.write(JToBytes(req));
if(resNum == -1) {
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at write 'fileStart'");
2023-04-18 14:14:46 +08:00
tcp.close();
return;
}
auto file = new QFile(info.filePath());
if(! file->open(QFile::ReadOnly)) {
emit emErr(tr("Open file failed")+" "+file->errorString());
tcp.close();
return;
}
while(remain > 0) {
2023-10-23 11:44:22 +08:00
resNum = file->read(buf, qMin(8192LL, remain));
if(resNum <= 0) {
2023-04-18 14:14:46 +08:00
emit emErr(tr("Read file failed")+" "+file->errorString());
tcp.close();
file->close();
return;
}
if(stoped) {
tcp.close();
file->close();
return;
};
2023-10-23 11:44:22 +08:00
if(! tcp.waitForBytesWritten(60000)) {
2024-06-06 21:55:36 +08:00
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at waitForWritten file: "+file->fileName());
2023-04-18 14:14:46 +08:00
tcp.close();
file->close();
return;
}
2023-10-23 11:44:22 +08:00
resNum = tcp.write(buf, resNum);
if(resNum <= 0) {
2024-06-06 21:55:36 +08:00
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at write file: "+file->fileName());
2023-04-18 14:14:46 +08:00
tcp.close();
file->close();
return;
}
if(stoped) {
tcp.close();
file->close();
return;
};
remain -= resNum;
sentBytes += resNum;
if(sentBytes != 0) emit emProgress(sentBytes * 99 / progSize);
}
file->close();
if(stoped) {
tcp.close();
return;
};
2024-06-06 21:55:36 +08:00
req = JObj();
2023-04-18 14:14:46 +08:00
req.insert("_type", "fileEnd");
req.insert("id", baseName);
req.insert("zVer", "xixun1");
2024-06-06 21:55:36 +08:00
resNum = tcp.write(JToBytes(req));
tcp.flush();
2023-04-18 14:14:46 +08:00
if(resNum == -1 || ! tcp.waitForBytesWritten()) {
2024-06-06 21:55:36 +08:00
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at write 'fileEnd'");
2023-04-18 14:14:46 +08:00
tcp.close();
return;
}
}
if(stoped) {
tcp.close();
return;
};
//5.发送结束
2024-06-06 21:55:36 +08:00
req = JObj();
2023-04-18 14:14:46 +08:00
req.insert("_type", "proEnd");
req.insert("proName", "program");
req.insert("zVer","xixun1");
2024-06-06 21:55:36 +08:00
resNum = tcp.write(JToBytes(req));
tcp.flush();
2023-04-18 14:14:46 +08:00
if(resNum == -1 || ! tcp.waitForBytesWritten()) {
2024-06-06 21:55:36 +08:00
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at write 'proEnd'");
2023-04-18 14:14:46 +08:00
tcp.close();
return;
};
if(! tcp.waitForReadyRead()) {
2024-06-06 21:55:36 +08:00
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at waitForRead 'proEnd'");
2023-04-18 14:14:46 +08:00
tcp.close();
return;
}
auto resp = tcp.readAll();
if(resp.isEmpty()) {
2024-06-06 21:55:36 +08:00
emit emErr(QString(socketErrKey(tcp.error()))+" ("+QString::number(tcp.error())+") "+tcp.errorString()+" at read 'proEnd'");
2023-04-18 14:14:46 +08:00
tcp.close();
return;
}
tcp.close();
emit emProgress(100);
emit emErr("OK");
}