From 15668ff112d0719410a84d55126c64bd30bf2e8e Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 7 Jan 2025 13:47:53 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E6=AD=A3=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=93=8D=E4=BD=9C=E6=94=B9=E4=B8=BAfstream=E5=BC=95?= =?UTF-8?q?=E5=8F=91=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 2 +- client/client.cpp | 53 +++++++++++++++++++++++++++++++------------ client/client.h | 2 ++ ofen | 2 +- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 84731ed..8ed18eb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -20,7 +20,7 @@ ], "visualizerFile": "${workspaceRoot}/.vscode/qt5.natvis", "args": [ - "-n", "1" + "-n", "0" ] }, "cmake.environment": { diff --git a/client/client.cpp b/client/client.cpp index 89b5207..e32e1b7 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -24,7 +24,7 @@ CClient::~CClient() { th_run_ = false; sleep_.contiune(); - if (down_->file_.is_open()) { + if (down_ && down_->file_.is_open()) { down_->file_.close(); } std::lock_guard lock(mutex_); @@ -75,6 +75,8 @@ void CClient::run(const std::string& ip, const std::string& port) } std::string cmd_input(line); + cmd_input = ofen::COfStr::trim(cmd_input); + if (cmd_input == "end") { th_run_ = false; std::this_thread::sleep_for(std::chrono::milliseconds(10)); @@ -145,6 +147,7 @@ bool CClient::down_task(const std::string& param) if (!down_one_file(task_list_[id]->id, item)) { break; } + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } return true; } @@ -246,11 +249,12 @@ bool CClient::down_one_file(const std::string& id, const std::string& file, cons down_->cur_remote_file_.clear(); return false; } + will_receive_ = true; down_->trans_state_ = TRANS_REDAY; cur_down_size_ = 0; float percent = 0.0; while (down_->trans_state_ != TRANS_DONE) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(std::chrono::milliseconds(down_check_wait)); if (cur_file_size_ > 0) { percent = (float)cur_down_size_ / cur_file_size_; CTransProtocal::display_progress(percent); @@ -261,7 +265,19 @@ bool CClient::down_one_file(const std::string& id, const std::string& file, cons return false; } } - return true; + if (cur_file_size_ > 0) { + percent = (float)cur_down_size_ / cur_file_size_; + CTransProtocal::display_progress(percent); + } + if (cur_file_size_ == cur_down_size_) { + logger_->warn("Trans done, close file {}, total:[{}/{}]", down_->cur_file_, cur_down_size_, + cur_file_size_); + return true; + } else { + logger_->warn("Trans failed, close file {}, total:[{}/{}]", down_->cur_file_, cur_down_size_, + cur_file_size_); + return false; + } } void CClient::report_trans_ret(TransState state, const std::string& key) @@ -270,6 +286,7 @@ void CClient::report_trans_ret(TransState state, const std::string& key) if (key.empty()) { t = down_; downloading_ = false; + will_receive_ = false; if (th_down_active_.joinable()) { th_down_active_.join(); } @@ -475,11 +492,14 @@ void CClient::handle_frame(CFrameBuffer* buf) downloading_ = true; th_down_active_ = std::thread([&]() { judget_down_active(); }); } - down_->file_.write(buf->data_, buf->len_); - if (down_->file_.fail()) { - logger_->warn("no matched write and data."); + if (will_receive_) { + down_->file_.write(buf->data_, buf->len_); + if (down_->file_.fail()) { + report_trans_ret(TRANS_FAILED); + logger_->warn("no matched write and data. {}", buf->len_); + } + cur_down_size_ += buf->len_; } - cur_down_size_ += buf->len_; break; } case TYPE_OPEN_FILE: { @@ -506,7 +526,6 @@ void CClient::handle_frame(CFrameBuffer* buf) break; } case TYPE_TRANS_DONE: { - logger_->warn("Trans done, close file {}.", down_->cur_file_); report_trans_ret(TRANS_DONE); break; } @@ -572,7 +591,7 @@ void CClient::handle_frame(CFrameBuffer* buf) std::string str_size(buf->data_, buf->len_); long long size = std::stoll(str_size); std::string show_str = OfUtil::get_file_size(size); - logger_->info("Ready Down Size:{}", show_str); + logger_->info("Ready Down Size: {}", show_str); cur_file_size_ = size; } default: @@ -597,6 +616,7 @@ void CClient::send_file_data_th(const char* keys) logger_->info("Start Trans File {} To {}", t->cur_file_, str_key); std::shared_ptr buf = std::make_shared(); buf->data_ = new char[g_BuffSize]{}; + buf->tid_ = str_key; // seekg 用于读,seekp 用于写。 t->file_.seekg(0, std::ios::end); @@ -604,6 +624,8 @@ void CClient::send_file_data_th(const char* keys) t->file_.seekg(0, std::ios::beg); buf->type_ = TYPE_FILE_SIZE; std::string str_size = std::to_string(size); + + logger_->info("To {} File Size: {} [{}]", str_key, ofen::OfUtil::get_file_size(size), size); buf->len_ = std::snprintf(buf->data_, g_BuffSize, "%s", str_size.c_str()); if (!send_frame(buf.get())) { report_trans_ret(TRANS_FAILED, str_key); @@ -611,22 +633,22 @@ void CClient::send_file_data_th(const char* keys) return; } buf->type_ = TYPE_TRANS_FILE; - buf->tid_ = str_key; buf->mark_ = 1; - while (t->file_.read(buf->data_, g_BuffSize)) { + while (!t->file_.eof()) { if (t->trans_state_ == TRANS_BREAK) { logger_->warn("Stop Trans {} To {} failed.", t->cur_file_, str_key); report_trans_ret(TRANS_FAILED, str_key); return; } + t->file_.read(buf->data_, g_BuffSize); buf->len_ = t->file_.gcount(); if (!send_frame(buf.get())) { report_trans_ret(TRANS_FAILED, str_key); logger_->error("Stop Trans {} To {} failed.", t->cur_file_, str_key); return; } - // std::this_thread::sleep_for(std::chrono::milliseconds(10)); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } buf->type_ = TYPE_TRANS_DONE; @@ -680,14 +702,15 @@ std::vector CFileOpr::get_file_list(const std::string& input) } auto vec = COfStr::split(backup, "|"); for (const auto& item : vec) { - std::string ret(item); + std::string ret = COfStr::trim(item); + std::string trim_item = ret; #ifdef _WIN32 if (item.find("\"") != std::string::npos) { - ret = COfStr::replace(item, "\"", ""); + ret = COfStr::replace(trim_item, "\"", ""); } #else if (item.find(R"(')") != std::string::npos) { - ret = COfStr::replace(item, R"(')", ""); + ret = COfStr::replace(trim_item, R"(')", ""); } #endif result.push_back(COfPath::to_full(ret)); diff --git a/client/client.h b/client/client.h index 959a20c..1af5697 100644 --- a/client/client.h +++ b/client/client.h @@ -30,6 +30,7 @@ struct TransInfomation { TransState trans_state_{TRANS_FAILED}; }; +constexpr int down_check_wait = 100; // millsec class CClient { public: @@ -66,6 +67,7 @@ private: std::thread hearts_; CThreadSleep sleep_; bool th_run_{false}; + bool will_receive_{false}; bool downloading_{false}; std::shared_ptr logger_; asio::io_context io_context_; diff --git a/ofen b/ofen index f690cd2..57269a5 160000 --- a/ofen +++ b/ofen @@ -1 +1 @@ -Subproject commit f690cd20e528923c6b0894b22d9f7cf40cedbcfd +Subproject commit 57269a5e733bf03c8e2d31fa464afa0053e2394a