transm/client/client.cpp

125 lines
2.9 KiB
C++
Raw Normal View History

2024-12-11 23:23:48 +08:00
#include "client.h"
#include <iostream>
#include <of_str.h>
2024-12-11 23:23:48 +08:00
using namespace ofen;
2024-12-11 23:23:48 +08:00
CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger)
{
client_ = std::make_shared<CTcpClient>(io_context_, logger_);
2024-12-13 16:59:31 +08:00
supported_.push_back("Get");
2024-12-11 23:23:48 +08:00
}
CClient::~CClient()
{
}
void CClient::run()
{
if (!client_->connect("127.0.0.1", "8080")) {
logger_->info("{} connect err.", __FUNCTION__);
return;
}
client_->register_func([&](CFrameBuffer* buf) { handle_frame(buf); });
client_->async_recv();
std::thread thread([&]() { io_context_.run(); });
2024-12-11 23:23:48 +08:00
char line[512]{};
while (std::cin.getline(line, 512)) {
std::string cmd_input(line);
2024-12-11 23:23:48 +08:00
if (std::strstr(line, "end")) {
break;
}
auto vec = COfStr::split(cmd_input, " ");
2024-12-13 16:59:31 +08:00
if (vec.size() < 1) {
logger_->error("input's invalid format.");
continue;
}
2024-12-13 16:59:31 +08:00
std::string cmd{};
std::string param{};
if (vec.size() == 1) {
cmd = vec[0];
}
2024-12-13 16:59:31 +08:00
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.");
2024-12-11 23:23:48 +08:00
}
client_->disconnect();
thread.join();
logger_->info("{} exit.", __FUNCTION__);
}
bool CClient::get_task_list()
{
char* send = nullptr;
int len{};
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->data_ = new char[512]{};
2024-12-13 16:59:31 +08:00
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;
}
2024-12-13 16:59:31 +08:00
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;
}
2024-12-13 16:59:31 +08:00
// logger_->debug("type: {}", buf->type_);
// logger_->debug("len: {}", buf->len_);
if (buf->type_ == 199) {
2024-12-13 16:59:31 +08:00
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);
}
}
}