add:添加心跳机制,检测传输失败时自动发现提示。
This commit is contained in:
parent
c3fde429ad
commit
3a68984857
@ -13,10 +13,13 @@ CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger
|
|||||||
send_pool_ = std::make_shared<CThreadPool>(g_SendPoolNum);
|
send_pool_ = std::make_shared<CThreadPool>(g_SendPoolNum);
|
||||||
send_pool_->init();
|
send_pool_->init();
|
||||||
supported_.push_back("Get");
|
supported_.push_back("Get");
|
||||||
|
sleep_.set_timeout(2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
CClient::~CClient()
|
CClient::~CClient()
|
||||||
{
|
{
|
||||||
|
th_run_ = false;
|
||||||
|
sleep_.contiune();
|
||||||
if (down_->file_) {
|
if (down_->file_) {
|
||||||
fclose(down_->file_);
|
fclose(down_->file_);
|
||||||
down_->file_ = nullptr;
|
down_->file_ = nullptr;
|
||||||
@ -28,20 +31,30 @@ CClient::~CClient()
|
|||||||
item.second->file_ = nullptr;
|
item.second->file_ = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (hearts_.joinable()) {
|
||||||
|
hearts_.join();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CClient::run(const std::string& ip, const std::string& port)
|
void CClient::run(const std::string& ip, const std::string& port)
|
||||||
{
|
{
|
||||||
|
th_run_ = true;
|
||||||
if (!client_->connect(ip, port)) {
|
if (!client_->connect(ip, port)) {
|
||||||
logger_->info("{} connect err.", __FUNCTION__);
|
logger_->info("{} connect err.", __FUNCTION__);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
client_->register_func([&](CFrameBuffer* buf) { handle_frame(buf); });
|
client_->register_func([&](CFrameBuffer* buf) { handle_frame(buf); });
|
||||||
client_->async_recv();
|
client_->async_recv();
|
||||||
|
hearts_ = std::thread([&]() { hearts(); });
|
||||||
std::thread thread([&]() { io_context_.run(); });
|
std::thread thread([&]() { io_context_.run(); });
|
||||||
logger_->warn("SupportCmd:Get|Up|Down|Cancel");
|
logger_->warn("SupportCmd:Get|Up|Down|Cancel");
|
||||||
char line[512]{};
|
char line[512]{};
|
||||||
while (std::cin.getline(line, 512)) {
|
while (std::cin.getline(line, 512)) {
|
||||||
|
|
||||||
|
if (!th_run_) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
std::string cmd_input(line);
|
std::string cmd_input(line);
|
||||||
if (cmd_input == "end") {
|
if (cmd_input == "end") {
|
||||||
break;
|
break;
|
||||||
@ -101,7 +114,9 @@ bool CClient::down_task(const std::string& param)
|
|||||||
|
|
||||||
// 开始传输文件
|
// 开始传输文件
|
||||||
for (const auto& item : vec) {
|
for (const auto& item : vec) {
|
||||||
down_one_file(task_list_[id]->id, item);
|
if (!down_one_file(task_list_[id]->id, item)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -175,8 +190,12 @@ bool CClient::down_one_file(const std::string& id, const std::string& file)
|
|||||||
down_->trans_state_ = TRANS_REDAY;
|
down_->trans_state_ = TRANS_REDAY;
|
||||||
while (down_->trans_state_ != TRANS_DONE) {
|
while (down_->trans_state_ != TRANS_DONE) {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
|
if (!th_run_) {
|
||||||
|
logger_->error("Interrup When Receive File.");
|
||||||
|
report_trans_ret(TRANS_FAILED);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,6 +233,7 @@ bool CClient::send_frame(CFrameBuffer* buf)
|
|||||||
logger_->error("{} pack failed.", __FUNCTION__);
|
logger_->error("{} pack failed.", __FUNCTION__);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
std::lock_guard<std::mutex> lock(send_mut_);
|
||||||
if (!client_->send(out_buf, out_len)) {
|
if (!client_->send(out_buf, out_len)) {
|
||||||
logger_->error("{} send failed.", __FUNCTION__);
|
logger_->error("{} send failed.", __FUNCTION__);
|
||||||
delete[] out_buf;
|
delete[] out_buf;
|
||||||
@ -369,3 +389,16 @@ void CClient::send_file_data_th()
|
|||||||
report_trans_ret(TRANS_DONE, 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CClient::hearts()
|
||||||
|
{
|
||||||
|
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
||||||
|
buf->type_ = TYPE_HEARTS;
|
||||||
|
while (th_run_) {
|
||||||
|
sleep_.sleep();
|
||||||
|
if (!send_frame(buf.get())) {
|
||||||
|
logger_->error("{} send failed.", __FUNCTION__);
|
||||||
|
th_run_ = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "file_oper.h"
|
#include "file_oper.h"
|
||||||
#include <of_util.h>
|
#include <of_util.h>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
using namespace ofen;
|
using namespace ofen;
|
||||||
struct DownClientInfo {
|
struct DownClientInfo {
|
||||||
@ -51,6 +52,7 @@ private:
|
|||||||
private:
|
private:
|
||||||
void handle_frame(CFrameBuffer* buf);
|
void handle_frame(CFrameBuffer* buf);
|
||||||
void send_file_data_th();
|
void send_file_data_th();
|
||||||
|
void hearts();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<spdlog::logger> logger_;
|
std::shared_ptr<spdlog::logger> logger_;
|
||||||
@ -61,6 +63,10 @@ private:
|
|||||||
std::shared_ptr<TransInfomation> down_;
|
std::shared_ptr<TransInfomation> down_;
|
||||||
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::mutex send_mut_;
|
||||||
std::shared_ptr<CThreadPool> send_pool_;
|
std::shared_ptr<CThreadPool> send_pool_;
|
||||||
std::string work_key_;
|
std::string work_key_;
|
||||||
|
std::thread hearts_;
|
||||||
|
CThreadSleep sleep_;
|
||||||
|
bool th_run_{false};
|
||||||
};
|
};
|
2
ofen
2
ofen
@ -1 +1 @@
|
|||||||
Subproject commit ab87624a333e385e08abb82ac623bce8da798539
|
Subproject commit cf6168a89a784521df0c9d63a049bc025daa1b85
|
@ -241,6 +241,10 @@ void CTcpServer::th_client(std::shared_ptr<asio::ip::tcp::socket> socket, const
|
|||||||
while (true) {
|
while (true) {
|
||||||
auto* frame = CTransProtocal::parse(cache->buffer_);
|
auto* frame = CTransProtocal::parse(cache->buffer_);
|
||||||
if (frame) {
|
if (frame) {
|
||||||
|
if (frame->type_ == TYPE_HEARTS) {
|
||||||
|
delete frame;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
frame->fid_ = client_key;
|
frame->fid_ = client_key;
|
||||||
std::lock_guard<std::mutex> lock(buf_mut_);
|
std::lock_guard<std::mutex> lock(buf_mut_);
|
||||||
cache_.push_back(frame);
|
cache_.push_back(frame);
|
||||||
|
@ -19,7 +19,8 @@ enum FrameType : int16_t {
|
|||||||
TYPE_READY_TRANS,
|
TYPE_READY_TRANS,
|
||||||
TYPE_INTERRUPT,
|
TYPE_INTERRUPT,
|
||||||
TYPE_NO_HIT_TASK,
|
TYPE_NO_HIT_TASK,
|
||||||
TYPE_WAITTING
|
TYPE_WAITTING,
|
||||||
|
TYPE_HEARTS
|
||||||
};
|
};
|
||||||
|
|
||||||
using namespace ofen;
|
using namespace ofen;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user