From 7ae8a6cf70a462b7337d3b8b1aab7d8d34d8a6c2 Mon Sep 17 00:00:00 2001 From: taynpg Date: Wed, 18 Dec 2024 19:52:04 +0800 Subject: [PATCH] =?UTF-8?q?debug=EF=BC=9A=E8=B0=83=E8=AF=95=E9=80=9A?= =?UTF-8?q?=E8=BF=87=EF=BC=8C=E5=BE=85=E8=BF=9B=E4=B8=80=E6=AD=A5=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/client.cpp | 25 ++++++++++++------------- client/client.h | 16 ++++++++-------- server/server.cpp | 2 +- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/client/client.cpp b/client/client.cpp index a10c0a4..a094bbc 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -6,12 +6,9 @@ #include namespace fs = std::filesystem; -constexpr int g_SendPoolNum = 1; CClient::CClient(const std::shared_ptr& logger) : logger_(logger) { client_ = std::make_shared(io_context_, logger_); - send_pool_ = std::make_shared(g_SendPoolNum); - send_pool_->init(); supported_.push_back("Get"); sleep_.set_timeout(2000); } @@ -34,6 +31,12 @@ CClient::~CClient() if (hearts_.joinable()) { hearts_.join(); } + + for (auto& item : ths_) { + if (item.joinable()) { + item.join(); + } + } } void CClient::run(const std::string& ip, const std::string& port) @@ -304,7 +307,7 @@ void CClient::handle_frame(CFrameBuffer* buf) break; } case TYPE_OPEN_FILE: { - char* keys = nullptr; + std::string keys{}; { std::lock_guard lock(mutex_); up_[buf->fid_] = std::make_shared(); @@ -314,15 +317,15 @@ void CClient::handle_frame(CFrameBuffer* buf) up_[buf->fid_]->cur_file_ = std::string(buf->data_, buf->len_); #endif up_[buf->fid_]->file_ = fopen(up_[buf->fid_]->cur_file_.c_str(), "rb"); + up_[buf->fid_]->trans_state_ = TRANS_REDAY; if (up_[buf->fid_]->file_ == nullptr) { logger_->error("Ready Send File {} Open Failed.", up_[buf->fid_]->cur_file_); break; } - keys = new char[512](); - std::snprintf(keys, 512, "%s", buf->fid_.c_str()); + keys = buf->fid_; } - if (keys) { - send_pool_->submit([&]() { send_file_data_th(keys); }); + if (!keys.empty()) { + ths_.emplace_back(std::thread([this, keys]() { send_file_data_th(keys.c_str()); })); } break; } @@ -348,14 +351,10 @@ void CClient::handle_frame(CFrameBuffer* buf) } } -void CClient::send_file_data_th(char* keys) +void CClient::send_file_data_th(const char* keys) { std::string str_key(keys); std::shared_ptr t = nullptr; - std::shared_ptr deleter(new int(0), [&](int* p) { - delete p; - delete[] keys; - }); { std::lock_guard lock(mutex_); diff --git a/client/client.h b/client/client.h index 1531ac1..7e52520 100644 --- a/client/client.h +++ b/client/client.h @@ -51,22 +51,22 @@ private: private: void handle_frame(CFrameBuffer* buf); - void send_file_data_th(char* keys); + void send_file_data_th(const char* keys); void hearts(); private: + std::mutex mutex_; + std::mutex send_mut_; + std::string work_key_; + std::thread hearts_; + CThreadSleep sleep_; + bool th_run_{false}; std::shared_ptr logger_; asio::io_context io_context_; std::shared_ptr client_; std::vector supported_; std::map> task_list_; std::shared_ptr down_; + std::vector ths_; std::map> up_; - std::mutex mutex_; - std::mutex send_mut_; - std::shared_ptr send_pool_; - std::string work_key_; - std::thread hearts_; - CThreadSleep sleep_; - bool th_run_{false}; }; \ No newline at end of file diff --git a/server/server.cpp b/server/server.cpp index 37f0771..6cd8f1c 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -136,7 +136,7 @@ void CTcpServer::trans_data(CFrameBuffer* buf) break; } default: - logger_->warn("No Mathched type."); + logger_->warn("No Mathched type. {}", static_cast(buf->type_)); break; } }