add:添加异常发送失败时,关闭打开的文件,添加退出时关闭打开的文件。

This commit is contained in:
taynpg 2024-12-18 13:20:23 +08:00
parent 0cafcaf643
commit c3fde429ad
3 changed files with 23 additions and 12 deletions

View File

@ -17,6 +17,17 @@ CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger
CClient::~CClient() CClient::~CClient()
{ {
if (down_->file_) {
fclose(down_->file_);
down_->file_ = nullptr;
}
std::lock_guard<std::mutex> 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) 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; 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<TransInfomation> t = nullptr; std::shared_ptr<TransInfomation> t = nullptr;
if (key.empty()) { if (key.empty()) {
@ -180,15 +191,14 @@ void CClient::cancel_trans_file(const std::string& key)
t = up_[key]; t = up_[key];
} }
} }
if (t == nullptr) { if (t == nullptr) {
return; return;
} }
t->trans_state_ = state;
t->trans_state_ = TRANS_FAILE;
if (t->file_) { if (t->file_) {
fclose(t->file_); fclose(t->file_);
if (key.empty()) { t->file_ = nullptr;
if (key.empty() && t->trans_state_ == TRANS_FAILED) {
fs::remove(t->cur_file_); fs::remove(t->cur_file_);
} }
} }
@ -296,7 +306,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
} }
if (t->file_ == nullptr) { if (t->file_ == nullptr) {
logger_->error("open file {} failed.", t->cur_file_); 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; break;
} }
std::shared_ptr<CFrameBuffer> tmp = std::make_shared<CFrameBuffer>(); std::shared_ptr<CFrameBuffer> tmp = std::make_shared<CFrameBuffer>();
@ -305,7 +315,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
tmp->tid_ = buf->fid_; tmp->tid_ = buf->fid_;
if (!send_frame(tmp.get())) { if (!send_frame(tmp.get())) {
logger_->error("TYPE_OPEN_FILE send ready failed."); 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; break;
} }
@ -346,6 +356,7 @@ void CClient::send_file_data_th()
while (!feof(t->file_)) { while (!feof(t->file_)) {
buf->len_ = fread(buf->data_, 1, g_BuffSize, t->file_); buf->len_ = fread(buf->data_, 1, g_BuffSize, t->file_);
if (!send_frame(buf.get())) { if (!send_frame(buf.get())) {
report_trans_ret(TRANS_FAILED, str_key);
logger_->error("send_file_data_th send failed."); logger_->error("send_file_data_th send failed.");
return; return;
} }
@ -355,6 +366,6 @@ void CClient::send_file_data_th()
if (!send_frame(buf.get())) { if (!send_frame(buf.get())) {
logger_->error("send_file_data_th send DONE failed."); 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); logger_->debug("Trans File {} To {} Done !!!", t->cur_file_, str_key);
} }

View File

@ -14,7 +14,7 @@ struct DownClientInfo {
}; };
enum TransState { enum TransState {
TRANS_FAILE, TRANS_FAILED,
TRANS_ING, TRANS_ING,
TRANS_REDAY, TRANS_REDAY,
TRANS_DONE TRANS_DONE
@ -25,7 +25,7 @@ struct TransInfomation {
std::string cur_remote_file_; std::string cur_remote_file_;
std::string cur_file_; std::string cur_file_;
FILE* file_{}; FILE* file_{};
TransState trans_state_{TRANS_FAILE}; TransState trans_state_{TRANS_FAILED};
}; };
class CClient class CClient
@ -43,7 +43,7 @@ public:
bool up_task(const std::string& cmd); bool up_task(const std::string& cmd);
bool cancel_task(); bool cancel_task();
bool down_one_file(const std::string& id, const std::string& file); 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: private:
bool send_frame(CFrameBuffer* buf); bool send_frame(CFrameBuffer* buf);

View File

@ -3,7 +3,7 @@
#include <of_util.h> #include <of_util.h>
using namespace ofen; using namespace ofen;
constexpr int g_MaxCacheLen = 1024 * 1024 * 50;
constexpr int g_ParseThreadNum = 1; constexpr int g_ParseThreadNum = 1;
CTcpServer::CTcpServer(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger) CTcpServer::CTcpServer(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger)
: io_context_(io_context), logger_(logger), acceptor_(io_context) : io_context_(io_context), logger_(logger), acceptor_(io_context)