From c3fde429ad95601cbd0f38b32887cf4b6b62c168 Mon Sep 17 00:00:00 2001 From: taynpg Date: Wed, 18 Dec 2024 13:20:23 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=8F=91=E9=80=81=E5=A4=B1=E8=B4=A5=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E6=89=93=E5=BC=80=E7=9A=84=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=80=80=E5=87=BA=E6=97=B6=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E6=89=93=E5=BC=80=E7=9A=84=E6=96=87=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/client.cpp | 27 +++++++++++++++++++-------- client/client.h | 6 +++--- server/server.cpp | 2 +- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/client/client.cpp b/client/client.cpp index c967f22..b43fd0b 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -17,6 +17,17 @@ CClient::CClient(const std::shared_ptr& logger) : logger_(logger CClient::~CClient() { + if (down_->file_) { + fclose(down_->file_); + down_->file_ = nullptr; + } + std::lock_guard lock(mutex_); + for (const auto& item : up_) { + if (item.second->file_) { + fclose(item.second->file_); + item.second->file_ = nullptr; + } + } } void CClient::run(const std::string& ip, const std::string& port) @@ -169,7 +180,7 @@ bool CClient::down_one_file(const std::string& id, const std::string& file) return true; } -void CClient::cancel_trans_file(const std::string& key) +void CClient::report_trans_ret(TransState state, const std::string& key) { std::shared_ptr t = nullptr; if (key.empty()) { @@ -180,15 +191,14 @@ void CClient::cancel_trans_file(const std::string& key) t = up_[key]; } } - if (t == nullptr) { return; } - - t->trans_state_ = TRANS_FAILE; + t->trans_state_ = state; if (t->file_) { fclose(t->file_); - if (key.empty()) { + t->file_ = nullptr; + if (key.empty() && t->trans_state_ == TRANS_FAILED) { fs::remove(t->cur_file_); } } @@ -296,7 +306,7 @@ void CClient::handle_frame(CFrameBuffer* buf) } if (t->file_ == nullptr) { logger_->error("open file {} failed.", t->cur_file_); - cancel_trans_file(buf->mark_ == 0 ? id : ""); + report_trans_ret(TRANS_FAILED, buf->mark_ == 0 ? id : ""); break; } std::shared_ptr tmp = std::make_shared(); @@ -305,7 +315,7 @@ void CClient::handle_frame(CFrameBuffer* buf) tmp->tid_ = buf->fid_; if (!send_frame(tmp.get())) { logger_->error("TYPE_OPEN_FILE send ready failed."); - cancel_trans_file(buf->mark_ == 0 ? id : ""); + report_trans_ret(TRANS_FAILED, buf->mark_ == 0 ? id : ""); } break; } @@ -346,6 +356,7 @@ void CClient::send_file_data_th() while (!feof(t->file_)) { buf->len_ = fread(buf->data_, 1, g_BuffSize, t->file_); if (!send_frame(buf.get())) { + report_trans_ret(TRANS_FAILED, str_key); logger_->error("send_file_data_th send failed."); return; } @@ -355,6 +366,6 @@ void CClient::send_file_data_th() if (!send_frame(buf.get())) { logger_->error("send_file_data_th send DONE failed."); } - cancel_trans_file(str_key); + report_trans_ret(TRANS_DONE, str_key); logger_->debug("Trans File {} To {} Done !!!", t->cur_file_, str_key); } diff --git a/client/client.h b/client/client.h index 56413f9..0670991 100644 --- a/client/client.h +++ b/client/client.h @@ -14,7 +14,7 @@ struct DownClientInfo { }; enum TransState { - TRANS_FAILE, + TRANS_FAILED, TRANS_ING, TRANS_REDAY, TRANS_DONE @@ -25,7 +25,7 @@ struct TransInfomation { std::string cur_remote_file_; std::string cur_file_; FILE* file_{}; - TransState trans_state_{TRANS_FAILE}; + TransState trans_state_{TRANS_FAILED}; }; class CClient @@ -43,7 +43,7 @@ public: bool up_task(const std::string& cmd); bool cancel_task(); bool down_one_file(const std::string& id, const std::string& file); - void cancel_trans_file(const std::string& key = ""); + void report_trans_ret(TransState state, const std::string& key = ""); private: bool send_frame(CFrameBuffer* buf); diff --git a/server/server.cpp b/server/server.cpp index a0e73a7..93dcfa2 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -3,7 +3,7 @@ #include using namespace ofen; - +constexpr int g_MaxCacheLen = 1024 * 1024 * 50; constexpr int g_ParseThreadNum = 1; CTcpServer::CTcpServer(asio::io_context& io_context, const std::shared_ptr& logger) : io_context_(io_context), logger_(logger), acceptor_(io_context)