fix:修正一个连续下载时,文件之间间隔停顿的问题。

This commit is contained in:
taynpg 2025-04-07 11:31:43 +08:00
parent 1677765111
commit 6cff24fd13

View File

@ -67,6 +67,7 @@ void CClient::run(const std::string& ip, const std::string& port, const std::str
client_->async_recv(); client_->async_recv();
hearts_ = std::thread([&]() { hearts(); }); hearts_ = std::thread([&]() { hearts(); });
std::thread thread([&]() { io_context_.run(); }); std::thread thread([&]() { io_context_.run(); });
th_down_active_ = std::thread([&]() { judget_down_active(); });
auto* bf = new CFrameBuffer(); auto* bf = new CFrameBuffer();
bf->type_ = TYPE_GET_ID; bf->type_ = TYPE_GET_ID;
@ -423,9 +424,6 @@ void CClient::report_trans_ret(TransState state, const std::string& key)
t = down_; t = down_;
downloading_ = false; downloading_ = false;
will_receive_ = false; will_receive_ = false;
if (th_down_active_.joinable()) {
th_down_active_.join();
}
} else { } else {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
if (up_.count(key)) { if (up_.count(key)) {
@ -763,7 +761,6 @@ void CClient::handle_frame(CFrameBuffer* buf)
case TYPE_TRANS_FILE: { case TYPE_TRANS_FILE: {
if (!downloading_) { if (!downloading_) {
downloading_ = true; downloading_ = true;
th_down_active_ = std::thread([&]() { judget_down_active(); });
} }
if (will_receive_) { if (will_receive_) {
down_->file_.write(buf->data_, buf->len_); down_->file_.write(buf->data_, buf->len_);
@ -1003,10 +1000,15 @@ void CClient::judget_down_active()
{ {
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>(); std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->type_ = TYPE_JUDGE_ACTIVE; buf->type_ = TYPE_JUDGE_ACTIVE;
buf->tid_ = down_->cur_remote_id_; while (th_run_) {
while (downloading_ && th_run_) { std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::this_thread::sleep_for(std::chrono::milliseconds(2000)); if (!downloading_) {
send_frame(buf.get()); continue;
}
if (down_) {
buf->tid_ = down_->cur_remote_id_;
send_frame(buf.get());
}
} }
} }