flow:流量限定初步调试完成。

This commit is contained in:
2025-11-09 11:53:59 +08:00
parent 089d542a8d
commit 0ac77752c9
7 changed files with 125 additions and 7 deletions

View File

@@ -223,6 +223,10 @@ void ClientCore::UseFrame(QSharedPointer<FrameBuffer> frame)
emit sigMsgAnswer(frame);
break;
}
case FBT_SER_FLOW_LIMIT: {
emit sigFlowBack(frame);
break;
}
default:
qCritical() << QString("未知的帧类型: %1").arg(frame->type);
break;

View File

@@ -80,6 +80,7 @@ signals:
void sigYourId(QSharedPointer<FrameBuffer> frame);
void sigMsgAsk(QSharedPointer<FrameBuffer> frame);
void sigMsgAnswer(QSharedPointer<FrameBuffer> frame);
void sigFlowBack(QSharedPointer<FrameBuffer> frame);
signals:
void conSuccess();

View File

@@ -1,5 +1,6 @@
#include "FileTrans.h"
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
@@ -362,6 +363,8 @@ void FileTrans::SendFile(const QSharedPointer<DoTransTask>& task)
{
auto* sendThread = new SendThread(clientCore_);
sendThread->setTask(task);
connect(clientCore_, &ClientCore::sigFlowBack, sendThread, &SendThread::fbtFlowBack);
QMutexLocker locker(&sthMut_);
upTasks_[task->task.localId] = sendThread;
sendThread->run();
@@ -371,13 +374,30 @@ SendThread::SendThread(ClientCore* clientCore) : cliCore_(clientCore)
{
}
// 发送速度控制
void SendThread::fbtFlowBack(QSharedPointer<FrameBuffer> frame)
{
auto msg = infoUnpack<InfoMsg>(frame->data);
delay_ = msg.mark * BLOCK_LEVEL_MULTIPLE;
}
// constexpr int SendThread::sendDelay()
// {
// // 这里限定,frlay的最大发送速度,目前是 200MB/s。
// constexpr int MAX_SEND_SPD = 1024 * 1024 * 200;
// constexpr double DELAY = (static_cast<double>(CHUNK_BUF_SIZE) / MAX_SEND_SPD) * 1000;
// int minDelay = qMax(static_cast<int>(DELAY), 1);
// return minDelay;
// }
void SendThread::run()
{
// task's file shoule be already opened.
isSuccess_ = true;
delay_ = 0;
bool invokeSuccess = false;
qInfo() << tr("开始发送文件:") << task_->file.fileName();
while (!task_->file.atEnd()) {
auto frame = QSharedPointer<FrameBuffer>::create();
frame->tid = task_->task.remoteId;
frame->type = FBT_CLI_FILE_BUFFER;
@@ -390,6 +410,9 @@ void SendThread::run()
break;
}
frame->data.resize(br);
if (delay_ > 0) {
QThread::msleep(delay_);
}
invokeSuccess = QMetaObject::invokeMethod(cliCore_, "SendFrame", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(bool, isSuccess_), Q_ARG(QSharedPointer<FrameBuffer>, frame));
if (!invokeSuccess || !isSuccess_) {
@@ -397,7 +420,10 @@ void SendThread::run()
break;
}
task_->tranSize += frame->data.size();
// 关键点:这里不调用,无法中途收到别人发的数据。
QCoreApplication::processEvents();
}
qInfo() << tr("结束发送文件:") << task_->file.fileName();
InfoMsg info;
auto f = cliCore_->GetBuffer(info, FBT_CLI_TRANS_DONE, task_->task.remoteId);
ClientCore::syncInvoke(cliCore_, f);

View File

@@ -48,11 +48,16 @@ public:
public:
void run() override;
void setTask(const QSharedPointer<DoTransTask>& task);
void fbtFlowBack(QSharedPointer<FrameBuffer> frame);
// private:
// static constexpr int sendDelay();
private:
bool isSuccess_{false};
ClientCore* cliCore_;
quint32 curSendCount_{0};
qint32 delay_{};
QSharedPointer<DoTransTask> task_;
};