debug:调试通过,待进一步测试。

This commit is contained in:
taynpg 2024-12-18 19:52:04 +08:00
parent 2cdeb965c3
commit 7ae8a6cf70
3 changed files with 21 additions and 22 deletions

View File

@ -6,12 +6,9 @@
#include <of_util.h>
namespace fs = std::filesystem;
constexpr int g_SendPoolNum = 1;
CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger)
{
client_ = std::make_shared<CTcpClient>(io_context_, logger_);
send_pool_ = std::make_shared<CThreadPool>(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<std::mutex> lock(mutex_);
up_[buf->fid_] = std::make_shared<TransInfomation>();
@ -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<TransInfomation> t = nullptr;
std::shared_ptr<int> deleter(new int(0), [&](int* p) {
delete p;
delete[] keys;
});
{
std::lock_guard<std::mutex> lock(mutex_);

View File

@ -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<spdlog::logger> logger_;
asio::io_context io_context_;
std::shared_ptr<CTcpClient> client_;
std::vector<std::string> supported_;
std::map<int, std::shared_ptr<DownClientInfo>> task_list_;
std::shared_ptr<TransInfomation> down_;
std::vector<std::thread> ths_;
std::map<std::string, std::shared_ptr<TransInfomation>> up_;
std::mutex mutex_;
std::mutex send_mut_;
std::shared_ptr<CThreadPool> send_pool_;
std::string work_key_;
std::thread hearts_;
CThreadSleep sleep_;
bool th_run_{false};
};

View File

@ -136,7 +136,7 @@ void CTcpServer::trans_data(CFrameBuffer* buf)
break;
}
default:
logger_->warn("No Mathched type.");
logger_->warn("No Mathched type. {}", static_cast<int>(buf->type_));
break;
}
}