update:进度更新。

This commit is contained in:
taynpg 2024-12-13 16:59:31 +08:00
parent b7eb7cd974
commit da8edd6765
7 changed files with 70 additions and 28 deletions

View File

@ -20,9 +20,9 @@
`type`:1 `type`:1
**GetTaskList:** 获取当前挂载到服务器的任务单。 **Get:** 获取当前挂载到服务器的任务单。
**DownTask:** 下载指定的任务清单,`param``GetTaskList`中列出的名称。 **DownTask:** 下载指定的任务清单,`param``Get`中列出的名称。
**UpTask:** 上载任务单,`param`为文件或者文件夹路径,可多个,使用`,`分隔。 **UpTask:** 上载任务单,`param`为文件或者文件夹路径,可多个,使用`,`分隔。
@ -32,10 +32,8 @@
`mark``0`时表示数据的最后一包,其他数据表示非最后一包。 `mark``0`时表示数据的最后一包,其他数据表示非最后一包。
`type`: 199 `type`: 199,特殊标记,表示询问在线客户端及挂载任务。
特殊标记,表示询问在线客户端 `type`: 198,特殊标记,下载任务
`type`: 198 `type`: 197,特殊标记,上载任务。
特殊标记,表示询问挂载任务。

View File

@ -6,7 +6,7 @@ using namespace ofen;
CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger) CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger)
{ {
client_ = std::make_shared<CTcpClient>(io_context_, logger_); client_ = std::make_shared<CTcpClient>(io_context_, logger_);
supported_.push_back("GetTaskList"); supported_.push_back("Get");
} }
CClient::~CClient() CClient::~CClient()
@ -29,15 +29,37 @@ void CClient::run()
break; break;
} }
auto vec = COfStr::split(cmd_input, " "); auto vec = COfStr::split(cmd_input, " ");
if (vec.size() < 2) { if (vec.size() < 1) {
logger_->error("input's invalid format."); logger_->error("input's invalid format.");
continue; continue;
} }
auto cmd = vec[0]; std::string cmd{};
if (cmd == "GetTaskList") { std::string param{};
bool ret = get_task_list(); if (vec.size() == 1) {
logger_->info("exec GetTaskList Command result is:{}.", ret); cmd = vec[0];
} }
else {
cmd = vec[0];
param = vec[1];
}
if (cmd == "Get") {
get_task_list();
continue;
}
if (cmd == "Down") {
int key = param.empty() ? -1 : std::stoi(param);
if (task_list_.count(key)) {
down_task();
}
else {
logger_->error("no task number find.");
}
continue;
}
if (cmd == "Up") {
continue;
}
logger_->error("No matched cmd.");
} }
client_->disconnect(); client_->disconnect();
thread.join(); thread.join();
@ -46,12 +68,11 @@ void CClient::run()
bool CClient::get_task_list() bool CClient::get_task_list()
{ {
logger_->info("{} start.", __FUNCTION__);
char* send = nullptr; char* send = nullptr;
int len{}; int len{};
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>(); std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->data_ = new char[512]{}; buf->data_ = new char[512]{};
auto flen = std::snprintf(buf->data_, 512, "%s", gGetTaskList); auto flen = std::snprintf(buf->data_, 512, "%s", gGet);
buf->len_ = flen; buf->len_ = flen;
buf->type_ = 199; buf->type_ = 199;
if (!CTransProtocal::pack(buf.get(), &send, len)) { if (!CTransProtocal::pack(buf.get(), &send, len)) {
@ -70,16 +91,34 @@ bool CClient::get_clients()
return false; return false;
} }
bool CClient::down_task()
{
return false;
}
bool CClient::up_task()
{
return false;
}
void CClient::handle_frame(CFrameBuffer* buf) void CClient::handle_frame(CFrameBuffer* buf)
{ {
if (buf == nullptr) { if (buf == nullptr) {
logger_->error("{} nullptr.", __FUNCTION__); logger_->error("{} nullptr.", __FUNCTION__);
return; return;
} }
logger_->debug("type: {}", buf->type_); // logger_->debug("type: {}", buf->type_);
logger_->debug("len: {}", buf->len_); // logger_->debug("len: {}", buf->len_);
if (buf->type_ == 199) { if (buf->type_ == 199) {
logger_->debug("data: {}", buf->data_); task_list_.clear();
std::string source(buf->data_);
int index = 0;
auto vec = COfStr::split(source, "|");
for (const auto& item : vec) {
task_list_[index] = item;
++index;
logger_->warn("{}:{}", index, item);
}
} }
} }

View File

@ -16,6 +16,8 @@ public:
public: public:
bool get_task_list(); bool get_task_list();
bool get_clients(); bool get_clients();
bool down_task();
bool up_task();
private: private:
void handle_frame(CFrameBuffer* buf); void handle_frame(CFrameBuffer* buf);
@ -25,4 +27,5 @@ private:
asio::io_context io_context_; asio::io_context io_context_;
std::shared_ptr<CTcpClient> client_; std::shared_ptr<CTcpClient> client_;
std::vector<std::string> supported_; std::vector<std::string> supported_;
std::map<int, std::string> task_list_;
}; };

View File

@ -39,7 +39,7 @@ bool CTcpClient::send(const char* data, int len)
{ {
try { try {
auto send_size = asio::write(socket_, asio::buffer(data, len)); auto send_size = asio::write(socket_, asio::buffer(data, len));
logger_->info("Need Send len: {} Real Send len: {}", len, send_size); //logger_->info("Need Send len: {} Real Send len: {}", len, send_size);
return static_cast<int>(send_size) == len; return static_cast<int>(send_size) == len;
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
logger_->error("Send failed: {}", ex.what()); logger_->error("Send failed: {}", ex.what());

View File

@ -47,12 +47,12 @@ void CTcpServer::stop()
client_threads_.clear(); client_threads_.clear();
} }
std::vector<std::string> CTcpServer::get_clients() std::vector<std::pair<std::string, std::string>> CTcpServer::get_clients()
{ {
std::vector<std::string> result; std::vector<std::pair<std::string, std::string>> result;
std::lock_guard<std::mutex> lock(cli_mut_); std::lock_guard<std::mutex> lock(cli_mut_);
for (const auto& item : client_map_) { for (const auto& item : client_map_) {
result.push_back(item.first); result.push_back(std::make_pair(item.first, item.second->task_));
} }
return result; return result;
} }
@ -66,9 +66,9 @@ SimpleBuffer* CTcpServer::get_client_list()
std::string msg; std::string msg;
for (const auto& item : vec) { for (const auto& item : vec) {
if (msg.empty()) { if (msg.empty()) {
msg.append(item); msg.append(item.first + "," + item.second);
} else { } else {
msg.append("|" + item); msg.append("|" + item.first + "," + item.second);
} }
} }
buf->data_ = new char[msg.size() + 1]; buf->data_ = new char[msg.size() + 1];
@ -116,9 +116,9 @@ void CTcpServer::handle_frame()
sbuf->id_ = buf->id_; sbuf->id_ = buf->id_;
std::lock_guard<std::mutex> lock(sbuf_mut_); std::lock_guard<std::mutex> lock(sbuf_mut_);
scache_.push(sbuf); scache_.push(sbuf);
continue;
} }
delete buf; delete buf;
buf = nullptr;
} }
} }
@ -150,6 +150,7 @@ void CTcpServer::send_simple_buf()
} }
socket->send(asio::buffer(buf->data_, buf->len_)); socket->send(asio::buffer(buf->data_, buf->len_));
delete buf; delete buf;
buf = nullptr;
} }
} }

View File

@ -11,6 +11,7 @@ struct ClientCache {
std::shared_ptr<asio::ip::tcp::socket> socket_; std::shared_ptr<asio::ip::tcp::socket> socket_;
CMutBuffer buffer_; CMutBuffer buffer_;
std::array<char, 1024> tmp_buf_; std::array<char, 1024> tmp_buf_;
std::string task_;
}; };
class CTcpServer class CTcpServer
@ -24,7 +25,7 @@ public:
void stop(); void stop();
private: private:
std::vector<std::string> get_clients(); std::vector<std::pair<std::string, std::string>> get_clients();
SimpleBuffer* get_client_list(); SimpleBuffer* get_client_list();
private: private:

View File

@ -6,7 +6,7 @@
#include <spdlog/sinks/stdout_color_sinks.h> #include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
constexpr auto gGetTaskList = "GetTaskList"; constexpr auto gGet = "Get";
using namespace ofen; using namespace ofen;
std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file); std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file);