add:添加下载进度,待进一步测试。

This commit is contained in:
taynpg 2025-01-07 12:07:43 +08:00
parent b00b7b91af
commit 654b637e7f
5 changed files with 77 additions and 22 deletions

View File

@ -24,15 +24,13 @@ CClient::~CClient()
{ {
th_run_ = false; th_run_ = false;
sleep_.contiune(); sleep_.contiune();
if (down_ && down_->file_) { if (down_->file_.is_open()) {
fclose(down_->file_); down_->file_.close();
down_->file_ = nullptr;
} }
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
for (const auto& item : up_) { for (const auto& item : up_) {
if (item.second->file_) { if (item.second->file_.is_open()) {
fclose(item.second->file_); item.second->file_.close();
item.second->file_ = nullptr;
} }
} }
for (auto& item : ths_) { for (auto& item : ths_) {
@ -136,7 +134,7 @@ bool CClient::down_task(const std::string& param)
} }
down_ = std::make_shared<TransInfomation>(); down_ = std::make_shared<TransInfomation>();
const auto& vec = task_list_[id]->files; const auto& vec = task_list_[id]->files;
if (vec.empty()) { if (vec.empty()) {
logger_->warn("No files List, Please Check!"); logger_->warn("No files List, Please Check!");
return false; return false;
@ -230,8 +228,8 @@ bool CClient::down_one_file(const std::string& id, const std::string& file, cons
} }
logger_->warn("Start Down => {} To {}", down_->cur_remote_file_, down_->cur_file_); logger_->warn("Start Down => {} To {}", down_->cur_remote_file_, down_->cur_file_);
down_->file_ = fopen(down_->cur_file_.c_str(), "wb"); down_->file_.open(down_->cur_file_, std::ios::out | std::ios::binary);
if (down_->file_ == nullptr) { if (!down_->file_.is_open()) {
logger_->error("Open {} Failed.", down_->cur_file_); logger_->error("Open {} Failed.", down_->cur_file_);
return false; return false;
} }
@ -249,8 +247,14 @@ bool CClient::down_one_file(const std::string& id, const std::string& file, cons
return false; return false;
} }
down_->trans_state_ = TRANS_REDAY; down_->trans_state_ = TRANS_REDAY;
cur_down_size_ = 0;
float percent = 0.0;
while (down_->trans_state_ != TRANS_DONE) { while (down_->trans_state_ != TRANS_DONE) {
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (cur_file_size_ > 0) {
percent = (float)cur_down_size_ / cur_file_size_;
CTransProtocal::display_progress(percent);
}
if (!th_run_) { if (!th_run_) {
logger_->error("Interrup When Receive File."); logger_->error("Interrup When Receive File.");
report_trans_ret(TRANS_FAILED); report_trans_ret(TRANS_FAILED);
@ -279,9 +283,8 @@ void CClient::report_trans_ret(TransState state, const std::string& key)
return; return;
} }
t->trans_state_ = state; t->trans_state_ = state;
if (t->file_) { if (t->file_.is_open()) {
fclose(t->file_); t->file_.close();
t->file_ = nullptr;
if (key.empty() && t->trans_state_ == TRANS_FAILED) { if (key.empty() && t->trans_state_ == TRANS_FAILED) {
fs::remove(t->cur_file_); fs::remove(t->cur_file_);
} }
@ -472,10 +475,12 @@ void CClient::handle_frame(CFrameBuffer* buf)
downloading_ = true; downloading_ = true;
th_down_active_ = std::thread([&]() { judget_down_active(); }); th_down_active_ = std::thread([&]() { judget_down_active(); });
} }
auto ws = fwrite(buf->data_, 1, buf->len_, down_->file_); down_->file_.write(buf->data_, buf->len_);
auto ws = down_->file_.gcount();
if (static_cast<int>(ws) != buf->len_) { if (static_cast<int>(ws) != buf->len_) {
logger_->warn("no matched write and data."); logger_->warn("no matched write and data.");
} }
cur_down_size_ += ws;
break; break;
} }
case TYPE_OPEN_FILE: { case TYPE_OPEN_FILE: {
@ -488,9 +493,9 @@ void CClient::handle_frame(CFrameBuffer* buf)
#else #else
up_[buf->fid_]->cur_file_ = std::string(buf->data_, buf->len_); up_[buf->fid_]->cur_file_ = std::string(buf->data_, buf->len_);
#endif #endif
up_[buf->fid_]->file_ = fopen(up_[buf->fid_]->cur_file_.c_str(), "rb"); up_[buf->fid_]->file_.open(up_[buf->fid_]->cur_file_, std::ios::in | std::ios::binary);
up_[buf->fid_]->trans_state_ = TRANS_REDAY; up_[buf->fid_]->trans_state_ = TRANS_REDAY;
if (up_[buf->fid_]->file_ == nullptr) { if (!up_[buf->fid_]->file_.is_open()) {
logger_->error("Ready Send File {} Open Failed.", up_[buf->fid_]->cur_file_); logger_->error("Ready Send File {} Open Failed.", up_[buf->fid_]->cur_file_);
break; break;
} }
@ -564,6 +569,13 @@ void CClient::handle_frame(CFrameBuffer* buf)
logger_->info("remote {} do task {} failed!", buf->fid_, list_file_); logger_->info("remote {} do task {} failed!", buf->fid_, list_file_);
break; break;
} }
case TYPE_FILE_SIZE: {
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);
cur_file_size_ = size;
}
default: default:
break; break;
} }
@ -585,17 +597,31 @@ void CClient::send_file_data_th(const char* keys)
logger_->info("Start Trans File {} To {}", t->cur_file_, str_key); logger_->info("Start Trans File {} To {}", t->cur_file_, str_key);
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>(); std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->data_ = new char[g_BuffSize]{};
// seekg 用于读,seekp 用于写。
t->file_.seekg(0, std::ios::end);
long long size = t->file_.tellg();
t->file_.seekg(0, std::ios::beg);
buf->type_ = TYPE_FILE_SIZE;
std::string str_size = std::to_string(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);
logger_->error("Stop Trans {} To {} failed.", t->cur_file_, str_key);
return;
}
buf->type_ = TYPE_TRANS_FILE; buf->type_ = TYPE_TRANS_FILE;
buf->tid_ = str_key; buf->tid_ = str_key;
buf->data_ = new char[g_BuffSize]{};
buf->mark_ = 1; buf->mark_ = 1;
while (!feof(t->file_)) {
while (t->file_.read(buf->data_, g_BuffSize)) {
if (t->trans_state_ == TRANS_BREAK) { if (t->trans_state_ == TRANS_BREAK) {
logger_->warn("Stop Trans {} To {} failed.", t->cur_file_, str_key); logger_->warn("Stop Trans {} To {} failed.", t->cur_file_, str_key);
report_trans_ret(TRANS_FAILED, str_key); report_trans_ret(TRANS_FAILED, str_key);
return; return;
} }
buf->len_ = fread(buf->data_, 1, g_BuffSize, t->file_); buf->len_ = t->file_.gcount();
if (!send_frame(buf.get())) { if (!send_frame(buf.get())) {
report_trans_ret(TRANS_FAILED, str_key); report_trans_ret(TRANS_FAILED, str_key);
logger_->error("Stop Trans {} To {} failed.", t->cur_file_, str_key); logger_->error("Stop Trans {} To {} failed.", t->cur_file_, str_key);

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <fstream>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <net_base.h> #include <net_base.h>
@ -25,7 +26,7 @@ struct TransInfomation {
std::string cur_remote_id_; std::string cur_remote_id_;
std::string cur_remote_file_; std::string cur_remote_file_;
std::string cur_file_; std::string cur_file_;
FILE* file_{}; std::fstream file_{};
TransState trans_state_{TRANS_FAILED}; TransState trans_state_{TRANS_FAILED};
}; };
@ -75,6 +76,8 @@ private:
std::vector<std::thread> ths_; std::vector<std::thread> ths_;
std::map<std::string, std::shared_ptr<TransInfomation>> up_; std::map<std::string, std::shared_ptr<TransInfomation>> up_;
std::thread th_down_active_; std::thread th_down_active_;
long long cur_file_size_{};
long long cur_down_size_{};
private: private:
std::string update_list_content_; std::string update_list_content_;

2
ofen

@ -1 +1 @@
Subproject commit 1d86de126afe83c7a9c934adca74006ca90ec65f Subproject commit f690cd20e528923c6b0894b22d9f7cf40cedbcfd

View File

@ -1,6 +1,7 @@
#include "util.h" #include "util.h"
#include <cstdint> #include <cstdint>
#include <thread> #include <thread>
#include <iostream>
std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file) std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file)
{ {
@ -117,6 +118,29 @@ bool CTransProtocal::pack(CFrameBuffer* buf, char** out_buf, int& len)
return true; return true;
} }
void CTransProtocal::display_progress(float percent)
{
if (percent > 1.0 || percent < 0.0) {
return;
}
const int barWidth = 50;
int pos = barWidth * percent;
std::cout << "[";
for (int i = 0; i < barWidth; ++i) {
if (i < pos) {
std::cout << "=";
} else if (i == pos) {
std::cout << ">";
} else {
std::cout << " ";
}
}
// \r 回到行首
std::cout << "] " << int(percent * 100.0f) << " %\r";
std::cout.flush();
}
CFrameBuffer::CFrameBuffer() CFrameBuffer::CFrameBuffer()
{ {
} }

View File

@ -28,7 +28,8 @@ enum FrameType : int16_t {
TYPE_UNCONFIRM_UPDATE_LIST, TYPE_UNCONFIRM_UPDATE_LIST,
TYPE_DONE_UPDATE_LIST, TYPE_DONE_UPDATE_LIST,
TYPE_FAILED_UPDATE_LIST, TYPE_FAILED_UPDATE_LIST,
TYPE_GET_ID TYPE_GET_ID,
TYPE_FILE_SIZE
}; };
using namespace ofen; using namespace ofen;
@ -71,4 +72,5 @@ public:
public: public:
static CFrameBuffer* parse(CMutBuffer& buffer); static CFrameBuffer* parse(CMutBuffer& buffer);
static bool pack(CFrameBuffer* buf, char** out_buf, int& len); static bool pack(CFrameBuffer* buf, char** out_buf, int& len);
static void display_progress(float percent);
}; };