debug:初步可以传一个文件(有BUG)

This commit is contained in:
taynpg 2024-12-14 18:20:58 +08:00
parent edbd3400a8
commit 7250279ce0
4 changed files with 19 additions and 9 deletions

View File

@ -241,9 +241,8 @@ void CClient::handle_frame(CFrameBuffer* buf)
if (buf->mark_ != 0) { if (buf->mark_ != 0) {
break; break;
} }
std::string* key = new std::string(); work_key_ = buf->fid_;
key->append(buf->fid_); send_pool_->submit([&]() { send_file_data_th(); });
send_pool_->submit([&]() { send_file_data_th(key); });
break; break;
} }
// 能接收到 TRANS 一定是客户端(这里不是指Server) // 能接收到 TRANS 一定是客户端(这里不是指Server)
@ -283,7 +282,11 @@ void CClient::handle_frame(CFrameBuffer* buf)
break; break;
} }
case TYPE_TRANS_DONE: { case TYPE_TRANS_DONE: {
fclose(down_->file_); logger_->warn("Trans done, close file {}.", down_->cur_file_);
if (down_->file_) {
fclose(down_->file_);
down_->file_ = nullptr;
}
down_->trans_state_ = TRANS_DONE; down_->trans_state_ = TRANS_DONE;
break; break;
} }
@ -292,9 +295,9 @@ void CClient::handle_frame(CFrameBuffer* buf)
} }
} }
void CClient::send_file_data_th(std::string* key) void CClient::send_file_data_th()
{ {
std::string str_key = *key; std::string str_key = work_key_;
std::shared_ptr<TransInfomation> t = nullptr; std::shared_ptr<TransInfomation> t = nullptr;
{ {
@ -306,15 +309,16 @@ void CClient::send_file_data_th(std::string* key)
t = up_[str_key]; t = up_[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->type_ = TYPE_TRANS_FILE; buf->type_ = TYPE_TRANS_FILE;
buf->tid_ = str_key; buf->tid_ = str_key;
buf->data_ = new char[1024]{}; buf->data_ = new char[1024]{};
buf->mark_ = 1;
while (!feof(t->file_)) { while (!feof(t->file_)) {
buf->len_ = fread(buf->data_, 1, 1024, t->file_); buf->len_ = fread(buf->data_, 1, 1024, t->file_);
if (!send_frame(buf.get())) { if (!send_frame(buf.get())) {
logger_->error("send_file_data_th send failed."); logger_->error("send_file_data_th send failed.");
delete key;
return; return;
} }
} }
@ -323,5 +327,6 @@ void CClient::send_file_data_th(std::string* key)
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.");
} }
delete key; cancel_trans_file(str_key);
logger_->debug("Trans File {} To {} Done !!!", t->cur_file_, str_key);
} }

View File

@ -50,7 +50,7 @@ private:
private: private:
void handle_frame(CFrameBuffer* buf); void handle_frame(CFrameBuffer* buf);
void send_file_data_th(std::string* key); void send_file_data_th();
private: private:
std::shared_ptr<spdlog::logger> logger_; std::shared_ptr<spdlog::logger> logger_;
@ -62,4 +62,5 @@ private:
std::map<std::string, std::shared_ptr<TransInfomation>> up_; std::map<std::string, std::shared_ptr<TransInfomation>> up_;
std::mutex mutex_; std::mutex mutex_;
std::shared_ptr<CThreadPool> send_pool_; std::shared_ptr<CThreadPool> send_pool_;
std::string work_key_;
}; };

View File

@ -169,12 +169,15 @@ void CTcpServer::handle_frame()
} }
if (client_map_.count(buf->fid_)) { if (client_map_.count(buf->fid_)) {
auto& cli = client_map_[buf->fid_]; auto& cli = client_map_[buf->fid_];
buf->mark_ = 1;
buf->fid_ = buf->tid_;
if (!send_frame(cli->socket_, buf)) { if (!send_frame(cli->socket_, buf)) {
logger_->error("[{}] turn fid_ failed to {}", buf->fid_, buf->tid_); logger_->error("[{}] turn fid_ failed to {}", buf->fid_, buf->tid_);
} }
} }
break; break;
}; };
case TYPE_TRANS_DONE:
case TYPE_READY_TRANS: case TYPE_READY_TRANS:
case TYPE_TRANS_FILE: { case TYPE_TRANS_FILE: {
std::lock_guard<std::mutex> lock(cli_mut_); std::lock_guard<std::mutex> lock(cli_mut_);

View File

@ -59,6 +59,7 @@ CFrameBuffer* CTransProtocal::parse(CMutBuffer& buffer)
} }
result = new CFrameBuffer(); result = new CFrameBuffer();
result->data_ = new char[len]; result->data_ = new char[len];
std::memset(result->data_, 0x0, len);
result->len_ = len; result->len_ = len;
result->fid_ = std::string(buffer.get_data() + find + 2 + 2 + 1); result->fid_ = std::string(buffer.get_data() + find + 2 + 2 + 1);
result->tid_ = std::string(buffer.get_data() + find + 2 + 2 + 1 + 32); result->tid_ = std::string(buffer.get_data() + find + 2 + 2 + 1 + 32);