update:可上载获取文件列表。
This commit is contained in:
parent
da8edd6765
commit
19fa6e3d48
@ -37,3 +37,5 @@
|
|||||||
`type`: 198,特殊标记,下载任务。
|
`type`: 198,特殊标记,下载任务。
|
||||||
|
|
||||||
`type`: 197,特殊标记,上载任务。
|
`type`: 197,特殊标记,上载任务。
|
||||||
|
|
||||||
|
`type`: 196,特殊标记,取消上载任务。
|
@ -8,5 +8,5 @@ if (MSVC)
|
|||||||
add_compile_options(/source-charset:utf-8)
|
add_compile_options(/source-charset:utf-8)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_executable(transmc main.cpp client.h client.cpp)
|
add_executable(transmc main.cpp client.h client.cpp file_oper.h file_oper.cpp)
|
||||||
target_link_libraries(transmc PRIVATE trans_net trans_util)
|
target_link_libraries(transmc PRIVATE trans_net trans_util)
|
@ -22,6 +22,7 @@ void CClient::run()
|
|||||||
client_->register_func([&](CFrameBuffer* buf) { handle_frame(buf); });
|
client_->register_func([&](CFrameBuffer* buf) { handle_frame(buf); });
|
||||||
client_->async_recv();
|
client_->async_recv();
|
||||||
std::thread thread([&]() { io_context_.run(); });
|
std::thread thread([&]() { io_context_.run(); });
|
||||||
|
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)) {
|
||||||
std::string cmd_input(line);
|
std::string cmd_input(line);
|
||||||
@ -37,8 +38,7 @@ void CClient::run()
|
|||||||
std::string param{};
|
std::string param{};
|
||||||
if (vec.size() == 1) {
|
if (vec.size() == 1) {
|
||||||
cmd = vec[0];
|
cmd = vec[0];
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
cmd = vec[0];
|
cmd = vec[0];
|
||||||
param = vec[1];
|
param = vec[1];
|
||||||
}
|
}
|
||||||
@ -50,13 +50,17 @@ void CClient::run()
|
|||||||
int key = param.empty() ? -1 : std::stoi(param);
|
int key = param.empty() ? -1 : std::stoi(param);
|
||||||
if (task_list_.count(key)) {
|
if (task_list_.count(key)) {
|
||||||
down_task();
|
down_task();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
logger_->error("no task number find.");
|
logger_->error("no task number find.");
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (cmd == "Up") {
|
if (cmd == "Up") {
|
||||||
|
up_task(cmd_input);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (cmd == "Cancel") {
|
||||||
|
cancel_task();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
logger_->error("No matched cmd.");
|
logger_->error("No matched cmd.");
|
||||||
@ -68,27 +72,7 @@ void CClient::run()
|
|||||||
|
|
||||||
bool CClient::get_task_list()
|
bool CClient::get_task_list()
|
||||||
{
|
{
|
||||||
char* send = nullptr;
|
return send_frame(199, gGet, std::strlen(gGet));
|
||||||
int len{};
|
|
||||||
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
|
||||||
buf->data_ = new char[512]{};
|
|
||||||
auto flen = std::snprintf(buf->data_, 512, "%s", gGet);
|
|
||||||
buf->len_ = flen;
|
|
||||||
buf->type_ = 199;
|
|
||||||
if (!CTransProtocal::pack(buf.get(), &send, len)) {
|
|
||||||
logger_->error("{} pack failed.", __FUNCTION__);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!client_->send(send, len)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
delete[] send;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CClient::get_clients()
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CClient::down_task()
|
bool CClient::down_task()
|
||||||
@ -96,10 +80,47 @@ bool CClient::down_task()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CClient::up_task()
|
bool CClient::up_task(const std::string& cmd)
|
||||||
{
|
{
|
||||||
|
auto list = CFileOpr::get_file_list(cmd);
|
||||||
|
std::string msg;
|
||||||
|
for (const auto& item : list) {
|
||||||
|
if (msg.empty()) {
|
||||||
|
msg.append(item);
|
||||||
|
} else {
|
||||||
|
msg.append("|" + item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return send_frame(197, msg.data(), msg.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CClient::cancel_task()
|
||||||
|
{
|
||||||
|
char buffer[] = "None";
|
||||||
|
return send_frame(196, buffer, sizeof(buffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CClient::send_frame(int type, const char* data, int len)
|
||||||
|
{
|
||||||
|
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
||||||
|
buf->type_ = type;
|
||||||
|
buf->data_ = new char[len + 1];
|
||||||
|
std::memset(buf->data_, 0x0, len + 1);
|
||||||
|
buf->len_ = std::snprintf(buf->data_, len + 1, "%s", data);
|
||||||
|
char* out_buf{};
|
||||||
|
int out_len{};
|
||||||
|
if (!CTransProtocal::pack(buf.get(), &out_buf, out_len)) {
|
||||||
|
logger_->error("{} pack failed.", __FUNCTION__);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!client_->send(out_buf, out_len)) {
|
||||||
|
logger_->error("{} send failed.", __FUNCTION__);
|
||||||
|
delete[] out_buf;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
delete[] out_buf;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void CClient::handle_frame(CFrameBuffer* buf)
|
void CClient::handle_frame(CFrameBuffer* buf)
|
||||||
{
|
{
|
||||||
@ -113,12 +134,22 @@ void CClient::handle_frame(CFrameBuffer* buf)
|
|||||||
if (buf->type_ == 199) {
|
if (buf->type_ == 199) {
|
||||||
task_list_.clear();
|
task_list_.clear();
|
||||||
std::string source(buf->data_);
|
std::string source(buf->data_);
|
||||||
int index = 0;
|
auto vec = COfStr::split(source, "\n");
|
||||||
auto vec = COfStr::split(source, "|");
|
|
||||||
for (const auto& item : vec) {
|
for (const auto& item : vec) {
|
||||||
task_list_[index] = item;
|
if (item.find("[") == std::string::npos) {
|
||||||
++index;
|
logger_->info("FILE ==> {}", item);
|
||||||
logger_->warn("{}:{}", index, item);
|
}
|
||||||
|
else {
|
||||||
|
logger_->debug("***********************************************");
|
||||||
|
logger_->debug("{}", item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// int index = 0;
|
||||||
|
// auto vec = COfStr::split(source, "|");
|
||||||
|
// for (const auto& item : vec) {
|
||||||
|
// task_list_[index] = item;
|
||||||
|
// ++index;
|
||||||
|
// logger_->warn("{}:{}", index, item);
|
||||||
|
// }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <util.h>
|
#include <util.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include "file_oper.h"
|
||||||
|
|
||||||
class CClient
|
class CClient
|
||||||
{
|
{
|
||||||
@ -15,9 +16,12 @@ public:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
bool get_task_list();
|
bool get_task_list();
|
||||||
bool get_clients();
|
|
||||||
bool down_task();
|
bool down_task();
|
||||||
bool up_task();
|
bool up_task(const std::string& cmd);
|
||||||
|
bool cancel_task();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool send_frame(int type, const char* data, int len);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void handle_frame(CFrameBuffer* buf);
|
void handle_frame(CFrameBuffer* buf);
|
||||||
|
25
client/file_oper.cpp
Normal file
25
client/file_oper.cpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#include "file_oper.h"
|
||||||
|
|
||||||
|
CFileOpr::CFileOpr()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
CFileOpr::~CFileOpr()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> CFileOpr::get_file_list(const std::string& input)
|
||||||
|
{
|
||||||
|
std::vector<std::string> result;
|
||||||
|
std::string backup = input;
|
||||||
|
backup.erase(0, backup.find_first_of(" "));
|
||||||
|
backup = COfStr::trim(backup);
|
||||||
|
if (backup.empty()) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
auto vec = COfStr::split(backup, "|");
|
||||||
|
for (const auto& item : vec) {
|
||||||
|
result.push_back(COfPath::to_full(item));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
15
client/file_oper.h
Normal file
15
client/file_oper.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <string>
|
||||||
|
#include <of_path.h>
|
||||||
|
#include <of_str.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace ofen;
|
||||||
|
class CFileOpr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CFileOpr();
|
||||||
|
~CFileOpr();
|
||||||
|
public:
|
||||||
|
static std::vector<std::string> get_file_list(const std::string& input);
|
||||||
|
};
|
2
ofen
2
ofen
@ -1 +1 @@
|
|||||||
Subproject commit 44fb416ca166aa9c4ce270ff71bea6b3df703d6e
|
Subproject commit 45d22c927b796181c4b53f426a4d9dd6ebc4c0ce
|
@ -1,16 +1,26 @@
|
|||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
#include <of_str.h>
|
||||||
|
|
||||||
|
using namespace ofen;
|
||||||
|
|
||||||
|
constexpr int g_ParseThreadNum = 1;
|
||||||
|
constexpr int g_SendThreadNum = 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)
|
||||||
{
|
{
|
||||||
th_run_ = true;
|
th_run_ = true;
|
||||||
handle_pool_ = std::make_shared<CThreadPool>(1);
|
handle_pool_ = std::make_shared<CThreadPool>(g_ParseThreadNum);
|
||||||
send_pool_ = std::make_shared<CThreadPool>(1);
|
send_pool_ = std::make_shared<CThreadPool>(g_SendThreadNum);
|
||||||
handle_pool_->init();
|
handle_pool_->init();
|
||||||
send_pool_->init();
|
send_pool_->init();
|
||||||
|
for (int i = 0; i < g_ParseThreadNum; ++i) {
|
||||||
handle_pool_->submit([&]() { handle_frame(); });
|
handle_pool_->submit([&]() { handle_frame(); });
|
||||||
|
}
|
||||||
|
for (int i = 0; i < g_ParseThreadNum; ++i) {
|
||||||
send_pool_->submit([&]() { send_simple_buf(); });
|
send_pool_->submit([&]() { send_simple_buf(); });
|
||||||
}
|
}
|
||||||
|
}
|
||||||
CTcpServer::~CTcpServer()
|
CTcpServer::~CTcpServer()
|
||||||
{
|
{
|
||||||
th_run_ = false;
|
th_run_ = false;
|
||||||
@ -47,12 +57,16 @@ void CTcpServer::stop()
|
|||||||
client_threads_.clear();
|
client_threads_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::pair<std::string, std::string>> CTcpServer::get_clients()
|
std::vector<TaskList> CTcpServer::get_clients()
|
||||||
{
|
{
|
||||||
std::vector<std::pair<std::string, std::string>> result;
|
std::vector<TaskList> 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(std::make_pair(item.first, item.second->task_));
|
TaskList t;
|
||||||
|
t.id_ = item.first;
|
||||||
|
t.task_ = item.second->task_;
|
||||||
|
t.time_ = item.second->time_;
|
||||||
|
result.push_back(t);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -64,11 +78,12 @@ SimpleBuffer* CTcpServer::get_client_list()
|
|||||||
|
|
||||||
auto vec = get_clients();
|
auto vec = get_clients();
|
||||||
std::string msg;
|
std::string msg;
|
||||||
|
int index = 1;
|
||||||
for (const auto& item : vec) {
|
for (const auto& item : vec) {
|
||||||
if (msg.empty()) {
|
msg.append(fmt::format("[{}][{}][{}]", index, item.id_, item.time_));
|
||||||
msg.append(item.first + "," + item.second);
|
auto files = COfStr::split(item.task_, "|");
|
||||||
} else {
|
for (const auto& file : files) {
|
||||||
msg.append("|" + item.first + "," + item.second);
|
msg.append("\n" + file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf->data_ = new char[msg.size() + 1];
|
buf->data_ = new char[msg.size() + 1];
|
||||||
@ -109,6 +124,7 @@ void CTcpServer::handle_frame()
|
|||||||
|
|
||||||
// 拿到该包后,要看转发给谁或者处理
|
// 拿到该包后,要看转发给谁或者处理
|
||||||
if (buf->type_ == 199) { // 询问在线客户端
|
if (buf->type_ == 199) { // 询问在线客户端
|
||||||
|
logger_->info("GetList.");
|
||||||
auto* sbuf = get_client_list();
|
auto* sbuf = get_client_list();
|
||||||
if (sbuf == nullptr) {
|
if (sbuf == nullptr) {
|
||||||
continue;
|
continue;
|
||||||
@ -116,6 +132,21 @@ 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);
|
||||||
|
} else if (buf->type_ == 197) {
|
||||||
|
logger_->info("UpList. {}", std::string(buf->data_, buf->len_));
|
||||||
|
std::lock_guard<std::mutex> lock(cli_mut_);
|
||||||
|
if (client_map_.count(buf->id_)) {
|
||||||
|
auto& cli = client_map_[buf->id_];
|
||||||
|
cli->task_ = std::string(buf->data_, buf->len_);
|
||||||
|
cli->time_ = OfUtil::now_time();
|
||||||
|
}
|
||||||
|
} else if (buf->type_ == 196) {
|
||||||
|
logger_->info("Cancle Task.");
|
||||||
|
std::lock_guard<std::mutex> lock(cli_mut_);
|
||||||
|
if (client_map_.count(buf->id_)) {
|
||||||
|
auto& cli = client_map_[buf->id_];
|
||||||
|
cli->task_.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
delete buf;
|
delete buf;
|
||||||
buf = nullptr;
|
buf = nullptr;
|
||||||
|
@ -12,6 +12,12 @@ struct ClientCache {
|
|||||||
CMutBuffer buffer_;
|
CMutBuffer buffer_;
|
||||||
std::array<char, 1024> tmp_buf_;
|
std::array<char, 1024> tmp_buf_;
|
||||||
std::string task_;
|
std::string task_;
|
||||||
|
std::string time_;
|
||||||
|
};
|
||||||
|
struct TaskList {
|
||||||
|
std::string id_;
|
||||||
|
std::string task_;
|
||||||
|
std::string time_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CTcpServer
|
class CTcpServer
|
||||||
@ -25,7 +31,7 @@ public:
|
|||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::pair<std::string, std::string>> get_clients();
|
std::vector<TaskList> get_clients();
|
||||||
SimpleBuffer* get_client_list();
|
SimpleBuffer* get_client_list();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user