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