🐞 fix(文件传输): 修改手机上传文件逻辑

This commit is contained in:
huheng@kylinos.cn 2023-03-20 17:39:50 +08:00
parent 9d136bd639
commit b192bd42ae
2 changed files with 66 additions and 32 deletions

View File

@ -365,6 +365,7 @@ void ConnectionService::onMessageReadyRead()
m_dataSize = 0;
m_data.clear();
m_currentFileIndex = 0;
m_writeSize = 0;
ReplyMessage replyMessage;
// 获取所有文件大小并且计算0kb文件数量
int zeroFileNum = 0;
@ -476,42 +477,32 @@ void ConnectionService::onDataReadyRead()
}
} break;
case DataType::FILE_DATA: {
while (m_data.size() >= m_filesInfo.file_info(m_currentFileIndex).size()) {
// 接收到的数据包含一整个文件的大小
QFile *file =
new QFile(m_fileSavePath + QString::fromStdString(m_filesInfo.file_info(m_currentFileIndex).name()));
bool openFlag = file->open(QIODevice::WriteOnly);
if (!openFlag) {
// 打开文件失败
qCritical() << "Fail to open file:"
+ QString::fromStdString(m_filesInfo.file_info(m_currentFileIndex).name());
m_data = m_data.mid(m_filesInfo.file_info(m_currentFileIndex).size(), m_data.size());
m_currentFileIndex++;
delete file;
file = nullptr;
continue;
if (m_writeFile == nullptr) {
openWriteFile();
}
// 切割单个文件数据
QByteArray fileDate = m_data.mid(0, m_filesInfo.file_info(m_currentFileIndex).size());
file->write(fileDate.data(), fileDate.size());
m_data = m_data.mid(m_filesInfo.file_info(m_currentFileIndex).size(),
m_data.size() - m_filesInfo.file_info(m_currentFileIndex).size());
m_currentFileIndex++;
file->flush();
file->close();
delete file;
if (m_currentFileIndex >= m_filesInfo.file_info_size()) {
qInfo() << "File transfer finish!";
Q_EMIT sigFileReceived(m_fileSavePath);
if (m_writeSize < m_filesInfo.file_info(m_currentFileIndex).size()) {
if (m_data.size() <= (m_filesInfo.file_info(m_currentFileIndex).size() - m_writeSize)) {
m_writeFile->write(m_data.data(), m_data.size());
m_writeSize += m_data.size();
m_data.clear();
m_dataType = DataType::FREE;
m_currentFileIndex = 0;
m_filesInfo.clear_file_info();
m_filesInfo.Clear();
return;
} else {
while (m_data.size() >= (m_filesInfo.file_info(m_currentFileIndex).size() - m_writeSize)) {
// 切割单个文件数据
int index = m_filesInfo.file_info(m_currentFileIndex).size() - m_writeSize;
QByteArray fileDate = m_data.mid(0, index);
m_writeFile->write(fileDate.data(), fileDate.size());
m_writeSize += fileDate.size();
m_data = m_data.mid(index, m_data.size() - index);
if (m_writeSize == m_filesInfo.file_info(m_currentFileIndex).size()) {
endFile();
}
}
}
}
if (m_writeSize == m_filesInfo.file_info(m_currentFileIndex).size()) {
endFile();
}
} break;
case DataType::STREAM_DATA: {
} break;
@ -786,6 +777,44 @@ bool ConnectionService::sendBusyMessage()
return sendRe;
}
void ConnectionService::openWriteFile()
{
qDebug() << "write file " << QString::fromStdString(m_filesInfo.file_info(m_currentFileIndex).name())
<< m_filesInfo.file_info(m_currentFileIndex).size();
m_writeSize = 0;
m_writeFile = new QFile(m_fileSavePath + QString::fromStdString(m_filesInfo.file_info(m_currentFileIndex).name()));
bool openFlag = m_writeFile->open(QIODevice::WriteOnly);
if (!openFlag) {
// 打开文件失败
qCritical() << "Fail to open file:" + QString::fromStdString(m_filesInfo.file_info(m_currentFileIndex).name());
delete m_writeFile;
m_writeFile = nullptr;
return;
}
}
void ConnectionService::endFile()
{
m_currentFileIndex++;
m_writeFile->flush();
m_writeFile->close();
delete m_writeFile;
m_writeFile = nullptr;
if (m_currentFileIndex >= m_filesInfo.file_info_size()) {
qInfo() << "File transfer finish!";
Q_EMIT sigFileReceived(m_fileSavePath);
m_data.clear();
m_dataType = DataType::FREE;
m_currentFileIndex = 0;
m_filesInfo.clear_file_info();
m_filesInfo.Clear();
return;
} else {
openWriteFile();
}
}
bool ConnectionService::sendStartAudioMessage()
{
qInfo() << "Notify the Android terminal to start the sound service!";

View File

@ -7,6 +7,7 @@
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>
#include <QFile>
#include "serverstatus.h"
#include "messageserialize.pb.h"
@ -132,6 +133,8 @@ private:
void disconnectSocket();
bool sendMessage(const ReplyMessage &message);
bool sendBusyMessage();
void openWriteFile();
void endFile();
bool sendStartAudioMessage();
void clearConnectionInfo();
void updateDevice();
@ -160,6 +163,7 @@ private:
QThread *m_statusThread = nullptr; // 判断服务器状态线程
ConnectSuccessCallBack m_connectSuccessCallBack = nullptr; // 连接成功回调函数
ConnectFailCallBack m_connectFailCallBack = nullptr; // 连接失败回调函数
QFile *m_writeFile = nullptr;
// usb
AdbProcess *m_adb = nullptr; // adb接口
QTimer *m_timer = nullptr; //获取socket状态定时器
@ -172,6 +176,7 @@ private:
bool m_isMsgSocketConnect = false; // 消息通道是否已连接
DataType m_dataType = DataType::FREE; // 当前数据类型
qint64 m_dataSize = 0; // 接收的数据大小
qint64 m_writeSize = 0; // 当前写入文件大小
bool m_isHead = true; // 是否为数据头部
int m_currentFileIndex = 0; // 当前文件索引
QByteArray m_data; // 需要发送的数据