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
**GetTaskList:** 获取当前挂载到服务器的任务单。
**Get:** 获取当前挂载到服务器的任务单。
**DownTask:** 下载指定的任务清单,`param``GetTaskList`中列出的名称。
**DownTask:** 下载指定的任务清单,`param``Get`中列出的名称。
**UpTask:** 上载任务单,`param`为文件或者文件夹路径,可多个,使用`,`分隔。
@ -32,10 +32,8 @@
`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)
{
client_ = std::make_shared<CTcpClient>(io_context_, logger_);
supported_.push_back("GetTaskList");
supported_.push_back("Get");
}
CClient::~CClient()
@ -29,15 +29,37 @@ void CClient::run()
break;
}
auto vec = COfStr::split(cmd_input, " ");
if (vec.size() < 2) {
if (vec.size() < 1) {
logger_->error("input's invalid format.");
continue;
}
auto cmd = vec[0];
if (cmd == "GetTaskList") {
bool ret = get_task_list();
logger_->info("exec GetTaskList Command result is:{}.", ret);
std::string cmd{};
std::string param{};
if (vec.size() == 1) {
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();
thread.join();
@ -46,12 +68,11 @@ void CClient::run()
bool CClient::get_task_list()
{
logger_->info("{} start.", __FUNCTION__);
char* send = nullptr;
int len{};
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
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->type_ = 199;
if (!CTransProtocal::pack(buf.get(), &send, len)) {
@ -70,16 +91,34 @@ bool CClient::get_clients()
return false;
}
bool CClient::down_task()
{
return false;
}
bool CClient::up_task()
{
return false;
}
void CClient::handle_frame(CFrameBuffer* buf)
{
if (buf == nullptr) {
logger_->error("{} nullptr.", __FUNCTION__);
return;
}
logger_->debug("type: {}", buf->type_);
logger_->debug("len: {}", buf->len_);
// logger_->debug("type: {}", buf->type_);
// logger_->debug("len: {}", buf->len_);
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:
bool get_task_list();
bool get_clients();
bool down_task();
bool up_task();
private:
void handle_frame(CFrameBuffer* buf);
@ -25,4 +27,5 @@ private:
asio::io_context io_context_;
std::shared_ptr<CTcpClient> client_;
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 {
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;
} catch (const std::exception& ex) {
logger_->error("Send failed: {}", ex.what());

View File

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

View File

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

View File

@ -6,7 +6,7 @@
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
constexpr auto gGetTaskList = "GetTaskList";
constexpr auto gGet = "Get";
using namespace ofen;
std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file);
@ -55,4 +55,4 @@ public:
public:
static CFrameBuffer* parse(CMutBuffer& buffer);
static bool pack(CFrameBuffer* buf, char** out_buf, int& len);
};
};