fix:修正文件操作改为fstream引发的问题。
This commit is contained in:
		
							parent
							
								
									9a284b846b
								
							
						
					
					
						commit
						15668ff112
					
				
							
								
								
									
										2
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							@ -20,7 +20,7 @@
 | 
				
			|||||||
        ],
 | 
					        ],
 | 
				
			||||||
        "visualizerFile": "${workspaceRoot}/.vscode/qt5.natvis",
 | 
					        "visualizerFile": "${workspaceRoot}/.vscode/qt5.natvis",
 | 
				
			||||||
        "args": [
 | 
					        "args": [
 | 
				
			||||||
            "-n", "1"
 | 
					            "-n", "0"
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "cmake.environment": {
 | 
					    "cmake.environment": {
 | 
				
			||||||
 | 
				
			|||||||
@ -24,7 +24,7 @@ CClient::~CClient()
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    th_run_ = false;
 | 
					    th_run_ = false;
 | 
				
			||||||
    sleep_.contiune();
 | 
					    sleep_.contiune();
 | 
				
			||||||
    if (down_->file_.is_open()) {
 | 
					    if (down_ && down_->file_.is_open()) {
 | 
				
			||||||
        down_->file_.close();
 | 
					        down_->file_.close();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    std::lock_guard<std::mutex> lock(mutex_);
 | 
					    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);
 | 
					        std::string cmd_input(line);
 | 
				
			||||||
 | 
					        cmd_input = ofen::COfStr::trim(cmd_input);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (cmd_input == "end") {
 | 
					        if (cmd_input == "end") {
 | 
				
			||||||
            th_run_ = false;
 | 
					            th_run_ = false;
 | 
				
			||||||
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
 | 
					            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)) {
 | 
					        if (!down_one_file(task_list_[id]->id, item)) {
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        std::this_thread::sleep_for(std::chrono::milliseconds(10));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return true;
 | 
					    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();
 | 
					        down_->cur_remote_file_.clear();
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    will_receive_ = true;
 | 
				
			||||||
    down_->trans_state_ = TRANS_REDAY;
 | 
					    down_->trans_state_ = TRANS_REDAY;
 | 
				
			||||||
    cur_down_size_ = 0;
 | 
					    cur_down_size_ = 0;
 | 
				
			||||||
    float percent = 0.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(100));
 | 
					        std::this_thread::sleep_for(std::chrono::milliseconds(down_check_wait));
 | 
				
			||||||
        if (cur_file_size_ > 0) {
 | 
					        if (cur_file_size_ > 0) {
 | 
				
			||||||
            percent = (float)cur_down_size_ / cur_file_size_;
 | 
					            percent = (float)cur_down_size_ / cur_file_size_;
 | 
				
			||||||
            CTransProtocal::display_progress(percent);
 | 
					            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 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)
 | 
					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()) {
 | 
					    if (key.empty()) {
 | 
				
			||||||
        t = down_;
 | 
					        t = down_;
 | 
				
			||||||
        downloading_ = false;
 | 
					        downloading_ = false;
 | 
				
			||||||
 | 
					        will_receive_ = false;
 | 
				
			||||||
        if (th_down_active_.joinable()) {
 | 
					        if (th_down_active_.joinable()) {
 | 
				
			||||||
            th_down_active_.join();
 | 
					            th_down_active_.join();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -475,11 +492,14 @@ 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(); });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        down_->file_.write(buf->data_, buf->len_);
 | 
					        if (will_receive_) {
 | 
				
			||||||
        if (down_->file_.fail()) {
 | 
					            down_->file_.write(buf->data_, buf->len_);
 | 
				
			||||||
            logger_->warn("no matched write and data.");
 | 
					            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;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case TYPE_OPEN_FILE: {
 | 
					    case TYPE_OPEN_FILE: {
 | 
				
			||||||
@ -506,7 +526,6 @@ void CClient::handle_frame(CFrameBuffer* buf)
 | 
				
			|||||||
        break;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case TYPE_TRANS_DONE: {
 | 
					    case TYPE_TRANS_DONE: {
 | 
				
			||||||
        logger_->warn("Trans done, close file {}.", down_->cur_file_);
 | 
					 | 
				
			||||||
        report_trans_ret(TRANS_DONE);
 | 
					        report_trans_ret(TRANS_DONE);
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -572,7 +591,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
 | 
				
			|||||||
        std::string str_size(buf->data_, buf->len_);
 | 
					        std::string str_size(buf->data_, buf->len_);
 | 
				
			||||||
        long long size = std::stoll(str_size);
 | 
					        long long size = std::stoll(str_size);
 | 
				
			||||||
        std::string show_str = OfUtil::get_file_size(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;
 | 
					        cur_file_size_ = size;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    default:
 | 
					    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);
 | 
					    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]{};
 | 
					    buf->data_ = new char[g_BuffSize]{};
 | 
				
			||||||
 | 
					    buf->tid_ = str_key;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // seekg 用于读,seekp 用于写。
 | 
					    // seekg 用于读,seekp 用于写。
 | 
				
			||||||
    t->file_.seekg(0, std::ios::end);
 | 
					    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);
 | 
					    t->file_.seekg(0, std::ios::beg);
 | 
				
			||||||
    buf->type_ = TYPE_FILE_SIZE;
 | 
					    buf->type_ = TYPE_FILE_SIZE;
 | 
				
			||||||
    std::string str_size = std::to_string(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());
 | 
					    buf->len_ = std::snprintf(buf->data_, g_BuffSize, "%s", str_size.c_str());
 | 
				
			||||||
    if (!send_frame(buf.get())) {
 | 
					    if (!send_frame(buf.get())) {
 | 
				
			||||||
        report_trans_ret(TRANS_FAILED, str_key);
 | 
					        report_trans_ret(TRANS_FAILED, str_key);
 | 
				
			||||||
@ -611,22 +633,22 @@ void CClient::send_file_data_th(const char* keys)
 | 
				
			|||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    buf->type_ = TYPE_TRANS_FILE;
 | 
					    buf->type_ = TYPE_TRANS_FILE;
 | 
				
			||||||
    buf->tid_ = str_key;
 | 
					 | 
				
			||||||
    buf->mark_ = 1;
 | 
					    buf->mark_ = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while (t->file_.read(buf->data_, g_BuffSize)) {
 | 
					    while (!t->file_.eof()) {
 | 
				
			||||||
        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;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        t->file_.read(buf->data_, g_BuffSize);
 | 
				
			||||||
        buf->len_ = t->file_.gcount();
 | 
					        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);
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // std::this_thread::sleep_for(std::chrono::milliseconds(10));
 | 
					        std::this_thread::sleep_for(std::chrono::milliseconds(10));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    buf->type_ = TYPE_TRANS_DONE;
 | 
					    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, "|");
 | 
					    auto vec = COfStr::split(backup, "|");
 | 
				
			||||||
    for (const auto& item : vec) {
 | 
					    for (const auto& item : vec) {
 | 
				
			||||||
        std::string ret(item);
 | 
					        std::string ret = COfStr::trim(item);
 | 
				
			||||||
 | 
					        std::string trim_item = ret;
 | 
				
			||||||
#ifdef _WIN32
 | 
					#ifdef _WIN32
 | 
				
			||||||
        if (item.find("\"") != std::string::npos) {
 | 
					        if (item.find("\"") != std::string::npos) {
 | 
				
			||||||
            ret = COfStr::replace(item, "\"", "");
 | 
					            ret = COfStr::replace(trim_item, "\"", "");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
        if (item.find(R"(')") != std::string::npos) {
 | 
					        if (item.find(R"(')") != std::string::npos) {
 | 
				
			||||||
            ret = COfStr::replace(item, R"(')", "");
 | 
					            ret = COfStr::replace(trim_item, R"(')", "");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
        result.push_back(COfPath::to_full(ret));
 | 
					        result.push_back(COfPath::to_full(ret));
 | 
				
			||||||
 | 
				
			|||||||
@ -30,6 +30,7 @@ struct TransInfomation {
 | 
				
			|||||||
    TransState trans_state_{TRANS_FAILED};
 | 
					    TransState trans_state_{TRANS_FAILED};
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					constexpr int down_check_wait = 100;   // millsec
 | 
				
			||||||
class CClient
 | 
					class CClient
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
@ -66,6 +67,7 @@ private:
 | 
				
			|||||||
    std::thread hearts_;
 | 
					    std::thread hearts_;
 | 
				
			||||||
    CThreadSleep sleep_;
 | 
					    CThreadSleep sleep_;
 | 
				
			||||||
    bool th_run_{false};
 | 
					    bool th_run_{false};
 | 
				
			||||||
 | 
					    bool will_receive_{false};
 | 
				
			||||||
    bool downloading_{false};
 | 
					    bool downloading_{false};
 | 
				
			||||||
    std::shared_ptr<spdlog::logger> logger_;
 | 
					    std::shared_ptr<spdlog::logger> logger_;
 | 
				
			||||||
    asio::io_context io_context_;
 | 
					    asio::io_context io_context_;
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								ofen
									
									
									
									
									
								
							
							
								
								
								
								
								
								
									
									
								
							
						
						
									
										2
									
								
								ofen
									
									
									
									
									
								
							@ -1 +1 @@
 | 
				
			|||||||
Subproject commit f690cd20e528923c6b0894b22d9f7cf40cedbcfd
 | 
					Subproject commit 57269a5e733bf03c8e2d31fa464afa0053e2394a
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user