change: some trans logic.

This commit is contained in:
2025-06-17 16:42:39 +08:00
parent 4b86ebc5c5
commit b0d07e6bc9
6 changed files with 125 additions and 71 deletions

View File

@@ -13,59 +13,51 @@ void FileTrans::SetTasks(const QVector<TransTask>& tasks)
void FileTrans::RegisterFrameCall()
{
clientCore_->SetFrameCall(FBT_CLI_REQ_SEND, [this](QSharedPointer<FrameBuffer> frame) { fbtReqSend(frame); });
clientCore_->SetFrameCall(FBT_CLI_REQ_RECV, [this](QSharedPointer<FrameBuffer> frame) { fbtReqRecv(frame); });
clientCore_->SetFrameCall(FBT_CLI_REQ_DOWN, [this](QSharedPointer<FrameBuffer> frame) { fbtReqDown(frame); });
clientCore_->SetFrameCall(FBT_CLI_TRANS_DONE, [this](QSharedPointer<FrameBuffer> frame) { fbtTransDone(frame); });
clientCore_->SetFrameCall(FBT_CLI_ANSSEND_SUCCESS, [this](QSharedPointer<FrameBuffer> frame) { fbtAnsSendSuccess(frame); });
clientCore_->SetFrameCall(FBT_CLI_ANSSEND_FAILED, [this](QSharedPointer<FrameBuffer> frame) { fbtAnsSendFailed(frame); });
clientCore_->SetFrameCall(FBT_CLI_ANSRECV_FAILED, [this](QSharedPointer<FrameBuffer> frame) { fbtAnsRecvFailed(frame); });
clientCore_->SetFrameCall(FBT_CLI_ANSRECV_SUCCESS, [this](QSharedPointer<FrameBuffer> frame) { fbtAnsRecvSuccess(frame); });
clientCore_->SetFrameCall(FBT_CLI_FILETRANS, [this](QSharedPointer<FrameBuffer> frame) { fbtFileTrans(frame); });
clientCore_->SetFrameCall(FBT_CLI_FILETRANS_FAILED, [this](QSharedPointer<FrameBuffer> frame) { fbtFileTransFailed(frame); });
clientCore_->SetFrameCall(FBT_CLI_CAN_SEND, [this](QSharedPointer<FrameBuffer> frame) { fbtCanSend(frame); });
clientCore_->SetFrameCall(FBT_CLI_CANOT_SEND, [this](QSharedPointer<FrameBuffer> frame) { fbtCanotSend(frame); });
clientCore_->SetFrameCall(FBT_CLI_CANOT_DOWN, [this](QSharedPointer<FrameBuffer> frame) { fbtCanotDown(frame); });
clientCore_->SetFrameCall(FBT_CLI_CAN_DOWN, [this](QSharedPointer<FrameBuffer> frame) { fbtCanDown(frame); });
clientCore_->SetFrameCall(FBT_CLI_FILE_BUFFER, [this](QSharedPointer<FrameBuffer> frame) { fbtFileBuffer(frame); });
clientCore_->SetFrameCall(FBT_CLI_TRANS_FAILED, [this](QSharedPointer<FrameBuffer> frame) { fbtTransFailed(frame); });
}
// The other party requests to send, prepare to receive.
void FileTrans::fbtReqSend(QSharedPointer<FrameBuffer> frame)
{
// judget is same client's same file.
// judget if file exits etc.
// send
InfoMsg info = infoUnpack<InfoMsg>(frame->data);
auto doTask = QSharedPointer<DoTransTask>::create();
doTask->file.setFileName(info.path);
if (!doTask->file.open(QIODevice::ReadOnly)) {
qCritical() << QString(tr("open file failed: %1")).arg(info.path);
qInfo() << QString(tr("%1 req send: %2 to %3")).arg(frame->fid).arg(info.fromPath).arg(info.toPath);
// judge is same client's same file.
// recv is single thread recv, judge idle
if (downTask_.state == TaskState::STATE_RUNNING) {
info.msg = QString(tr("busy"));
clientCore_->Send<InfoMsg>(info, FBT_CLI_CANOT_SEND, frame->fid);
return;
}
doTask->task.isUpload = true;
doTask->task.localPath = info.path;
doTask->task.remoteId = frame->fid;
SendFile(doTask);
}
void FileTrans::fbtReqRecv(QSharedPointer<FrameBuffer> frame)
{
// recv is single thread recv.
// judge idle
// reply msg
// recv
InfoMsg info = infoUnpack<InfoMsg>(frame->data);
downTask_.file.setFileName(info.path);
auto newerPath = Util::Get2FilePath(info.fromPath, info.toPath);
downTask_.file.setFileName(newerPath);
if (!downTask_.file.open(QIODevice::WriteOnly)) {
info.msg = QString(tr("open file failed: %1")).arg(info.path);
info.msg = QString(tr("open file failed: %1")).arg(newerPath);
qCritical() << info.msg;
if (!clientCore_->Send<InfoMsg>(info, FBT_CLI_ANSRECV_FAILED, frame->fid)) {
if (!clientCore_->Send<InfoMsg>(info, FBT_CLI_CANOT_SEND, frame->fid)) {
qCritical() << QString(tr("open recv file:%2 failed, and reply %2 failed.")).arg(info.msg).arg(frame->fid);
downTask_.file.close();
return;
}
return;
}
info.msg = QString(tr("open recv file success: %1")).arg(info.path);
if (!clientCore_->Send<InfoMsg>(info, FBT_CLI_ANSRECV_SUCCESS, frame->fid)) {
downTask_.totalSize = info.size;
downTask_.tranSize = 0;
downTask_.permission = info.permissions;
info.msg = QString(tr("open recv file success: %1")).arg(newerPath);
if (!clientCore_->Send<InfoMsg>(info, FBT_CLI_CAN_SEND, frame->fid)) {
qCritical() << QString(tr("open recv file:%2 success, but reply %2 failed.")).arg(info.msg).arg(frame->fid);
downTask_.file.close();
return;
@@ -73,6 +65,27 @@ void FileTrans::fbtReqRecv(QSharedPointer<FrameBuffer> frame)
downTask_.state = TaskState::STATE_RUNNING;
}
// The other party requests to download, prepare to send.
void FileTrans::fbtReqDown(QSharedPointer<FrameBuffer> frame)
{
InfoMsg info = infoUnpack<InfoMsg>(frame->data);
// judge is same client's same file.
// judge if file exits etc.
// send
auto doTask = QSharedPointer<DoTransTask>::create();
doTask->file.setFileName(info.fromPath);
if (!doTask->file.open(QIODevice::ReadOnly)) {
qCritical() << QString(tr("open file failed: %1")).arg(info.fromPath);
return;
}
doTask->task.isUpload = true;
doTask->task.localPath = info.fromPath;
doTask->task.remoteId = frame->fid;
SendFile(doTask);
}
void FileTrans::fbtTransDone(QSharedPointer<FrameBuffer> frame)
{
auto info = infoUnpack<InfoMsg>(frame->data);
@@ -80,35 +93,35 @@ void FileTrans::fbtTransDone(QSharedPointer<FrameBuffer> frame)
downTask_.file.close();
downTask_.state = TaskState::STATE_FINISH;
qInfo() << QString(tr("recv file:%1 success.")).arg(downTask_.file.fileName());
clientCore_->Send<InfoMsg>(info, FBT_CLI_ANSRECV_SUCCESS, frame->fid);
clientCore_->Send<InfoMsg>(info, FBT_CLI_CAN_DOWN, frame->fid);
return;
}
qCritical() << QString(tr("recv file:%1 done sigal, but file not opened.")).arg(info.msg);
}
void FileTrans::fbtAnsRecvSuccess(QSharedPointer<FrameBuffer> frame)
void FileTrans::fbtCanDown(QSharedPointer<FrameBuffer> frame)
{
// ready to send
InfoMsg info = infoUnpack<InfoMsg>(frame->data);
auto doTask = QSharedPointer<DoTransTask>::create();
doTask->file.setFileName(info.path);
doTask->file.setFileName(info.fromPath);
if (!doTask->file.open(QIODevice::ReadOnly)) {
qCritical() << QString(tr("open file failed: %1")).arg(info.path);
qCritical() << QString(tr("open file failed: %1")).arg(info.fromPath);
return;
}
doTask->task.isUpload = true;
doTask->task.localPath = info.path;
doTask->task.localPath = "";
doTask->task.remoteId = frame->fid;
SendFile(doTask);
}
void FileTrans::fbtAnsRecvFailed(QSharedPointer<FrameBuffer> frame)
void FileTrans::fbtCanotDown(QSharedPointer<FrameBuffer> frame)
{
InfoMsg info = infoUnpack<InfoMsg>(frame->data);
qCritical() << QString(tr("request send file:%1 failed. reason:%2")).arg(info.path).arg(info.msg);
qCritical() << QString(tr("request send file:%1 failed. reason:%2")).arg(info.fromPath).arg(info.msg);
}
void FileTrans::fbtFileTrans(QSharedPointer<FrameBuffer> frame)
void FileTrans::fbtFileBuffer(QSharedPointer<FrameBuffer> frame)
{
if (downTask_.state != TaskState::STATE_RUNNING) {
return;
@@ -119,22 +132,27 @@ void FileTrans::fbtFileTrans(QSharedPointer<FrameBuffer> frame)
downTask_.state = TaskState::STATE_FAILED;
InfoMsg info;
info.msg = downTask_.file.errorString();
clientCore_->Send<InfoMsg>(info, FBT_CLI_FILETRANS_FAILED, frame->fid);
clientCore_->Send<InfoMsg>(info, FBT_CLI_TRANS_FAILED, frame->fid);
}
}
void FileTrans::fbtAnsSendFailed(QSharedPointer<FrameBuffer> frame)
void FileTrans::fbtCanotSend(QSharedPointer<FrameBuffer> frame)
{
InfoMsg info = infoUnpack<InfoMsg>(frame->data);
qCritical() << QString(tr("request file:%1 failed. reason:%2")).arg(info.path).arg(info.msg);
qCritical() << QString(tr("request file:%1 failed. reason:%2")).arg(info.fromPath).arg(info.msg);
}
void FileTrans::fbtAnsSendSuccess(QSharedPointer<FrameBuffer> frame)
void FileTrans::fbtCanSend(QSharedPointer<FrameBuffer> frame)
{
}
void FileTrans::fbtFileTransFailed(QSharedPointer<FrameBuffer> frame)
void FileTrans::fbtTransFailed(QSharedPointer<FrameBuffer> frame)
{
qCritical() << QString(tr("trans file:%1 failed.")).arg(downTask_.file.fileName());
if (downTask_.file.isOpen()) {
downTask_.file.close();
}
downTask_.state = TaskState::STATE_FAILED;
}
void FileTrans::SendFile(const QSharedPointer<DoTransTask>& task)
@@ -155,7 +173,7 @@ void SendThread::run()
// task's file shoule be already opened.
auto frame = QSharedPointer<FrameBuffer>::create();
frame->tid = task_->task.remoteId;
frame->type = FBT_CLI_FILETRANS;
frame->type = FBT_CLI_FILE_BUFFER;
bool suc = true;
while (!task_->file.atEnd()) {

View File

@@ -27,6 +27,9 @@ enum class TaskState {
struct DoTransTask {
QFile file;
quint64 totalSize{0};
quint64 tranSize{0};
quint32 permission{};
TaskState state = TaskState::STATE_NONE;
TransTask task;
};
@@ -58,14 +61,14 @@ public:
private:
void fbtReqSend(QSharedPointer<FrameBuffer> frame);
void fbtReqRecv(QSharedPointer<FrameBuffer> frame);
void fbtReqDown(QSharedPointer<FrameBuffer> frame);
void fbtTransDone(QSharedPointer<FrameBuffer> frame);
void fbtAnsRecvSuccess(QSharedPointer<FrameBuffer> frame);
void fbtAnsRecvFailed(QSharedPointer<FrameBuffer> frame);
void fbtFileTrans(QSharedPointer<FrameBuffer> frame);
void fbtAnsSendFailed(QSharedPointer<FrameBuffer> frame);
void fbtAnsSendSuccess(QSharedPointer<FrameBuffer> frame);
void fbtFileTransFailed(QSharedPointer<FrameBuffer> frame);
void fbtCanDown(QSharedPointer<FrameBuffer> frame);
void fbtCanotDown(QSharedPointer<FrameBuffer> frame);
void fbtFileBuffer(QSharedPointer<FrameBuffer> frame);
void fbtCanotSend(QSharedPointer<FrameBuffer> frame);
void fbtCanSend(QSharedPointer<FrameBuffer> frame);
void fbtTransFailed(QSharedPointer<FrameBuffer> frame);
private:
void SendFile(const QSharedPointer<DoTransTask>& task);