fix:修正文件操作改为fstream引发的问题。

This commit is contained in:
taynpg 2025-01-07 13:47:53 +08:00
parent 9a284b846b
commit 15668ff112
4 changed files with 42 additions and 17 deletions

View File

@ -20,7 +20,7 @@
],
"visualizerFile": "${workspaceRoot}/.vscode/qt5.natvis",
"args": [
"-n", "1"
"-n", "0"
]
},
"cmake.environment": {

View File

@ -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<std::mutex> 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<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
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<std::string> 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));

View File

@ -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<spdlog::logger> logger_;
asio::io_context io_context_;

2
ofen

@ -1 +1 @@
Subproject commit f690cd20e528923c6b0894b22d9f7cf40cedbcfd
Subproject commit 57269a5e733bf03c8e2d31fa464afa0053e2394a